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

オープン
#9,474 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
clickhouse, python, sql

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
9.8k
フォーク
1.5k
平均マージ
10時間 54分
マージ済み PR(30日)
310

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tobymao/sqlglot のほかの issue

tobymao/sqlglot の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。