Skip to content

Commit f297e94

Browse files
authored
Merge pull request #3 from featuremesh/fix/json-extract-returns-union-type
json extract shoul return union type json_get
2 parents 863af2f + 9cb3823 commit f297e94

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/json_extract.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::common::{invoke, parse_jsonpath, return_type_check};
22
use crate::common_macros::make_udf_function;
3-
use crate::json_get_json::jiter_json_get_json;
4-
use datafusion::arrow::array::StringArray;
53
use datafusion::arrow::datatypes::{DataType, DataType::Utf8};
64
use datafusion::common::{exec_err, Result as DataFusionResult, ScalarValue};
75
use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
86
use std::any::Any;
7+
use crate::common_union::JsonUnion;
8+
use crate::json_get::jiter_json_get_union;
99

1010
make_udf_function!(
1111
JsonExtract,
@@ -46,7 +46,7 @@ impl ScalarUDFImpl for JsonExtract {
4646
}
4747

4848
fn return_type(&self, arg_types: &[DataType]) -> DataFusionResult<DataType> {
49-
return_type_check(arg_types, self.name(), Utf8)
49+
return_type_check(arg_types, self.name(), JsonUnion::data_type())
5050
}
5151

5252
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> DataFusionResult<ColumnarValue> {
@@ -73,7 +73,9 @@ impl ScalarUDFImpl for JsonExtract {
7373

7474
let path = parse_jsonpath(path_str);
7575

76-
invoke::<StringArray>(&[json_arg.clone()], |json, _| jiter_json_get_json(json, &path))
76+
invoke::<JsonUnion>(&[json_arg.clone()], |json, _| {
77+
jiter_json_get_union(json, &path)
78+
})
7779
}
7880

7981
fn aliases(&self) -> &[String] {

tests/json_extract_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn json_data() -> String {
1212
#[rstest]
1313
#[case(
1414
"$.a.ab",
15-
"[{\"ac\": \"Dune\", \"ca\": \"Frank Herbert\"},{\"ad\": \"Foundation\", \"da\": \"Isaac Asimov\"}]"
15+
"{array=[{\"ac\": \"Dune\", \"ca\": \"Frank Herbert\"},{\"ad\": \"Foundation\", \"da\": \"Isaac Asimov\"}]}"
1616
)]
1717
#[tokio::test]
1818
async fn test_json_paths(json_data: String, #[case] path: &str, #[case] expected: &str) {

0 commit comments

Comments
 (0)