Skip to content

Commit

Permalink
resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
vigimite committed Nov 29, 2024
2 parents 7ea7f9b + 9d72f5d commit 0ab5da2
Show file tree
Hide file tree
Showing 24 changed files with 1,219 additions and 223 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ jobs:
- id: cache-rust
uses: Swatinem/rust-cache@v2

- run: cargo test --all-features
# - uses: taiki-e/install-action@cargo-llvm-cov
#
# - run: cargo llvm-cov --all-features --codecov --output-path codecov.json
#
# - uses: codecov/codecov-action@v3
# with:
# files: codecov.json
# env_vars: RUST_VERSION
- uses: taiki-e/install-action@cargo-llvm-cov

- run: cargo llvm-cov --all-features --codecov --output-path codecov.json

- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: codecov.json
env_vars: RUST_VERSION

# https://github.com/marketplace/actions/alls-green#why used for branch protection checks
check:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
.idea
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repos:
pass_filenames: false
- id: clippy
name: Clippy
entry: cargo clippy
entry: cargo clippy -- -D warnings
types: [rust]
language: system
pass_filenames: false
Expand Down
15 changes: 5 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
[package]
name = "datafusion-functions-json"
version = "0.41.0"
version = "0.43.0"
edition = "2021"
description = "JSON functions for DataFusion"
readme = "README.md"
license = "Apache-2.0"
keywords = ["datafusion", "JSON", "SQL"]
categories = ["database-implementations", "parsing"]
repository = "https://github.com/datafusion-contrib/datafusion-functions-json/"
rust-version = "1.76.0"
rust-version = "1.79.0"

[dependencies]
arrow = "52.2"
arrow-schema = "52.2"
datafusion-common = "41"
datafusion-expr = "41"
datafusion-execution = "41"
datafusion = "43"
jiter = "0.5"
paste = "1"
log = "0.4"

[dev-dependencies]
codspeed-criterion-compat = "2.3"
codspeed-criterion-compat = "2.6"
criterion = "0.5.1"
datafusion = "41"
clap = "4"
tokio = { version = "1.37", features = ["full"] }
tokio = { version = "1.38", features = ["full"] }

[lints.clippy]
dbg_macro = "deny"
Expand Down
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,47 @@ To use these functions, you'll just need to call:
```rust
datafusion_functions_json::register_all(&mut ctx)?;
```

To register the below JSON functions in your `SessionContext`.

# Examples

```sql
-- Create a table with a JSON column stored as a string
CREATE TABLE test_table (id INT, json_col VARCHAR) AS VALUES
(1, '{}'),
(2, '{ "a": 1 }'),
(3, '{ "a": 2 }'),
(4, '{ "a": 1, "b": 2 }'),
(5, '{ "a": 1, "b": 2, "c": 3 }');

-- Check if each document contains the key 'b'
SELECT id, json_contains(json_col, 'b') as json_contains FROM test_table;
-- Results in
-- +----+---------------+
-- | id | json_contains |
-- +----+---------------+
-- | 1 | false |
-- | 2 | false |
-- | 3 | false |
-- | 4 | true |
-- | 5 | true |
-- +----+---------------+

-- Get the value of the key 'a' from each document
SELECT id, json_col->'a' as json_col_a FROM test_table

-- +----+------------+
-- | id | json_col_a |
-- +----+------------+
-- | 1 | {null=} |
-- | 2 | {int=1} |
-- | 3 | {int=2} |
-- | 4 | {int=1} |
-- | 5 | {int=1} |
-- +----+------------+
```


## Done

* [x] `json_contains(json: str, *keys: str | int) -> bool` - true if a JSON string has a specific key (used for the `?` operator)
Expand All @@ -27,6 +65,11 @@ To register the below JSON functions in your `SessionContext`.
* [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)
* [x] `json_length(json: str, *keys: str | int) -> int` - get the length of a JSON string or array

- [x] `->` operator - alias for `json_get`
- [x] `->>` operator - alias for `json_as_text`
- [x] `?` operator - alias for `json_contains`

### Notes
Cast expressions with `json_get` are rewritten to the appropriate method, e.g.

```sql
Expand Down
8 changes: 4 additions & 4 deletions benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};

use datafusion_common::ScalarValue;
use datafusion_expr::ColumnarValue;
use datafusion::common::ScalarValue;
use datafusion::logical_expr::ColumnarValue;
use datafusion_functions_json::udfs::{json_contains_udf, json_get_str_udf};

fn bench_json_contains(b: &mut Bencher) {
Expand All @@ -14,7 +14,7 @@ fn bench_json_contains(b: &mut Bencher) {
ColumnarValue::Scalar(ScalarValue::Utf8(Some("aa".to_string()))),
];

b.iter(|| json_contains.invoke(args).unwrap());
b.iter(|| json_contains.invoke_batch(args, 1).unwrap());
}

fn bench_json_get_str(b: &mut Bencher) {
Expand All @@ -27,7 +27,7 @@ fn bench_json_get_str(b: &mut Bencher) {
ColumnarValue::Scalar(ScalarValue::Utf8(Some("aa".to_string()))),
];

b.iter(|| json_get_str.invoke(args).unwrap());
b.iter(|| json_get_str.invoke_batch(args, 1).unwrap());
}

fn criterion_benchmark(c: &mut Criterion) {
Expand Down
Loading

0 comments on commit 0ab5da2

Please sign in to comment.