You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/12/01 23:53:31 UTC
[GitHub] [spark] Daniel-Davies opened a new pull request, #38867: [WIP] [SPARK-41234] Add functionality for array_insert
Daniel-Davies opened a new pull request, #38867:
URL: https://github.com/apache/spark/pull/38867
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'core/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1334664461
Please let me know when this one ready to review :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1042198929
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+ val newArrayLength = baseArr.numElements() + 1
+
+ if (newArrayLength > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(newArrayLength)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](newArrayLength)
+ baseArr.foreach(arrayElementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ newArray(validatedPosInt) = item
+
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
+ throw QueryExecutionErrors.elementAtByIndexZeroError(getContextOrNull())
Review Comment:
Amended, thank you
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090906741
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
ANSI SQL is 1-based. Note that one based indexing is critical to exploit relative indexing counting from the end.
That's used frequently in SparkSQL for STRINGs and getting more common fro ARRAYs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1157449697
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -431,6 +431,104 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 3, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 3, 4):array<int>>
+-- !query output
+[1,2,4,3]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
Review Comment:
I think this 0 behavior is a trap. It invites the belief that array_isert is 0 based.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063338503
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
+
+ baseArr.foreach(arrayElementType, (i, v) => {
+ var elementPosition = i
+ if (newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1 + math.abs(posInt + baseArr.numElements())
+ }
+ if (i >= itemInsertionIndex && !newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1
+ }
+ newArray(elementPosition) = v
+ })
+
+ newArray(itemInsertionIndex) = item
+
+ return new GenericArrayData(newArray)
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val f = (arrExpr: ExprCode, posExpr: ExprCode, itemExpr: ExprCode) => {
+ val arr = arrExpr.value
+ val pos = posExpr.value
+ val item = itemExpr.value
+
+ val itemInsertionIndex = ctx.freshName("itemInsertionIndex")
+ val newPosExtendsArrayLeft = ctx.freshName("newPosExtendsArrayLeft")
+ val adjustedAllocIdx = ctx.freshName("adjustedAllocIdx")
+ val resLength = ctx.freshName("resLength")
+ val insertedItemIsNull = ctx.freshName("insertedItemIsNull")
+ val i = ctx.freshName("i")
+ val j = ctx.freshName("j")
+ val values = ctx.freshName("values")
+
+ val allocation = CodeGenerator.createArrayData(
+ values, elementType, resLength, s"$prettyName failed.")
+ val assignment = CodeGenerator.createArrayAssignment(values, elementType, arr,
+ adjustedAllocIdx, i, true)
+
+ val defaultIntValue = CodeGenerator.defaultValue(CodeGenerator.JAVA_INT, false)
+ s"""
+ |${CodeGenerator.JAVA_INT} $itemInsertionIndex = 0;
Review Comment:
for example, line 4730 change to use `int ` or other `int`(such as line 4752) change to use `${CodeGenerator.JAVA_INT}` ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090137809
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
We are not switching to 0-based index. The reason for adding `Get` is ANSI fallback: https://github.com/apache/spark/commit/b6cf3e4598fb6ae9f9ed28c7d5a0d4152453a669
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1060406276
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
Review Comment:
Basically I don't think we should be exactly reproducing snowflake; there are some clear differences between Spark and Snowflake, including the 1-based indexing. But I'm happy to follow your guidance
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1064152931
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
Thanks @zhengruifeng! In which case, this PR should be ready for review. Thank you for the update.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1069045751
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,230 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
Review Comment:
```suggestion
usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
```
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala:
##########
@@ -2250,6 +2250,77 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
Seq(2d))
}
+ test("Array Insert") {
+ val a1 = Literal.create(Seq(1, 2, 4), ArrayType(IntegerType))
+ val a2 = Literal.create(Seq(1, 2, null, 4, 5, null), ArrayType(IntegerType))
+ val a3 = Literal.create(Seq[Boolean](true, false, true), ArrayType(BooleanType))
+ val a4 = Literal.create(Seq[Byte](1, 2, 3, 2), ArrayType(ByteType))
+ val a7 = Literal.create(Seq[Short](1, 2, 3, 2), ArrayType(ShortType))
+ val a9 = Literal.create(Seq[Float](1.1F, 2.2F, 3.3F, 2.2F), ArrayType(FloatType))
+ val a11 = Literal.create(Seq[Double](1.1, 2.2, 3.3, 2.2), ArrayType(DoubleType))
+ val a13 = Literal.create(Seq(1L, 2L, 4L), ArrayType(LongType))
+ val a15 = Literal.create(Seq("b", "a", "c"), ArrayType(StringType, false))
+ val a16 = Literal.create(Seq("b", null, "a", "g", null), ArrayType(StringType, true))
+ val a18 = Literal.create(null, ArrayType(StringType))
+
+ val litTwoInt = Literal.create(2, IntegerType)
+ val litThreeInt = Literal.create(3, IntegerType)
+ val litOneInt = Literal.create(1, IntegerType)
+ val litFourInt = Literal.create(4, IntegerType)
+ val litNullInt = Literal.create(null, IntegerType)
+ val litZeroInt = Literal.create(0, IntegerType)
+ val litTenInt = Literal.create(10, IntegerType)
+ val litMinusTwoInt = Literal.create(-2, IntegerType)
+ val litMinusTenInt = Literal.create(-10, IntegerType)
+ val litThreeLong = Literal.create(3L, LongType)
+ val litBoolTrue = Literal.create(true, BooleanType)
+ val litThreeByte = Literal.create(5.asInstanceOf[Byte], ByteType)
Review Comment:
`litFiveByte` ?
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala:
##########
@@ -2250,6 +2250,77 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
Seq(2d))
}
+ test("Array Insert") {
+ val a1 = Literal.create(Seq(1, 2, 4), ArrayType(IntegerType))
+ val a2 = Literal.create(Seq(1, 2, null, 4, 5, null), ArrayType(IntegerType))
+ val a3 = Literal.create(Seq[Boolean](true, false, true), ArrayType(BooleanType))
+ val a4 = Literal.create(Seq[Byte](1, 2, 3, 2), ArrayType(ByteType))
+ val a7 = Literal.create(Seq[Short](1, 2, 3, 2), ArrayType(ShortType))
+ val a9 = Literal.create(Seq[Float](1.1F, 2.2F, 3.3F, 2.2F), ArrayType(FloatType))
+ val a11 = Literal.create(Seq[Double](1.1, 2.2, 3.3, 2.2), ArrayType(DoubleType))
+ val a13 = Literal.create(Seq(1L, 2L, 4L), ArrayType(LongType))
+ val a15 = Literal.create(Seq("b", "a", "c"), ArrayType(StringType, false))
+ val a16 = Literal.create(Seq("b", null, "a", "g", null), ArrayType(StringType, true))
+ val a18 = Literal.create(null, ArrayType(StringType))
+
+ val litTwoInt = Literal.create(2, IntegerType)
+ val litThreeInt = Literal.create(3, IntegerType)
+ val litOneInt = Literal.create(1, IntegerType)
+ val litFourInt = Literal.create(4, IntegerType)
+ val litNullInt = Literal.create(null, IntegerType)
+ val litZeroInt = Literal.create(0, IntegerType)
+ val litTenInt = Literal.create(10, IntegerType)
+ val litMinusTwoInt = Literal.create(-2, IntegerType)
+ val litMinusTenInt = Literal.create(-10, IntegerType)
+ val litThreeLong = Literal.create(3L, LongType)
+ val litBoolTrue = Literal.create(true, BooleanType)
+ val litThreeByte = Literal.create(5.asInstanceOf[Byte], ByteType)
+ val litThreeShort = Literal.create(3.asInstanceOf[Short], ShortType)
+ val litFourFourFloat = Literal.create(4.4F, FloatType)
+ val litFourFourDouble = Literal.create(4.4, DoubleType)
+ val litDString = Literal.create("d", StringType)
+
+ // basic additions per type
+ checkEvaluation(ArrayInsert(a1, litTwoInt, litThreeInt), Seq(1, 2, 3, 4))
Review Comment:
what about directly using the Literal expressions for `pos` and `value` ?
e.g.
```suggestion
checkEvaluation(ArrayInsert(a1, Literal(2), Literal.create(3, IntegerType)), Seq(1, 2, 3, 4))
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,230 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeCheckResult.TypeCheckSuccess
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
Review Comment:
I feel a bit hard to follow the implementation. What about having 3 code paths for it?
1, `pos` >= `baseArr.length`;
2, `pos` < `- baseArr.length`;
3, otherwise;
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala:
##########
@@ -2250,6 +2250,77 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
Seq(2d))
}
+ test("Array Insert") {
+ val a1 = Literal.create(Seq(1, 2, 4), ArrayType(IntegerType))
+ val a2 = Literal.create(Seq(1, 2, null, 4, 5, null), ArrayType(IntegerType))
+ val a3 = Literal.create(Seq[Boolean](true, false, true), ArrayType(BooleanType))
+ val a4 = Literal.create(Seq[Byte](1, 2, 3, 2), ArrayType(ByteType))
+ val a7 = Literal.create(Seq[Short](1, 2, 3, 2), ArrayType(ShortType))
+ val a9 = Literal.create(Seq[Float](1.1F, 2.2F, 3.3F, 2.2F), ArrayType(FloatType))
+ val a11 = Literal.create(Seq[Double](1.1, 2.2, 3.3, 2.2), ArrayType(DoubleType))
+ val a13 = Literal.create(Seq(1L, 2L, 4L), ArrayType(LongType))
+ val a15 = Literal.create(Seq("b", "a", "c"), ArrayType(StringType, false))
+ val a16 = Literal.create(Seq("b", null, "a", "g", null), ArrayType(StringType, true))
+ val a18 = Literal.create(null, ArrayType(StringType))
+
+ val litTwoInt = Literal.create(2, IntegerType)
+ val litThreeInt = Literal.create(3, IntegerType)
+ val litOneInt = Literal.create(1, IntegerType)
+ val litFourInt = Literal.create(4, IntegerType)
+ val litNullInt = Literal.create(null, IntegerType)
+ val litZeroInt = Literal.create(0, IntegerType)
+ val litTenInt = Literal.create(10, IntegerType)
+ val litMinusTwoInt = Literal.create(-2, IntegerType)
+ val litMinusTenInt = Literal.create(-10, IntegerType)
+ val litThreeLong = Literal.create(3L, LongType)
+ val litBoolTrue = Literal.create(true, BooleanType)
+ val litThreeByte = Literal.create(5.asInstanceOf[Byte], ByteType)
+ val litThreeShort = Literal.create(3.asInstanceOf[Short], ShortType)
+ val litFourFourFloat = Literal.create(4.4F, FloatType)
+ val litFourFourDouble = Literal.create(4.4, DoubleType)
+ val litDString = Literal.create("d", StringType)
+
+ // basic additions per type
+ checkEvaluation(ArrayInsert(a1, litTwoInt, litThreeInt), Seq(1, 2, 3, 4))
+ checkEvaluation(
+ ArrayInsert(a3, litTwoInt, litBoolTrue),
+ Seq[Boolean](true, false, true, true)
+ )
+ checkEvaluation(ArrayInsert(a4, litTwoInt, litThreeByte), Seq[Byte](1, 2, 5, 3, 2))
Review Comment:
why there is a `5`?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,230 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeCheckResult.TypeCheckSuccess
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
Review Comment:
```suggestion
val newPosExtendsArrayLeft = posInt < 0 && -posInt > baseArr.numElements() - 1
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,230 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeCheckResult.TypeCheckSuccess
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
Review Comment:
```
if (newArrayLength > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(newArrayLength)
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1042161861
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
Review Comment:
the `posExpr` should be `IntegerType`, but as to `srcArrayExpr` and `itemExpr`, I think you may refer to `ArrayRemove`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+ val newArrayLength = baseArr.numElements() + 1
+
+ if (newArrayLength > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(newArrayLength)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](newArrayLength)
+ baseArr.foreach(arrayElementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ newArray(validatedPosInt) = item
+
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
+ throw QueryExecutionErrors.elementAtByIndexZeroError(getContextOrNull())
Review Comment:
0 should be acceptable
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+ val newArrayLength = baseArr.numElements() + 1
+
+ if (newArrayLength > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(newArrayLength)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](newArrayLength)
+ baseArr.foreach(arrayElementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ newArray(validatedPosInt) = item
+
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
Review Comment:
since this method is only used once, I think we do not need it.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+ val newArrayLength = baseArr.numElements() + 1
Review Comment:
this is not the case, refer to the second example in https://docs.snowflake.com/en/sql-reference/functions/array_insert.html
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1097054656
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,257 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1, or start from the end if start is negative).\",",
Review Comment:
Can we document the behavior of `pos` is larger than the array length?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] AmplabJenkins commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1334656975
Can one of the admins verify this patch?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063284902
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
No worries- I can revert to the last commit if we want to go with (1). My understanding is that standard SQL99 specifies that arrays are 1 indexed, which is why Spark arrays are 1 indexed.
Personally, (1) sounds like a good option to me. However I appreciate that this is out of my knowledge level, so I'm happy to pick whichever one of my commits that works for you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] beliefer commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
beliefer commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063381636
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
Please hold on. We are not get the final conclusion. cc @cloud-fan @gengliangwang @zhengruifeng Do you have an idea or preference ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063338503
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
+
+ baseArr.foreach(arrayElementType, (i, v) => {
+ var elementPosition = i
+ if (newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1 + math.abs(posInt + baseArr.numElements())
+ }
+ if (i >= itemInsertionIndex && !newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1
+ }
+ newArray(elementPosition) = v
+ })
+
+ newArray(itemInsertionIndex) = item
+
+ return new GenericArrayData(newArray)
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val f = (arrExpr: ExprCode, posExpr: ExprCode, itemExpr: ExprCode) => {
+ val arr = arrExpr.value
+ val pos = posExpr.value
+ val item = itemExpr.value
+
+ val itemInsertionIndex = ctx.freshName("itemInsertionIndex")
+ val newPosExtendsArrayLeft = ctx.freshName("newPosExtendsArrayLeft")
+ val adjustedAllocIdx = ctx.freshName("adjustedAllocIdx")
+ val resLength = ctx.freshName("resLength")
+ val insertedItemIsNull = ctx.freshName("insertedItemIsNull")
+ val i = ctx.freshName("i")
+ val j = ctx.freshName("j")
+ val values = ctx.freshName("values")
+
+ val allocation = CodeGenerator.createArrayData(
+ values, elementType, resLength, s"$prettyName failed.")
+ val assignment = CodeGenerator.createArrayAssignment(values, elementType, arr,
+ adjustedAllocIdx, i, true)
+
+ val defaultIntValue = CodeGenerator.defaultValue(CodeGenerator.JAVA_INT, false)
+ s"""
+ |${CodeGenerator.JAVA_INT} $itemInsertionIndex = 0;
Review Comment:
for example, line 4730 change to use `int ` or other `int`(such as line 4752/4768/4772) change to use `${CodeGenerator.JAVA_INT}` ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1405131063
@zhengruifeng I've made the requested changes as per the latest review. Apologies for the delay- I'm not readily available until merge of this PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1365028273
also cc @beliefer
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1060276034
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
yes, the indices start at 0, see the examples in https://github.com/apache/spark/pull/38867#issuecomment-1365013704
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1059780745
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -427,6 +427,103 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 4, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 4, 4):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
+-- !query schema
+struct<array_insert(array(2, 3, 4), 0, 1):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 1, 1)
+-- !query schema
+struct<array_insert(array(2, 3, 4), 1, 1):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(1, 3, 4), -2, 2)
+-- !query schema
+struct<array_insert(array(1, 3, 4), -2, 2):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(1, 2, 3), 4, "4")
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+ "errorClass" : "DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES",
+ "messageParameters" : {
+ "dataType" : "\"ARRAY\"",
+ "functionName" : "`array_insert`",
+ "leftType" : "\"ARRAY<INT>\"",
+ "rightType" : "\"STRING\"",
+ "sqlExpr" : "\"array_insert(array(1, 2, 3), 4, 4)\""
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 8,
+ "stopIndex" : 43,
+ "fragment" : "array_insert(array(1, 2, 3), 4, \"4\")"
+ } ]
+}
+
+
+-- !query
+select array_insert(cast(NULL as ARRAY<INT>), 1, 1)
+-- !query schema
+struct<array_insert(NULL, 1, 1):array<int>>
+-- !query output
+NULL
+
+
+-- !query
+select array_insert(array(1, 2, 3, NULL), cast(NULL as INT), 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3, NULL), CAST(NULL AS INT), 4):array<int>>
+-- !query output
+NULL
+
+
+-- !query
+select array_insert(array(1, 2, 3, NULL), 4, cast(NULL as INT))
+-- !query schema
+struct<array_insert(array(1, 2, 3, NULL), 4, CAST(NULL AS INT)):array<int>>
+-- !query output
+[1,2,3,null,null]
+
+
+-- !query
+select array_insert(array(2, 3, null, 4), 6, 5)
+-- !query schema
+struct<array_insert(array(2, 3, NULL, 4), 6, 5):array<int>>
+-- !query output
+[2,3,null,4,null,5]
Review Comment:
There doesn't seem to be a way to specify that the array can have NULL elements in the raw SQL syntax. I've added a null to the base array in the meantime, but please let me know if there is a better way to avoid the following outcome:
`-- !query
select array_insert(array(2, 3, 4), 5, 1)
-- !query schema
struct<array_insert(array(2, 3, 4), 5, 1):array<int>>
-- !query output
[2,3,4,0,1]`
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -427,6 +427,103 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 4, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 4, 4):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
+-- !query schema
+struct<array_insert(array(2, 3, 4), 0, 1):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 1, 1)
+-- !query schema
+struct<array_insert(array(2, 3, 4), 1, 1):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(1, 3, 4), -2, 2)
+-- !query schema
+struct<array_insert(array(1, 3, 4), -2, 2):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(1, 2, 3), 4, "4")
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+ "errorClass" : "DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES",
+ "messageParameters" : {
+ "dataType" : "\"ARRAY\"",
+ "functionName" : "`array_insert`",
+ "leftType" : "\"ARRAY<INT>\"",
+ "rightType" : "\"STRING\"",
+ "sqlExpr" : "\"array_insert(array(1, 2, 3), 4, 4)\""
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 8,
+ "stopIndex" : 43,
+ "fragment" : "array_insert(array(1, 2, 3), 4, \"4\")"
+ } ]
+}
+
+
+-- !query
+select array_insert(cast(NULL as ARRAY<INT>), 1, 1)
+-- !query schema
+struct<array_insert(NULL, 1, 1):array<int>>
+-- !query output
+NULL
+
+
+-- !query
+select array_insert(array(1, 2, 3, NULL), cast(NULL as INT), 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3, NULL), CAST(NULL AS INT), 4):array<int>>
+-- !query output
+NULL
+
+
+-- !query
+select array_insert(array(1, 2, 3, NULL), 4, cast(NULL as INT))
+-- !query schema
+struct<array_insert(array(1, 2, 3, NULL), 4, CAST(NULL AS INT)):array<int>>
+-- !query output
+[1,2,3,null,null]
+
+
+-- !query
+select array_insert(array(2, 3, null, 4), 6, 5)
+-- !query schema
+struct<array_insert(array(2, 3, NULL, 4), 6, 5):array<int>>
+-- !query output
+[2,3,null,4,null,5]
Review Comment:
There doesn't seem to be a way to specify that the array can have NULL elements in the raw SQL syntax. I've added a null to the base array in the meantime, but please let me know if there is a better way to avoid the following outcome:
-- !query
select array_insert(array(2, 3, 4), 5, 1)
-- !query schema
struct<array_insert(array(2, 3, 4), 5, 1):array<int>>
-- !query output
[2,3,4,0,1]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1064153224
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
Review Comment:
Fixed by now following 0 index, thanks @beliefer
##########
sql/core/src/test/resources/sql-tests/inputs/array.sql:
##########
@@ -130,6 +130,18 @@ select get(array(1, 2, 3), 3);
select get(array(1, 2, 3), null);
select get(array(1, 2, 3), -1);
+-- function array_insert()
+select array_insert(array(1, 2, 3), 3, 4);
+select array_insert(array(2, 3, 4), 0, 1);
+select array_insert(array(2, 3, 4), 1, 1);
+select array_insert(array(1, 3, 4), -2, 2);
+select array_insert(array(1, 2, 3), 3, "4");
+select array_insert(cast(NULL as ARRAY<INT>), 1, 1);
+select array_insert(array(1, 2, 3, NULL), cast(NULL as INT), 4);
Review Comment:
There doesn't seem to be a way to specify that the array can have NULL elements in the raw SQL syntax. I've added a null to the base array in the meantime, but please let me know if there is a better way to avoid the following outcome:
-- !query
select array_insert(array(2, 3, 4), 5, 1)
-- !query schema
struct<array_insert(array(2, 3, 4), 5, 1):array>
-- !query output
[2,3,4,0,1]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090137183
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
I don't think we should follow snowflake now as it's too late. Existing functions already use 1-based indices. For example, `Slice`, `ArrayPosition`. cc @srielau
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063299861
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
Review Comment:
Oh sorry- you're right, I copied this from ArrayUnion I think. This should just say TypeCheckResult.TypeCheckSuccess
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1041949049
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,155 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with SupportQueryContext
+ with QueryErrorsBase {
+
+ @transient private lazy val elementType: DataType =
+ srcArrayExpr.dataType.asInstanceOf[ArrayType].elementType
+
+ override def dataType: DataType = srcArrayExpr.dataType
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+
+ if (baseArr.numElements() + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(baseArr.numElements() + 1)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](arr.asInstanceOf[ArrayData].numElements() + 1)
+ arr.asInstanceOf[ArrayData].foreach(elementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ if (item != null) {
+ newArray(validatedPosInt) = item
+ }
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
Review Comment:
I hope `array_insert(pos=0)` equals `array_prepend`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1345642760
@LuciferYang @zhengruifeng @HyukjinKwon Think this one is more or less ready to review fully; I've tried to follow the comments & guidance given on the [array_append](https://github.com/apache/spark/pull/38865) PR. The main question I'd appreciate guidance on is what happens in the following two edge cases:
- Input item parameter is null: this has been discussed already in array_append, where the decision was to return a null result if the input item parameter is null, but let me know if it should be different for this function
- Index out of bounds: if the user tries to put an item into the array at an index that is larger than the (new) array size, this PR returns a result of null. Alternatives here are to (a) perform an array_append (or prepend, if index is negative) (b) implement the snowflake behaviour of filling the array with nulls up to the index (see existing discussion on this PR) or (c) no-op
Thanks all for the input so far, I hope the code is sufficient in it's current form
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1365013704
@Daniel-Davies Sorry for the late reply.
On `Input item parameter is null:`
The issue of NULL handling was controversial, and we spend some time to discuss with some SQL experts and decided to adopt snowflake's method of NULL handling which is more reasonable compared with existing SparkSQL's method, that is:
1, if the array is NULL, returns NULL;
2, if the array is NOT NULL and the element is NULL, insert this NULL into the array;
```
+--------------------------------------------+
| ARRAY_INSERT(ARRAY_CONSTRUCT(1,2,3), 0, 4) |
|--------------------------------------------|
| [ |
| 4, |
| 1, |
| 2, |
| 3 |
| ] |
+--------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.139s
+--------------------------------------------+
| ARRAY_INSERT(ARRAY_CONSTRUCT(1,2,3), 2, 4) |
|--------------------------------------------|
| [ |
| 1, |
| 2, |
| 4, |
| 3 |
| ] |
+--------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.116s
+-----------------------------------------------+
| ARRAY_INSERT(ARRAY_CONSTRUCT(1,2,3), 2, NULL) |
|-----------------------------------------------|
| [ |
| 1, |
| 2, |
| undefined, |
| 3 |
| ] |
+-----------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.130s
+--------------------------+
| ARRAY_INSERT(NULL, 2, 1) |
|--------------------------|
| NULL |
+--------------------------+
1 Row(s) produced. Time Elapsed: 0.106s
+-----------------------------+
| ARRAY_INSERT(NULL, 2, NULL) |
|-----------------------------|
| NULL |
+-----------------------------+
1 Row(s) produced. Time Elapsed: 0.113s
```
On `Index out of bounds`: I think we should choose option b, filling with nulls
```
+---------------------------------------------+
| ARRAY_INSERT(ARRAY_CONSTRUCT(1,2,3), 10, 1) |
|---------------------------------------------|
| [ |
| 1, |
| 2, |
| 3, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| 1 |
| ] |
+---------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.116s
+----------------------------------------------+
| ARRAY_INSERT(ARRAY_CONSTRUCT(1,2,3), -10, 1) |
|----------------------------------------------|
| [ |
| 1, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| 1, |
| 2, |
| 3 |
| ] |
+----------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.111s
+------------------------------------------------+
| ARRAY_INSERT(ARRAY_CONSTRUCT(1,2,3), 10, NULL) |
|------------------------------------------------|
| [ |
| 1, |
| 2, |
| 3, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined |
| ] |
+------------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.109s
+-------------------------------------------------+
| ARRAY_INSERT(ARRAY_CONSTRUCT(1,2,3), -10, NULL) |
|-------------------------------------------------|
| [ |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| undefined, |
| 1, |
| 2, |
| 3 |
| ] |
+-------------------------------------------------+
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090939773
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
We are right now a bit broken, but the "ones" have it IMHO
0 based:
- get() (introduced a a fallback as Wenchen notes)
- posexpode()
- posexplode_outer()
1 based:
- []
- array_position()
- element_at()
- try_element_at()
- slice()
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng closed pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng closed pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
URL: https://github.com/apache/spark/pull/38867
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063338503
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
+
+ baseArr.foreach(arrayElementType, (i, v) => {
+ var elementPosition = i
+ if (newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1 + math.abs(posInt + baseArr.numElements())
+ }
+ if (i >= itemInsertionIndex && !newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1
+ }
+ newArray(elementPosition) = v
+ })
+
+ newArray(itemInsertionIndex) = item
+
+ return new GenericArrayData(newArray)
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val f = (arrExpr: ExprCode, posExpr: ExprCode, itemExpr: ExprCode) => {
+ val arr = arrExpr.value
+ val pos = posExpr.value
+ val item = itemExpr.value
+
+ val itemInsertionIndex = ctx.freshName("itemInsertionIndex")
+ val newPosExtendsArrayLeft = ctx.freshName("newPosExtendsArrayLeft")
+ val adjustedAllocIdx = ctx.freshName("adjustedAllocIdx")
+ val resLength = ctx.freshName("resLength")
+ val insertedItemIsNull = ctx.freshName("insertedItemIsNull")
+ val i = ctx.freshName("i")
+ val j = ctx.freshName("j")
+ val values = ctx.freshName("values")
+
+ val allocation = CodeGenerator.createArrayData(
+ values, elementType, resLength, s"$prettyName failed.")
+ val assignment = CodeGenerator.createArrayAssignment(values, elementType, arr,
+ adjustedAllocIdx, i, true)
+
+ val defaultIntValue = CodeGenerator.defaultValue(CodeGenerator.JAVA_INT, false)
+ s"""
+ |${CodeGenerator.JAVA_INT} $itemInsertionIndex = 0;
Review Comment:
for example, line 4730 change to use `int ` or other `int`(such as line 4752) change to use `${CodeGenerator.JAVA_INT}`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1061515779
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
@beliefer @zhengruifeng I've amended the code to be 0 indexed now, and hence, exactly reproduce what snowflake are doing. Thank you for your prompt feedback.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on pull request #38867: [WIP] [SPARK-41234] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1334649000
cc @zhengruifeng FYI
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1042205784
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
Review Comment:
Good spot- have amended, thank you
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1408689550
> @Daniel-Davies Don't worry. Thanks for working on it!
>
> BTW, the scala linter seems failed
Hey @zhengruifeng - should be fixed now; I've updated both nullSafeEval and doGenCode, let me know if the method here is more understandable than previously
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1091003986
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
UNNEST at least has the benefit of being more widely used (at least in my experience)
ANSI mode as a config option would be ideal (but it seems like quite a high engineering lift for the benefits); and I suppose posexplode being the only function- outside of "get"- using 0 based indexing isn't the end of the world from my perspective (name not as widely recognised, and also probably not as frequently used as regular explode, but I don't have any data on that)
It sounds like we want to probably make array_insert 1 indexed from the above; I'll await confirmation from some of the previous commenters before changing the code
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1157449697
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -431,6 +431,104 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 3, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 3, 4):array<int>>
+-- !query output
+[1,2,4,3]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
Review Comment:
I think this 0 behavior is a trap. It invites the belief that array_insert is 0 based. @cloud-fan
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1103632167
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,257 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1, or start from the end if start is negative).\",",
Review Comment:
@cloud-fan Apologies for the delay, something came up at work. Please see the doc fix here: https://github.com/apache/spark/pull/39975 (aligned to the python docstring also).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1365494699
@zhengruifeng Great direction provided by your message above, thank you. Thank you very much also for consulting with experts to resolve the conflict of ideas. I'll implement the behaviour you've stated. Is the discussion public anywhere? I'd love to know some of the reasoning, if at all possible.
@LuciferYang certainly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] beliefer commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
beliefer commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1365779642
@Daniel-Davies Could you add the syntax, arguments, examples and the list of the mainstream database supports `array_insert` ? refer https://github.com/apache/spark/pull/38865
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1406030290
@Daniel-Davies Don't worry. Thanks for working on it!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1340595280
> I've used a regular for-loop to implement the behaviour; there is also a scala library 'patch' function that could do the same thing, but it seems to only accept a Seq() as an item parameter, so I didn't want to introduce the overhead. Please let me know if this should be changed.
`for-loop` is ok for me
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1335296230
> Please let me know when this one ready to review :)
Will do! Thank you for your quick replies on this. I'm just trying to work out what doCodeGen works, after which, it will be ready soon!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1418658725
Merged into master and branch-3.4, thank you @Daniel-Davies so much for working on it!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063277816
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
Review Comment:
`e2` needs to be orderable?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063376033
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
+
+ baseArr.foreach(arrayElementType, (i, v) => {
+ var elementPosition = i
+ if (newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1 + math.abs(posInt + baseArr.numElements())
+ }
+ if (i >= itemInsertionIndex && !newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1
+ }
+ newArray(elementPosition) = v
+ })
+
+ newArray(itemInsertionIndex) = item
+
+ return new GenericArrayData(newArray)
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val f = (arrExpr: ExprCode, posExpr: ExprCode, itemExpr: ExprCode) => {
+ val arr = arrExpr.value
+ val pos = posExpr.value
+ val item = itemExpr.value
+
+ val itemInsertionIndex = ctx.freshName("itemInsertionIndex")
+ val newPosExtendsArrayLeft = ctx.freshName("newPosExtendsArrayLeft")
+ val adjustedAllocIdx = ctx.freshName("adjustedAllocIdx")
+ val resLength = ctx.freshName("resLength")
+ val insertedItemIsNull = ctx.freshName("insertedItemIsNull")
+ val i = ctx.freshName("i")
+ val j = ctx.freshName("j")
+ val values = ctx.freshName("values")
+
+ val allocation = CodeGenerator.createArrayData(
+ values, elementType, resLength, s"$prettyName failed.")
+ val assignment = CodeGenerator.createArrayAssignment(values, elementType, arr,
+ adjustedAllocIdx, i, true)
+
+ val defaultIntValue = CodeGenerator.defaultValue(CodeGenerator.JAVA_INT, false)
+ s"""
+ |${CodeGenerator.JAVA_INT} $itemInsertionIndex = 0;
Review Comment:
oh i see- good catch, thank you
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090675422
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
Hey @cloud-fan - thanks for your review; there's a thread with discussion here
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
Hey @cloud-fan - thanks for your review; there's a thread with discussion here: https://github.com/apache/spark/pull/38867#pullrequestreview-1234077537
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090951068
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
I can only add that from a user perspective, the most common functions for array manipulation I've come across have been slice and element_at, so I would have been caught out by a 0 based array_insert if I didn't read the docs
Also ,MySQL loosely has array_insert [for json](https://dev.mysql.com/doc/refman/5.7/en/json-modification-functions.html#function_json-array-insert)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090966732
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
Given mySQL has a different name and operates on JSON I'm at peace with JSON_ARRAY_INSERT.
I wonder if we could fix posexplode().. at least in ANSI mode...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1041917483
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,155 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with SupportQueryContext
+ with QueryErrorsBase {
+
+ @transient private lazy val elementType: DataType =
+ srcArrayExpr.dataType.asInstanceOf[ArrayType].elementType
+
+ override def dataType: DataType = srcArrayExpr.dataType
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+
+ if (baseArr.numElements() + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(baseArr.numElements() + 1)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](arr.asInstanceOf[ArrayData].numElements() + 1)
+ arr.asInstanceOf[ArrayData].foreach(elementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ if (item != null) {
+ newArray(validatedPosInt) = item
+ }
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
Review Comment:
Hey- yes, theoretically array_prepend and array_append should probably be implemented using array_insert
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1041919524
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,155 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with SupportQueryContext
+ with QueryErrorsBase {
+
+ @transient private lazy val elementType: DataType =
+ srcArrayExpr.dataType.asInstanceOf[ArrayType].elementType
+
+ override def dataType: DataType = srcArrayExpr.dataType
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+
+ if (baseArr.numElements() + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(baseArr.numElements() + 1)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](arr.asInstanceOf[ArrayData].numElements() + 1)
+ arr.asInstanceOf[ArrayData].foreach(elementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ if (item != null) {
+ newArray(validatedPosInt) = item
+ }
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
Review Comment:
Oh sorry- you'd like array_insert(pos=0) to equal array_insert(pos=1) right? Happy to implement; alternatively, we could just make 'pos' be zero indexed instead of one indexed. What are your thoughts?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1157447487
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,155 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with SupportQueryContext
+ with QueryErrorsBase {
+
+ @transient private lazy val elementType: DataType =
+ srcArrayExpr.dataType.asInstanceOf[ArrayType].elementType
+
+ override def dataType: DataType = srcArrayExpr.dataType
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+
+ if (baseArr.numElements() + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(baseArr.numElements() + 1)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](arr.asInstanceOf[ArrayData].numElements() + 1)
+ arr.asInstanceOf[ArrayData].foreach(elementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ if (item != null) {
+ newArray(validatedPosInt) = item
+ }
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
Review Comment:
Ugh, this is a bit of a trap. If I'm testing array_insert() with 0 I walk away with the impression that it is 0 based.
Why would we want this. An error seems to be much more educational.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,155 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with SupportQueryContext
+ with QueryErrorsBase {
+
+ @transient private lazy val elementType: DataType =
+ srcArrayExpr.dataType.asInstanceOf[ArrayType].elementType
+
+ override def dataType: DataType = srcArrayExpr.dataType
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+
+ if (baseArr.numElements() + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(baseArr.numElements() + 1)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](arr.asInstanceOf[ArrayData].numElements() + 1)
+ arr.asInstanceOf[ArrayData].foreach(elementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ if (item != null) {
+ newArray(validatedPosInt) = item
+ }
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
Review Comment:
Ugh, this is a bit of a trap. If I'm testing array_insert() with 0 I walk away with the impression that it is 0 based.
Why would we want this. An error seems to be much more educational. @cloud-fan
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1340587996
> Core behaviour: one interesting property of snowflake's array_insert function is that it will let you extend the array further than (numElements + 1) if you specify a far away index. For example, array_insert([1,2,3], 10, 4) will print [1,2,3,null,null,null,null,null,null,4]. It would worry me a bit if an array could grow to astronomical sizes through some kind of mistake (e.g. are we happy with taking a risk of the 'pos' column containing a value of 2,000,000,000?), so I've returned a null if the provided array 'pos' index is out of bounds. Let me know if the snowflake behaviour should be exactly reproduced instead.
Personally, I think we do not need to exactly match Snowflake, and if it is out of bounds, is it more appropriate to throw an exception?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063934870
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
thanks for the update.
> All array functions of Spark follow a 1-base index to my understanding
the old index were problematic, and we are switching to 0-indexed, see
https://github.com/apache/spark/blob/4321604fe0b8f54b7cc5d1372f9b89a91fbad8b2/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala#L2172-L2195
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1059780745
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -427,6 +427,103 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 4, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 4, 4):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
+-- !query schema
+struct<array_insert(array(2, 3, 4), 0, 1):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 1, 1)
+-- !query schema
+struct<array_insert(array(2, 3, 4), 1, 1):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(1, 3, 4), -2, 2)
+-- !query schema
+struct<array_insert(array(1, 3, 4), -2, 2):array<int>>
+-- !query output
+[1,2,3,4]
+
+
+-- !query
+select array_insert(array(1, 2, 3), 4, "4")
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+ "errorClass" : "DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES",
+ "messageParameters" : {
+ "dataType" : "\"ARRAY\"",
+ "functionName" : "`array_insert`",
+ "leftType" : "\"ARRAY<INT>\"",
+ "rightType" : "\"STRING\"",
+ "sqlExpr" : "\"array_insert(array(1, 2, 3), 4, 4)\""
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 8,
+ "stopIndex" : 43,
+ "fragment" : "array_insert(array(1, 2, 3), 4, \"4\")"
+ } ]
+}
+
+
+-- !query
+select array_insert(cast(NULL as ARRAY<INT>), 1, 1)
+-- !query schema
+struct<array_insert(NULL, 1, 1):array<int>>
+-- !query output
+NULL
+
+
+-- !query
+select array_insert(array(1, 2, 3, NULL), cast(NULL as INT), 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3, NULL), CAST(NULL AS INT), 4):array<int>>
+-- !query output
+NULL
+
+
+-- !query
+select array_insert(array(1, 2, 3, NULL), 4, cast(NULL as INT))
+-- !query schema
+struct<array_insert(array(1, 2, 3, NULL), 4, CAST(NULL AS INT)):array<int>>
+-- !query output
+[1,2,3,null,null]
+
+
+-- !query
+select array_insert(array(2, 3, null, 4), 6, 5)
+-- !query schema
+struct<array_insert(array(2, 3, NULL, 4), 6, 5):array<int>>
+-- !query output
+[2,3,null,4,null,5]
Review Comment:
There doesn't seem to be a way to specify that the array can have NULL elements in the raw SQL syntax. I've added a null to the base array in the meantime, but please let me know if there is a better way to avoid the following outcome:
-- !query
select array_insert(array(2, 3, 4), 5, 1)
-- !query schema
struct<array_insert(array(2, 3, 4), 5, 1):array<int>>
-- !query output
[2,3,4,0,1]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090945476
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
Also mySQL doesn't seem to have ARRAY_INSERT, so Snowflake is alone on this one.
It seems like Snowflake is 0 based across the board.
I think we need to consider that we can't be good to our users AND migration scenarios at the same time.
Perhaps we should Default to 1 based natively (across th eboard). And allow users to config (if they want to be zero based that badly...)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1097174696
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,257 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1, or start from the end if start is negative).\",",
Review Comment:
@cloud-fan I will post a PR for this later today
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1097175487
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -431,6 +431,104 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 3, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 3, 4):array<int>>
+-- !query output
+[1,2,4,3]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
Review Comment:
No worries @LuciferYang - it was about this:
https://github.com/apache/spark/pull/38867#discussion_r1041949049
We have implemented array_insert(arr, 0, item) as array_prepend; let me know if this is incorrect
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1092124391
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala:
##########
@@ -3106,6 +3106,50 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSparkSession {
)
}
+ test("array_insert functions") {
+ val fiveShort: Short = 5
+
+ val df1 = Seq((Array[Int](3, 2, 5, 1, 2), 5, 3)).toDF("a", "b", "c")
+ val df2 = Seq((Array[Short](1, 2, 3, 4), 4, fiveShort)).toDF("a", "b", "c")
+ val df3 = Seq((Array[Double](3.0, 2.0, 5.0, 1.0, 2.0), 1, 3.0)).toDF("a", "b", "c")
+ val df4 = Seq((Array[Boolean](true, false), 2, false)).toDF("a", "b", "c")
+ val df5 = Seq((Array[String]("a", "b", "c"), 0, "d")).toDF("a", "b", "c")
+ val df6 = Seq((Array[String]("a", null, "b", "c"), 4, "d")).toDF("a", "b", "c")
+ val df7 = Seq((Array[Integer](3, 2, 5, null, 1, 2), 5, 3)).toDF("a", "b", "c")
+
+ checkAnswer(df1.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq(3, 2, 5, 1, 2, 3))))
+ checkAnswer(df2.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq[Short](1, 2, 3, 4, 5))))
+ checkAnswer(
+ df3.selectExpr("array_insert(a, b, c)"),
+ Seq(Row(Seq[Double](3.0, 3.0, 2.0, 5.0, 1.0, 2.0)))
+ )
+ checkAnswer(df4.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq(true, false, false))))
+ checkAnswer(df5.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq("d", "a", "b", "c"))))
+
+ // null checks
+ checkAnswer(df6.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq("a", null, "b", "c", "d"))))
+ checkAnswer(df5.select(
+ array_insert(col("a"), col("b"), lit(null).cast("string"))),
+ Seq(Row(Seq(null, "a", "b", "c")))
+ )
+ checkAnswer(df6.select(
+ array_insert(col("a"), col("b"), lit(null).cast("string"))),
+ Seq(Row(Seq("a", null, "b", "c", null)))
+ )
+ checkAnswer(
+ df5.select(array_insert(col("a"), lit(null).cast("integer"), col("c"))),
+ Seq(Row(null))
+ )
+ checkAnswer(
+ df5.select(array_insert(lit(null).cast("array<string>"), col("b"), col("c"))),
+ Seq(Row(null))
+ )
+ checkAnswer(
+ df7.selectExpr("array_insert(a, 7, c)"), Seq(Row(Seq(3, 2, 5, null, 1, 2, null, 3))))
Review Comment:
It seems that, because the array was specified as type `Array[Int]`, null entries are not permitted. Changing this to `Array[Integer]` seems to fix this. However, I still don't know why this worked a few weeks ago.
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -431,6 +431,104 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 3, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 3, 4):array<int>>
+-- !query output
+[1,2,4,3]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
Review Comment:
No problem at all :)
For pos=0, this was to implement the behaviour specified [here](https://github.com/apache/spark/pull/38867#discussion_r1041949049). However, the other spark functions (elementAt, slice) throw an exception i.e. see [slice](https://github.com/apache/spark/blob/c13fea90595cb1489046135c45c92d3bacb85818/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala#L1662). What would your preference be? Maybe we should throw an exception, considering we are no longer implementing the snowflake behaviour?
##########
python/pyspark/sql/functions.py:
##########
@@ -7679,6 +7679,41 @@ def array_distinct(col: "ColumnOrName") -> Column:
return _invoke_function_over_columns("array_distinct", col)
+@try_remote_functions
+def array_insert(arr: "ColumnOrName", pos: "ColumnOrName", value: "ColumnOrName") -> Column:
+ """
+ Collection function: adds an item into a given array at a specified position. A position
+ specified beyond the size of the current array (plus additional element)
+ is extended with 'null' elements.
+
+ .. versionadded:: 3.4.0
+
+ Parameters
+ ----------
+ arr : :class:`~pyspark.sql.Column` or str
+ name of column containing an array
+ pos : :class:`~pyspark.sql.Column` or str
+ name of Numeric type column indicating position of insertion (starting index 1)
Review Comment:
Of course, will make sure to do so throughout!
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -431,6 +431,104 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 3, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 3, 4):array<int>>
+-- !query output
+[1,2,4,3]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
Review Comment:
CC @LuciferYang for opinion also, as original commenter, and @cloud-fan for any additional context
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala:
##########
@@ -3106,6 +3106,50 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSparkSession {
)
}
+ test("array_insert functions") {
+ val fiveShort: Short = 5
+
+ val df1 = Seq((Array[Int](3, 2, 5, 1, 2), 5, 3)).toDF("a", "b", "c")
+ val df2 = Seq((Array[Short](1, 2, 3, 4), 4, fiveShort)).toDF("a", "b", "c")
+ val df3 = Seq((Array[Double](3.0, 2.0, 5.0, 1.0, 2.0), 1, 3.0)).toDF("a", "b", "c")
+ val df4 = Seq((Array[Boolean](true, false), 2, false)).toDF("a", "b", "c")
+ val df5 = Seq((Array[String]("a", "b", "c"), 0, "d")).toDF("a", "b", "c")
+ val df6 = Seq((Array[String]("a", null, "b", "c"), 4, "d")).toDF("a", "b", "c")
+ val df7 = Seq((Array[Integer](3, 2, 5, null, 1, 2), 5, 3)).toDF("a", "b", "c")
+
+ checkAnswer(df1.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq(3, 2, 5, 1, 2, 3))))
+ checkAnswer(df2.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq[Short](1, 2, 3, 4, 5))))
+ checkAnswer(
+ df3.selectExpr("array_insert(a, b, c)"),
+ Seq(Row(Seq[Double](3.0, 3.0, 2.0, 5.0, 1.0, 2.0)))
+ )
+ checkAnswer(df4.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq(true, false, false))))
+ checkAnswer(df5.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq("d", "a", "b", "c"))))
+
+ // null checks
+ checkAnswer(df6.selectExpr("array_insert(a, b, c)"), Seq(Row(Seq("a", null, "b", "c", "d"))))
+ checkAnswer(df5.select(
+ array_insert(col("a"), col("b"), lit(null).cast("string"))),
+ Seq(Row(Seq(null, "a", "b", "c")))
+ )
+ checkAnswer(df6.select(
+ array_insert(col("a"), col("b"), lit(null).cast("string"))),
+ Seq(Row(Seq("a", null, "b", "c", null)))
+ )
+ checkAnswer(
+ df5.select(array_insert(col("a"), lit(null).cast("integer"), col("c"))),
+ Seq(Row(null))
+ )
+ checkAnswer(
+ df5.select(array_insert(lit(null).cast("array<string>"), col("b"), col("c"))),
+ Seq(Row(null))
+ )
+ checkAnswer(
+ df7.selectExpr("array_insert(a, 7, c)"), Seq(Row(Seq(3, 2, 5, null, 1, 2, null, 3))))
Review Comment:
This test has started behaving differently in the last few weeks; previously the below worked:
`checkAnswer(df1.selectExpr("array_insert(a, 6, c)"), Seq(Row(Seq(3, 2, 5, 1, 2, null, 3))))`
Now, it fails, as the result ends up being `Seq(3, 2, 5, 1, 2, 0, 3)`- i.e. the nulls are replaced with a 0. I've run the test on my logic from a few weeks ago, and it still fails, so something has changed in the test suite in the last 3 weeks.
I'm not sure how to specify that the Array can have null elements in the scala syntax, so for now, I've just added a null to the test array (i.e. `(3, 2, 5, 1, 2)` => `(3, 2, 5, null, 1, 2)`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1087954361
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,230 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeCheckResult.TypeCheckSuccess
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
Review Comment:
Hey @zhengruifeng - 100%, thank you for the feedback. I've split it into two main codepaths that should now be much easier to grasp rather than one. It's a bit more verbose but worth it I think. Let me know if it's still not clear!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "srielau (via GitHub)" <gi...@apache.org>.
srielau commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090968624
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
Or we could do UNNEST and "retire" pos-explode.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1340786990
> > Core behaviour: one interesting property of snowflake's array_insert function is that it will let you extend the array further than (numElements + 1) if you specify a far away index. For example, array_insert([1,2,3], 10, 4) will print [1,2,3,null,null,null,null,null,null,4]. It would worry me a bit if an array could grow to astronomical sizes through some kind of mistake (e.g. are we happy with taking a risk of the 'pos' column containing a value of 2,000,000,000?), so I've returned a null if the provided array 'pos' index is out of bounds. Let me know if the snowflake behaviour should be exactly reproduced instead.
>
> Personally, I think we do not need to exactly match Snowflake, and if it is out of bounds, is it more appropriate to throw an exception?
I'd be keen to maintain consistency; in this case, ElementAt with out of bounds indices return a null. With that said, I'd happy to change ElementAt in a separate PR if that's of interest?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1060550263
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
Thanks for the quick response @beliefer - two questions:
1. I can't find any function descriptions that tell users that arrays are 0 based; could you link some? For example, here is the live documentation for [elementAt](https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.functions.element_at.html), which uses 1 based indexing.
2. The SQL99 standard specifies that SQL Arrays use 1 based indexing. For example, PostGres uses 1 based index Arrays also. Even if we ignored all other legacy spark functions, is there a reason you prefer 0 based indexing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] beliefer commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
beliefer commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1060266937
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
Review Comment:
The behavior is different from snowflake.
The result should be `[5,3,4,2,1]`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
Review Comment:
snowflake starts at 0.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
The behavior is different from snowflake.
The result should be [1,2,3,4,undefined,5]
I don't know if we should use undefined here too. cc @zhengruifeng
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090676792
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
Oh sorry, you commented there also- I will hold off on reverting to 1 based index for now until everybody has decided on an implementation, since the opinion seems to vary a lot
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "Daniel-Davies (via GitHub)" <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1090677648
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,247 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 0)",
Review Comment:
I personally think 1 based index makes more sense but the thread linked above disagreed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1097049802
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -431,6 +431,104 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 3, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 3, 4):array<int>>
+-- !query output
+[1,2,4,3]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
Review Comment:
Sorry, missed some message
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1368449072
Thanks @zhengruifeng! Updated the logic to implement the above discussion.
@beliefer updated description as requested.
The changes should now be ready for review. I have left one question about the SQL tests in a code comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063327331
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
+
+ baseArr.foreach(arrayElementType, (i, v) => {
+ var elementPosition = i
+ if (newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1 + math.abs(posInt + baseArr.numElements())
+ }
+ if (i >= itemInsertionIndex && !newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1
+ }
+ newArray(elementPosition) = v
+ })
+
+ newArray(itemInsertionIndex) = item
+
+ return new GenericArrayData(newArray)
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val f = (arrExpr: ExprCode, posExpr: ExprCode, itemExpr: ExprCode) => {
+ val arr = arrExpr.value
+ val pos = posExpr.value
+ val item = itemExpr.value
+
+ val itemInsertionIndex = ctx.freshName("itemInsertionIndex")
+ val newPosExtendsArrayLeft = ctx.freshName("newPosExtendsArrayLeft")
+ val adjustedAllocIdx = ctx.freshName("adjustedAllocIdx")
+ val resLength = ctx.freshName("resLength")
+ val insertedItemIsNull = ctx.freshName("insertedItemIsNull")
+ val i = ctx.freshName("i")
+ val j = ctx.freshName("j")
+ val values = ctx.freshName("values")
+
+ val allocation = CodeGenerator.createArrayData(
+ values, elementType, resLength, s"$prettyName failed.")
+ val assignment = CodeGenerator.createArrayAssignment(values, elementType, arr,
+ adjustedAllocIdx, i, true)
+
+ val defaultIntValue = CodeGenerator.defaultValue(CodeGenerator.JAVA_INT, false)
+ s"""
+ |${CodeGenerator.JAVA_INT} $itemInsertionIndex = 0;
Review Comment:
hmm could you clarify what the replacement should be? I don't quite understand- would you like to replace the '0' with defaultIntValue?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] beliefer commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
beliefer commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063236793
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
@Daniel-Davies I'm sorry. I said wrong. It seems the array functions supported by Spark starts at 1. So there are two ways:
1. All the new array functions still starts at 1.
2. Another way, the legacy functions starts at 1 is a mistake indeed. For follow snowflake, we have to break change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1338706555
A bit busy today, I may have to wait until evening to review
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1042113271
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,155 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with SupportQueryContext
+ with QueryErrorsBase {
+
+ @transient private lazy val elementType: DataType =
+ srcArrayExpr.dataType.asInstanceOf[ArrayType].elementType
+
+ override def dataType: DataType = srcArrayExpr.dataType
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+
+ if (baseArr.numElements() + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(baseArr.numElements() + 1)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](arr.asInstanceOf[ArrayData].numElements() + 1)
+ arr.asInstanceOf[ArrayData].foreach(elementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ if (item != null) {
+ newArray(validatedPosInt) = item
+ }
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
Review Comment:
Got it- amended to make this work (though spark arrays being 1 indexed means the example here is more akin to "inserting at position 1 has the same effect as using array_prepend")
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1042200500
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+ val newArrayLength = baseArr.numElements() + 1
Review Comment:
See thread here: https://github.com/apache/spark/pull/38867#issuecomment-1340786990 - happy to reproduce snowflake exactly if required, but based on existing spark functions, it didn't really make sense to me. Let me know your thoughts
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1064048215
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
Review Comment:
Amended, thank you for reviewing!
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
+
+ baseArr.foreach(arrayElementType, (i, v) => {
+ var elementPosition = i
+ if (newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1 + math.abs(posInt + baseArr.numElements())
+ }
+ if (i >= itemInsertionIndex && !newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1
+ }
+ newArray(elementPosition) = v
+ })
+
+ newArray(itemInsertionIndex) = item
+
+ return new GenericArrayData(newArray)
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val f = (arrExpr: ExprCode, posExpr: ExprCode, itemExpr: ExprCode) => {
+ val arr = arrExpr.value
+ val pos = posExpr.value
+ val item = itemExpr.value
+
+ val itemInsertionIndex = ctx.freshName("itemInsertionIndex")
+ val newPosExtendsArrayLeft = ctx.freshName("newPosExtendsArrayLeft")
+ val adjustedAllocIdx = ctx.freshName("adjustedAllocIdx")
+ val resLength = ctx.freshName("resLength")
+ val insertedItemIsNull = ctx.freshName("insertedItemIsNull")
+ val i = ctx.freshName("i")
+ val j = ctx.freshName("j")
+ val values = ctx.freshName("values")
+
+ val allocation = CodeGenerator.createArrayData(
+ values, elementType, resLength, s"$prettyName failed.")
+ val assignment = CodeGenerator.createArrayAssignment(values, elementType, arr,
+ adjustedAllocIdx, i, true)
+
+ val defaultIntValue = CodeGenerator.defaultValue(CodeGenerator.JAVA_INT, false)
+ s"""
+ |${CodeGenerator.JAVA_INT} $itemInsertionIndex = 0;
Review Comment:
Amended, thank you for reviewing!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1063297447
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 4, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), -3, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with QueryErrorsBase {
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case (ArrayType(e1, _), e2, e3) if e1.sameType(e3) =>
+ TypeUtils.checkForOrderingExpr(e2, prettyName)
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "ARRAY_FUNCTION_DIFF_TYPES",
+ messageParameters = Map(
+ "functionName" -> toSQLId(prettyName),
+ "dataType" -> toSQLType(ArrayType),
+ "leftType" -> toSQLType(first.dataType),
+ "rightType" -> toSQLType(third.dataType)
+ )
+ )
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val value1 = first.eval(input)
+ if (value1 != null) {
+ val value2 = second.eval(input)
+ if (value2 != null) {
+ val value3 = third.eval(input)
+ return nullSafeEval(value1, value2, value3)
+ }
+ }
+ null
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+
+ val newPosExtendsArrayLeft = posInt < 0 && math.abs(posInt) > baseArr.numElements() - 1
+
+ val itemInsertionIndex = if (newPosExtendsArrayLeft) {
+ 0
+ } else if (posInt < 0) {
+ posInt + baseArr.numElements()
+ } else {
+ posInt
+ }
+
+ val newArrayLength = if (newPosExtendsArrayLeft) {
+ math.abs(posInt) + 1
+ } else {
+ math.max(baseArr.numElements() + 1, itemInsertionIndex + 1)
+ }
+
+ val newArray = new Array[Any](newArrayLength)
+
+ baseArr.foreach(arrayElementType, (i, v) => {
+ var elementPosition = i
+ if (newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1 + math.abs(posInt + baseArr.numElements())
+ }
+ if (i >= itemInsertionIndex && !newPosExtendsArrayLeft) {
+ elementPosition = elementPosition + 1
+ }
+ newArray(elementPosition) = v
+ })
+
+ newArray(itemInsertionIndex) = item
+
+ return new GenericArrayData(newArray)
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val f = (arrExpr: ExprCode, posExpr: ExprCode, itemExpr: ExprCode) => {
+ val arr = arrExpr.value
+ val pos = posExpr.value
+ val item = itemExpr.value
+
+ val itemInsertionIndex = ctx.freshName("itemInsertionIndex")
+ val newPosExtendsArrayLeft = ctx.freshName("newPosExtendsArrayLeft")
+ val adjustedAllocIdx = ctx.freshName("adjustedAllocIdx")
+ val resLength = ctx.freshName("resLength")
+ val insertedItemIsNull = ctx.freshName("insertedItemIsNull")
+ val i = ctx.freshName("i")
+ val j = ctx.freshName("j")
+ val values = ctx.freshName("values")
+
+ val allocation = CodeGenerator.createArrayData(
+ values, elementType, resLength, s"$prettyName failed.")
+ val assignment = CodeGenerator.createArrayAssignment(values, elementType, arr,
+ adjustedAllocIdx, i, true)
+
+ val defaultIntValue = CodeGenerator.defaultValue(CodeGenerator.JAVA_INT, false)
+ s"""
+ |${CodeGenerator.JAVA_INT} $itemInsertionIndex = 0;
Review Comment:
`CodeGenerator.JAVA_*` only used here, the following code is no longer used, better unify
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1093215215
##########
python/pyspark/sql/functions.py:
##########
@@ -7679,6 +7679,41 @@ def array_distinct(col: "ColumnOrName") -> Column:
return _invoke_function_over_columns("array_distinct", col)
+@try_remote_functions
+def array_insert(arr: "ColumnOrName", pos: "ColumnOrName", value: "ColumnOrName") -> Column:
+ """
+ Collection function: adds an item into a given array at a specified position. A position
+ specified beyond the size of the current array (plus additional element)
+ is extended with 'null' elements.
+
+ .. versionadded:: 3.4.0
+
+ Parameters
+ ----------
+ arr : :class:`~pyspark.sql.Column` or str
+ name of column containing an array
+ pos : :class:`~pyspark.sql.Column` or str
+ name of Numeric type column indicating position of insertion (starting index 1)
Review Comment:
would you mind also doc the meaning of negative position?
##########
sql/core/src/test/resources/sql-tests/results/array.sql.out:
##########
@@ -431,6 +431,104 @@ struct<get(array(1, 2, 3), -1):int>
NULL
+-- !query
+select array_insert(array(1, 2, 3), 3, 4)
+-- !query schema
+struct<array_insert(array(1, 2, 3), 3, 4):array<int>>
+-- !query output
+[1,2,4,3]
+
+
+-- !query
+select array_insert(array(2, 3, 4), 0, 1)
Review Comment:
pos=0 also means the first item, is this expected? If so, I think we need to document it.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
@cloud-fan thank you for the clarification
@Daniel-Davies sorry for ask you to switch to 0-based index, my bad.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1042206176
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,153 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with ComplexTypeMergingExpression
+ with SupportQueryContext with QueryErrorsBase{
+
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+ val arrayElementType = dataType.asInstanceOf[ArrayType].elementType
+ val newArrayLength = baseArr.numElements() + 1
+
+ if (newArrayLength > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(newArrayLength)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](newArrayLength)
+ baseArr.foreach(arrayElementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ newArray(validatedPosInt) = item
+
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
Review Comment:
Removed, thank you
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on a diff in pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1041915384
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4600,3 +4600,155 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
+ > SELECT _FUNC_(array(5, 3, 2, 1), 2, 4);
+ [5,4,3,2,1]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayInsert(srcArrayExpr: Expression, posExpr: Expression, itemExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes with SupportQueryContext
+ with QueryErrorsBase {
+
+ @transient private lazy val elementType: DataType =
+ srcArrayExpr.dataType.asInstanceOf[ArrayType].elementType
+
+ override def dataType: DataType = srcArrayExpr.dataType
+ override def inputTypes: Seq[AbstractDataType] = {
+ (srcArrayExpr.dataType, posExpr.dataType, itemExpr.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType, e3) if (e2 != LongType) =>
+ TypeCoercion.findTightestCommonType(e1, e3) match {
+ case Some(dt) => Seq(ArrayType(dt, hasNull), IntegerType, dt)
+ case _ => Seq.empty
+ }
+ case (e1, e2, e3) => Seq.empty
+ }
+ Seq.empty
+ }
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ (first.dataType, second.dataType, third.dataType) match {
+ case (_: ArrayType, e2, e3) if e2 != IntegerType =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> "2",
+ "requiredType" -> toSQLType(IntegerType),
+ "inputSql" -> toSQLExpr(second),
+ "inputType" -> toSQLType(second.dataType))
+ )
+ case _ => TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val arr = first.eval(input)
+ val pos = second.eval(input)
+ val item = third.eval(input)
+
+ if (arr == null || pos == null) {
+ null
+ } else {
+ nullSafeEval(arr, pos, item)
+ }
+ }
+
+ override def nullSafeEval(arr: Any, pos: Any, item: Any): Any = {
+ val baseArr = arr.asInstanceOf[ArrayData]
+ val posInt = pos.asInstanceOf[Int]
+
+ if (baseArr.numElements() + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw QueryExecutionErrors.concatArraysWithElementsExceedLimitError(baseArr.numElements() + 1)
+ }
+ val validatedPosInt = this.getValidPosIndexOrThrow(posInt, baseArr.numElements())
+
+ if (validatedPosInt < 0 || validatedPosInt > baseArr.numElements()) {
+ null
+ } else {
+ val newArray = new Array[Any](arr.asInstanceOf[ArrayData].numElements() + 1)
+ arr.asInstanceOf[ArrayData].foreach(elementType, (i, v) =>
+ if (i >= validatedPosInt) {
+ newArray(i + 1) = v
+ } else {
+ newArray(i) = v
+ }
+ )
+ if (item != null) {
+ newArray(validatedPosInt) = item
+ }
+ new GenericArrayData(newArray)
+ }
+ }
+
+ private def getValidPosIndexOrThrow(pos: Int, numArrayElements: Int): Int = {
+ val validIdx = if (pos == 0) {
Review Comment:
https://docs.snowflake.com/en/developer-guide/snowpark/reference/python/api/snowflake.snowpark.functions.array_insert.html
For `pos` is 0, Is the behavior ``inserting at position 0 has the same effect as using array_prepend`` easy to implement?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] LuciferYang commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
LuciferYang commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1365032081
@Daniel-Davies could you resolve the conflict?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1365726898
@Daniel-Davies
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on pull request #38867: [WIP] [SPARK-41234][SQL][PYTHON] Add functionality for array_insert
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on PR #38867:
URL: https://github.com/apache/spark/pull/38867#issuecomment-1337219140
@LuciferYang for quick feedback I'd be grateful for an overarching review of the method, and some assistance on the following questions:
- Core behaviour: one interesting property of snowflake's array_insert function is that it will let you extend the array further than (numElements + 1) if you specify a far away index. For example, array_insert([1,2,3], 10, 4) will print [1,2,3,null,null,null,null,null,null,4). It would worry me a bit if an array could grow to astronomical sizes through some kind of mistake (e.g. are we happy with taking a risk of the 'pos' column containing a value of 2,000,000,000?), so I've returned a null if the provided array 'pos' index is out of bounds. Let me know if the snowflake behaviour should be exactly reproduced instead.
- I've used the scala library 'patch' function to implement the behaviour, which prioritises minimal code over performance. Please let me know if this should be changed.
- I'm not too clear on how to implicitly cast the provided 'item' parameter yet (i.e. I provide an array of LongType and try to insert an IntegerType.). I think the array type should probably not change though (e.g. if I provide a LongType array but a StringType item for insertion, it doesn't feel right to cast the whole Array to a StringType).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1060395813
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
All array functions of Spark follow a 1-base index to my understanding; for example, many functions of the collectionOperations.scala have something like the following comment:
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala#L2080
To me, it sounds like a bad idea to be inconsistent between these new functions and legacy functions. I'm happy to amend the code if we want to follow a 0 based index however (but I do think people would likely make mistakes in their code if we implemented array_insert as the only array function using a 0 index)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] Daniel-Davies commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
Daniel-Davies commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1060397210
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
Review Comment:
https://github.com/apache/spark/pull/38867#discussion_r1060395813
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] beliefer commented on a diff in pull request #38867: [SPARK-41234][SQL][PYTHON] Add `array_insert` function
Posted by GitBox <gi...@apache.org>.
beliefer commented on code in PR #38867:
URL: https://github.com/apache/spark/pull/38867#discussion_r1060503592
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -4601,6 +4601,231 @@ case class ArrayExcept(left: Expression, right: Expression) extends ArrayBinaryL
newLeft: Expression, newRight: Expression): ArrayExcept = copy(left = newLeft, right = newRight)
}
+@ExpressionDescription(
+ usage = "_FUNC_(x, pos, val) - Places val into index pos of array x (array indices start at 1)",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 5, 5);
+ [1,2,3,4,5]
Review Comment:
Yes. This is an issue indeed before we discuss how to do it.
These legacy functions let users know each array function starts at 0, if we change the new functions based on 1, it will cause confusion.
Another way, the legacy functions is a mistake indeed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org