Skip to content

Commit 9991144

Browse files
samuelcolvinalamb
andauthored
Remove any aliases in Filter::try_new rather than erroring (#11307)
* allow alias in predicate, fix #11306 * Fix typo. Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org> * unalise predicate * use unalias_nested --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 894a879 commit 9991144

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

datafusion/core/tests/user_defined/user_defined_sql_planner.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use datafusion::execution::FunctionRegistry;
2424
use datafusion::logical_expr::Operator;
2525
use datafusion::prelude::*;
2626
use datafusion::sql::sqlparser::ast::BinaryOperator;
27+
use datafusion_common::ScalarValue;
28+
use datafusion_expr::expr::Alias;
2729
use datafusion_expr::planner::{PlannerResult, RawBinaryExpr, UserDefinedSQLPlanner};
2830
use datafusion_expr::BinaryExpr;
2931

@@ -50,13 +52,22 @@ impl UserDefinedSQLPlanner for MyCustomPlanner {
5052
op: Operator::Plus,
5153
})))
5254
}
55+
BinaryOperator::Question => {
56+
Ok(PlannerResult::Planned(Expr::Alias(Alias::new(
57+
Expr::Literal(ScalarValue::Boolean(Some(true))),
58+
None::<&str>,
59+
format!("{} ? {}", expr.left, expr.right),
60+
))))
61+
}
5362
_ => Ok(PlannerResult::Original(expr)),
5463
}
5564
}
5665
}
5766

5867
async fn plan_and_collect(sql: &str) -> Result<Vec<RecordBatch>> {
59-
let mut ctx = SessionContext::new();
68+
let config =
69+
SessionConfig::new().set_str("datafusion.sql_parser.dialect", "postgres");
70+
let mut ctx = SessionContext::new_with_config(config);
6071
ctx.register_user_defined_sql_planner(Arc::new(MyCustomPlanner))?;
6172
ctx.sql(sql).await?.collect().await
6273
}
@@ -86,3 +97,27 @@ async fn test_custom_operators_long_arrow() {
8697
];
8798
assert_batches_eq!(&expected, &actual);
8899
}
100+
101+
#[tokio::test]
102+
async fn test_question_select() {
103+
let actual = plan_and_collect("select a ? 2 from (select 1 as a);")
104+
.await
105+
.unwrap();
106+
let expected = [
107+
"+--------------+",
108+
"| a ? Int64(2) |",
109+
"+--------------+",
110+
"| true |",
111+
"+--------------+",
112+
];
113+
assert_batches_eq!(&expected, &actual);
114+
}
115+
116+
#[tokio::test]
117+
async fn test_question_filter() {
118+
let actual = plan_and_collect("select a from (select 1 as a) where a ? 2;")
119+
.await
120+
.unwrap();
121+
let expected = ["+---+", "| a |", "+---+", "| 1 |", "+---+"];
122+
assert_batches_eq!(&expected, &actual);
123+
}

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::sync::Arc;
2525
use super::dml::CopyTo;
2626
use super::DdlStatement;
2727
use crate::builder::{change_redundant_column, unnest_with_options};
28-
use crate::expr::{Alias, Placeholder, Sort as SortExpr, WindowFunction};
28+
use crate::expr::{Placeholder, Sort as SortExpr, WindowFunction};
2929
use crate::expr_rewriter::{create_col_from_scalar_expr, normalize_cols};
3030
use crate::logical_plan::display::{GraphvizVisitor, IndentVisitor};
3131
use crate::logical_plan::extension::UserDefinedLogicalNode;
@@ -2130,16 +2130,10 @@ impl Filter {
21302130
}
21312131
}
21322132

2133-
// filter predicates should not be aliased
2134-
if let Expr::Alias(Alias { expr, name, .. }) = predicate {
2135-
return plan_err!(
2136-
"Attempted to create Filter predicate with \
2137-
expression `{expr}` aliased as '{name}'. Filter predicates should not be \
2138-
aliased."
2139-
);
2140-
}
2141-
2142-
Ok(Self { predicate, input })
2133+
Ok(Self {
2134+
predicate: predicate.unalias_nested().data,
2135+
input,
2136+
})
21432137
}
21442138

21452139
/// Is this filter guaranteed to return 0 or 1 row in a given instantiation?

0 commit comments

Comments
 (0)