Skip to content

refactor: move array expression support checks to getSupportLevel#4677

Merged
andygrove merged 1 commit into
apache:mainfrom
andygrove:refactor-arrays-support-level
Jun 18, 2026
Merged

refactor: move array expression support checks to getSupportLevel#4677
andygrove merged 1 commit into
apache:mainfrom
andygrove:refactor-arrays-support-level

Conversation

@andygrove

Copy link
Copy Markdown
Member

Which issue does this PR close?

Part of #4673.

Rationale for this change

The serde framework intends that an expression declares whether it can be accelerated through getSupportLevel, with the central dispatcher in QueryPlanSerde translating Incompatible/Unsupported into the appropriate fallback. Several array serdes instead made these decisions inside convert by calling withFallbackReason directly, even though the decisions are statically determinable from the expression.

What changes are included in this PR?

Move static support decisions out of convert and into getSupportLevel for the array expressions:

  • ArrayRemove, Flatten, ArrayPosition: unsupported input data types, via a shared ArraysBase.childTypesSupportLevel helper.
  • ArrayPosition: all-foldable arguments fall back so Spark's ConstantFolding can handle them.
  • SortArray: ascendingOrder must be a boolean literal (folded into the existing getSupportLevel).
  • ElementAt: the input must be an array (Map inputs fall back).

This is behavior-preserving: every case that previously fell back to Spark continues to fall back with the same reason. The remaining withFallbackReason calls in this file are child-reason rollups, which are the intended use inside convert.

ArrayExcept and ArrayReverse are intentionally left for a follow-up. Their isTypeSupported checks interact with the codegen-dispatch routing used for Incompatible expressions (a binary-array reverse, for example, stays native via the codegen dispatcher), so reclassifying those checks as Unsupported would change routing and is not behavior-preserving in isolation.

How are these changes tested?

Covered by existing tests in CometArrayExpressionSuite (41 tests), which exercise both the supported and fallback paths for these expressions.

Static support decisions for several array expressions are moved out of
the serde convert methods and into getSupportLevel:

- ArrayRemove, Flatten, ArrayPosition: unsupported input data types (via
  a shared ArraysBase.childTypesSupportLevel helper)
- ArrayPosition: all-foldable args fall back so Spark can constant-fold
- SortArray: ascendingOrder must be a boolean literal
- ElementAt: input must be an array

Behavior is unchanged: each case that previously fell back to Spark now
reports Unsupported with the same reason. The remaining withFallbackReason
calls are child-reason rollups.

ArrayExcept and ArrayReverse are intentionally left for a follow-up: their
type checks interact with the codegen-dispatch routing for Incompatible
expressions, so moving them is not behavior-preserving in isolation.
"Input must be an array. `Map` inputs are not supported.")

override def getSupportLevel(expr: ElementAt): SupportLevel = {
if (expr.left.dataType.isInstanceOf[ArrayType]) {

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.

in Apache Spark, the ElementAt supports Map, and Comet gates for Array only, we should support Map

  override def inputTypes: Seq[AbstractDataType] = {
    (left.dataType, right.dataType) match {
      case (arr: ArrayType, e2: IntegralType) if (e2 != LongType) =>
        Seq(arr, IntegerType)
      case (MapType(keyType, valueType, hasNull), e2) =>
        TypeCoercion.findTightestCommonType(keyType, e2) match {
          case Some(dt) => Seq(MapType(dt, valueType, hasNull), dt)
          case _ => Seq.empty
        }
      case (l, r) => Seq.empty

    }
  }

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.

@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

@andygrove andygrove merged commit 6010c5b into apache:main Jun 18, 2026
136 of 137 checks passed
@andygrove andygrove deleted the refactor-arrays-support-level branch June 18, 2026 21:52
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.

2 participants