Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support use of ordinal values in SQL ORDER BY clause #16745

Merged
merged 1 commit into from
Jun 6, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jun 5, 2024

Closes #15793.

Factored out the common code we're using to handle GROUP BY1 ordinals and used it to integrate support for the same capability in ORDER BY2 clauses.

Example

df = pl.DataFrame({
    "a": ["xx", "xx", "yy", "yy"],
    "b": [4000, 3000, 2000, 1000],
})
df.sql("""
  SELECT * 
    FROM self 
    ORDER BY 1 DESC, 2 ASC
""")
# shape: (4, 2)
# ┌─────┬──────┐
# │ a   ┆ b    │
# │ --- ┆ ---  │
# │ str ┆ i64  │
# ╞═════╪══════╡
# │ yy  ┆ 1000 │
# │ yy  ┆ 2000 │
# │ xx  ┆ 3000 │
# │ xx  ┆ 4000 │
# └─────┴──────┘
df.sql("""
  SELECT a, SUM(b) AS sum_b 
    FROM self 
    GROUP BY 1 
    ORDER BY 2 DESC
""")
# shape: (2, 2)
# ┌─────┬───────┐
# │ a   ┆ sum_b │
# │ --- ┆ ---   │
# │ str ┆ i64   │
# ╞═════╪═══════╡
# │ xx  ┆ 7000  │
# │ yy  ┆ 3000  │
# └─────┴───────┘

Footnotes

  1. https://github.com/pola-rs/polars/pull/15584

  2. "A sort_expression can also be the column label or number of an output column" https://www.postgresql.org/docs/current/queries-order.html#QUERIES-ORDER

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Jun 5, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jun 5, 2024
Copy link

codspeed-hq bot commented Jun 5, 2024

CodSpeed Performance Report

Merging #16745 will degrade performances by 17.33%

Comparing alexander-beedie:sql-ordinal-order-by (25df658) with main (6f3fd8e)

Summary

❌ 1 regressions
✅ 36 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main alexander-beedie:sql-ordinal-order-by Change
test_groupby_h2oai_q1 2.1 ms 2.6 ms -17.33%

@ritchie46 ritchie46 merged commit 935e8e8 into pola-rs:main Jun 6, 2024
30 checks passed
@alexander-beedie alexander-beedie deleted the sql-ordinal-order-by branch June 6, 2024 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql Area: Polars SQL functionality enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support referencing columns in SQL ORDER BY clauses by ordinal value
2 participants