Skip to content
Open
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
34 changes: 33 additions & 1 deletion datafusion/sql/src/expr/binary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
// under the License.

use crate::planner::{ContextProvider, SqlToRel};
use datafusion_common::{Result, not_impl_err};
use datafusion_common::{Result, internal_datafusion_err, not_impl_err};
use datafusion_expr::Operator;
use datafusion_expr::expr::ScalarFunction;
use datafusion_expr::{BinaryExpr, Expr};
use sqlparser::ast::BinaryOperator;

impl<S: ContextProvider> SqlToRel<'_, S> {
Expand Down Expand Up @@ -72,4 +74,34 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
_ => not_impl_err!("Unsupported binary operator: {:?}", op),
}
}

pub(crate) fn build_binary_expr(
&self,
op: &BinaryOperator,
left: Expr,
right: Expr,
) -> Result<Expr> {
if matches!(op, BinaryOperator::PGExp) {
let fun_name = "power";
let fun = self
.context_provider
.get_function_meta(fun_name)
.ok_or_else(|| {
internal_datafusion_err!(
"Unable to find expected '{fun_name}' function"
)
})?;

return Ok(Expr::ScalarFunction(ScalarFunction::new_udf(
fun,
vec![left, right],
)));
}

Ok(Expr::BinaryExpr(BinaryExpr::new(
Box::new(left),
self.parse_sql_binary_op(op)?,
Box::new(right),
)))
}
}
6 changes: 1 addition & 5 deletions datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}

let RawBinaryExpr { op, left, right } = binary_expr;
Ok(Expr::BinaryExpr(BinaryExpr::new(
Box::new(left),
self.parse_sql_binary_op(&op)?,
Box::new(right),
)))
self.build_binary_expr(&op, left, right)
}

pub fn sql_to_expr_with_alias(
Expand Down
6 changes: 6 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,12 @@ NULL -32
statement ok
set datafusion.sql_parser.dialect = postgresql;

# postgresql exponentiation uses caret
query R
select 2 ^ 3;
----
8

# postgresql bitwise xor with column and scalar
query I rowsort
select c # 856 from signed_integers;
Expand Down
Loading