sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

SQLite source CONCAT loses NULL-skipping semantics during transpilation

Open emme1t opened this issue 2 months ago • 5 comments

CONCAT parsed with read="sqlite" loses its NULL-skipping behavior when written back to SQLite. SQLite's native function returns an empty string when all arguments are NULL; the generated || expression propagates NULL.

Reproduced on current main, 5bd170049d9d4d079e7200f55eaaacde294d1ca1, with Python 3.13.13 and SQLite 3.50.4:

import sqlite3
import sqlglot

db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE t (id INTEGER, x TEXT, y TEXT)")
db.executemany(
    "INSERT INTO t VALUES (?, ?, ?)",
    [(1, "a", None), (2, None, "b"), (3, None, None), (4, "a", "b")],
)

source = "SELECT id, CONCAT(x, y) AS value FROM t ORDER BY id"
generated = sqlglot.transpile(source, read="sqlite", write="sqlite")[0]
print(generated)
print(db.execute(source).fetchall())
print(db.execute(generated).fetchall())

Output:

SELECT id, x || y AS value FROM t ORDER BY id
[(1, 'a'), (2, 'b'), (3, ''), (4, 'ab')]
[(1, None), (2, None), (3, None), (4, 'ab')]

The two result sets should match. SQLite documents that concat() concatenates its non-NULL arguments and returns an empty string when all arguments are NULL.

This is related to #8283. That fix preserves coalesce=True for source dialects such as DuckDB and PostgreSQL. The SQLite source parser currently creates this Concat with coalesce=False, so the same-dialect example above still loses the source behavior.

OpenAI Codex assisted with source review, the reproducer, and this description. Both SQL statements were executed locally against SQLite to verify the differing results.

emme1t avatar Sep 12 '26 07:09 emme1t

I’m taking this issue. I’m working on it and will raise a PR ASAP

Kunal8954 avatar Sep 12 '26 09:09 Kunal8954

I am testing an Astra-powered automated testing tool that helps maintainers refine code and resolve bugs on GitHub. All issues and PRs are auto-generated by the program. I’ve reviewed all the reports produced by Astra. This message from emmett is a manual reply. BR emme1t ---- 回复的原邮件 ---- @.>发送日期2026年09月12日 17:11 @.> @.>, @.>主题Re: [tobymao/sqlglot] SQLite source CONCAT loses NULL-skipping semantics during transpilation (Issue #8343) Kunal8954 left a comment (tobymao/sqlglot#8343) ’m currently working on this issue. I’ll raise a PR as soon as possible. — Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today! You are receiving this because you authored the thread.Message ID: @.***>

emme1t avatar Sep 12 '26 09:09 emme1t

I’ve traced the SQLite CONCAT() NULL semantics and the existing #8283 implementation. There are two possible fixes: Option A: Make SQLite’s CONCAT_COALESCE semantics explicit and emit native CONCAT(...) for SQLite targets. This preserves same-dialect round-tripping, but requires SQLite 3.44+ for generated CONCAT() SQL. Option B: Keep SQLite targets using ||, but have the SQLite parser mark CONCAT(...) as coalescing so SQLGlot emits COALESCE(a, '') || COALESCE(b, ''). This is version-agnostic and preserves the existing SQLite || output convention. Which direction would you prefer for #8343? I’m happy to implement either and add the regression coverage.

Kunal8954 avatar Sep 12 '26 09:09 Kunal8954

@Kunal8954 Thanks for investigating this and laying out both options. Could we also consider Option C: target-version/capability-aware generation, with B as the default?

  • When the caller explicitly identifies the target as SQLite 3.44+ with native CONCAT support, emit CONCAT(a, b).
  • For older targets, emit COALESCE(a, '') || COALESCE(b, '').
  • When the target version/capability is unknown, default to the B output.

The SQLite parser would still need to preserve the source CONCAT's NULL-skipping semantics in the expression. The target capability would then determine the generated SQL.

For callers with a database connection, the application could obtain the version through SELECT sqlite_version() on the actual execution target and pass that information to SQLGlot. Explicit configuration could cover offline generation. This would keep the choice tied to the database that will execute the SQL.

Does SQLGlot already have a suitable target-version or capability mechanism for this? If adding one would broaden the scope substantially, B seems like a good immediate fix, with C discussed as a follow-up enhancement.

Regression coverage could compare both outputs for single/all-NULL arguments, empty strings, and non-string inputs to verify conversion behavior as well.

This suggestion was drafted with Codex assistance.

emme1t avatar Sep 12 '26 12:09 emme1t

Thanks for the clarification. I’ll proceed with Option B as the immediate fix and keep the target-version/capability-aware generation as a possible follow-up.

Kunal8954 avatar Sep 12 '26 15:09 Kunal8954