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-33391][SQL] element_at with CreateArray not respect one based index. #30296

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,36 @@ case class ElementAt(left: Expression, right: Expression)
}
}

private def nullability(elements: Seq[Expression], ordinal: Int): Boolean = {
if (ordinal == 0) {
false
} else if (elements.length < math.abs(ordinal)) {
true
} else {
if (ordinal < 0) {
elements(elements.length + ordinal).nullable
} else {
elements(ordinal - 1).nullable
}
}
}

override def computeNullabilityFromArray(child: Expression, ordinal: Expression): Boolean = {
if (ordinal.foldable && !ordinal.nullable) {
val intOrdinal = ordinal.eval().asInstanceOf[Number].intValue()
child match {
case CreateArray(ar, _) =>
nullability(ar, intOrdinal)
case GetArrayStructFields(CreateArray(elements, _), field, _, _, _) =>
nullability(elements, intOrdinal) || field.nullable
case _ =>
true
}
} else {
true
}
}

override def nullable: Boolean = left.dataType match {
case _: ArrayType => computeNullabilityFromArray(left, right)
case _: MapType => true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,11 +1122,18 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
val a = AttributeReference("a", IntegerType, nullable = false)()
val b = AttributeReference("b", IntegerType, nullable = true)()
val array = CreateArray(a :: b :: Nil)
assert(!ElementAt(array, Literal(0)).nullable)
assert(ElementAt(array, Literal(1)).nullable)
assert(!ElementAt(array, Subtract(Literal(2), Literal(2))).nullable)
assert(!ElementAt(array, Literal(1)).nullable)
assert(!ElementAt(array, Literal(-2)).nullable)
assert(ElementAt(array, Literal(2)).nullable)
assert(ElementAt(array, Literal(-1)).nullable)
assert(!ElementAt(array, Subtract(Literal(2), Literal(1))).nullable)
Copy link
Contributor

Choose a reason for hiding this comment

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

let's test valid negative ordinals.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

assert(ElementAt(array, AttributeReference("ordinal", IntegerType)()).nullable)

// CreateArray case invalid indices
assert(!ElementAt(array, Literal(0)).nullable)
assert(ElementAt(array, Literal(4)).nullable)
assert(ElementAt(array, Literal(-4)).nullable)

// GetArrayStructFields case
val f1 = StructField("a", IntegerType, nullable = false)
val f2 = StructField("b", IntegerType, nullable = true)
Expand All @@ -1135,19 +1142,34 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
val inputArray1 = CreateArray(c :: Nil)
val inputArray1ContainsNull = c.nullable
val stArray1 = GetArrayStructFields(inputArray1, f1, 0, 2, inputArray1ContainsNull)
assert(!ElementAt(stArray1, Literal(0)).nullable)
assert(!ElementAt(stArray1, Literal(1)).nullable)
assert(!ElementAt(stArray1, Literal(-1)).nullable)
val stArray2 = GetArrayStructFields(inputArray1, f2, 1, 2, inputArray1ContainsNull)
assert(ElementAt(stArray2, Literal(0)).nullable)
assert(ElementAt(stArray2, Literal(1)).nullable)
assert(ElementAt(stArray2, Literal(-1)).nullable)

val d = AttributeReference("d", structType, nullable = true)()
val inputArray2 = CreateArray(c :: d :: Nil)
val inputArray2ContainsNull = c.nullable || d.nullable
val stArray3 = GetArrayStructFields(inputArray2, f1, 0, 2, inputArray2ContainsNull)
assert(!ElementAt(stArray3, Literal(0)).nullable)
assert(ElementAt(stArray3, Literal(1)).nullable)
assert(!ElementAt(stArray3, Literal(1)).nullable)
assert(!ElementAt(stArray3, Literal(-2)).nullable)
assert(ElementAt(stArray3, Literal(2)).nullable)
assert(ElementAt(stArray3, Literal(-1)).nullable)
val stArray4 = GetArrayStructFields(inputArray2, f2, 1, 2, inputArray2ContainsNull)
assert(ElementAt(stArray4, Literal(0)).nullable)
assert(ElementAt(stArray4, Literal(1)).nullable)
assert(ElementAt(stArray4, Literal(-2)).nullable)
assert(ElementAt(stArray4, Literal(2)).nullable)
assert(ElementAt(stArray4, Literal(-1)).nullable)

// GetArrayStructFields case invalid indices
assert(!ElementAt(stArray3, Literal(0)).nullable)
assert(ElementAt(stArray3, Literal(4)).nullable)
assert(ElementAt(stArray3, Literal(-4)).nullable)

assert(ElementAt(stArray4, Literal(0)).nullable)
assert(ElementAt(stArray4, Literal(4)).nullable)
assert(ElementAt(stArray4, Literal(-4)).nullable)
}

test("Concat") {
Expand Down