Skip to content

Commit 32eb462

Browse files
committed
Add support for parsing common JSON operators
1 parent f373a86 commit 32eb462

File tree

9 files changed

+65
-5
lines changed

9 files changed

+65
-5
lines changed

datafusion/expr/src/operator.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ pub enum Operator {
8989
BitwiseShiftLeft,
9090
/// String concat
9191
StringConcat,
92+
/// Question, like `?`
93+
Question,
94+
/// Arrow, like `->`
95+
Arrow,
96+
/// Long arrow, like `->>`
97+
LongArrow,
9298
/// At arrow, like `@>`
9399
AtArrow,
94100
/// Arrow at, like `<@`
@@ -129,6 +135,9 @@ impl Operator {
129135
| Operator::BitwiseShiftRight
130136
| Operator::BitwiseShiftLeft
131137
| Operator::StringConcat
138+
| Operator::Question
139+
| Operator::Arrow
140+
| Operator::LongArrow
132141
| Operator::AtArrow
133142
| Operator::ArrowAt => None,
134143
}
@@ -213,7 +222,10 @@ impl Operator {
213222
| Operator::BitwiseXor
214223
| Operator::BitwiseShiftRight
215224
| Operator::BitwiseShiftLeft
216-
| Operator::StringConcat => None,
225+
| Operator::StringConcat
226+
| Operator::Question
227+
| Operator::Arrow
228+
| Operator::LongArrow => None,
217229
}
218230
}
219231

@@ -247,6 +259,9 @@ impl Operator {
247259
| Operator::BitwiseShiftRight
248260
| Operator::BitwiseXor
249261
| Operator::StringConcat
262+
| Operator::Question
263+
| Operator::Arrow
264+
| Operator::LongArrow
250265
| Operator::AtArrow
251266
| Operator::ArrowAt => 0,
252267
}
@@ -285,6 +300,9 @@ impl fmt::Display for Operator {
285300
Operator::BitwiseShiftRight => ">>",
286301
Operator::BitwiseShiftLeft => "<<",
287302
Operator::StringConcat => "||",
303+
Operator::Question => "?",
304+
Operator::Arrow => "->",
305+
Operator::LongArrow => "->>",
288306
Operator::AtArrow => "@>",
289307
Operator::ArrowAt => "<@",
290308
};

datafusion/expr/src/type_coercion/binary.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ fn signature(lhs: &DataType, op: &Operator, rhs: &DataType) -> Result<Signature>
121121
)
122122
})
123123
}
124+
Question => {
125+
match (lhs, rhs) {
126+
(Utf8 | LargeUtf8, Utf8 | LargeUtf8) => Ok(Signature{
127+
lhs: lhs.clone(),
128+
rhs: rhs.clone(),
129+
ret: Boolean,
130+
}),
131+
_ => Err(plan_datafusion_err!(
132+
"Cannot coerce question operation {lhs} {op} {rhs} to vaild types"
133+
))
134+
}
135+
}
136+
Arrow | LongArrow => {
137+
match (lhs, rhs) {
138+
(Utf8 | LargeUtf8, Utf8 | LargeUtf8) => Ok(Signature{
139+
lhs: lhs.clone(),
140+
rhs: rhs.clone(),
141+
ret: lhs.clone(),
142+
}),
143+
_ => Err(plan_datafusion_err!(
144+
"Cannot coerce arrow operation {lhs} {op} {rhs} to vaild types"
145+
))
146+
}
147+
}
124148
AtArrow | ArrowAt => {
125149
// ArrowAt and AtArrow check for whether one array is contained in another.
126150
// The result type is boolean. Signature::comparison defines this signature.

datafusion/physical-expr/src/expressions/binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ impl BinaryExpr {
628628
BitwiseShiftRight => bitwise_shift_right_dyn(left, right),
629629
BitwiseShiftLeft => bitwise_shift_left_dyn(left, right),
630630
StringConcat => binary_string_array_op!(left, right, concat_elements),
631-
AtArrow | ArrowAt => {
632-
unreachable!("ArrowAt and AtArrow should be rewritten to function")
631+
Question | Arrow | LongArrow | AtArrow | ArrowAt => {
632+
unreachable!("Question, Arrow, LongArrow, ArrowAt, and AtArrow should be rewritten to function")
633633
}
634634
}
635635
}

datafusion/proto/src/logical_plan/from_proto.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,9 @@ pub fn from_proto_binary_op(op: &str) -> Result<Operator, Error> {
707707
"RegexNotIMatch" => Ok(Operator::RegexNotIMatch),
708708
"RegexNotMatch" => Ok(Operator::RegexNotMatch),
709709
"StringConcat" => Ok(Operator::StringConcat),
710+
"Question" => Ok(Operator::Question),
711+
"Arrow" => Ok(Operator::Arrow),
712+
"LongArrow" => Ok(Operator::LongArrow),
710713
"AtArrow" => Ok(Operator::AtArrow),
711714
"ArrowAt" => Ok(Operator::ArrowAt),
712715
other => Err(proto_error(format!(

datafusion/proto/tests/cases/roundtrip_logical_plan.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,9 @@ fn roundtrip_binary_op() {
15951595
let ctx = SessionContext::new();
15961596
roundtrip_expr_test(test_expr, ctx);
15971597
}
1598+
test(Operator::Question);
1599+
test(Operator::Arrow);
1600+
test(Operator::LongArrow);
15981601
test(Operator::ArrowAt);
15991602
test(Operator::AtArrow);
16001603
test(Operator::StringConcat);

datafusion/sql/src/expr/binary_op.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
5151
BinaryOperator::PGBitwiseShiftRight => Ok(Operator::BitwiseShiftRight),
5252
BinaryOperator::PGBitwiseShiftLeft => Ok(Operator::BitwiseShiftLeft),
5353
BinaryOperator::StringConcat => Ok(Operator::StringConcat),
54+
BinaryOperator::Question => Ok(Operator::Question),
55+
BinaryOperator::Arrow => Ok(Operator::Arrow),
56+
BinaryOperator::LongArrow => Ok(Operator::LongArrow),
5457
BinaryOperator::ArrowAt => Ok(Operator::ArrowAt),
5558
BinaryOperator::AtArrow => Ok(Operator::AtArrow),
5659
_ => not_impl_err!("Unsupported SQL binary operator {op:?}"),

datafusion/sql/src/unparser/expr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,11 @@ impl Unparser<'_> {
639639
Operator::BitwiseShiftRight => Ok(ast::BinaryOperator::PGBitwiseShiftRight),
640640
Operator::BitwiseShiftLeft => Ok(ast::BinaryOperator::PGBitwiseShiftLeft),
641641
Operator::StringConcat => Ok(ast::BinaryOperator::StringConcat),
642-
Operator::AtArrow => not_impl_err!("unsupported operation: {op:?}"),
643-
Operator::ArrowAt => not_impl_err!("unsupported operation: {op:?}"),
642+
Operator::Question => Ok(ast::BinaryOperator::Question),
643+
Operator::Arrow => Ok(ast::BinaryOperator::Arrow),
644+
Operator::LongArrow => Ok(ast::BinaryOperator::LongArrow),
645+
Operator::AtArrow => Ok(ast::BinaryOperator::AtArrow),
646+
Operator::ArrowAt => Ok(ast::BinaryOperator::ArrowAt),
644647
}
645648
}
646649

datafusion/substrait/src/logical_plan/consumer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ pub fn name_to_op(name: &str) -> Result<Operator> {
119119
"bitwise_and" => Ok(Operator::BitwiseAnd),
120120
"bitwise_or" => Ok(Operator::BitwiseOr),
121121
"str_concat" => Ok(Operator::StringConcat),
122+
"question" => Ok(Operator::Question),
123+
"arrow" => Ok(Operator::Arrow),
124+
"long_arrow" => Ok(Operator::LongArrow),
122125
"at_arrow" => Ok(Operator::AtArrow),
123126
"arrow_at" => Ok(Operator::ArrowAt),
124127
"bitwise_xor" => Ok(Operator::BitwiseXor),

datafusion/substrait/src/logical_plan/producer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,9 @@ pub fn operator_to_name(op: Operator) -> &'static str {
713713
Operator::BitwiseAnd => "bitwise_and",
714714
Operator::BitwiseOr => "bitwise_or",
715715
Operator::StringConcat => "str_concat",
716+
Operator::Question => "question",
717+
Operator::Arrow => "arrow",
718+
Operator::LongArrow => "long_arrow",
716719
Operator::AtArrow => "at_arrow",
717720
Operator::ArrowAt => "arrow_at",
718721
Operator::BitwiseXor => "bitwise_xor",

0 commit comments

Comments
 (0)