SQLite: CAST(x AS JSON) is generated verbatim, which SQLite evaluates as a numeric cast
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- Mezza giornata
- Idoneità per principianti
- 32/100
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
- Type affinity rules (a declared type with none of INT/CHAR/CLOB/TEXT/BLOB/REAL/FLOA/DOUB is NUMERIC): https://www.sqlite.org/datatype3.html#determination_of_column_affinity
- CAST to NUMERIC affinity: https://www.sqlite.org/lang_expr.html#castexpr
json(X): https://www.sqlite.org/json1.html#jmini
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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di tobymao/sqlglot
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
I maintainer di solito rispondono entro 1 giorno
-
Optimizer drops parentheses for `NOT (<date> = DATE_TRUNC(...))`, changing query resultsForse già presa @parthdembla007 l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 28/100
I maintainer di solito rispondono entro 1 giorno
-
BigQuery: region-qualified INFORMATION_SCHEMA reference is rewritten across transpile passes, gaining an invented aliasForse già presa @ansh-rohilla l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 28/100
I maintainer di solito rispondono entro 1 giorno
-
DATE_ADD/DATE_SUB with a single argument raises AttributeError (BigQuery, MySQL, Doris, StarRocks)Forse già presa @akchhayasharma l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 42/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di tobymao/sqlglot
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
alltheplaces/alltheplaces#20570 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
area:ci bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
catincloud-labs/dagster-dex#206 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
number_to_words raises ValueError with threshold= for ordinals ("12th") and comma-grouped strings ("1,000")Forse già presa @hemtilva l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
providers security
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
agentbreeder/agentbreeder#1031 ·
I maintainer di solito rispondono entro 1 giorno