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 1 commit
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
72 changes: 69 additions & 3 deletions datafusion/expr-common/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ pub enum Operator {
AtArrow,
/// Arrow at, like `<@`
ArrowAt,
/// Arrow, like `->`
Arrow,
/// Long arrow, like `->>`
LongArrow,
/// Hash arrow, like `#>`
HashArrow,
/// Hash long arrow, like `#>>`
HashLongArrow,
/// At at, like `@@`
AtAt,
/// Integer division operator, like `DIV` from MySQL or `//` from DuckDB
IntegerDivide,
/// Hash Minis, like `#-`
HashMinus,
/// At question, like `@?`
AtQuestion,
/// Question, like `?`
Question,
/// Question and, like `?&`
QuestionAnd,
/// Question pipe, like `?|`
QuestionPipe,
}

impl Operator {
Expand Down Expand Up @@ -123,7 +145,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 +249,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 +289,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 +341,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
10 changes: 5 additions & 5 deletions datafusion/expr-common/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,17 @@ impl<'a> BinaryTypeCoercer<'a> {
)
})
}
AtArrow | ArrowAt => {
// ArrowAt and AtArrow check for whether one array is contained in another.
AtArrow | ArrowAt | Arrow | LongArrow | HashArrow | HashLongArrow | AtAt | HashMinus |
AtQuestion | Question | QuestionAnd | QuestionPipe |IntegerDivide=> {
// These operators check for whether one array is contained in another or other JSON operations.
// 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(|| {
plan_datafusion_err!(
"Cannot infer common array type for arrow operation {} {} {}", self.lhs, self.op, self.rhs
"Cannot infer common array type for 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
6 changes: 4 additions & 2 deletions datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,10 @@ 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 => {
unreachable!("These operators should be rewritten to functions")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this code is unreachable -- I got it to panic:

DataFusion CLI v46.0.1
> select 'foo' @> 'bar';

thread 'main' panicked at datafusion/physical-expr/src/expressions/binary.rs:799:17:
internal error: entered unreachable code: These operators should be rewritten to functions
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized <@ and @> are only supported for lists 🤣

}
}
}
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
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