Operators#

Category

Description

Logical

Boolean logic operators (AND, OR, NOT).

Comparison

Value comparison and membership tests (=, >, <, IS, BETWEEN, IN, ALL, ANY).

Numeric

Arithmetic operators (+, -, *, /, //, %, unary).

Bitwise

Bitwise integer operators (&, |, XOR).

String

String matching and concatenation (LIKE, ILIKE, ||, ^@).

Regex

Regular expression matching (~, ~*, REGEXP, RLIKE).

Indexing

Struct field and array element access (->, ->>, #>, [n]).

Logical#

Operator

Description

AND

Combines conditions, returning True if both conditions are True.

OR

Combines conditions, returning True if either condition is True.

NOT

Negates a condition, returning True if the condition is False.

AND: Combines conditions, returning True if both conditions are True.

Example:

df = pl.DataFrame({"foo": [10, 20, 50, 35], "bar": [10, 10, 50, 35]})
df.sql("""
  SELECT * FROM self
  WHERE (foo >= bar) AND (bar < 50) AND (foo != 10)
""")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ foo ┆ bar β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ i64 β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 20  ┆ 10  β”‚
# β”‚ 35  ┆ 35  β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

OR: Combines conditions, returning True if either condition is True.

Example:

df = pl.DataFrame({"foo": [10, 20, 50, 35], "bar": [10, 10, 50, 35]})
df.sql("""
  SELECT * FROM self
  WHERE (foo % 2 != 0) OR (bar > 40)
""")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ foo ┆ bar β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ i64 β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 50  ┆ 50  β”‚
# β”‚ 35  ┆ 35  β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

NOT: Negates a condition, returning True if the condition is False.

Example:

df = pl.DataFrame({"foo": [10, 20, 50, 35], "bar": [10, 10, 50, 35]})
df.sql("""
  SELECT * FROM self
  WHERE NOT(foo % 2 != 0 OR bar > 40)
""")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ foo ┆ bar β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ i64 β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 10  ┆ 10  β”‚
# β”‚ 20  ┆ 10  β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

Comparison#

Operator

Description

>, >=

Greater than (or equal to).

<, <=

Less than (or equal to).

==, !=, <>

Equal to (or not equal to).

IS [NOT]

Check if a value is one of the special values NULL, TRUE, or FALSE.

IS [NOT] DISTINCT FROM, <=>

Check if a value is distinct from another value; this is the NULL-safe equivalent of == (and !=).

[NOT] BETWEEN

Check if a value is between two other values.

[NOT] IN

Check if a value is present in a list of values.

> ALL, < ALL, …

Compare a value against all values in a column.

> ANY, < ANY, = ANY, …

Compare a value against any value in a column.

Returns True if the first value is greater than the second value.

>: Greater than.
>=: Greater than or equal to.

Example:

df = pl.DataFrame({"n": [1000, 3000, 5000]})
df.sql("SELECT * FROM self WHERE n > 3000")
# shape: (1, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 5000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE n >= 3000")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 3000 β”‚
# β”‚ 5000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

Returns True if the first value is less than the second value.

<: Less than.
<=: Less than or equal to.

Example:

df = pl.DataFrame({"n": [1000, 3000, 5000]})
df.sql("SELECT * FROM self WHERE n < 3000")
# shape: (1, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 1000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE n <= 3000")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 1000 β”‚
# β”‚ 3000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

Returns True if the two values are considered equal.

=: Equal to.
!=, <>: Not equal to.

Example:

df = pl.DataFrame({"n": [1000, 3000, 5000]})
df.sql("SELECT * FROM self WHERE n = 3000")
# shape: (1, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 3000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE n != 3000")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 1000 β”‚
# β”‚ 5000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

IS: Returns True if the first value is identical to the second value (typically one of NULL, TRUE, or FALSE). Unlike == (and !=), this operator will always return TRUE or FALSE, never NULL.

Example:

df = pl.DataFrame({"lbl": ["aa", "bb", "cc"], "n": [1000, None, 5000]})
df.sql("SELECT * FROM self WHERE n IS NULL")
# shape: (1, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
# β”‚ lbl ┆ n    β”‚
# β”‚ --- ┆ ---  β”‚
# β”‚ str ┆ i64  β”‚
# β•žβ•β•β•β•β•β•ͺ══════║
# β”‚ bb  ┆ null β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE n IS NOT NULL")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
# β”‚ lbl ┆ n    β”‚
# β”‚ --- ┆ ---  β”‚
# β”‚ str ┆ i64  β”‚
# β•žβ•β•β•β•β•β•ͺ══════║
# β”‚ aa  ┆ 1000 β”‚
# β”‚ cc  ┆ 5000 β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
IS DISTINCT FROM: Compare two values; this operator assumes that NULL values are equal.
The inverse, IS NOT DISTINCT FROM, can also be written using the <=> operator.
Equivalent to == (or !=) for non-NULL values.

Example:

df = pl.DataFrame({"n1": [2222, None, 8888], "n2": [4444, None, 8888]})
df.sql("SELECT * FROM self WHERE n1 IS DISTINCT FROM n2")
# shape: (1, 2)
# β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
# β”‚ n1   ┆ n2   β”‚
# β”‚ ---  ┆ ---  β”‚
# β”‚ i64  ┆ i64  β”‚
# β•žβ•β•β•β•β•β•β•ͺ══════║
# β”‚ 2222 ┆ 4444 β”‚
# β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE n1 IS NOT DISTINCT FROM n2")
df.sql("SELECT * FROM self WHERE n1 <=> n2")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
# β”‚ n1   ┆ n2   β”‚
# β”‚ ---  ┆ ---  β”‚
# β”‚ i64  ┆ i64  β”‚
# β•žβ•β•β•β•β•β•β•ͺ══════║
# β”‚ null ┆ null β”‚
# β”‚ 8888 ┆ 8888 β”‚
# β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜

BETWEEN: Returns True if the first value is between the second and third values (inclusive).

Example:

df = pl.DataFrame({"n": [1000, 2000, 3000, 4000]})
df.sql("SELECT * FROM self WHERE n BETWEEN 2000 AND 3000")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 2000 β”‚
# β”‚ 3000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE n NOT BETWEEN 2000 AND 3000")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ n    β”‚
# β”‚ ---  β”‚
# β”‚ i64  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ 1000 β”‚
# β”‚ 4000 β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜

IN: Returns True if the value is found in the given list (or subquery result).

Example:

df = pl.DataFrame({"n": [1, 2, 3, 4, 5]})
df.sql("SELECT * FROM self WHERE n IN (2, 4)")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”
# β”‚ n   β”‚
# β”‚ --- β”‚
# β”‚ i64 β”‚
# β•žβ•β•β•β•β•β•‘
# β”‚ 2   β”‚
# β”‚ 4   β”‚
# β””β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE n NOT IN (2, 4)")
# shape: (3, 1)
# β”Œβ”€β”€β”€β”€β”€β”
# β”‚ n   β”‚
# β”‚ --- β”‚
# β”‚ i64 β”‚
# β•žβ•β•β•β•β•β•‘
# β”‚ 1   β”‚
# β”‚ 3   β”‚
# β”‚ 5   β”‚
# β””β”€β”€β”€β”€β”€β”˜
ALL: Compare a value against all values in a column expression.
Supported comparison operators: >, <, >=, <=.

Example:

df = pl.DataFrame({"a": [1, 5, 10], "b": [3, 3, 3]})
df.sql("SELECT * FROM self WHERE a > ALL(b)")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ a   ┆ b   β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ i64 β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 5   ┆ 3   β”‚
# β”‚ 10  ┆ 3   β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
ANY: Compare a value against any value in a column expression.
Supported comparison operators: >, <, >=, <=, =, !=.

Example:

df = pl.DataFrame({"a": [1, 5, 10], "b": [3, 7, 3]})
df.sql("SELECT * FROM self WHERE a < ANY(b)")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ a   ┆ b   β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ i64 β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 1   ┆ 3   β”‚
# β”‚ 5   ┆ 7   β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

Numeric#

Operator

Description

+, -

Addition and subtraction.

*, /

Multiplication and division.

%

Modulo (remainder).

Unary +, -

Unary plus and minus.

+: Addition.
-: Subtraction.

Example:

df = pl.DataFrame({"a": [10, 20, 30], "b": [3, 7, 4]})
df.sql("SELECT a + b AS sum, a - b AS diff FROM self")
# shape: (3, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
# β”‚ sum ┆ diff β”‚
# β”‚ --- ┆ ---  β”‚
# β”‚ i64 ┆ i64  β”‚
# β•žβ•β•β•β•β•β•ͺ══════║
# β”‚ 13  ┆ 7    β”‚
# β”‚ 27  ┆ 13   β”‚
# β”‚ 34  ┆ 26   β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
*: Multiplication.
/: Division (always returns a float).
//: Integer division.

Changed in version 1.41.0: The / operator now performs true division, always returning a float result. Use // for integer (floor) division.

Example:

df = pl.DataFrame({"a": [10, 20, 30], "b": [3, 4, 5]})
df.sql("""
    SELECT
      *,
      a * b AS mul,
      a / b AS div,
      a // b AS int_div
    FROM self
""")
# shape: (3, 5)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ a   ┆ b   ┆ mul ┆ div      ┆ int_div β”‚
# β”‚ --- ┆ --- ┆ --- ┆ ---      ┆ ---     β”‚
# β”‚ i64 ┆ i64 ┆ i64 ┆ f64      ┆ i64     β”‚
# β•žβ•β•β•β•β•β•ͺ═════β•ͺ═════β•ͺ══════════β•ͺ═════════║
# β”‚ 10  ┆ 3   ┆ 30  ┆ 3.333333 ┆ 3       β”‚
# β”‚ 20  ┆ 4   ┆ 80  ┆ 5.0      ┆ 5       β”‚
# β”‚ 30  ┆ 5   ┆ 150 ┆ 6.0      ┆ 6       β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

%: Returns the remainder of the division.

Example:

df = pl.DataFrame({"a": [10, 20, 30], "b": [3, 7, 4]})
df.sql("SELECT a % b AS mod FROM self")
# shape: (3, 1)
# β”Œβ”€β”€β”€β”€β”€β”
# β”‚ mod β”‚
# β”‚ --- β”‚
# β”‚ i64 β”‚
# β•žβ•β•β•β•β•β•‘
# β”‚ 1   β”‚
# β”‚ 6   β”‚
# β”‚ 2   β”‚
# β””β”€β”€β”€β”€β”€β”˜
+: Unary plus (no-op; returns the value unchanged).
-: Unary minus (negates the value).

Example:

df = pl.DataFrame({"n": [1, -2, 3]})
df.sql("SELECT -n AS neg FROM self")
# shape: (3, 1)
# β”Œβ”€β”€β”€β”€β”€β”
# β”‚ neg β”‚
# β”‚ --- β”‚
# β”‚ i64 β”‚
# β•žβ•β•β•β•β•β•‘
# β”‚ -1  β”‚
# β”‚ 2   β”‚
# β”‚ -3  β”‚
# β””β”€β”€β”€β”€β”€β”˜

Bitwise#

Operator

Description

&

Bitwise AND.

|

Bitwise OR.

XOR

Bitwise exclusive OR.

&: Performs a bitwise AND on two integer values.

Example:

df = pl.DataFrame({"a": [12, 10], "b": [10, 6]})
df.sql("SELECT a & b AS bw_and FROM self")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ bw_and β”‚
# β”‚ ---    β”‚
# β”‚ i64    β”‚
# β•žβ•β•β•β•β•β•β•β•β•‘
# β”‚ 8      β”‚
# β”‚ 2      β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜

|: Performs a bitwise OR on two integer values.

Example:

df = pl.DataFrame({"a": [12, 10], "b": [10, 6]})
df.sql("SELECT a | b AS bw_or FROM self")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”
# β”‚ bw_or β”‚
# β”‚ ---   β”‚
# β”‚ i64   β”‚
# β•žβ•β•β•β•β•β•β•β•‘
# β”‚ 14    β”‚
# β”‚ 14    β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”˜

XOR: Performs a bitwise exclusive OR on two integer values.

Example:

df = pl.DataFrame({"a": [12, 10], "b": [10, 6]})
df.sql("SELECT a XOR b AS bw_xor FROM self")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ bw_xor β”‚
# β”‚ ---    β”‚
# β”‚ i64    β”‚
# β•žβ•β•β•β•β•β•β•β•β•‘
# β”‚ 6      β”‚
# β”‚ 12     β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜

String#

Operator

Description

[NOT] LIKE, ~~, !~~

Matches a SQL β€œLIKE” expression.

[NOT] ILIKE, ~~*, !~~*

Matches a SQL β€œILIKE” expression (case-insensitive β€œLIKE”).

||

Concatenate two string values.

^@

Check if a string starts with a given prefix.

Note

The LIKE operators match on string patterns, using the following wildcards:

  • The percent sign % represents zero or more characters.

  • An underscore _ represents one character.

LIKE, ~~: Matches a SQL β€œLIKE” pattern.
NOT LIKE, !~~: Does not match a SQL β€œLIKE” pattern.

Example:

df = pl.DataFrame({"name": ["Alice", "Bob", "Charlie", "David"]})
df.sql("SELECT * FROM self WHERE name LIKE 'A%'")
# shape: (1, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”
# β”‚ name  β”‚
# β”‚ ---   β”‚
# β”‚ str   β”‚
# β•žβ•β•β•β•β•β•β•β•‘
# β”‚ Alice β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE name NOT LIKE '%o%'")
# shape: (3, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ name    β”‚
# β”‚ ---     β”‚
# β”‚ str     β”‚
# β•žβ•β•β•β•β•β•β•β•β•β•‘
# β”‚ Alice   β”‚
# β”‚ Charlie β”‚
# β”‚ David   β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Using the PostgreSQL-style operator syntax:

df.sql("SELECT * FROM self WHERE name ~~ '_o%'")
# shape: (1, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”
# β”‚ name β”‚
# β”‚ ---  β”‚
# β”‚ str  β”‚
# β•žβ•β•β•β•β•β•β•‘
# β”‚ Bob  β”‚
# β””β”€β”€β”€β”€β”€β”€β”˜
ILIKE, ~~*: Matches a SQL β€œLIKE” pattern, case insensitively.
NOT ILIKE, !~~*: Does not match a SQL β€œLIKE” pattern, case insensitively.

Example:

df = pl.DataFrame({"name": ["Alice", "bob", "CHARLIE", "David"]})
df.sql("SELECT * FROM self WHERE name ILIKE '%LI%'")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ name    β”‚
# β”‚ ---     β”‚
# β”‚ str     β”‚
# β•žβ•β•β•β•β•β•β•β•β•β•‘
# β”‚ Alice   β”‚
# β”‚ CHARLIE β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE name NOT ILIKE 'b%'")
# shape: (3, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ name    β”‚
# β”‚ ---     β”‚
# β”‚ str     β”‚
# β•žβ•β•β•β•β•β•β•β•β•β•‘
# β”‚ Alice   β”‚
# β”‚ CHARLIE β”‚
# β”‚ David   β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

||: Concatenate two or more string values.

Example:

df = pl.DataFrame({"first": ["Alice", "Bob"], "last": ["Smith", "Jones"]})
df.sql("SELECT first || ' ' || last AS full_name FROM self")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ full_name   β”‚
# β”‚ ---         β”‚
# β”‚ str         β”‚
# β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•‘
# β”‚ Alice Smith β”‚
# β”‚ Bob Jones   β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Note

Non-string values are automatically cast to string when used with the || operator.

^@: Returns True if the first string value starts with the second string value.

Example:

df = pl.DataFrame({"name": ["Alice", "Bob", "Arthur", "David"]})
df.sql("SELECT * FROM self WHERE name ^@ 'A'")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ name   β”‚
# β”‚ ---    β”‚
# β”‚ str    β”‚
# β•žβ•β•β•β•β•β•β•β•β•‘
# β”‚ Alice  β”‚
# β”‚ Arthur β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Regex#

Operator

Description

~, !~

Match a regular expression.

~*, !~*

Match a regular expression, case insensitively.

[NOT] REGEXP, [NOT] RLIKE

Match a regular expression using keyword syntax.

~: Match a regular expression.
!~: Does not match a regular expression.

Example:

df = pl.DataFrame({"idx": [0, 1, 2, 3], "lbl": ["foo", "bar", "baz", "zap"]})
df.sql("SELECT * FROM self WHERE lbl ~ '.*[aeiou]$'")
# shape: (1, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ idx ┆ lbl β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ str β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 0   ┆ foo β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE lbl !~ '^b[aeiou]'")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ idx ┆ lbl β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ str β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 0   ┆ foo β”‚
# β”‚ 3   ┆ zap β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
~*: Match a regular expression, case insensitively.
!~*: Does not match a regular expression, case insensitively.

Example:

df = pl.DataFrame({"idx": [0, 1, 2, 3], "lbl": ["FOO", "bar", "Baz", "ZAP"]})
df.sql("SELECT * FROM self WHERE lbl ~* '.*[aeiou]$'")
# shape: (1, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ idx ┆ lbl β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ str β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 0   ┆ FOO β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE lbl !~* '^b[aeiou]'")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ idx ┆ lbl β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ str β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 0   ┆ FOO β”‚
# β”‚ 3   ┆ ZAP β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
REGEXP, RLIKE: Match a regular expression using keyword syntax.
NOT REGEXP, NOT RLIKE: Does not match a regular expression.

These are equivalent to the ~ and !~ operators, but use keyword syntax instead of symbolic operators.

Example:

df = pl.DataFrame({"idx": [0, 1, 2, 3], "lbl": ["foo", "bar", "baz", "zap"]})
df.sql("SELECT * FROM self WHERE lbl REGEXP '^b'")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ idx ┆ lbl β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ str β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 1   ┆ bar β”‚
# β”‚ 2   ┆ baz β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

df.sql("SELECT * FROM self WHERE lbl NOT RLIKE '[aeiou]$'")
# shape: (3, 2)
# β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ idx ┆ lbl β”‚
# β”‚ --- ┆ --- β”‚
# β”‚ i64 ┆ str β”‚
# β•žβ•β•β•β•β•β•ͺ═════║
# β”‚ 1   ┆ bar β”‚
# β”‚ 2   ┆ baz β”‚
# β”‚ 3   ┆ zap β”‚
# β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

Indexing#

Operator

Description

->

Access a struct field by name or index, returning the native type (or a JSON string value).

->>

Access a struct field (or a JSON string value) by name or index, returning a string.

#>, #>>

Access a struct field (or a JSON string value) by path, returning the native type or a string.

[n]

Access an array element by index (1-indexed), or a map value by key.

->: Access a struct field by name or by ordinal index, returning the value in its native type.

Example:

df = pl.DataFrame(
    {"data": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}
)
df.sql("SELECT data -> 'name' AS name, data -> 'age' AS age FROM self")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
# β”‚ name  ┆ age β”‚
# β”‚ ---   ┆ --- β”‚
# β”‚ str   ┆ i64 β”‚
# β•žβ•β•β•β•β•β•β•β•ͺ═════║
# β”‚ Alice ┆ 30  β”‚
# β”‚ Bob   ┆ 25  β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

->>: Access a struct field by name or by ordinal index, returning the value cast to a string.

Example:

df = pl.DataFrame(
    {"data": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}
)
df.sql("SELECT data ->> 'age' AS age FROM self")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”
# β”‚ age β”‚
# β”‚ --- β”‚
# β”‚ str β”‚
# β•žβ•β•β•β•β•β•‘
# β”‚ 30  β”‚
# β”‚ 25  β”‚
# β””β”€β”€β”€β”€β”€β”˜

On a string column the operators read JSON text: a name is an object key, an integer is an array index (negative from the end) and a name that starts with $ is a JSONPath. Both -> and ->> return the value as a string, without the quotes of a JSON string.

df = pl.DataFrame(
    {"data": ['{"name": "Alice", "tags": ["a", "b"]}', '{"name": "Bob", "tags": []}']}
)
df.sql("SELECT data ->> 'name' AS name, data -> 'tags' ->> 0 AS first_tag FROM self")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ name  ┆ first_tag β”‚
# β”‚ ---   ┆ ---       β”‚
# β”‚ str   ┆ str       β”‚
# β•žβ•β•β•β•β•β•β•β•ͺ═══════════║
# β”‚ Alice ┆ a         β”‚
# β”‚ Bob   ┆ null      β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
#>: Access a struct field by path, returning the native type.
#>>: Access a struct field by path, returning a string.

Example:

df = pl.DataFrame(
    {"data": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}
)
df.sql("SELECT data #>> 'name' AS name FROM self")
# shape: (2, 1)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”
# β”‚ name  β”‚
# β”‚ ---   β”‚
# β”‚ str   β”‚
# β•žβ•β•β•β•β•β•β•β•‘
# β”‚ Alice β”‚
# β”‚ Bob   β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”˜

[n]: Access an array element by index. SQL array indices are 1-based. On a map, m[k] gives the value of key k, or NULL if the map does not have it.

Example:

df = pl.DataFrame({"arr": [[10, 20, 30], [40, 50, 60]]})
df.sql("SELECT arr[1] AS first, arr[3] AS last FROM self")
# shape: (2, 2)
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
# β”‚ first ┆ last β”‚
# β”‚ ---   ┆ ---  β”‚
# β”‚ i64   ┆ i64  β”‚
# β•žβ•β•β•β•β•β•β•β•ͺ══════║
# β”‚ 10    ┆ 30   β”‚
# β”‚ 40    ┆ 60   β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜