Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-49924][SQL] Keep containsNull after ArrayCompact replacement #48410

Closed

Conversation

zhengruifeng
Copy link
Contributor

@zhengruifeng zhengruifeng commented Oct 10, 2024

What changes were proposed in this pull request?

Fix containsNull of ArrayCompact, by adding a new expression KnownNotContainsNull

Why are the changes needed?

#47430 attempted to set containsNull = false for ArrayCompact for further optimization, but in an incomplete way:

The ArrayCompact is a runtime replaceable expression, so will be replaced in optimizer, and cause the containsNull be reverted, e.g.

select array_compact(array(1, null))

Rule ReplaceExpressions changed containsNull: false -> true

old schema:
StructField(array_compact(array(1, NULL)),ArrayType(IntegerType,false),false)

new schema
StructField(array_compact(array(1, NULL)),ArrayType(IntegerType,true),false)

Does this PR introduce any user-facing change?

no

How was this patch tested?

added tests

Was this patch authored or co-authored using generative AI tooling?

no

@github-actions github-actions bot added the SQL label Oct 10, 2024
@zhengruifeng zhengruifeng marked this pull request as draft October 10, 2024 12:21
val array1 = ArrayCompact(CreateArray(Literal(1) :: Literal.apply(null) :: Nil, false))
val plan1 = Project(Alias(array1, "arr")() :: Nil, OneRowRelation()).analyze
val optimized1 = optimizer.execute(plan1)
assert(plan1.schema === optimized1.schema)
Copy link
Contributor Author

@zhengruifeng zhengruifeng Oct 10, 2024

Choose a reason for hiding this comment

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

before the fix, fails with

[info]   StructType(StructField("arr", ArrayType(IntegerType, false), false, {})) did not equal StructType(StructField("arr", ArrayType(IntegerType, true), false, {})) (OptimizerSuite.scala:327)

val array2 = ArrayCompact(CreateArray(struct :: Literal.apply(null) :: Nil, false))
val plan2 = Project(Alias(MapFromEntries(array2), "map")() :: Nil, OneRowRelation()).analyze
val optimized2 = optimizer.execute(plan2)
assert(plan2.schema === optimized2.schema)
Copy link
Contributor Author

@zhengruifeng zhengruifeng Oct 10, 2024

Choose a reason for hiding this comment

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

before the fix, fails with:

[info]   StructType(StructField("map", MapType(IntegerType, IntegerType, false), false, {})) did not equal StructType(StructField("map", MapType(IntegerType, IntegerType, false), true, {})) (OptimizerSuite.scala:333)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should explicitly write out the expected schema. Most cases the schema of RuntimeReplaceable and its replacement have the same schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good

@zhengruifeng zhengruifeng marked this pull request as ready for review October 10, 2024 12:30
@zhengruifeng zhengruifeng changed the title [SPARK-49924][SQL] Fix containsNull of ArrayCompact [SPARK-49924][SQL] Keep containsNull of ArrayCompact after replacement Oct 10, 2024
@zhengruifeng zhengruifeng changed the title [SPARK-49924][SQL] Keep containsNull of ArrayCompact after replacement [SPARK-49924][SQL] Keep containsNull after ArrayCompact replacement Oct 10, 2024
@HyukjinKwon
Copy link
Member

Merged to master.

@zhengruifeng zhengruifeng deleted the fix_array_compact_null branch October 12, 2024 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants