alamb commented on issue #8736: URL: https://github.com/apache/datafusion/issues/8736#issuecomment-2176140997
This came up again on discord: https://discord.com/channels/885562378132000778/1166447479609376850/1252616019357470731 Here is some code that @Omega359 shared ```rust pub(crate) fn build_expression(norm: &FieldNormalizer, df: &DataFrame, expression: &str) -> Result<Expr> { let parser = Parser::new(&AnsiDialect {}); let tokenized = parser.try_with_sql(expression); if tokenized.is_err() { bail!("Unable to tokenize expression '{}': {:?}", &expression, tokenized.err()) } let parsed = tokenized?.parse_expr(); if parsed.is_err() { bail!("Unable to parse expression '{}': {:?}", &expression, parsed.err()) } let expr = parsed?; debug!("Parsed expression was '{}'", expr); let parser_options = ParserOptions { enable_ident_normalization: false, parse_float_as_decimal: false, }; let table_schema_provider = TableSchemaProvider::new(norm.table_name.clone(), norm.session_context.clone()); let sql_to_rel = SqlToRel::new_with_options(&table_schema_provider, parser_options); let sql_to_expr = sql_to_rel.sql_to_expr(expr.clone(), df.schema(), &mut PlannerContext::new()); if sql_to_expr.is_err() { bail!("Unable to transform sql expression '{}' to datafusion Expr: {:?}", &expression, sql_to_expr.err()) } Ok(sql_to_expr?) } ``` I think it would be great to add an API to `DataFrame` and `SessionContext` to do this: ```rust impl SessionContext { /// parse the provided expression as a string fn parse_sql(&self, sql: &str) -> Result<Expr> { ... } } ``` ```rust impl DataFrame { /// parse the provided expression as a string fn parse_sql(&self, sql: &str) -> Result<Expr> { ... } } ``` Perhaps @xinlifoobar has time to help here 🤔 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org