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

Abierto
#9,113 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@Gowthamsuryagolla ya está trabajando en esto.

Desde el 15/11/2026.

  • #9116 de @Gowthamsuryagolla — abierto

Evaluación

Dificultad
3/5
Tiempo estimado
Medio día
Aptitud para principiantes
32/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
postgresql, python, sqlite
Área
databases

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
9.8k
Forks
1.4k
Merge medio
13 h 19 min
PR fusionados (30 d)
243

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tobymao/sqlglot

Todos los issues de tobymao/sqlglot

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.