Skip to content
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

include some BinaryOperator from sqlparser #15327

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 103 additions & 5 deletions datafusion/expr-common/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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}")
}
Expand Down
27 changes: 19 additions & 8 deletions datafusion/expr-common/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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| {
Expand Down
11 changes: 8 additions & 3 deletions datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
)
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion datafusion/sql/src/expr/binary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,29 @@ 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),
BinaryOperator::PGBitwiseShiftLeft => Ok(Operator::BitwiseShiftLeft),
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),
}
}
}
24 changes: 24 additions & 0 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,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:?}"),
}
}
Expand Down Expand Up @@ -951,6 +964,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),
}
}

Expand Down
50 changes: 50 additions & 0 deletions datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions datafusion/substrait/src/logical_plan/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down