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: Initial support for SQL ARRAY literals and the UNNEST table function #16330

Merged
merged 6 commits into from
May 31, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented May 20, 2024

Update

Started extending our SQL interface support to list/array data:

  • Can now use SQL ARRAY literals and declare UNNEST table functions.
  • Can write array literals as either [1,2,3] or ARRAY[1,2,3].
  • Cast with col::type[] or CAST(col AS type[]) syntax.

Examples

import polars as pl

with pl.SQLContext(df=None) as sql:
    df1 = sql.execute(
        """
        SELECT 
          ['x','y'] AS a1,
          [3,4,5,6,7,8]::float4[] AS a2 
        FROM df
        """
    ).collect()
    
    # shape: (1, 2)
    # ┌────────────┬───────────────────┐
    # │ a1         ┆ a2                │
    # │ ---        ┆ ---               │
    # │ list[str]  ┆ list[f32]         │
    # ╞════════════╪═══════════════════╡
    # │ ["x", "y"] ┆ [3.0, 4.0, … 8.0] │
    # └────────────┴───────────────────┘

    df2 = sql.execute(
        """
        SELECT * FROM
          UNNEST(
            [10, 20, 30, 40],
            ['ww','xx','yy','zz'],
            [23.0, 24.5, 28.0, 27.5]
          ) AS tbl (x,y,z)
        """
    ).collect()

    # shape: (4, 3)
    # ┌─────┬─────┬──────┐
    # │ x   ┆ y   ┆ z    │
    # │ --- ┆ --- ┆ ---  │
    # │ i64 ┆ str ┆ f64  │
    # ╞═════╪═════╪══════╡
    # │ 10  ┆ ww  ┆ 23.0 │
    # │ 20  ┆ xx  ┆ 24.5 │
    # │ 30  ┆ yy  ┆ 28.0 │
    # │ 40  ┆ zz  ┆ 27.5 │
    # └─────┴─────┴──────┘

Follow-ups

  • Contribute a PR to sqlparser-rs to support UNNEST ... WITH ORDINALITY.
  • Support nested array literals (eg: [[1,2,3], [4,5,6]]).

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

codecov bot commented May 20, 2024

Codecov Report

Attention: Patch coverage is 94.68085% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 81.50%. Comparing base (b85c5e0) to head (008e0fb).
Report is 2 commits behind head on main.

Files Patch % Lines
crates/polars-sql/src/sql_expr.rs 94.11% 3 Missing ⚠️
crates/polars-sql/src/context.rs 95.34% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16330      +/-   ##
==========================================
- Coverage   81.51%   81.50%   -0.01%     
==========================================
  Files        1414     1414              
  Lines      185944   185997      +53     
  Branches     3027     3027              
==========================================
+ Hits       151569   151606      +37     
- Misses      33844    33861      +17     
+ Partials      531      530       -1     

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

Copy link

codspeed-hq bot commented May 20, 2024

CodSpeed Performance Report

Merging #16330 will improve performances by 19.76%

Comparing alexander-beedie:sql-array-literal (008e0fb) with main (b85c5e0)

Summary

⚡ 1 improvements
✅ 36 untouched benchmarks

Benchmarks breakdown

Benchmark main alexander-beedie:sql-array-literal Change
test_tpch_q22 6.3 ms 5.3 ms +19.76%

@alexander-beedie alexander-beedie force-pushed the sql-array-literal branch 7 times, most recently from 156f31f to 9a6945e Compare May 30, 2024 07:35
@ritchie46 ritchie46 merged commit 1cafcbc into pola-rs:main May 31, 2024
26 checks passed
@alexander-beedie alexander-beedie deleted the sql-array-literal branch May 31, 2024 13:21
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