Skip to content

Expose fixed-size Array inner product in Polars SQL #28928

Description

@0guban0v

Description

Follow-up to #28504

Polars provides native row-wise dot product for fixed-size Array expressions:

import polars as pl

df = pl.DataFrame(
    {
        "lhs": [[1.0, 2.0], [3.0, 4.0]],
        "rhs": [[10.0, 20.0], [30.0, 40.0]],
    },
    schema={
        "lhs": pl.Array(pl.Float64, 2),
        "rhs": pl.Array(pl.Float64, 2),
    },
)

print(df.select(dot=pl.col("lhs").arr.dot("rhs")))

Output:

shape: (2, 1)
┌───────┐
│ dot   │
│ ---   │
│ f64   │
╞═══════╡
│ 50.0  │
│ 250.0 │
└───────┘

Equivalent operation is not exposed in Polars SQL:

df.sql("SELECT ARRAY_INNER_PRODUCT(lhs, rhs) AS dot FROM self")

Expected

Expose native fixed-size Array dot product as ARRAY_INNER_PRODUCT(lhs, rhs) with ARRAY_DOT_PRODUCT as alias. Both names follow existing SQL precedent in DuckDB here

For two fixed-size Array expressions, SQL function should lower directly to lhs.arr.dot(rhs) and inherit existing supertype coercion, broadcasting, null handling, output dtype, integer overflow, and execution-engine behavior.
It should not expand into (lhs * rhs).arr.sum(), which materializes an intermediate product Array.

Known-width SQL literals should also be supported:

SELECT ARRAY_INNER_PRODUCT(lhs, [10.0, 20.0]) AS dot FROM self

Polars SQL currently lowers [10.0, 20.0] to List(Float64). We need to infer inner dtype for conversion to fixed-size Array that would result one-row Array then use existing broadcasting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-sqlArea: Polars SQL functionalityenhancementNew feature or an improvement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions