Skip to content

Commit 0e6be30

Browse files
authored
feat: Implement SessionState::create_logical_expr_from_sql_expr (#18423)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #18278 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Convenience method for when parsing has already been done, and we want to start from a an expr object instead of SQL string. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Added test ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> Yes, new public api. <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent a6fab6b commit 0e6be30

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
@@ -547,6 +547,16 @@ impl SessionState {
547547

548548
let sql_expr = self.sql_to_expr_with_alias(sql, &dialect)?;
549549

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

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

0 commit comments

Comments
 (0)