-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-33386][SQL] Accessing array elements in ElementAt/Elt/GetArrayItem should failed if index is out of bound #30297
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da4f5ee
Accessing array elements should failed if index is out of bound.
leanken-zz 7dcba56
1. update doc.
leanken-zz f8cfa5b
UT refine and Doc refine.
leanken-zz 6109838
update doc.
leanken-zz a9312a0
doc refine.
leanken-zz 6729d7b
line 100 exceed.
leanken-zz 36d235a
1. fix GetArrayItem nullablity issue.
leanken-zz 4b161a4
naming.
leanken-zz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1906,8 +1906,10 @@ case class ArrayPosition(left: Expression, right: Expression) | |
| @ExpressionDescription( | ||
| usage = """ | ||
| _FUNC_(array, index) - Returns element of array at given (1-based) index. If index < 0, | ||
| accesses elements from the last to the first. Returns NULL if the index exceeds the length | ||
| of the array. | ||
| accesses elements from the last to the first. The function returns NULL | ||
| if the index exceeds the length of the array and `spark.sql.ansi.enabled` is set to false. | ||
| If `spark.sql.ansi.enabled` is set to true, it throws ArrayIndexOutOfBoundsException | ||
| for invalid indices. | ||
|
|
||
| _FUNC_(map, key) - Returns value for given key, or NULL if the key is not contained in the map | ||
| """, | ||
|
|
@@ -1919,9 +1921,14 @@ case class ArrayPosition(left: Expression, right: Expression) | |
| b | ||
| """, | ||
| since = "2.4.0") | ||
| case class ElementAt(left: Expression, right: Expression) | ||
| case class ElementAt( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the usage above of |
||
| left: Expression, | ||
| right: Expression, | ||
| failOnError: Boolean = SQLConf.get.ansiEnabled) | ||
viirya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| extends GetMapValueUtil with GetArrayItemUtil with NullIntolerant { | ||
|
|
||
| def this(left: Expression, right: Expression) = this(left, right, SQLConf.get.ansiEnabled) | ||
|
|
||
| @transient private lazy val mapKeyType = left.dataType.asInstanceOf[MapType].keyType | ||
|
|
||
| @transient private lazy val arrayContainsNull = left.dataType.asInstanceOf[ArrayType].containsNull | ||
|
|
@@ -1969,7 +1976,7 @@ case class ElementAt(left: Expression, right: Expression) | |
| if (ordinal == 0) { | ||
| false | ||
| } else if (elements.length < math.abs(ordinal)) { | ||
| true | ||
| !failOnError | ||
| } else { | ||
| if (ordinal < 0) { | ||
| elements(elements.length + ordinal).nullable | ||
|
|
@@ -1979,24 +1986,9 @@ case class ElementAt(left: Expression, right: Expression) | |
| } | ||
| } | ||
|
|
||
| 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 _: ArrayType => | ||
| computeNullabilityFromArray(left, right, failOnError, nullability) | ||
| case _: MapType => true | ||
| } | ||
|
|
||
|
|
@@ -2008,7 +2000,12 @@ case class ElementAt(left: Expression, right: Expression) | |
| val array = value.asInstanceOf[ArrayData] | ||
| val index = ordinal.asInstanceOf[Int] | ||
| if (array.numElements() < math.abs(index)) { | ||
| null | ||
| if (failOnError) { | ||
| throw new ArrayIndexOutOfBoundsException( | ||
| s"Invalid index: $index, numElements: ${array.numElements()}") | ||
| } else { | ||
| null | ||
| } | ||
| } else { | ||
| val idx = if (index == 0) { | ||
| throw new ArrayIndexOutOfBoundsException("SQL array indices start at 1") | ||
|
|
@@ -2042,10 +2039,20 @@ case class ElementAt(left: Expression, right: Expression) | |
| } else { | ||
| "" | ||
| } | ||
|
|
||
| val indexOutOfBoundBranch = if (failOnError) { | ||
| s"""throw new ArrayIndexOutOfBoundsException( | ||
| | "Invalid index: " + $index + ", numElements: " + $eval1.numElements() | ||
| |); | ||
| """.stripMargin | ||
| } else { | ||
| s"${ev.isNull} = true;" | ||
| } | ||
|
|
||
| s""" | ||
| |int $index = (int) $eval2; | ||
| |if ($eval1.numElements() < Math.abs($index)) { | ||
| | ${ev.isNull} = true; | ||
| | $indexOutOfBoundBranch | ||
| |} else { | ||
| | if ($index == 0) { | ||
| | throw new ArrayIndexOutOfBoundsException("SQL array indices start at 1"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ import org.apache.spark.sql.catalyst.analysis.{FunctionRegistry, TypeCheckResult | |
| import org.apache.spark.sql.catalyst.expressions.codegen._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.Block._ | ||
| import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData, TypeUtils} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.UTF8StringBuilder | ||
| import org.apache.spark.unsafe.types.{ByteArray, UTF8String} | ||
|
|
@@ -231,15 +232,24 @@ case class ConcatWs(children: Seq[Expression]) | |
| */ | ||
| // scalastyle:off line.size.limit | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(n, input1, input2, ...) - Returns the `n`-th input, e.g., returns `input2` when `n` is 2.", | ||
| usage = """ | ||
| _FUNC_(n, input1, input2, ...) - Returns the `n`-th input, e.g., returns `input2` when `n` is 2. | ||
| The function returns NULL if the index exceeds the length of the array | ||
| and `spark.sql.ansi.enabled` is set to false. If `spark.sql.ansi.enabled` is set to true, | ||
| it throws ArrayIndexOutOfBoundsException for invalid indices. | ||
| """, | ||
| examples = """ | ||
| Examples: | ||
| > SELECT _FUNC_(1, 'scala', 'java'); | ||
| scala | ||
| """, | ||
| since = "2.0.0") | ||
| // scalastyle:on line.size.limit | ||
| case class Elt(children: Seq[Expression]) extends Expression { | ||
| case class Elt( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| children: Seq[Expression], | ||
| failOnError: Boolean = SQLConf.get.ansiEnabled) extends Expression { | ||
|
|
||
| def this(children: Seq[Expression]) = this(children, SQLConf.get.ansiEnabled) | ||
|
|
||
| private lazy val indexExpr = children.head | ||
| private lazy val inputExprs = children.tail.toArray | ||
|
|
@@ -275,7 +285,12 @@ case class Elt(children: Seq[Expression]) extends Expression { | |
| } else { | ||
| val index = indexObj.asInstanceOf[Int] | ||
| if (index <= 0 || index > inputExprs.length) { | ||
| null | ||
| if (failOnError) { | ||
| throw new ArrayIndexOutOfBoundsException( | ||
| s"Invalid index: $index, numElements: ${inputExprs.length}") | ||
| } else { | ||
| null | ||
| } | ||
| } else { | ||
| inputExprs(index - 1).eval(input) | ||
| } | ||
|
|
@@ -323,6 +338,17 @@ case class Elt(children: Seq[Expression]) extends Expression { | |
| """.stripMargin | ||
| }.mkString) | ||
|
|
||
| val indexOutOfBoundBranch = if (failOnError) { | ||
| s""" | ||
| |if (!$indexMatched) { | ||
| | throw new ArrayIndexOutOfBoundsException( | ||
| | "Invalid index: " + ${index.value} + ", numElements: " + ${inputExprs.length}); | ||
| |} | ||
| """.stripMargin | ||
| } else { | ||
| "" | ||
| } | ||
|
|
||
| ev.copy( | ||
| code""" | ||
| |${index.code} | ||
|
|
@@ -332,6 +358,7 @@ case class Elt(children: Seq[Expression]) extends Expression { | |
| |do { | ||
| | $codes | ||
| |} while (false); | ||
| |$indexOutOfBoundBranch | ||
| |final ${CodeGenerator.javaType(dataType)} ${ev.value} = $inputVal; | ||
| |final boolean ${ev.isNull} = ${ev.value} == null; | ||
| """.stripMargin) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to make
ElementAtbehavior consistent on map type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's about to support ANSI mode for map type in next PR.