Skip to content

Commit c718319

Browse files
committed
Implement create_logical_expr_from_sql_expr() and add test
1 parent e65dafe commit c718319

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

datafusion/core/src/execution/session_state.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,16 @@ impl SessionState {
545545

546546
let sql_expr = self.sql_to_expr_with_alias(sql, &dialect)?;
547547

548+
self.create_logical_expr_from_sql_expr(sql_expr, df_schema)
549+
}
550+
551+
/// Creates a datafusion style AST [`Expr`] from a SQL expression.
552+
#[cfg(feature = "sql")]
553+
pub fn create_logical_expr_from_sql_expr(
554+
&self,
555+
sql_expr: SQLExprWithAlias,
556+
df_schema: &DFSchema,
557+
) -> datafusion_common::Result<Expr> {
548558
let provider = SessionContextProvider {
549559
state: self,
550560
tables: HashMap::new(),
@@ -2095,6 +2105,36 @@ mod tests {
20952105
assert!(sql_to_expr(&state).is_err())
20962106
}
20972107

2108+
#[test]
2109+
#[cfg(feature = "sql")]
2110+
fn test_create_logical_expr_from_sql_expr() {
2111+
let state = SessionStateBuilder::new().with_default_features().build();
2112+
2113+
let provider = SessionContextProvider {
2114+
state: &state,
2115+
tables: HashMap::new(),
2116+
};
2117+
2118+
let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
2119+
let df_schema = DFSchema::try_from(schema).unwrap();
2120+
let dialect = state.config.options().sql_parser.dialect;
2121+
let query = SqlToRel::new_with_options(&provider, state.get_parser_options());
2122+
2123+
for sql in ["[1,2,3]", "a > 10", "SUM(a) AS s"] {
2124+
let sql_expr = state.sql_to_expr(sql, &dialect).unwrap();
2125+
let from_str = query
2126+
.sql_to_expr(sql_expr, &df_schema, &mut PlannerContext::new())
2127+
.unwrap();
2128+
2129+
let sql_expr_with_alias =
2130+
state.sql_to_expr_with_alias(sql, &dialect).unwrap();
2131+
let from_expr = state
2132+
.create_logical_expr_from_sql_expr(sql_expr_with_alias, &df_schema)
2133+
.unwrap();
2134+
assert_eq!(from_str, from_expr);
2135+
}
2136+
}
2137+
20982138
#[test]
20992139
fn test_from_existing() -> Result<()> {
21002140
fn employee_batch() -> RecordBatch {

0 commit comments

Comments
 (0)