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 SQL VALUES clause and inline renaming of columns in CTE & derived table definitions #16851

Merged
merged 2 commits into from
Jun 11, 2024

Conversation

alexander-beedie
Copy link
Collaborator

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

Adds new SQL interface support for...

  • The VALUES1 clause (doesn't support inline literal casts or typed literals yet).
  • Column (re)naming in derived table aliases (eg: CTEs and VALUES clause).

Example

Standalone VALUES clause:

pl.sql("VALUES (1,2), (3,4)").collect()
# shape: (2, 2)
# ┌──────────┬──────────┐
# │ column_0 ┆ column_1 │
# │ ---      ┆ ---      │
# │ i32      ┆ i32      │
# ╞══════════╪══════════╡
# │ 1        ┆ 2        │
# │ 3        ┆ 4        │
# └──────────┴──────────┘

Select from VALUES, applying column names with the table alias:

pl.sql("SELECT * FROM (VALUES(1,2), (3,4)) AS tbl(colx, coly)").collect()
# shape: (2, 2)
# ┌──────┬──────┐
# │ colx ┆ coly │
# │ ---  ┆ ---  │
# │ i32  ┆ i32  │
# ╞══════╪══════╡
# │ 1    ┆ 2    │
# │ 3    ┆ 4    │
# └──────┴──────┘

Demonstrate/stress-test name/alias resolution; select from inner VALUES and show that we successfully map column/table names through multiple levels of nested CTEs and derived table column aliasing.

df = pl.sql(
  """
  WITH
    x AS (SELECT w.* FROM (VALUES(1,2), (3,4)) AS w(a, b)),
    y (m, n) AS (
      WITH z(c, d) AS (SELECT a, b FROM x)
        SELECT d*2 AS d2, c*3 AS c3 FROM z
  )
  SELECT n, m FROM y
  """,
  eager=True,
)
# shape: (2, 2)
# ┌─────┬─────┐
# │ n   ┆ m   │
# │ --- ┆ --- │
# │ i32 ┆ i32 │
# ╞═════╪═════╡
# │ 3   ┆ 4   │
# │ 9   ┆ 8   │
# └─────┴─────┘

Footnotes

  1. PostgreSQL VALUES clause: https://www.postgresql.org/docs/current/sql-values.html

@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 10, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jun 10, 2024
Copy link

codecov bot commented Jun 10, 2024

Codecov Report

Attention: Patch coverage is 97.56098% with 1 line in your changes missing coverage. Please review.

Project coverage is 81.39%. Comparing base (3a56f09) to head (73a63a3).
Report is 8 commits behind head on main.

Files Patch % Lines
crates/polars-sql/src/context.rs 97.56% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16851      +/-   ##
==========================================
- Coverage   81.39%   81.39%   -0.01%     
==========================================
  Files        1425     1425              
  Lines      187627   187701      +74     
  Branches     2702     2702              
==========================================
+ Hits       152727   152784      +57     
- Misses      34404    34421      +17     
  Partials      496      496              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ritchie46 ritchie46 merged commit 13d68ae into pola-rs:main Jun 11, 2024
33 checks passed
@alexander-beedie alexander-beedie deleted the sql-values-clause branch June 11, 2024 06:35
Wouittone pushed a commit to Wouittone/polars that referenced this pull request Jun 22, 2024
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.

2 participants