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(rust,python,cli): add SQL engine support for IFNULL function #13432

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions crates/polars-sql/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,28 @@ pub(crate) enum PolarsSQLFunctions {
/// SELECT UPPER(column_1) from df;
/// ```
Upper,
/// SQL 'nullif' function
/// Returns NULL if two expressions are equal, otherwise returns the first
/// ```sql
/// SELECT NULLIF(column_1, column_2) from df;
/// ```
NullIf,

// ----
// Conditional functions
// ----
/// SQL 'coalesce' function
/// Returns the first non-null value in the provided values/columns
/// ```sql
/// SELECT COALESCE(column_1, ...) from df;
/// ```
Coalesce,
/// SQL 'ifnull' function
/// If an expression value is NULL, return an alternative value.
/// ```sql
/// SELECT IFNULL(string_col, 'n/a') from df;
/// ```
IfNull,
/// SQL 'nullif' function
/// Returns NULL if two expressions are equal, otherwise returns the first
/// ```sql
/// SELECT NULLIF(column_1, column_2) from df;
/// ```
NullIf,

// ----
// Aggregate functions
Expand Down Expand Up @@ -581,9 +591,10 @@ impl PolarsSQLFunctions {
"radians" => Self::Radians,

// ----
// Comparison functions
// Conditional functions
// ----
"coalesce" => Self::Coalesce,
"ifnull" => Self::IfNull,
"nullif" => Self::NullIf,

// ----
Expand Down Expand Up @@ -701,15 +712,19 @@ impl SQLFunctionVisitor<'_> {
}))
}),
_ => {
polars_bail!(InvalidOperation:"Invalid number of arguments for Round: {}",function.args.len());
polars_bail!(InvalidOperation:"Invalid number of arguments for Round: {}", function.args.len());
},
},

// ----
// Comparison functions
// Conditional functions
// ----
NullIf => self.visit_binary(|l, r: Expr| when(l.clone().eq(r)).then(lit(LiteralValue::Null)).otherwise(l)),
Coalesce => self.visit_variadic(coalesce),
IfNull => match function.args.len() {
2 => self.visit_variadic(coalesce),
_ => polars_bail!(InvalidOperation:"Invalid number of arguments for IfNull: {}", function.args.len())
},
NullIf => self.visit_binary(|l, r: Expr| when(l.clone().eq(r)).then(lit(LiteralValue::Null)).otherwise(l)),

// ----
// String functions
Expand Down
21 changes: 14 additions & 7 deletions py-polars/tests/unit/sql/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,28 +1161,35 @@ def test_sql_nullif_coalesce(foods_ipc_path: Path) -> None:
{
"x": [1, None, 2, 3, None, 4],
"y": [5, 4, None, 3, None, 2],
"z": [3, 4, None, 3, None, None],
"z": [3, 4, None, 3, 6, None],
}
)

res = pl.SQLContext(df=nums).execute(
"""
SELECT
COALESCE(x,y,z) as "coal",
NULLIF(x,y) as "nullif x_y",
NULLIF(y,z) as "nullif y_z",
COALESCE(x, NULLIF(y,z)) as "both"
COALESCE(x,y,z) as "coalsc",
NULLIF(x, y) as "nullif x_y",
NULLIF(y, z) as "nullif y_z",
IFNULL(x, y) as "ifnull x_y",
IFNULL(y,-1) as "inullf y_z",
COALESCE(x, NULLIF(y,z)) as "both"
FROM df
""",
eager=True,
)

assert res.to_dict(as_series=False) == {
"coal": [1, 4, 2, 3, None, 4],
"coalsc": [1, 4, 2, 3, 6, 4],
"nullif x_y": [1, None, 2, None, None, 4],
"nullif y_z": [5, None, None, None, None, 2],
"ifnull x_y": [1, 4, 2, 3, None, 4],
"inullf y_z": [5, 4, -1, 3, -1, 2],
"both": [1, None, 2, 3, None, 4],
}
for null_func in ("IFNULL", "NULLIF"):
# both functions expect only 2 arguments
with pytest.raises(InvalidOperationError):
pl.SQLContext(df=nums).execute(f"SELECT {null_func}(x,y,z) FROM df")


def test_sql_order_by(foods_ipc_path: Path) -> None:
Expand Down