DuckDB: printf-style FORMAT from BigQuery/Spark/SQLite/Postgres is written as DuckDB FORMAT, which uses {} placeholders

Đang mở
#9,112 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
30/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
postgresql, python, sql, sqlite
Lĩnh vực
databases

Hướng nghiên cứu

Start by running the provided sqlglot.transpile snippet to confirm the four faulty outputs, then trace how exp.Format is parsed from BigQuery/Spark/SQLite/Postgres versus DuckDB and where DuckDBGenerator.format_sql writes it. The issue notes DuckDB PRINTF currently parses as Anonymous, so compare that parse path with DuckDB fmt-style FORMAT and the UUID_STRING template. Done means printf-style formats emit DuckDB PRINTF (or {} FORMAT for %s-only), DuckDB fmt-style FORMAT round-trips correctly, and tests cover the affected dialect pairs.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Fully reproducible code snippet

BigQuery FORMAT, Spark FORMAT_STRING, SQLite FORMAT and Postgres FORMAT all use printf-style % specifiers and parse to exp.Format. The DuckDB generator writes that as DuckDB FORMAT, but DuckDB's FORMAT uses fmt-style {} placeholders, so the query runs and just returns the format string:

import sqlglot, duckdb

for d, q in [
    ("bigquery", "SELECT FORMAT('%d-%s', 1, 'a')"),
    ("spark", "SELECT FORMAT_STRING('%d-%s', 1, 'a')"),
    ("sqlite", "SELECT FORMAT('%d-%s', 1, 'a')"),
    ("postgres", "SELECT FORMAT('%s-%s', 1, 'a')"),
]:
    out = sqlglot.transpile(q, read=d, write="duckdb")[0]
    print(d, out, "->", duckdb.sql(out).fetchall()[0][0])

On main (b294f7f) with DuckDB 2.0.0:

bigquery SELECT FORMAT('%d-%s', 1, 'a') -> %d-%s
spark    SELECT FORMAT('%d-%s', 1, 'a') -> %d-%s
sqlite   SELECT FORMAT('%d-%s', 1, 'a') -> %d-%s
postgres SELECT FORMAT('%s-%s', 1, 'a') -> %s-%s

Expected 1-a in all four. DuckDB has PRINTF for this (SELECT PRINTF('%d-%s', 1, 'a') -> 1-a). One catch: DuckDB's PRINTF only takes strings for %s (PRINTF('%s', 1) errors), so a %s-only format like the Postgres one is better written as FORMAT('{}-{}', 1, 'a'). DuckDBGenerator.format_sql already does this for the single '%s' case.

The other direction has the same mix-up: DuckDB FORMAT('{}-{}', 1, 2) parses to exp.Format too and is written as BigQuery FORMAT('{}-{}', 1, 2) / Spark FORMAT_STRING('{}-{}', 1, 2), and DuckDB PRINTF parses as Anonymous.

So exp.Format currently means printf-style when read from BigQuery/Spark/SQLite/Postgres and fmt-style when read from DuckDB (the DuckDB UUID_STRING template also builds FORMAT('{:02x}', ...) as exp.Format). Possible fix: treat exp.Format as printf-style, parse DuckDB PRINTF into it, generate PRINTF for DuckDB (or FORMAT with {} when %s is the only specifier), and give DuckDB's fmt-style FORMAT its own handling. I have a branch for the generator side and can open a PR if this direction sounds right.

Official Documentation

Ngôn ngữ chính
Python
Star
9.8k
Fork
1.4k
Merge trung bình
13 giờ 19 phút
Pull request đã merge (30 ngày)
243

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của tobymao/sqlglot

Tất cả issue của tobymao/sqlglot

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.