lance icon indicating copy to clipboard operation
lance copied to clipboard

bug: filter with an integer literal outside an integer column's type fails to plan

Open aimanmalib opened this issue 2 weeks ago • 0 comments

Description

A filter that compares an integer column with an integer literal outside the column's type fails to plan. u8_col > -1, u8_col < 300, i8_col = 200, i32_col < 3000000000 and i64_col < 18446744073709551615 all fail with Received literal Int64(-1) and could not convert to literal of type 'UInt8'. The same comparisons with a float literal already work: u8_col > -1.0 and i8_col < 200.0 plan as constant comparisons since #9333. IN, NOT IN and BETWEEN fail the same way, and so do delete and update predicates.

The integer case falls through in lower in rust/lance-datafusion/src/numeric_coercion.rs: a non-float operand goes to safe_coerce_scalar, which returns None when the value does not fit, so the rewrite is skipped and the old coercion raises the error.

Steps to reproduce

import tempfile
import lance
import pyarrow as pa

ds = lance.write_dataset(
    pa.table({
        "u8": pa.array([0, 7, 255, None], pa.uint8()),
        "i8": pa.array([-128, 0, 127, None], pa.int8()),
    }),
    tempfile.mkdtemp(),
)
ds.count_rows("u8 > -1.0")  # 3
ds.count_rows("i8 < 200.0")  # 3
ds.count_rows("u8 > -1")
# OSError: Invalid user input: Error resolving filter expression u8 > -1: Invalid user input:
# Received literal Int64(-1) and could not convert to literal of type 'UInt8'
ds.count_rows("i8 IN (127, 200)")
# OSError: ... Received literal Int64(200) and could not convert to literal of type 'Int8'

Expected behavior

The comparison should use the literal's value, as it does for float literals and as DataFusion and DuckDB do: u8 > -1 is true for every non-null row, i8 = 200 is never true, and 200 drops out of i8 IN (127, 200). NULL rows stay NULL, and the comparison stays on the bare column so a scalar index still applies.

Lance version

pylance 15.0.0-beta.25 (f5dbd55dd). numeric_coercion.rs is unchanged on main 5e9cc4317.

aimanmalib avatar Nov 18 '26 20:11 aimanmalib