Databricks/Spark: PERCENTILE_CONT/DISC … WITHIN GROUP is rewritten to PERCENTILE_APPROX, dropping the sort direction
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
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:
- An exact percentile silently becomes an approximation.
- 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
- percentile_cont (https://docs.databricks.com/aws/en/sql/language-manual/functions/percentile_cont): percentile_cont(percentile) WITHIN GROUP (ORDER BY sortKey [ASC | DESC]), Databricks SQL and Runtime 10.4 LTS and above.
- percentile_disc (https://docs.databricks.com/aws/en/sql/language-manual/functions/percentile_disc): the same syntax, Runtime 11.3 LTS and above.
- approx_percentile (https://docs.databricks.com/aws/en/sql/language-manual/functions/approx_percentile) is documented as a separate, approximate function.
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
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của tobymao/sqlglot
-
DuckDB generator: WITHIN GROUP percentile with a CAST fraction produces unbalanced parenthesesĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của tobymao/sqlglot
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
py-econometrics/pyfixest#2072 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
~planning docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Kromatic-Innovation/athenaeum#3084 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bosch Room Thermostat II number entities offer -500 to 3000 °C setpoint limits and a -32768 to 32767 °C outdoor temperatureCó thể đã có người làm @zigpy-review-bot đã nhận hôm nay. Đang mởbot: code audit
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
zigpy/zha-device-handlers#5769 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 80/100
R0Y00/Xbox2Windows#254 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Kludex/python-multipart#375 ·
Maintainer thường phản hồi trong vòng 1 ngày