Skip to content

Commit 622065d

Browse files
committed
array_insert: return null when pos is null
1 parent f9213cf commit 622065d

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

native/spark-expr/src/array_funcs/array_insert.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ fn array_insert<O: OffsetSizeTrait>(
222222
let start = window[0].as_usize();
223223
let end = window[1].as_usize();
224224
let len = end - start;
225+
226+
// Return null for the entire row when pos is null (consistent with Spark's behavior)
227+
if pos_data.is_null(row_index) {
228+
new_offsets.push(new_offsets[row_index]);
229+
list_valid.push(false);
230+
continue;
231+
}
225232
let pos = pos_data.value(row_index);
226233

227234
if list_array.is_null(row_index) {

spark/src/test/scala/org/apache/comet/CometArrayExpressionSuite.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,13 @@ class CometArrayExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelp
211211
.withColumn("arrInsertResult", expr("array_insert(arr, 1, 1)"))
212212
.withColumn("arrInsertNegativeIndexResult", expr("array_insert(arr, -1, 1)"))
213213
.withColumn("arrPosGreaterThanSize", expr("array_insert(arr, 8, 1)"))
214+
.withColumn("arrPosIsNull", expr("array_insert(arr, cast(null as int), 1)"))
214215
.withColumn("arrNegPosGreaterThanSize", expr("array_insert(arr, -8, 1)"))
215216
.withColumn("arrInsertNone", expr("array_insert(arr, 1, null)"))
216217
checkSparkAnswerAndOperator(df.select("arrInsertResult"))
217218
checkSparkAnswerAndOperator(df.select("arrInsertNegativeIndexResult"))
218219
checkSparkAnswerAndOperator(df.select("arrPosGreaterThanSize"))
220+
checkSparkAnswerAndOperator(df.select("arrPosIsNull"))
219221
checkSparkAnswerAndOperator(df.select("arrNegPosGreaterThanSize"))
220222
checkSparkAnswerAndOperator(df.select("arrInsertNone"))
221223
})

0 commit comments

Comments
 (0)