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: Add SQL support for TRY_CAST function #16589

Merged
merged 1 commit into from
May 30, 2024

Conversation

alexander-beedie
Copy link
Collaborator

Added SQL support for the TRY_CAST function (as implemented by various1 databases).

Example

import polars as pl
df = pl.DataFrame({"n": [-10, 0, 10, 20]})

Strict (default) CAST fails on invalid values ...

df.sql("SELECT CAST(n AS uint1) FROM df")
# ComputeError: conversion from `i64` to `u8` failed in column 'n' for 1 out of 4 values: [-10]

... whereas TRY_CAST replace invalid values with null:

df.sql("SELECT TRY_CAST(n AS uint1) FROM df")
# shape: (4, 1)
# ┌──────┐
# │ n    │
# │ ---  │
# │ u8   │
# ╞══════╡
# │ null │
# │ 0    │
# │ 10   │
# │ 20   │
# └──────┘

Footnotes

  1. TRY_CAST: SQL Server, DuckDB

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

codecov bot commented May 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.48%. Comparing base (ef1c0c8) to head (2097e7e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16589      +/-   ##
==========================================
- Coverage   81.50%   81.48%   -0.02%     
==========================================
  Files        1414     1414              
  Lines      185612   185619       +7     
  Branches     2997     2997              
==========================================
- Hits       151288   151259      -29     
- Misses      33808    33844      +36     
  Partials      516      516              

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

@ritchie46 ritchie46 merged commit fd808ea into pola-rs:main May 30, 2024
27 checks passed
@alexander-beedie alexander-beedie deleted the sql-try-cast branch May 30, 2024 06:28
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.

3 participants