libenchao commented on a change in pull request #12436:
URL: https://github.com/apache/flink/pull/12436#discussion_r433717924



##########
File path: 
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/calls/ScalarOperatorGens.scala
##########
@@ -1643,19 +1644,35 @@ object ScalarOperatorGens {
     val resultTypeTerm = primitiveTypeTermForType(componentInfo)
     val defaultTerm = primitiveDefaultValue(componentInfo)
 
+    if (index.literalValue.isDefined &&
+        index.literalValue.get.isInstanceOf[Int] &&
+        index.literalValue.get.asInstanceOf[Int] < 1) {
+      throw new ValidationException(s"Array element access needs an index 
starting at 1 but was " +

Review comment:
       If we cannot do this in calcite's parsing/validating phase, it's ok to 
me to check it here.

##########
File path: 
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/calls/ScalarOperatorGens.scala
##########
@@ -1643,19 +1644,35 @@ object ScalarOperatorGens {
     val resultTypeTerm = primitiveTypeTermForType(componentInfo)
     val defaultTerm = primitiveDefaultValue(componentInfo)
 
+    if (index.literalValue.isDefined &&
+        index.literalValue.get.isInstanceOf[Int] &&
+        index.literalValue.get.asInstanceOf[Int] < 1) {
+      throw new ValidationException(s"Array element access needs an index 
starting at 1 but was " +
+        s"${index.literalValue.get.asInstanceOf[Int]}.")
+    }
     val idxStr = s"${index.resultTerm} - 1"
     val arrayIsNull = s"${array.resultTerm}.isNullAt($idxStr)"
     val arrayGet =
       rowFieldReadAccess(ctx, idxStr, array.resultTerm, componentInfo)
 
+    /**
+     * Return null when array index out of bounds which follows Calcite's 
behaviour.
+     * @see org.apache.calcite.sql.fun.SqlStdOperatorTable
+     */
     val arrayAccessCode =
-      s"""
-         |${array.code}
-         |${index.code}
-         |boolean $nullTerm = ${array.nullTerm} || ${index.nullTerm} || 
$arrayIsNull;
-         |$resultTypeTerm $resultTerm = $nullTerm ? $defaultTerm : $arrayGet;
-         |""".stripMargin
-
+    s"""
+        |${array.code}
+        |${index.code}
+        |$resultTypeTerm $resultTerm;
+        |boolean $nullTerm;
+        |if (${idxStr} < 0 || ${idxStr} >= ${array.resultTerm}.size()) {

Review comment:
       `${array.resultTerm}` maybe null?

##########
File path: 
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/calls/ScalarOperatorGens.scala
##########
@@ -1643,19 +1644,35 @@ object ScalarOperatorGens {
     val resultTypeTerm = primitiveTypeTermForType(componentInfo)
     val defaultTerm = primitiveDefaultValue(componentInfo)
 
+    if (index.literalValue.isDefined &&
+        index.literalValue.get.isInstanceOf[Int] &&
+        index.literalValue.get.asInstanceOf[Int] < 1) {
+      throw new ValidationException(s"Array element access needs an index 
starting at 1 but was " +
+        s"${index.literalValue.get.asInstanceOf[Int]}.")
+    }
     val idxStr = s"${index.resultTerm} - 1"
     val arrayIsNull = s"${array.resultTerm}.isNullAt($idxStr)"
     val arrayGet =
       rowFieldReadAccess(ctx, idxStr, array.resultTerm, componentInfo)
 
+    /**
+     * Return null when array index out of bounds which follows Calcite's 
behaviour.
+     * @see org.apache.calcite.sql.fun.SqlStdOperatorTable
+     */
     val arrayAccessCode =
-      s"""
-         |${array.code}
-         |${index.code}
-         |boolean $nullTerm = ${array.nullTerm} || ${index.nullTerm} || 
$arrayIsNull;
-         |$resultTypeTerm $resultTerm = $nullTerm ? $defaultTerm : $arrayGet;
-         |""".stripMargin
-
+    s"""
+        |${array.code}
+        |${index.code}
+        |$resultTypeTerm $resultTerm;
+        |boolean $nullTerm;
+        |if (${idxStr} < 0 || ${idxStr} >= ${array.resultTerm}.size()) {

Review comment:
       it seems `idxStr` could ` < 0` at runtime (if it's not a literal, and 
cannot validate before runtime), I think we can throw exception for this case.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to