Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address review comments
  • Loading branch information
kiszk committed Apr 18, 2018
commit 9a0321d67cd6e7c65cbf54e22099cc0cfae03463
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ case class ArrayPosition(left: Expression, right: Expression)
Seq(ArrayType, left.dataType.asInstanceOf[ArrayType].elementType)

override def nullable: Boolean = {
left.nullable || right.nullable || left.dataType.asInstanceOf[ArrayType].containsNull
left.nullable || right.nullable
}

override def nullSafeEval(arr: Any, value: Any): Any = {
Expand All @@ -551,15 +551,15 @@ case class ArrayPosition(left: Expression, right: Expression)
val i = ctx.freshName("i")
val getValue = CodeGenerator.getValue(arr, right.dataType, i)
s"""
|int ${pos} = 0;
|int $pos = 0;
|for (int $i = 0; $i < $arr.numElements(); $i ++) {
| if (${ctx.genEqual(right.dataType, value, getValue)}) {
| ${pos} = $i + 1;
| if (!$arr.isNullAt($i) && ${ctx.genEqual(right.dataType, value, getValue)}) {
| $pos = $i + 1;
| break;
| }
|}
|${ev.value} = (long) $pos;
"""
""".stripMargin
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
}

test("Array Position") {
val a0 = Literal.create(Seq(1, 2, 3), ArrayType(IntegerType))
val a0 = Literal.create(Seq(1, null, 2, 3), ArrayType(IntegerType))
val a1 = Literal.create(Seq[String](null, ""), ArrayType(StringType))
val a2 = Literal.create(Seq(null), ArrayType(LongType))
val a3 = Literal.create(null, ArrayType(StringType))

checkEvaluation(ArrayPosition(a0, Literal(3)), 4L)
checkEvaluation(ArrayPosition(a0, Literal(1)), 1L)
checkEvaluation(ArrayPosition(a0, Literal(0)), 0L)
checkEvaluation(ArrayPosition(a0, Literal.create(null, IntegerType)), null)
Expand Down