Conditional#

Function

Description

COALESCE

Returns the first non-null value in the provided values/columns.

GREATEST

Returns the greatest value in the list of expressions.

IF

Returns expr1 if the boolean condition provided as the first parameter evaluates to true, and expr2 otherwise.

IFNULL

If an expression value is NULL, return an alternative value.

LEAST

Returns the smallest value in the list of expressions.

NULLIF

Returns NULL if two expressions are equal, otherwise returns the first.

COALESCE#

Returns the first non-null value in the provided values/columns.

Example:

df = pl.DataFrame(
  {
    "foo": [1, None, 3, None],
    "bar": [1, 2, None, 4],
  }
)
df.sql("""
  SELECT foo, bar, COALESCE(foo, bar) AS baz FROM self
""")
# shape: (4, 3)
# ┌──────┬──────┬─────┐
# │ foo  ┆ bar  ┆ baz │
# │ ---  ┆ ---  ┆ --- │
# │ i64  ┆ i64  ┆ i64 │
# ╞══════╪══════╪═════╡
# │ 1    ┆ 1    ┆ 1   │
# │ null ┆ 2    ┆ 2   │
# │ 3    ┆ null ┆ 3   │
# │ null ┆ 4    ┆ 4   │
# └──────┴──────┴─────┘

GREATEST#

Returns the greatest value in the list of expressions.

Example:

df = pl.DataFrame(
  {
    "foo": [100, 200, 300, 400],
    "bar": [20, 10, 30, 40]
  }
)
df.sql("""
  SELECT GREATEST(foo, bar) AS baz FROM self
""")
# shape: (4, 1)
# ┌─────┐
# │ baz │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 100 │
# │ 200 │
# │ 300 │
# │ 400 │
# └─────┘

IF#

Returns expr1 if the boolean condition provided as the first parameter evaluates to true, and expr2 otherwise.

Example:

df = pl.DataFrame(
  {
    "foo": [100, 200, 300, 400],
    "bar": [10, 20, 30, 40]
  }
)
df.sql("""
  SELECT IF(foo < 250, 111, 999) AS baz FROM self
""")
# shape: (4, 1)
# ┌─────┐
# │ baz │
# │ --- │
# │ i32 │
# ╞═════╡
# │ 111 │
# │ 111 │
# │ 999 │
# │ 999 │
# └─────┘

IFNULL#

If an expression value is NULL, return an alternative value.

Example:

df = pl.DataFrame(
  {
    "foo": ["a", None, None, "d"],
    "bar": [1, 2, 3, 4],
  }
)
df.sql("""
  SELECT IFNULL(foo, 'n/a') AS baz FROM self
""")
# shape: (4, 1)
# ┌─────┐
# │ baz │
# │ --- │
# │ str │
# ╞═════╡
# │ a   │
# │ n/a │
# │ n/a │
# │ d   │
# └─────┘

LEAST#

Returns the smallest value in the list of expressions.

Example:

df = pl.DataFrame(
  {
    "foo": [100, 200, 300, 400],
    "bar": [20, 10, 30, 40]
  }
)
df.sql("""
  SELECT LEAST(foo, bar) AS baz FROM self
""")
# shape: (4, 1)
# ┌─────┐
# │ baz │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 20  │
# │ 10  │
# │ 30  │
# │ 40  │
# └─────┘

NULLIF#

Returns NULL if two expressions are equal, otherwise returns the first.

Example:

df = pl.DataFrame(
  {
    "foo": [100, 200, 300, 400],
    "bar": [20, 10, 30, 40]
  }
)
df.sql("""
  SELECT NULLIF(foo, bar) AS baz FROM self
""")
# shape: (4, 1)
# ┌─────┐
# │ baz │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 100 │
# │ 200 │
# │ 300 │
# │ 400 │
# └─────┘