bug: SQL array literals take the type of their first element, so [0, 0.5, 1], [NULL, 1] and [] fail
Description
An SQL array literal takes the type of its first element and coerces every other element to that type. So [0, 0.5, 1] fails with Array expressions must have a consistent datatype. Expected: Int64, got: Float64, while [0.5, 0, 1] works. A NULL element fails in any position: [NULL, 1] gives Expected: Null, got: Int64, and [1, NULL] gives It is not possible to concatenate arrays of different data types. An empty literal [] fails with concat requires input of at least one array.
This shows up in update on list and vector columns, for example from the LanceDB JS client, which writes the whole number 0 as 0. The code is the SQLExpr::Array branch of Planner::parse_sql_expr in rust/lance-datafusion/src/planner.rs.
Steps to reproduce
import tempfile
import lance
import pyarrow as pa
ds = lance.write_dataset(
pa.table({"id": [1, 2], "f": pa.array([[1.0], [3.5]], pa.list_(pa.float64()))}),
tempfile.mkdtemp(),
)
ds.update({"f": "[0.5, 0, 1]"}, where="id = 1") # works
ds.update({"f": "[0, 0.5, 1]"}, where="id = 1")
# ValueError: Invalid user input: Array expressions must have a consistent datatype. Expected: Int64, got: Float64
ds.update({"f": "[1, NULL]"}, where="id = 1")
# ValueError: ... It is not possible to concatenate arrays of different data types
ds.update({"f": "[]"}, where="id = 1")
# ValueError: ... concat requires input of at least one array
Vector (FixedSizeList<float32>) columns fail the same way with [0, 0.5, 1] and [NULL, 1].
Expected behavior
Element order should not matter. The literal should use the type all elements share, as DataFusion's make_array does: [0, 0.5, 1] becomes [0.0, 0.5, 1.0], NULL becomes a null element, and [] becomes an empty list. Elements with no shared type, such as [1, 'a'], should still be rejected.
Lance version
pylance 15.0.0-beta.22 (49e29d7be). The planner code is unchanged on main 8888a674d.