|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use arrow_array::RecordBatch; |
| 19 | +use std::sync::Arc; |
| 20 | + |
| 21 | +use datafusion::common::{assert_batches_eq, DFSchema}; |
| 22 | +use datafusion::error::Result; |
| 23 | +use datafusion::execution::FunctionRegistry; |
| 24 | +use datafusion::logical_expr::Operator; |
| 25 | +use datafusion::prelude::*; |
| 26 | +use datafusion::sql::sqlparser::ast::BinaryOperator; |
| 27 | +use datafusion_expr::planner::{PlannerResult, RawBinaryExpr, UserDefinedSQLPlanner}; |
| 28 | +use datafusion_expr::BinaryExpr; |
| 29 | + |
| 30 | +struct MyCustomPlanner; |
| 31 | + |
| 32 | +impl UserDefinedSQLPlanner for MyCustomPlanner { |
| 33 | + fn plan_binary_op( |
| 34 | + &self, |
| 35 | + expr: RawBinaryExpr, |
| 36 | + _schema: &DFSchema, |
| 37 | + ) -> Result<PlannerResult<RawBinaryExpr>> { |
| 38 | + match &expr.op { |
| 39 | + BinaryOperator::Arrow => { |
| 40 | + Ok(PlannerResult::Planned(Expr::BinaryExpr(BinaryExpr { |
| 41 | + left: Box::new(expr.left.clone()), |
| 42 | + right: Box::new(expr.right.clone()), |
| 43 | + op: Operator::StringConcat, |
| 44 | + }))) |
| 45 | + } |
| 46 | + BinaryOperator::LongArrow => { |
| 47 | + Ok(PlannerResult::Planned(Expr::BinaryExpr(BinaryExpr { |
| 48 | + left: Box::new(expr.left.clone()), |
| 49 | + right: Box::new(expr.right.clone()), |
| 50 | + op: Operator::Plus, |
| 51 | + }))) |
| 52 | + } |
| 53 | + _ => Ok(PlannerResult::Original(expr)), |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +async fn plan_and_collect(sql: &str) -> Result<Vec<RecordBatch>> { |
| 59 | + let mut ctx = SessionContext::new(); |
| 60 | + ctx.register_user_defined_sql_planner(Arc::new(MyCustomPlanner))?; |
| 61 | + ctx.sql(sql).await?.collect().await |
| 62 | +} |
| 63 | + |
| 64 | +#[tokio::test] |
| 65 | +async fn test_custom_operators_arrow() { |
| 66 | + let actual = plan_and_collect("select 'foo'->'bar';").await.unwrap(); |
| 67 | + let expected = [ |
| 68 | + "+----------------------------+", |
| 69 | + "| Utf8(\"foo\") || Utf8(\"bar\") |", |
| 70 | + "+----------------------------+", |
| 71 | + "| foobar |", |
| 72 | + "+----------------------------+", |
| 73 | + ]; |
| 74 | + assert_batches_eq!(&expected, &actual); |
| 75 | +} |
| 76 | + |
| 77 | +#[tokio::test] |
| 78 | +async fn test_custom_operators_long_arrow() { |
| 79 | + let actual = plan_and_collect("select 1->>2;").await.unwrap(); |
| 80 | + let expected = [ |
| 81 | + "+---------------------+", |
| 82 | + "| Int64(1) + Int64(2) |", |
| 83 | + "+---------------------+", |
| 84 | + "| 3 |", |
| 85 | + "+---------------------+", |
| 86 | + ]; |
| 87 | + assert_batches_eq!(&expected, &actual); |
| 88 | +} |
0 commit comments