Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version = "1.94.0"
datafusion = { version = "55", default-features = false, features = [
"sql",
] }
jiter = "0.15.0"
jiter = "0.16.0"
log = "0.4"
paste = "1"
serde_json = "1"
Expand All @@ -25,6 +25,7 @@ datafusion = { version = "55", default-features = false, features = [
"nested_expressions",
"sql",
] }
proptest = "1.11.0"
tokio = { version = "1.43", features = ["full"] }

[lints.clippy]
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ SELECT id, json_col->'a' as json_col_a FROM test_table
- [x] `->` operator - alias for `json_get`
- [x] `->>` operator - alias for `json_as_text`
- [x] `?` operator - alias for `json_contains`
- [x] `#>` operator - `json_get` over a path, so `a #> array['x', 'y']` is `a -> 'x' -> 'y'`
- [x] `#>>` operator - `json_as_text` over a path, so `a #>> array['x', 'y']` is `a -> 'x' ->> 'y'`

The right-hand side of a path operator is a `text[]` list expression:

```sql
select json_col #>> array['a', 'b'] from test_table;
select json_col #>> array['a', 'b']::text[] from test_table;
select json_col #>> $1::text[] from test_table;
```

Postgres's array literal syntax, `'{a,b}'`, is not supported. As in postgres, each element is
a key when it reaches an object and an index when it reaches an array, so
`array['items', '0', 'name']` walks to `items`, then element zero, then `name`. A list of
integers is a path of indices, and a negative index counts from the end of the array. The
`json_get*` and `json_as_text` functions accept the same list as a path, e.g.
`json_as_text(json_col, array['a', 'b'])`.

### Notes
Cast expressions with `json_get` are rewritten to the appropriate method, e.g.
Expand Down
90 changes: 89 additions & 1 deletion benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use datafusion::arrow::array::{StringArray, StringViewArray};
use datafusion::arrow::datatypes::{DataType, Field};
use datafusion::logical_expr::ColumnarValue;
use datafusion::{common::ScalarValue, logical_expr::ScalarFunctionArgs};
use datafusion_functions_json::udfs::{json_contains_udf, json_get_str_udf};
use datafusion_functions_json::udfs::{json_contains_udf, json_get_str_udf, json_length_udf};

fn bench_json_contains(b: &mut Bencher) {
let json_contains = json_contains_udf();
Expand Down Expand Up @@ -134,7 +134,95 @@ fn bench_json_get_str_view_array(b: &mut Bencher) {
});
}

/// A document with a 64 element array, so that stepping through it is measurable.
fn array_doc() -> String {
let items: Vec<String> = (0..64).map(|i| format!(r#""v{i}""#)).collect();
format!(r#"{{"a": [{}]}}"#, items.join(", "))
}

fn bench_array_path(b: &mut Bencher, udf: &datafusion::logical_expr::ScalarUDF, index: &ScalarValue) {
let args = &[
ColumnarValue::Array(Arc::new(StringArray::from_iter_values(std::iter::repeat_n(
array_doc(),
1024,
)))),
ColumnarValue::Scalar(ScalarValue::Utf8(Some("a".to_string()))),
ColumnarValue::Scalar(index.clone()),
];

let arg_fields = vec![
Arc::new(Field::new("arg0", DataType::Utf8, false)),
Arc::new(Field::new("arg1", DataType::Utf8, false)),
Arc::new(Field::new("arg2", index.data_type(), false)),
];

let return_field = Arc::new(Field::new(
"out",
udf.return_type(&[DataType::Utf8, DataType::Utf8, index.data_type()])
.unwrap(),
false,
));

b.iter(|| {
udf.invoke_with_args(ScalarFunctionArgs {
args: args.to_vec(),
arg_fields: arg_fields.clone(),
number_rows: 1024,
return_field: return_field.clone(),
config_options: Arc::new(datafusion::config::ConfigOptions::default()),
})
.unwrap();
});
}

fn bench_json_get_str_index(b: &mut Bencher) {
bench_array_path(b, &json_get_str_udf(), &ScalarValue::Int64(Some(32)));
}

fn bench_json_get_str_index_last(b: &mut Bencher) {
bench_array_path(b, &json_get_str_udf(), &ScalarValue::Int64(Some(63)));
}

fn bench_json_get_str_negative_index(b: &mut Bencher) {
bench_array_path(b, &json_get_str_udf(), &ScalarValue::Int64(Some(-32)));
}

fn bench_json_get_str_text_index(b: &mut Bencher) {
bench_array_path(b, &json_get_str_udf(), &ScalarValue::Utf8(Some("32".to_string())));
}

fn bench_json_length_array(b: &mut Bencher) {
let args = &[
ColumnarValue::Array(Arc::new(StringArray::from_iter_values(std::iter::repeat_n(
array_doc(),
1024,
)))),
ColumnarValue::Scalar(ScalarValue::Utf8(Some("a".to_string()))),
];
let arg_fields = vec![
Arc::new(Field::new("arg0", DataType::Utf8, false)),
Arc::new(Field::new("arg1", DataType::Utf8, false)),
];
let return_field = Arc::new(Field::new("json_length", DataType::UInt64, false));
let udf = json_length_udf();
b.iter(|| {
udf.invoke_with_args(ScalarFunctionArgs {
args: args.to_vec(),
arg_fields: arg_fields.clone(),
number_rows: 1024,
return_field: return_field.clone(),
config_options: Arc::new(datafusion::config::ConfigOptions::default()),
})
.unwrap();
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("json_get_str_index", bench_json_get_str_index);
c.bench_function("json_get_str_index_last", bench_json_get_str_index_last);
c.bench_function("json_get_str_negative_index", bench_json_get_str_negative_index);
c.bench_function("json_get_str_text_index", bench_json_get_str_text_index);
c.bench_function("json_length_array", bench_json_length_array);
c.bench_function("json_contains", bench_json_contains);
c.bench_function("json_get_str_scalar", bench_json_get_str_scalar);
c.bench_function("json_get_str_array", bench_json_get_str_array);
Expand Down
Loading
Loading