-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add a custom ExprPlanner to support colon operator #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Samyak2
wants to merge
4
commits into
datafusion-contrib:main
Choose a base branch
from
Samyak2:colon-operator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−13
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use datafusion::{ | ||
| common::DFSchema, | ||
| error::DataFusionError, | ||
| logical_expr::{ | ||
| ScalarUDF, | ||
| expr::ScalarFunction, | ||
| planner::{ExprPlanner, PlannerResult, RawBinaryExpr}, | ||
| }, | ||
| prelude::Expr, | ||
| sql::sqlparser::ast::BinaryOperator, | ||
| }; | ||
|
|
||
| use crate::VariantGetUdf; | ||
|
|
||
| /// Custom [`ExprPlanner`] used to handle variant-specific syntax such as colon operator. | ||
| /// | ||
| /// Currently implements: | ||
| /// - Colon operator: short-hand syntax for `variant_get`. | ||
| #[derive(Debug)] | ||
| pub struct VariantExprPlanner; | ||
|
|
||
| impl ExprPlanner for VariantExprPlanner { | ||
| fn plan_binary_op( | ||
| &self, | ||
| expr: RawBinaryExpr, | ||
| _schema: &DFSchema, | ||
| ) -> Result<PlannerResult<RawBinaryExpr>, DataFusionError> { | ||
| match &expr.op { | ||
| BinaryOperator::Custom(s) if s == ":" => Ok(PlannerResult::Planned( | ||
| Expr::ScalarFunction(ScalarFunction::new_udf( | ||
| Arc::new(ScalarUDF::new_from_impl(VariantGetUdf::default())), | ||
| vec![expr.left, expr.right], | ||
| )), | ||
| )), | ||
| _ => Ok(PlannerResult::Original(expr)), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| statement ok | ||
| CREATE TABLE json_data (id INT, json_str TEXT) AS VALUES | ||
| (1, '{"name": "Alice", "age": 30}'), | ||
| (2, '{"name": "Bob", "age": 25}'), | ||
| (3, '{"items": [1, 2, 3], "count": 3}'), | ||
| (4, '{"items": [{"name": "Sam", "age": 33}, "a", 3], "count": 3}'), | ||
| (5, 'null'), | ||
| (6, '"simple string"'), | ||
| (7, '["looooooooong string", "hehe", "a"]'), | ||
| (8, 'true'); | ||
|
|
||
| # field access | ||
| query T | ||
| select variant_pretty(json_to_variant(json_str):name) from json_data; | ||
| ---- | ||
| ShortString(ShortString("Alice")) | ||
| ShortString(ShortString("Bob")) | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
|
|
||
| # field + array access | ||
| query T | ||
| select variant_pretty(json_to_variant(json_str):items[0]) from json_data; | ||
| ---- | ||
| NULL | ||
| NULL | ||
| Int8(1) | ||
| {"age": Int8(33), "name": ShortString(ShortString("Sam"))} | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
|
|
||
| # array access with non-array | ||
| query T | ||
| select variant_pretty(json_to_variant(json_str):age[0]) from json_data; | ||
| ---- | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
|
|
||
| # field -> array -> field access | ||
| query T | ||
| select variant_pretty(json_to_variant(json_str):items[0]["name"]) from json_data; | ||
| ---- | ||
| NULL | ||
| NULL | ||
| NULL | ||
| ShortString(ShortString("Sam")) | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL | ||
|
|
||
| # field -> array -> field access but with single quotes | ||
| query T | ||
| select variant_pretty(json_to_variant(json_str):items[0]['name']) from json_data; | ||
| ---- | ||
| NULL | ||
| NULL | ||
| NULL | ||
| ShortString(ShortString("Sam")) | ||
| NULL | ||
| NULL | ||
| NULL | ||
| NULL |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some comments here that explain what this does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Please take a look