Skip to content

feat: add array_prepend support#4716

Merged
comphead merged 1 commit into
apache:mainfrom
andygrove:feat/array-prepend
Jun 24, 2026
Merged

feat: add array_prepend support#4716
comphead merged 1 commit into
apache:mainfrom
andygrove:feat/array-prepend

Conversation

@andygrove

Copy link
Copy Markdown
Member

Which issue does this PR close?

No dedicated issue. array_prepend was previously listed as planned (🔜) in the expression reference.

Rationale for this change

array_prepend is a RuntimeReplaceable expression in Spark. In every version that defines it (3.5.8, 4.0.1, 4.1.1) the analyzer rewrites it to ArrayInsert(arr, Literal(1), elem) before Comet's serde runs, and that replacement is identical across all three versions. Comet already implements ArrayInsert as Compatible, so array_prepend already executes natively end to end. No new Scala serde, protobuf, or Rust code is required. This PR locks that in with a regression test and corrects the documented support status.

What changes are included in this PR?

  • Add a Comet SQL file test at spark/src/test/resources/sql-tests/expressions/array/array_prepend.sql. It is guarded with MinSparkVersion: 3.5 because array_prepend was added in Spark 3.5.0 and does not exist in 3.4. Coverage includes column/literal/mixed arguments, NULL array yielding NULL, NULL element prepended, empty array, the int/string/boolean/double (including NaN)/long/multibyte-UTF8 element types, and the three cases Spark's own DataFrameFunctionsSuite exercises: type coercion (array_prepend(array(1, 2), 1.23D) -> [1.23, 1.0, 2.0]), nested-array elements, and binary elements.
  • Mark array_prepend as supported (✅) in docs/source/user-guide/latest/expressions.md.
  • Record the cross-version audit in docs/source/contributor-guide/expression-audits/array_funcs.md.

The implement-comet-expression skill was used to scaffold this work, including the audit-comet-expression cross-version comparison against Spark 3.4.3, 3.5.8, 4.0.1, and 4.1.1.

How are these changes tested?

New SQL file test array_prepend.sql, run via ./mvnw test -Dsuites="org.apache.comet.CometSqlFileTestSuite array_prepend" -Dtest=none. The framework runs each query under both Spark and Comet and asserts the results match and that the expression runs natively.

array_prepend is a RuntimeReplaceable that the analyzer rewrites to
array_insert(arr, 1, elem) before Comet serde runs, so it already
executes natively through the existing ArrayInsert path. Add a SQL file
test to lock in coverage, mark array_prepend as supported in the
expressions reference, and record the cross-version audit.
@andygrove andygrove requested a review from comphead June 24, 2026 20:43

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andygrove

@comphead comphead merged commit f6aa10c into apache:main Jun 24, 2026
51 checks passed
@andygrove andygrove deleted the feat/array-prepend branch June 24, 2026 23:50

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice this sql would fail on comet with error sth like: "INVALID_INDEX_OF_ZERO", and the root cause is because comet's array_insert execution didnt Spark’s short-circuit evaluation.

statement
CREATE TABLE test_array_prepend(
  arr ARRAY<INT>,
  idx INT
) USING parquet

statement
INSERT INTO test_array_prepend VALUES
  (NULL, 0),
  (array(1), 1)

query
SELECT array_prepend(arr, element_at(array(9), idx))
FROM test_array_prepend
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened a pr for this: #4726. PTAL, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants