Repository navigation
Conversation
What do you mean? That it's not possible to set a custom SQL dialect from a string name, e.g. What does the config return once you set a custom dialect using these APIs ( |
Makes sense, most likely it makes sense for users using this API to make sure they set their |
|
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days. |
## Which issue does this PR close? - Closes apache#23109. This picks up the approach of apache#23235, which the stale bot closed without a review decision. This PR adds the clone-path, `SET` and expression-path coverage, and documents the precedence rule. ## Rationale for this change `datafusion.sql_parser.dialect` accepts only a closed list of names. `SessionState::sql_to_statement` and `SessionState::sql_to_expr_with_alias` resolve that name with `dialect_from_str` on every parse. Thus an application with its own `sqlparser::dialect::Dialect`, for example one built with `derive_dialect!` on top of `PostgreSqlDialect` (apache/datafusion-sqlparser-rs#2174), cannot plug it in. It must wrap every parse entry point itself. A call to a stock method such as `SessionContext::sql` then parses with the wrong dialect, and the query fails with a parse error. ## What changes are included in this PR? - New `SessionStateBuilder::with_sql_dialect(Arc<dyn Dialect + Send + Sync>)`, plus the getters `SessionState::sql_dialect()` and `SessionStateBuilder::sql_dialect()`. - `sql_to_statement` and `sql_to_expr_with_alias` use the dialect object when it is set. All other parse paths go through these two methods: `SessionContext::sql`, `sql_with_options`, `create_logical_plan`, `create_logical_expr` and `sql_to_expr`. - Precedence: the dialect object wins over the dialect name. This includes an explicit `dialect` argument to `sql_to_statement` or `sql_to_expr`, and a later `SET datafusion.sql_parser.dialect = ...`. `SET` still changes the config value, but not the parser. - `SessionStateBuilder::new_from_existing` and `From<SessionState>` keep the dialect object. - If no dialect object is set, the behavior does not change. `datafusion-cli` resolves the dialect name itself and does not expose a way to set a dialect object, so this PR does not change it. ## Are these changes tested? Yes. `test_custom_sql_dialect` uses a small hand-written dialect: PostgreSQL plus `SELECT * EXCEPT (...)`. The `derive-dialect` feature of sqlparser is not enabled in this workspace. The test checks that: - the PostgreSQL dialect name alone rejects the query (default behavior unchanged), - `ctx.sql(...)`, `create_logical_plan`, `sql_to_statement`, `sql_to_expr` and `create_logical_expr` parse with the dialect object, - a `SET datafusion.sql_parser.dialect` statement does not replace it, - `new_from_existing` and `From<SessionState>` keep it. The rustdoc example on `with_sql_dialect` runs as a doctest. ## Are there any user-facing changes? Yes. This PR adds a new public builder method and two getters. There are no breaking changes. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Which issue does this PR close?
Fixes #23109.
Rationale for this change
What changes are included in this PR?
This is one approach that tries solving #23109, by allowing the SQL parser dialect to be configured at the
SessionStatelevel.It's still a bit inconsistent, because it doesn't follow what is set at the config level (see issue), but I guess better than nothing?
Are these changes tested?
Yes.
Are there any user-facing changes?
No.