SQLite: CAST(x AS JSON) is generated verbatim, which SQLite evaluates as a numeric cast

Aperta
#9,113 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Gowthamsuryagolla ci sta già lavorando.

Dal 15/11/2026.

  • #9116 di @Gowthamsuryagolla — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
32/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
postgresql, python, sqlite
Ambito
databases

Direzione di ricerca

The SQLite generator lives in sqlglot/dialects/sqlite.py; start by finding where exp.Cast is rendered there and compare with the temporal-cast handling added in #8793. Reproduce with the snippet in the issue, then check the existing expectations in tests/dialects/test_sqlite.py. Done means CAST(x AS JSON)/JSONB emits JSON(x) while non-JSON casts are untouched, and the round-trip test plus the full dialect test file pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Fully reproducible code snippet

SQLite has no JSON type, so CAST(x AS JSON) gets NUMERIC affinity: CAST('{"a": 1}' AS JSON) evaluates to the integer 0. The sqlite generator emits JSON casts unchanged, so a very common Postgres pattern silently returns NULL after transpiling:

import sqlite3, sqlglot

sql = """SELECT '{"a": 1}'::json ->> 'a'"""
out = sqlglot.transpile(sql, read="postgres", write="sqlite")[0]
print(out)
# SELECT CAST('{"a": 1}' AS JSON) ->> '$.a'
print(sqlite3.connect(":memory:").execute(out).fetchall())
# [(None,)]   Postgres returns '1'

Same with read="duckdb" / read="mysql": SELECT CAST('{"a": 1}' AS JSON) stays as is, and SQLite returns 0 instead of the JSON text.

Expected: JSON(x) (or just x) for JSON/JSONB casts, like the temporal casts that became DATE(x) / DATETIME(x) / TIME(x) in #8793. JSON(x) keeps the cast's validation (it errors on malformed input), and JSON('{"a": 1}') ->> '$.a' returns 1. BigQuery's JSON '...' literal already becomes a plain string here, so the target output works fine with SQLite's JSON functions.

Official Documentation

Tested on main at b294f7f, SQLite 3.54.1.

Lingua principale
Python
Stelle
9.8k
Fork
1.4k
Merge medio
13h 19m
PR unite (30g)
243

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tobymao/sqlglot

Tutte le issue di tobymao/sqlglot

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.