Skip to content

Commit 5738ff3

Browse files
authored
Improve readme with examples and operator usage (#45)
1 parent f3d5366 commit 5738ff3

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
Cargo.lock
3+
.idea

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,47 @@ To use these functions, you'll just need to call:
1212
```rust
1313
datafusion_functions_json::register_all(&mut ctx)?;
1414
```
15-
1615
To register the below JSON functions in your `SessionContext`.
1716

17+
# Examples
18+
19+
```sql
20+
-- Create a table with a JSON column stored as a string
21+
CREATE TABLE test_table (id INT, json_col VARCHAR) AS VALUES
22+
(1, '{}'),
23+
(2, '{ "a": 1 }'),
24+
(3, '{ "a": 2 }'),
25+
(4, '{ "a": 1, "b": 2 }'),
26+
(5, '{ "a": 1, "b": 2, "c": 3 }');
27+
28+
-- Check if each document contains the key 'b'
29+
SELECT id, json_contains(json_col, 'b') as json_contains FROM test_table;
30+
-- Results in
31+
-- +----+---------------+
32+
-- | id | json_contains |
33+
-- +----+---------------+
34+
-- | 1 | false |
35+
-- | 2 | false |
36+
-- | 3 | false |
37+
-- | 4 | true |
38+
-- | 5 | true |
39+
-- +----+---------------+
40+
41+
-- Get the value of the key 'a' from each document
42+
SELECT id, json_col->'a' as json_col_a FROM test_table
43+
44+
-- +----+------------+
45+
-- | id | json_col_a |
46+
-- +----+------------+
47+
-- | 1 | {null=} |
48+
-- | 2 | {int=1} |
49+
-- | 3 | {int=2} |
50+
-- | 4 | {int=1} |
51+
-- | 5 | {int=1} |
52+
-- +----+------------+
53+
```
54+
55+
1856
## Done
1957

2058
* [x] `json_contains(json: str, *keys: str | int) -> bool` - true if a JSON string has a specific key (used for the `?` operator)
@@ -27,6 +65,11 @@ To register the below JSON functions in your `SessionContext`.
2765
* [x] `json_as_text(json: str, *keys: str | int) -> str` - Get any value from a JSON string by its "path", represented as a string (used for the `->>` operator)
2866
* [x] `json_length(json: str, *keys: str | int) -> int` - get the length of a JSON string or array
2967

68+
- [x] `->` operator - alias for `json_get`
69+
- [x] `->>` operator - alias for `json_as_text`
70+
- [x] `?` operator - alias for `json_contains`
71+
72+
### Notes
3073
Cast expressions with `json_get` are rewritten to the appropriate method, e.g.
3174

3275
```sql

0 commit comments

Comments
 (0)