Skip to content

Apache DataFusion SQL Query Engine

feat: include some BinaryOperator from sqlparser (#15327)

Ruihang Xia916ccc39f2af
diff --git a/datafusion/expr-common/src/operator.rs b/datafusion/expr-common/src/operator.rs
index 6ca0f04897..19fc6b8074 100644
--- a/datafusion/expr-common/src/operator.rs
+++ b/datafusion/expr-common/src/operator.rs
@@ -82,10 +82,64 @@ pub enum Operator {
BitwiseShiftLeft,
/// String concat
StringConcat,
- /// At arrow, like `@>`
+ /// At arrow, like `@>`.
+ ///
+ /// Currently only supported to be used with lists:
+ /// ```sql
+ /// select [1,3] <@ [1,2,3]
+ /// ```
AtArrow,
- /// Arrow at, like `<@`
+ /// Arrow at, like `<@`.
+ ///
+ /// Currently only supported to be used with lists:
+ /// ```sql
+ /// select [1,2,3] @> [1,3]
+ /// ```
ArrowAt,
+ /// Arrow, like `->`.
+ ///
+ /// Not implemented in DataFusion yet.
+ Arrow,
+ /// Long arrow, like `->>`
+ ///
+ /// Not implemented in DataFusion yet.
+ LongArrow,
+ /// Hash arrow, like `#>`
+ ///
+ /// Not implemented in DataFusion yet.
+ HashArrow,
+ /// Hash long arrow, like `#>>`
+ ///
+ /// Not implemented in DataFusion yet.
+ HashLongArrow,
+ /// At at, like `@@`
+ ///
+ /// Not implemented in DataFusion yet.
+ AtAt,
+ /// Integer division operator, like `DIV` from MySQL or `//` from DuckDB
+ ///
+ /// Not implemented in DataFusion yet.
+ IntegerDivide,
+ /// Hash Minis, like `#-`
+ ///
+ /// Not implemented in DataFusion yet.
+ HashMinus,
+ /// At question, like `@?`
+ ///
+ /// Not implemented in DataFusion yet.
+ AtQuestion,
+ /// Question, like `?`
+ ///
+ /// Not implemented in DataFusion yet.
+ Question,
+ /// Question and, like `?&`
+ ///
+ /// Not implemented in DataFusion yet.
+ QuestionAnd,
+ /// Question pipe, like `?|`
+ ///
+ /// Not implemented in DataFusion yet.
+ QuestionPipe,
}
impl Operator {
@@ -123,7 +177,18 @@ impl Operator {
| Operator::BitwiseShiftLeft
| Operator::StringConcat
| Operator::AtArrow
- | Operator::ArrowAt => None,
+ | Operator::ArrowAt
+ | Operator::Arrow
+ | Operator::LongArrow
+ | Operator::HashArrow
+ | Operator::HashLongArrow
+ | Operator::AtAt
+ | Operator::IntegerDivide
+ | Operator::HashMinus
+ | Operator::AtQuestion
+ | Operator::Question
+ | Operator::QuestionAnd
+ | Operator::QuestionPipe => None,
}
}
@@ -216,7 +281,18 @@ impl Operator {
| Operator::BitwiseXor
| Operator::BitwiseShiftRight
| Operator::BitwiseShiftLeft
- | Operator::StringConcat => None,
+ | Operator::StringConcat
+ | Operator::Arrow
+ | Operator::LongArrow
+ | Operator::HashArrow
+ | Operator::HashLongArrow
+ | Operator::AtAt
+ | Operator::IntegerDivide
+ | Operator::HashMinus
+ | Operator::AtQuestion
+ | Operator::Question
+ | Operator::QuestionAnd
+ | Operator::QuestionPipe => None,
}
}
@@ -245,7 +321,18 @@ impl Operator {
| Operator::BitwiseXor
| Operator::StringConcat
| Operator::AtArrow
- | Operator::ArrowAt => 30,
+ | Operator::ArrowAt
+ | Operator::Arrow
+ | Operator::LongArrow
+ | Operator::HashArrow
+ | Operator::HashLongArrow
+ | Operator::AtAt
+ | Operator::IntegerDivide
+ | Operator::HashMinus
+ | Operator::AtQuestion
+ | Operator::Question
+ | Operator::QuestionAnd
+ | Operator::QuestionPipe => 30,
Operator::Plus | Operator::Minus => 40,
Operator::Multiply | Operator::Divide | Operator::Modulo => 45,
}
@@ -286,6 +373,17 @@ impl fmt::Display for Operator {
Operator::StringConcat => "||",
Operator::AtArrow => "@>",
Operator::ArrowAt => "<@",
+ Operator::Arrow => "->",
+ Operator::LongArrow => "->>",
+ Operator::HashArrow => "#>",
+ Operator::HashLongArrow => "#>>",
+ Operator::AtAt => "@@",
+ Operator::IntegerDivide => "DIV",
+ Operator::HashMinus => "#-",
+ Operator::AtQuestion => "@?",
+ Operator::Question => "?",
+ Operator::QuestionAnd => "?&",
+ Operator::QuestionPipe => "?|",
};
write!(f, "{display}")
}
diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rs
index fb559e163b..c8331dfe98 100644
--- a/datafusion/expr-common/src/type_coercion/binary.rs
+++ b/datafusion/expr-common/src/type_coercion/binary.rs
@@ -30,8 +30,8 @@ use arrow::datatypes::{
};
use datafusion_common::types::NativeType;
use datafusion_common::{
- exec_err, internal_err, plan_datafusion_err, plan_err, Diagnostic, Result, Span,
- Spans,
+ exec_err, internal_err, not_impl_err, plan_datafusion_err, plan_err, Diagnostic,
+ Result, Span, Spans,
};
use itertools::Itertools;
@@ -182,16 +182,23 @@ impl<'a> BinaryTypeCoercer<'a> {
})
}
AtArrow | ArrowAt => {
- // ArrowAt and AtArrow check for whether one array is contained in another.
- // The result type is boolean. Signature::comparison defines this signature.
- // Operation has nothing to do with comparison
- array_coercion(self.lhs, self.rhs).map(Signature::comparison).ok_or_else(|| {
+ // Array contains or search (similar to LIKE) operation
+ array_coercion(self.lhs, self.rhs)
+ .or_else(|| like_coercion(self.lhs, self.rhs)).map(Signature::comparison).ok_or_else(|| {
+ plan_datafusion_err!(
+ "Cannot infer common argument type for operation {} {} {}", self.lhs, self.op, self.rhs
+ )
+ })
+ }
+ AtAt => {
+ // text search has similar signature to LIKE
+ like_coercion(self.lhs, self.rhs).map(Signature::comparison).ok_or_else(|| {
plan_datafusion_err!(
- "Cannot infer common array type for arrow operation {} {} {}", self.lhs, self.op, self.rhs
+ "Cannot infer common argument type for AtAt operation {} {} {}", self.lhs, self.op, self.rhs
)
})
}
- Plus | Minus | Multiply | Divide | Modulo => {
+ Plus | Minus | Multiply | Divide | Modulo => {
let get_result = |lhs, rhs| {
use arrow::compute::kernels::numeric::*;
let l = new_empty_array(lhs);
@@ -248,6 +255,10 @@ impl<'a> BinaryTypeCoercer<'a> {
"Cannot coerce arithmetic expression {} {} {} to valid types", self.lhs, self.op, self.rhs
)
}
+ },
+ IntegerDivide | Arrow | LongArrow | HashArrow | HashLongArrow
+ | HashMinus | AtQuestion | Question | QuestionAnd | QuestionPipe => {
+ not_impl_err!("Operator {} is not yet supported", self.op)
}
};
result.map_err(|err| {
diff --git a/datafusion/physical-expr/src/expressions/binary.rs b/datafusion/physical-expr/src/expressions/binary.rs
index a00d135ef3..f2c68729c5 100644
--- a/datafusion/physical-expr/src/expressions/binary.rs
+++ b/datafusion/physical-expr/src/expressions/binary.rs
@@ -33,7 +33,7 @@ use arrow::compute::{cast, ilike, like, nilike, nlike};
use arrow::datatypes::*;
use arrow::error::ArrowError;
use datafusion_common::cast::as_boolean_array;
-use datafusion_common::{internal_err, Result, ScalarValue};
+use datafusion_common::{internal_err, not_impl_err, Result, ScalarValue};
use datafusion_expr::binary::BinaryTypeCoercer;
use datafusion_expr::interval_arithmetic::{apply_operator, Interval};
use datafusion_expr::sort_properties::ExprProperties;
@@ -793,8 +793,13 @@ impl BinaryExpr {
BitwiseShiftRight => bitwise_shift_right_dyn(left, right),
BitwiseShiftLeft => bitwise_shift_left_dyn(left, right),
StringConcat => concat_elements(left, right),
- AtArrow | ArrowAt => {
- unreachable!("ArrowAt and AtArrow should be rewritten to function")
+ AtArrow | ArrowAt | Arrow | LongArrow | HashArrow | HashLongArrow | AtAt
+ | HashMinus | AtQuestion | Question | QuestionAnd | QuestionPipe
+ | IntegerDivide => {
+ not_impl_err!(
+ "Binary operator '{:?}' is not supported in the physical expr",
+ self.op
+ )
}
}
}
diff --git a/datafusion/sql/src/expr/binary_op.rs b/datafusion/sql/src/expr/binary_op.rs
index 729304066d..1c06f5ee92 100644
--- a/datafusion/sql/src/expr/binary_op.rs
+++ b/datafusion/sql/src/expr/binary_op.rs
@@ -46,6 +46,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
BinaryOperator::PGNotILikeMatch => Ok(Operator::NotILikeMatch),
BinaryOperator::BitwiseAnd => Ok(Operator::BitwiseAnd),
BinaryOperator::BitwiseOr => Ok(Operator::BitwiseOr),
+ BinaryOperator::Xor => Ok(Operator::BitwiseXor),
BinaryOperator::BitwiseXor => Ok(Operator::BitwiseXor),
BinaryOperator::PGBitwiseXor => Ok(Operator::BitwiseXor),
BinaryOperator::PGBitwiseShiftRight => Ok(Operator::BitwiseShiftRight),
@@ -53,8 +54,21 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
BinaryOperator::StringConcat => Ok(Operator::StringConcat),
BinaryOperator::ArrowAt => Ok(Operator::ArrowAt),
BinaryOperator::AtArrow => Ok(Operator::AtArrow),
+ BinaryOperator::Arrow => Ok(Operator::Arrow),
+ BinaryOperator::LongArrow => Ok(Operator::LongArrow),
+ BinaryOperator::HashArrow => Ok(Operator::HashArrow),
+ BinaryOperator::HashLongArrow => Ok(Operator::HashLongArrow),
+ BinaryOperator::AtAt => Ok(Operator::AtAt),
BinaryOperator::Spaceship => Ok(Operator::IsNotDistinctFrom),
- _ => not_impl_err!("Unsupported SQL binary operator {op:?}"),
+ BinaryOperator::DuckIntegerDivide | BinaryOperator::MyIntegerDivide => {
+ Ok(Operator::IntegerDivide)
+ }
+ BinaryOperator::HashMinus => Ok(Operator::HashMinus),
+ BinaryOperator::AtQuestion => Ok(Operator::AtQuestion),
+ BinaryOperator::Question => Ok(Operator::Question),
+ BinaryOperator::QuestionAnd => Ok(Operator::QuestionAnd),
+ BinaryOperator::QuestionPipe => Ok(Operator::QuestionPipe),
+ _ => not_impl_err!("Unsupported binary operator: {:?}", op),
}
}
}
diff --git a/datafusion/sql/src/unparser/expr.rs b/datafusion/sql/src/unparser/expr.rs
index 4836037758..064adde55b 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -925,6 +925,19 @@ impl Unparser<'_> {
BinaryOperator::StringConcat => Ok(Operator::StringConcat),
BinaryOperator::AtArrow => Ok(Operator::AtArrow),
BinaryOperator::ArrowAt => Ok(Operator::ArrowAt),
+ BinaryOperator::Arrow => Ok(Operator::Arrow),
+ BinaryOperator::LongArrow => Ok(Operator::LongArrow),
+ BinaryOperator::HashArrow => Ok(Operator::HashArrow),
+ BinaryOperator::HashLongArrow => Ok(Operator::HashLongArrow),
+ BinaryOperator::AtAt => Ok(Operator::AtAt),
+ BinaryOperator::DuckIntegerDivide | BinaryOperator::MyIntegerDivide => {
+ Ok(Operator::IntegerDivide)
+ }
+ BinaryOperator::HashMinus => Ok(Operator::HashMinus),
+ BinaryOperator::AtQuestion => Ok(Operator::AtQuestion),
+ BinaryOperator::Question => Ok(Operator::Question),
+ BinaryOperator::QuestionAnd => Ok(Operator::QuestionAnd),
+ BinaryOperator::QuestionPipe => Ok(Operator::QuestionPipe),
_ => not_impl_err!("unsupported operation: {op:?}"),
}
}
@@ -962,6 +975,17 @@ impl Unparser<'_> {
Operator::StringConcat => Ok(BinaryOperator::StringConcat),
Operator::AtArrow => Ok(BinaryOperator::AtArrow),
Operator::ArrowAt => Ok(BinaryOperator::ArrowAt),
+ Operator::Arrow => Ok(BinaryOperator::Arrow),
+ Operator::LongArrow => Ok(BinaryOperator::LongArrow),
+ Operator::HashArrow => Ok(BinaryOperator::HashArrow),
+ Operator::HashLongArrow => Ok(BinaryOperator::HashLongArrow),
+ Operator::AtAt => Ok(BinaryOperator::AtAt),
+ Operator::IntegerDivide => Ok(BinaryOperator::DuckIntegerDivide),
+ Operator::HashMinus => Ok(BinaryOperator::HashMinus),
+ Operator::AtQuestion => Ok(BinaryOperator::AtQuestion),
+ Operator::Question => Ok(BinaryOperator::Question),
+ Operator::QuestionAnd => Ok(BinaryOperator::QuestionAnd),
+ Operator::QuestionPipe => Ok(BinaryOperator::QuestionPipe),
}
}
diff --git a/datafusion/sqllogictest/test_files/expr.slt b/datafusion/sqllogictest/test_files/expr.slt
index 24f7c3ea15..e4d0b72338 100644
--- a/datafusion/sqllogictest/test_files/expr.slt
+++ b/datafusion/sqllogictest/test_files/expr.slt
@@ -1648,6 +1648,56 @@ true
true
true
+#### Other binary operators
+
+# ArrowAt for strings
+query error
+select 'foo' <@ 'bar'
+
+# AtArrow for strings
+query error
+select 'foo' @> 'bar'
+
+# AtAt for strings
+query error
+select 'foo' @@ 'bar'
+
+# Arrow for arrays
+query error
+select make_array(1,2,3) -> 2
+
+# LongArrow for arrays
+query error
+select make_array(1,2,3) ->> 2
+
+# HashArrow for structs
+query error
+select struct(1,2,3) #> 0
+
+# HashLongArrow for structs
+query error
+select struct(1,2,3) #>> 0
+
+# HashMinus for structs
+query error
+select struct(1,2,3) #- 0
+
+# AtQuestion for JSON/structs
+query error
+select struct(1,2,3) @? 'a.b.c'
+
+# Question for JSON/structs
+query error
+select struct(1,2,3) ? 'a.b.c'
+
+# QuestionPipe for JSON/structs
+query error
+select struct(1,2,3) ?| array['a','b','c']
+
+# QuestionAnd for JSON/structs
+query error
+select struct(1,2,3) ?& array['a','b','c']
+
#### binary_mathematical_operator_with_null_lt
# 1. Integer and NULL
diff --git a/datafusion/substrait/src/logical_plan/producer.rs b/datafusion/substrait/src/logical_plan/producer.rs
index 44baf27778..07bf0cb96a 100644
--- a/datafusion/substrait/src/logical_plan/producer.rs
+++ b/datafusion/substrait/src/logical_plan/producer.rs
@@ -1127,6 +1127,17 @@ pub fn operator_to_name(op: Operator) -> &'static str {
Operator::StringConcat => "str_concat",
Operator::AtArrow => "at_arrow",
Operator::ArrowAt => "arrow_at",
+ Operator::Arrow => "arrow",
+ Operator::LongArrow => "long_arrow",
+ Operator::HashArrow => "hash_arrow",
+ Operator::HashLongArrow => "hash_long_arrow",
+ Operator::AtAt => "at_at",
+ Operator::IntegerDivide => "integer_divide",
+ Operator::HashMinus => "hash_minus",
+ Operator::AtQuestion => "at_question",
+ Operator::Question => "question",
+ Operator::QuestionAnd => "question_and",
+ Operator::QuestionPipe => "question_pipe",
Operator::BitwiseXor => "bitwise_xor",
Operator::BitwiseShiftRight => "bitwise_shift_right",
Operator::BitwiseShiftLeft => "bitwise_shift_left",