Databricks/Spark: PERCENTILE_CONT/DISC … WITHIN GROUP is rewritten to PERCENTILE_APPROX, dropping the sort direction

Đang mở
#9,306 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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
66/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ệ
python, spark, sql
Lĩnh vực
databases

Hướng nghiên cứu

Start in sqlglot/dialects/spark.py, where Spark2Generator maps exp.WithinGroup through transforms.remove_within_group_for_percentiles; confirm that SparkGenerator and the Databricks generator inherit this mapping. Run the reproduction from the issue to see the approximation and lost DESC. The fix should scope the rewrite to Spark 2 only and preserve WITHIN GROUP for Spark 3+/Databricks, with tests in the Spark and Databricks dialect test files covering both PERCENTILE_CONT and PERCENTILE_DISC and sort direction.

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

Mô tả

When writing the databricks or spark dialects, an exact PERCENTILE_CONT or PERCENTILE_DISC with WITHIN GROUP is rewritten to PERCENTILE_APPROX(col, p). This has two effects:

  1. An exact percentile silently becomes an approximation.
  2. ORDER BY … DESC is dropped, so the result is a different value, not just a less precise one.

The same rewrite happens when the source dialect is Databricks, so Databricks to Databricks changes the query's meaning.

Reproduction (sqlglot 30.24.0)

import sqlglot

for sql in [
"SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x) FROM t",
"SELECT PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY x DESC) FROM t",
"SELECT PERCENTILE_DISC(0.9) WITHIN GROUP (ORDER BY x) FROM t",
]:
for dialect in ("postgres", "duckdb", "snowflake", "databricks", "spark"):
print(dialect.ljust(11), sqlglot.transpile(sql, read="postgres", write=dialect)[0])

Databricks to Databricks

print(sqlglot.transpile(
"SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x DESC) FROM t",
read="databricks", write="databricks")[0])

Actual output (second query shown; the other two behave the same way)

postgres SELECT PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY x DESC) FROM t
duckdb SELECT QUANTILE_CONT(x, 0.25 ORDER BY x DESC NULLS FIRST) FROM t
snowflake SELECT PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY x DESC) FROM t
databricks SELECT PERCENTILE_APPROX(x, 0.25) FROM t
spark SELECT PERCENTILE_APPROX(x, 0.25) FROM t

The Databricks to Databricks case prints SELECT PERCENTILE_APPROX(x, 0.5) FROM t.

Expected

Databricks supports the exact form, so the output should keep it, for example:

SELECT PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY x DESC) FROM t

Cause

Spark2Generator has:

exp.WithinGroup: transforms.preprocess([transforms.remove_within_group_for_percentiles]),

SparkGenerator (the Spark 3+ dialect) and DatabricksGenerator inherit it without overriding it. remove_within_group_for_percentiles replaces the percentile with exp.ApproxQuantile built from the sort column and the fraction, and discards the Order node, including its direction. That seems right for Spark 2, which lacks the WITHIN GROUP form, but it also applies to Spark 3+ and Databricks, which have it as of Spark 3.3.0

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

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.