Skip to content

Add ability to configure SQL parser dialect in SessionState - #23235

Closed
Viicos wants to merge 1 commit into
apache:mainfrom
Viicos:vp/session-state-sql-dialect
Closed

Viicos wants to merge 1 commit into
apache:mainfrom
Viicos:vp/session-state-sql-dialect

Conversation

@Viicos

@Viicos Viicos commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

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 SessionState level.

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.

@github-actions github-actions Bot added the core Core DataFusion crate label Jun 29, 2026
@adriangb

Copy link
Copy Markdown
Contributor

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?

What do you mean? That it's not possible to set a custom SQL dialect from a string name, e.g. SET datafusion.sql_parser.dialect = 'custom'? If so this seems like a non-issue to me. There's plenty of dyn APIs that are not configurable from SQL, I'd consider this a pretty advanced usage of DataFusion.

What does the config return once you set a custom dialect using these APIs (SHOW datafusion.sql_parser.dialect;)?

@Viicos

Viicos commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor Author

What do you mean? That it's not possible to set a custom SQL dialect from a string name, e.g. SET datafusion.sql_parser.dialect = 'custom'? If so this seems like a non-issue to me. There's plenty of dyn APIs that are not configurable from SQL, I'd consider this a pretty advanced usage of DataFusion.

Makes sense, most likely it makes sense for users using this API to make sure they set their datafusion.sql_parser.dialect config to a custom value like 'my_custom_dialect' or something, but actually I'm wondering if this is possible right now, as the config only allows a fixed set of values.

@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the Stale PR has not had any activity for some time label Sep 16, 2026
@github-actions github-actions Bot closed this Sep 24, 2026
adriangb added a commit to adriangb/datafusion that referenced this pull request Nov 25, 2026
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate Stale PR has not had any activity for some time

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to configure custom SQL parser dialects on session config

2 participants