Skip to content

Commit

Permalink
add failing test_json_get_equals test
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Apr 22, 2024
1 parent 23a55a3 commit 28ee103
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ print_stdout = "warn"
# certain lints which we don't want to enforce (for now)
pedantic = { level = "warn", priority = -1 }
missing_errors_doc = "allow"
cast_possible_truncation = "allow"
5 changes: 2 additions & 3 deletions src/json_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl ScalarUDFImpl for JsonGet {
let mut union = JsonUnion::new(json_data.len());
for opt_json in json_data {
if let Some(union_field) = jiter_json_get(opt_json, &path) {
dbg!(&union_field);
union.push(union_field);
} else {
union.push_none();
Expand Down Expand Up @@ -126,11 +125,11 @@ fn _jiter_json_get(jiter: &mut Jiter, peek: Peek, path: &[JsonPath]) -> Result<J
let next_peek = match peek {
Peek::Array => match first {
JsonPath::Index(index) => jiter_array_get(jiter, *index),
_ => Err(GetError),
JsonPath::Key(_) => Err(GetError),
},
Peek::Object => match first {
JsonPath::Key(key) => jiter_object_get(jiter, key),
_ => Err(GetError),
JsonPath::Index(_) => Err(GetError),
},
_ => Err(GetError),
}?;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;
mod json_get;
mod json_obj_contains;
mod macros;
mod rewrite;
mod union;

pub mod functions {
Expand All @@ -24,6 +25,7 @@ pub fn register_all(registry: &mut dyn FunctionRegistry) -> Result<()> {
}
Ok(()) as Result<()>
})?;
registry.register_function_rewrite(Arc::new(rewrite::JsonFunctionRewriter))?;

Ok(())
}
19 changes: 19 additions & 0 deletions src/rewrite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use datafusion_common::config::ConfigOptions;
use datafusion_common::tree_node::Transformed;
use datafusion_common::DFSchema;
use datafusion_common::Result;
use datafusion_expr::expr_rewriter::FunctionRewrite;
use datafusion_expr::Expr;

pub(crate) struct JsonFunctionRewriter;

impl FunctionRewrite for JsonFunctionRewriter {
fn name(&self) -> &str {
"JsonFunctionRewriter"
}

fn rewrite(&self, expr: Expr, _schema: &DFSchema, _config: &ConfigOptions) -> Result<Transformed<Expr>> {
dbg!(&expr, _schema);
Ok(Transformed::no(expr))
}
}
22 changes: 22 additions & 0 deletions tests/test_json_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,25 @@ async fn test_json_get() {
.unwrap();
assert_batches_eq!(expected, &batches);
}

#[tokio::test]
async fn test_json_get_equals() {
let expected = [
"+------------------+--------------------------------------+",
"| name | json_get(test.json_data,Utf8(\"foo\")) |",
"+------------------+--------------------------------------+",
"| object_foo | {int=123} |",
"| object_foo_array | {array=[1]} |",
"| object_foo_obj | {object={}} |",
"| object_foo_null | {null=true} |",
"| object_bar | {null=} |",
"| list_foo | {null=} |",
"| invalid_json | {null=} |",
"+------------------+--------------------------------------+",
];

let batches = run_query("select name, json_get(json_data, 'foo')=123 from test")
.await
.unwrap();
assert_batches_eq!(expected, &batches);
}

0 comments on commit 28ee103

Please sign in to comment.