ClickHouse: DAY_OF_WEEK is read as DayOfWeek, so every call is written back as toDayOfWeek(t, 3)

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

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
clickhouse, python, sql

Línea de trabajo

Start by tracing how the ClickHouse parser maps DAY_OF_WEEK and how DayOfWeek stores its arguments; compare that path with the correctly round-tripping DAYOFWEEK spelling. Run the reproductions in the issue and inspect the related ClickHouse parsing and generation tests. Done when calls with no mode and with modes 1, 2, or 3 round-trip with their documented results preserved.

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

Descripción

ClickHouse documents DAY_OF_WEEK as an alias of toDayOfWeek, like dayOfWeek and DAYOFWEEK. SQLGlot reads DAY_OF_WEEK as DayOfWeek (Sunday = 1) instead, so every call, with or without a mode, is written back as mode 3:

import sqlglot

for sql in ["SELECT DAY_OF_WEEK(t)", "SELECT DAY_OF_WEEK(t, 1)", "SELECT DAY_OF_WEEK(t, 2)"]:
    print(sqlglot.transpile(sql, read="clickhouse", write="clickhouse")[0])
# SELECT toDayOfWeek(t, 3)
# SELECT toDayOfWeek(t, 3)
# SELECT toDayOfWeek(t, 3)

The other spellings round-trip correctly:

sqlglot.transpile("SELECT DAYOFWEEK(t)", read="clickhouse", write="clickhouse")
# ['SELECT toDayOfWeek(t)']
sqlglot.transpile("SELECT DAYOFWEEK(t, 2)", read="clickhouse", write="clickhouse")
# ['SELECT toDayOfWeek(t, 2)']

Expected: DAY_OF_WEEK(t) reads like toDayOfWeek(t) (mode 0, Monday = 1 … Sunday = 7), and DAY_OF_WEEK(t, mode) keeps its mode. The mode changes the numbering: 0 Monday = 1 … 7, 1 Monday = 0 … 6, 2 Sunday = 0 … 6, 3 Sunday = 1 … 7. So the rewrite to mode 3 changes the result of DAY_OF_WEEK(t) on every day of the week.

DayOfWeek holds this and zero_based, so a two-argument call passes the arity check and the mode is dropped silently.

Docs: https://clickhouse.com/docs/reference/functions/regular-functions/date-time-functions (toDayOfWeek: "Aliases: DAYOFWEEK, DAY_OF_WEEK", and its mode table).

Checked on 30.37.0. I used an LLM (Claude) to help narrow this down and write it up.

Lenguaje dominante
Python
Estrellas
9.8k
Forks
1.5k
Merge medio
10 h 54 min
PR fusionados (30 d)
310

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.