You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "nebojsa-db (via GitHub)" <gi...@apache.org> on 2024/07/12 14:06:58 UTC

[PR] [WIP][SC-170296] GROUP BY with MapType nested inside complex type [spark]

nebojsa-db opened a new pull request, #47331:
URL: https://github.com/apache/spark/pull/47331

   <!--
   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
        'common/utils/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.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1696785824


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   So MapType as key type should be forbidden, but when MapType is type of value, this approach with MapSort won't work. We can create separate ticket for that one or try to address it as part of this PR? 
   Most likely we would need to modify MapSort with that recursive approach, similar with what we have done for initial version 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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1700331592


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,56 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case m: MapType =>
+        // Check if value type of MapType contains MapType (possibly nested)
+        // and special handle this case.
+        val mapSortExpr = if (m.valueType.existsRecursively(_.isInstanceOf[MapType])) {
+          MapFromArrays(MapKeys(e), insertMapSortRecursively(MapValues(e)))
+        }
+        else {
+          e
+        }

Review Comment:
   ```suggestion
           } else {
             e
           }
   ```



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1700332255


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,56 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case m: MapType =>
+        // Check if value type of MapType contains MapType (possibly nested)
+        // and special handle this case.
+        val mapSortExpr = if (m.valueType.existsRecursively(_.isInstanceOf[MapType])) {
+          MapFromArrays(MapKeys(e), insertMapSortRecursively(MapValues(e)))
+        }
+        else {
+          e
+        }
+
+        MapSort(mapSortExpr)
+
+      case StructType(fields) =>
+        val struct = CreateNamedStruct(fields.zipWithIndex.flatMap { case (f, i) =>
+          Seq(Literal(f.name), insertMapSortRecursively(
+            GetStructField(e, i, Some(f.name))))
+        }.toImmutableArraySeq)
+        if (struct.valExprs.forall(_.isInstanceOf[GetStructField])) {
+          // No field needs MapSort processing, just return the original expression.
+          e
+        } else if (e.nullable) {
+          If(IsNull(e), Literal(null, struct.dataType), struct)
+        } else {
+          struct
+        }
+
+      case ArrayType(et, containsNull) =>
+        val param = NamedLambdaVariable("x", et, containsNull)
+        val funcBody = insertMapSortRecursively(param)
+
+        ArrayTransform(e, LambdaFunction(funcBody, Seq(param)))

Review Comment:
   we should only do this if `et` contains MapType



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


Re: [PR] [SPARK-49093][SQL] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #47331: [SPARK-49093][SQL] GROUP BY with MapType nested inside complex type
URL: https://github.com/apache/spark/pull/47331


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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1692937421


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -892,132 +892,108 @@ case class MapFromEntries(child: Expression)
     copy(child = newChild)
 }
 
+// Sorts all MapType expressions based on the ordering of their keys.
+// This is used when GROUP BY is done with a MapType (possibly nested) column.
 case class MapSort(base: Expression)
-  extends UnaryExpression with NullIntolerant with QueryErrorsBase {
+  extends UnaryExpression with NullIntolerant with QueryErrorsBase with CodegenFallback {
 
-  val keyType: DataType = base.dataType.asInstanceOf[MapType].keyType
-  val valueType: DataType = base.dataType.asInstanceOf[MapType].valueType
+  override lazy val canonicalized: Expression = base.canonicalized
+
+  override lazy val deterministic: Boolean = base.deterministic
 
   override def child: Expression = base
 
   override def dataType: DataType = base.dataType
 
-  override def checkInputDataTypes(): TypeCheckResult = base.dataType match {
-    case m: MapType if RowOrdering.isOrderable(m.keyType) =>
-        TypeCheckResult.TypeCheckSuccess
+  def recursiveCheckDataTypes(dataType: DataType): TypeCheckResult = dataType match {
+    case a: ArrayType => recursiveCheckDataTypes(a.elementType)
+    case StructType(fields) =>
+      fields.collect(sf => recursiveCheckDataTypes(sf.dataType)).filter(_.isFailure).headOption
+        .getOrElse(TypeCheckResult.TypeCheckSuccess)
+    case m: MapType if RowOrdering.isOrderable(m.keyType) => TypeCheckResult.TypeCheckSuccess
     case _: MapType =>
       DataTypeMismatch(
         errorSubClass = "INVALID_ORDERING_TYPE",
         messageParameters = Map(
           "functionName" -> toSQLId(prettyName),
-          "dataType" -> toSQLType(base.dataType)
+          "dataType" -> toSQLType(dataType)
         )
       )
-    case _ =>
-      DataTypeMismatch(
+    case _ => TypeCheckResult.TypeCheckSuccess
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (!dataType.existsRecursively(_.isInstanceOf[MapType])) {
+      return DataTypeMismatch(
         errorSubClass = "UNEXPECTED_INPUT_TYPE",
         messageParameters = Map(
           "paramIndex" -> ordinalNumber(0),
           "requiredType" -> toSQLType(MapType),
           "inputSql" -> toSQLExpr(base),
           "inputType" -> toSQLType(base.dataType))
       )
-  }
-
-  override def nullSafeEval(array: Any): Any = {
-    // put keys and their respective values inside a tuple and sort them
-    // according to the key ordering. Extract the new sorted k/v pairs to form a sorted map
-
-    val mapData = array.asInstanceOf[MapData]
-    val numElements = mapData.numElements()
-    val keys = mapData.keyArray()
-    val values = mapData.valueArray()
-
-    val ordering = PhysicalDataType.ordering(keyType)
-
-    val sortedMap = Array
-      .tabulate(numElements)(i => (keys.get(i, keyType).asInstanceOf[Any],
-        values.get(i, valueType).asInstanceOf[Any]))
-      .sortBy(_._1)(ordering)
-
-    new ArrayBasedMapData(new GenericArrayData(sortedMap.map(_._1)),
-      new GenericArrayData(sortedMap.map(_._2)))
-  }
-
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    nullSafeCodeGen(ctx, ev, b => sortCodegen(ctx, ev, b))
-  }
-
-  private def sortCodegen(ctx: CodegenContext, ev: ExprCode,
-      base: String): String = {
-
-    val arrayBasedMapData = classOf[ArrayBasedMapData].getName
-    val genericArrayData = classOf[GenericArrayData].getName
-
-    val numElements = ctx.freshName("numElements")
-    val keys = ctx.freshName("keys")
-    val values = ctx.freshName("values")
-    val sortArray = ctx.freshName("sortArray")
-    val i = ctx.freshName("i")
-    val o1 = ctx.freshName("o1")
-    val o1entry = ctx.freshName("o1entry")
-    val o2 = ctx.freshName("o2")
-    val o2entry = ctx.freshName("o2entry")
-    val c = ctx.freshName("c")
-    val newKeys = ctx.freshName("newKeys")
-    val newValues = ctx.freshName("newValues")
-
-    val boxedKeyType = CodeGenerator.boxedType(keyType)
-    val boxedValueType = CodeGenerator.boxedType(valueType)
-    val javaKeyType = CodeGenerator.javaType(keyType)
+    }
 
-    val simpleEntryType = s"java.util.AbstractMap.SimpleEntry<$boxedKeyType, $boxedValueType>"
+    if (dataType.existsRecursively(dt =>
+      dt.isInstanceOf[MapType] && !RowOrdering.isOrderable(dt.asInstanceOf[MapType].keyType))) {
+      DataTypeMismatch(
+        errorSubClass = "INVALID_ORDERING_TYPE",
+        messageParameters = Map(
+          "functionName" -> toSQLId(prettyName),
+          "dataType" -> toSQLType(dataType)
+        )
+      )
+    }
 
-    val comp = if (CodeGenerator.isPrimitiveType(keyType)) {
-      val v1 = ctx.freshName("v1")
-      val v2 = ctx.freshName("v2")
-      s"""
-         |$javaKeyType $v1 = (($boxedKeyType) $o1).${javaKeyType}Value();
-         |$javaKeyType $v2 = (($boxedKeyType) $o2).${javaKeyType}Value();
-         |int $c = ${ctx.genComp(keyType, v1, v2)};
-       """.stripMargin
-    } else {
-      s"int $c = ${ctx.genComp(keyType, s"(($javaKeyType) $o1)", s"(($javaKeyType) $o2)")};"
+    TypeCheckResult.TypeCheckSuccess
+  }
+
+  // Evaluates the expression recursively by taking into
+  // account complex types and nesting
+  def nullSafeEvalRecursive(input: Any, dataType: DataType): Any = {
+
+    dataType match {
+      // For ArrayType recursively call evaluate for
+      // all its children since MapType can be nested
+      // as array element
+      case ArrayType(elementType, _) =>

Review Comment:
   What about the case where i.e. ArrayType is nested into StructType? 
   Something like StructType<a:ArrayType<MapType<INT,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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1677988075


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -3060,6 +3060,12 @@
     ],
     "sqlState" : "22023"
   },
+  "MAP_KEY_BAD_DATATYPE" : {

Review Comment:
   I don't think that we plan to support Map with key of Map type, as it was forbidden by your PR earlier.



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1699897861


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   Done and added corresponding tests. 



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1677775691


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -3060,6 +3060,12 @@
     ],
     "sqlState" : "22023"
   },
+  "MAP_KEY_BAD_DATATYPE" : {

Review Comment:
   Will we support it eventually? If yes we can use a temp error class, with a name starting with `_LEGACY_ERROR_TEMP`



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1681174046


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -892,132 +892,108 @@ case class MapFromEntries(child: Expression)
     copy(child = newChild)
 }
 
+// Sorts all MapType expressions based on the ordering of their keys.
+// This is used when GROUP BY is done with a MapType (possibly nested) column.
 case class MapSort(base: Expression)
-  extends UnaryExpression with NullIntolerant with QueryErrorsBase {
+  extends UnaryExpression with NullIntolerant with QueryErrorsBase with CodegenFallback {
 
-  val keyType: DataType = base.dataType.asInstanceOf[MapType].keyType
-  val valueType: DataType = base.dataType.asInstanceOf[MapType].valueType
+  override lazy val canonicalized: Expression = base.canonicalized
+
+  override lazy val deterministic: Boolean = base.deterministic
 
   override def child: Expression = base
 
   override def dataType: DataType = base.dataType
 
-  override def checkInputDataTypes(): TypeCheckResult = base.dataType match {
-    case m: MapType if RowOrdering.isOrderable(m.keyType) =>
-        TypeCheckResult.TypeCheckSuccess
+  def recursiveCheckDataTypes(dataType: DataType): TypeCheckResult = dataType match {
+    case a: ArrayType => recursiveCheckDataTypes(a.elementType)
+    case StructType(fields) =>
+      fields.collect(sf => recursiveCheckDataTypes(sf.dataType)).filter(_.isFailure).headOption
+        .getOrElse(TypeCheckResult.TypeCheckSuccess)
+    case m: MapType if RowOrdering.isOrderable(m.keyType) => TypeCheckResult.TypeCheckSuccess
     case _: MapType =>
       DataTypeMismatch(
         errorSubClass = "INVALID_ORDERING_TYPE",
         messageParameters = Map(
           "functionName" -> toSQLId(prettyName),
-          "dataType" -> toSQLType(base.dataType)
+          "dataType" -> toSQLType(dataType)
         )
       )
-    case _ =>
-      DataTypeMismatch(
+    case _ => TypeCheckResult.TypeCheckSuccess
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (!dataType.existsRecursively(_.isInstanceOf[MapType])) {
+      return DataTypeMismatch(
         errorSubClass = "UNEXPECTED_INPUT_TYPE",
         messageParameters = Map(
           "paramIndex" -> ordinalNumber(0),
           "requiredType" -> toSQLType(MapType),
           "inputSql" -> toSQLExpr(base),
           "inputType" -> toSQLType(base.dataType))
       )
-  }
-
-  override def nullSafeEval(array: Any): Any = {
-    // put keys and their respective values inside a tuple and sort them
-    // according to the key ordering. Extract the new sorted k/v pairs to form a sorted map
-
-    val mapData = array.asInstanceOf[MapData]
-    val numElements = mapData.numElements()
-    val keys = mapData.keyArray()
-    val values = mapData.valueArray()
-
-    val ordering = PhysicalDataType.ordering(keyType)
-
-    val sortedMap = Array
-      .tabulate(numElements)(i => (keys.get(i, keyType).asInstanceOf[Any],
-        values.get(i, valueType).asInstanceOf[Any]))
-      .sortBy(_._1)(ordering)
-
-    new ArrayBasedMapData(new GenericArrayData(sortedMap.map(_._1)),
-      new GenericArrayData(sortedMap.map(_._2)))
-  }
-
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    nullSafeCodeGen(ctx, ev, b => sortCodegen(ctx, ev, b))
-  }
-
-  private def sortCodegen(ctx: CodegenContext, ev: ExprCode,
-      base: String): String = {
-
-    val arrayBasedMapData = classOf[ArrayBasedMapData].getName
-    val genericArrayData = classOf[GenericArrayData].getName
-
-    val numElements = ctx.freshName("numElements")
-    val keys = ctx.freshName("keys")
-    val values = ctx.freshName("values")
-    val sortArray = ctx.freshName("sortArray")
-    val i = ctx.freshName("i")
-    val o1 = ctx.freshName("o1")
-    val o1entry = ctx.freshName("o1entry")
-    val o2 = ctx.freshName("o2")
-    val o2entry = ctx.freshName("o2entry")
-    val c = ctx.freshName("c")
-    val newKeys = ctx.freshName("newKeys")
-    val newValues = ctx.freshName("newValues")
-
-    val boxedKeyType = CodeGenerator.boxedType(keyType)
-    val boxedValueType = CodeGenerator.boxedType(valueType)
-    val javaKeyType = CodeGenerator.javaType(keyType)
+    }
 
-    val simpleEntryType = s"java.util.AbstractMap.SimpleEntry<$boxedKeyType, $boxedValueType>"
+    if (dataType.existsRecursively(dt =>
+      dt.isInstanceOf[MapType] && !RowOrdering.isOrderable(dt.asInstanceOf[MapType].keyType))) {
+      DataTypeMismatch(
+        errorSubClass = "INVALID_ORDERING_TYPE",
+        messageParameters = Map(
+          "functionName" -> toSQLId(prettyName),
+          "dataType" -> toSQLType(dataType)
+        )
+      )
+    }
 
-    val comp = if (CodeGenerator.isPrimitiveType(keyType)) {
-      val v1 = ctx.freshName("v1")
-      val v2 = ctx.freshName("v2")
-      s"""
-         |$javaKeyType $v1 = (($boxedKeyType) $o1).${javaKeyType}Value();
-         |$javaKeyType $v2 = (($boxedKeyType) $o2).${javaKeyType}Value();
-         |int $c = ${ctx.genComp(keyType, v1, v2)};
-       """.stripMargin
-    } else {
-      s"int $c = ${ctx.genComp(keyType, s"(($javaKeyType) $o1)", s"(($javaKeyType) $o2)")};"
+    TypeCheckResult.TypeCheckSuccess
+  }
+
+  // Evaluates the expression recursively by taking into
+  // account complex types and nesting
+  def nullSafeEvalRecursive(input: Any, dataType: DataType): Any = {
+
+    dataType match {
+      // For ArrayType recursively call evaluate for
+      // all its children since MapType can be nested
+      // as array element
+      case ArrayType(elementType, _) =>

Review Comment:
   It's not doable here. We should do it in the rule where we add `MapSort` expression.



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1696267761


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {

Review Comment:
   instead of doing two recursive checks ahead, we can simply skip adding `MapSort` in `insertMapSortRecursively` if the expression is already `MapSort`.



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1677779465


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -892,132 +892,108 @@ case class MapFromEntries(child: Expression)
     copy(child = newChild)
 }
 
+// Sorts all MapType expressions based on the ordering of their keys.
+// This is used when GROUP BY is done with a MapType (possibly nested) column.
 case class MapSort(base: Expression)
-  extends UnaryExpression with NullIntolerant with QueryErrorsBase {
+  extends UnaryExpression with NullIntolerant with QueryErrorsBase with CodegenFallback {
 
-  val keyType: DataType = base.dataType.asInstanceOf[MapType].keyType
-  val valueType: DataType = base.dataType.asInstanceOf[MapType].valueType
+  override lazy val canonicalized: Expression = base.canonicalized
+
+  override lazy val deterministic: Boolean = base.deterministic
 
   override def child: Expression = base
 
   override def dataType: DataType = base.dataType
 
-  override def checkInputDataTypes(): TypeCheckResult = base.dataType match {
-    case m: MapType if RowOrdering.isOrderable(m.keyType) =>
-        TypeCheckResult.TypeCheckSuccess
+  def recursiveCheckDataTypes(dataType: DataType): TypeCheckResult = dataType match {
+    case a: ArrayType => recursiveCheckDataTypes(a.elementType)
+    case StructType(fields) =>
+      fields.collect(sf => recursiveCheckDataTypes(sf.dataType)).filter(_.isFailure).headOption
+        .getOrElse(TypeCheckResult.TypeCheckSuccess)
+    case m: MapType if RowOrdering.isOrderable(m.keyType) => TypeCheckResult.TypeCheckSuccess
     case _: MapType =>
       DataTypeMismatch(
         errorSubClass = "INVALID_ORDERING_TYPE",
         messageParameters = Map(
           "functionName" -> toSQLId(prettyName),
-          "dataType" -> toSQLType(base.dataType)
+          "dataType" -> toSQLType(dataType)
         )
       )
-    case _ =>
-      DataTypeMismatch(
+    case _ => TypeCheckResult.TypeCheckSuccess
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (!dataType.existsRecursively(_.isInstanceOf[MapType])) {
+      return DataTypeMismatch(
         errorSubClass = "UNEXPECTED_INPUT_TYPE",
         messageParameters = Map(
           "paramIndex" -> ordinalNumber(0),
           "requiredType" -> toSQLType(MapType),
           "inputSql" -> toSQLExpr(base),
           "inputType" -> toSQLType(base.dataType))
       )
-  }
-
-  override def nullSafeEval(array: Any): Any = {
-    // put keys and their respective values inside a tuple and sort them
-    // according to the key ordering. Extract the new sorted k/v pairs to form a sorted map
-
-    val mapData = array.asInstanceOf[MapData]
-    val numElements = mapData.numElements()
-    val keys = mapData.keyArray()
-    val values = mapData.valueArray()
-
-    val ordering = PhysicalDataType.ordering(keyType)
-
-    val sortedMap = Array
-      .tabulate(numElements)(i => (keys.get(i, keyType).asInstanceOf[Any],
-        values.get(i, valueType).asInstanceOf[Any]))
-      .sortBy(_._1)(ordering)
-
-    new ArrayBasedMapData(new GenericArrayData(sortedMap.map(_._1)),
-      new GenericArrayData(sortedMap.map(_._2)))
-  }
-
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    nullSafeCodeGen(ctx, ev, b => sortCodegen(ctx, ev, b))
-  }
-
-  private def sortCodegen(ctx: CodegenContext, ev: ExprCode,
-      base: String): String = {
-
-    val arrayBasedMapData = classOf[ArrayBasedMapData].getName
-    val genericArrayData = classOf[GenericArrayData].getName
-
-    val numElements = ctx.freshName("numElements")
-    val keys = ctx.freshName("keys")
-    val values = ctx.freshName("values")
-    val sortArray = ctx.freshName("sortArray")
-    val i = ctx.freshName("i")
-    val o1 = ctx.freshName("o1")
-    val o1entry = ctx.freshName("o1entry")
-    val o2 = ctx.freshName("o2")
-    val o2entry = ctx.freshName("o2entry")
-    val c = ctx.freshName("c")
-    val newKeys = ctx.freshName("newKeys")
-    val newValues = ctx.freshName("newValues")
-
-    val boxedKeyType = CodeGenerator.boxedType(keyType)
-    val boxedValueType = CodeGenerator.boxedType(valueType)
-    val javaKeyType = CodeGenerator.javaType(keyType)
+    }
 
-    val simpleEntryType = s"java.util.AbstractMap.SimpleEntry<$boxedKeyType, $boxedValueType>"
+    if (dataType.existsRecursively(dt =>
+      dt.isInstanceOf[MapType] && !RowOrdering.isOrderable(dt.asInstanceOf[MapType].keyType))) {
+      DataTypeMismatch(
+        errorSubClass = "INVALID_ORDERING_TYPE",
+        messageParameters = Map(
+          "functionName" -> toSQLId(prettyName),
+          "dataType" -> toSQLType(dataType)
+        )
+      )
+    }
 
-    val comp = if (CodeGenerator.isPrimitiveType(keyType)) {
-      val v1 = ctx.freshName("v1")
-      val v2 = ctx.freshName("v2")
-      s"""
-         |$javaKeyType $v1 = (($boxedKeyType) $o1).${javaKeyType}Value();
-         |$javaKeyType $v2 = (($boxedKeyType) $o2).${javaKeyType}Value();
-         |int $c = ${ctx.genComp(keyType, v1, v2)};
-       """.stripMargin
-    } else {
-      s"int $c = ${ctx.genComp(keyType, s"(($javaKeyType) $o1)", s"(($javaKeyType) $o2)")};"
+    TypeCheckResult.TypeCheckSuccess
+  }
+
+  // Evaluates the expression recursively by taking into
+  // account complex types and nesting
+  def nullSafeEvalRecursive(input: Any, dataType: DataType): Any = {
+
+    dataType match {
+      // For ArrayType recursively call evaluate for
+      // all its children since MapType can be nested
+      // as array element
+      case ArrayType(elementType, _) =>

Review Comment:
   We should reuse existing expressions to do the recursion over complicated types. See `CharVarcharUtils.stringLengthCheck` as an example, we use `ArrayTransform` expression to handle elements of array.



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1694560083


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -892,132 +892,108 @@ case class MapFromEntries(child: Expression)
     copy(child = newChild)
 }
 
+// Sorts all MapType expressions based on the ordering of their keys.
+// This is used when GROUP BY is done with a MapType (possibly nested) column.
 case class MapSort(base: Expression)
-  extends UnaryExpression with NullIntolerant with QueryErrorsBase {
+  extends UnaryExpression with NullIntolerant with QueryErrorsBase with CodegenFallback {
 
-  val keyType: DataType = base.dataType.asInstanceOf[MapType].keyType
-  val valueType: DataType = base.dataType.asInstanceOf[MapType].valueType
+  override lazy val canonicalized: Expression = base.canonicalized
+
+  override lazy val deterministic: Boolean = base.deterministic
 
   override def child: Expression = base
 
   override def dataType: DataType = base.dataType
 
-  override def checkInputDataTypes(): TypeCheckResult = base.dataType match {
-    case m: MapType if RowOrdering.isOrderable(m.keyType) =>
-        TypeCheckResult.TypeCheckSuccess
+  def recursiveCheckDataTypes(dataType: DataType): TypeCheckResult = dataType match {
+    case a: ArrayType => recursiveCheckDataTypes(a.elementType)
+    case StructType(fields) =>
+      fields.collect(sf => recursiveCheckDataTypes(sf.dataType)).filter(_.isFailure).headOption
+        .getOrElse(TypeCheckResult.TypeCheckSuccess)
+    case m: MapType if RowOrdering.isOrderable(m.keyType) => TypeCheckResult.TypeCheckSuccess
     case _: MapType =>
       DataTypeMismatch(
         errorSubClass = "INVALID_ORDERING_TYPE",
         messageParameters = Map(
           "functionName" -> toSQLId(prettyName),
-          "dataType" -> toSQLType(base.dataType)
+          "dataType" -> toSQLType(dataType)
         )
       )
-    case _ =>
-      DataTypeMismatch(
+    case _ => TypeCheckResult.TypeCheckSuccess
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (!dataType.existsRecursively(_.isInstanceOf[MapType])) {
+      return DataTypeMismatch(
         errorSubClass = "UNEXPECTED_INPUT_TYPE",
         messageParameters = Map(
           "paramIndex" -> ordinalNumber(0),
           "requiredType" -> toSQLType(MapType),
           "inputSql" -> toSQLExpr(base),
           "inputType" -> toSQLType(base.dataType))
       )
-  }
-
-  override def nullSafeEval(array: Any): Any = {
-    // put keys and their respective values inside a tuple and sort them
-    // according to the key ordering. Extract the new sorted k/v pairs to form a sorted map
-
-    val mapData = array.asInstanceOf[MapData]
-    val numElements = mapData.numElements()
-    val keys = mapData.keyArray()
-    val values = mapData.valueArray()
-
-    val ordering = PhysicalDataType.ordering(keyType)
-
-    val sortedMap = Array
-      .tabulate(numElements)(i => (keys.get(i, keyType).asInstanceOf[Any],
-        values.get(i, valueType).asInstanceOf[Any]))
-      .sortBy(_._1)(ordering)
-
-    new ArrayBasedMapData(new GenericArrayData(sortedMap.map(_._1)),
-      new GenericArrayData(sortedMap.map(_._2)))
-  }
-
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    nullSafeCodeGen(ctx, ev, b => sortCodegen(ctx, ev, b))
-  }
-
-  private def sortCodegen(ctx: CodegenContext, ev: ExprCode,
-      base: String): String = {
-
-    val arrayBasedMapData = classOf[ArrayBasedMapData].getName
-    val genericArrayData = classOf[GenericArrayData].getName
-
-    val numElements = ctx.freshName("numElements")
-    val keys = ctx.freshName("keys")
-    val values = ctx.freshName("values")
-    val sortArray = ctx.freshName("sortArray")
-    val i = ctx.freshName("i")
-    val o1 = ctx.freshName("o1")
-    val o1entry = ctx.freshName("o1entry")
-    val o2 = ctx.freshName("o2")
-    val o2entry = ctx.freshName("o2entry")
-    val c = ctx.freshName("c")
-    val newKeys = ctx.freshName("newKeys")
-    val newValues = ctx.freshName("newValues")
-
-    val boxedKeyType = CodeGenerator.boxedType(keyType)
-    val boxedValueType = CodeGenerator.boxedType(valueType)
-    val javaKeyType = CodeGenerator.javaType(keyType)
+    }
 
-    val simpleEntryType = s"java.util.AbstractMap.SimpleEntry<$boxedKeyType, $boxedValueType>"
+    if (dataType.existsRecursively(dt =>
+      dt.isInstanceOf[MapType] && !RowOrdering.isOrderable(dt.asInstanceOf[MapType].keyType))) {
+      DataTypeMismatch(
+        errorSubClass = "INVALID_ORDERING_TYPE",
+        messageParameters = Map(
+          "functionName" -> toSQLId(prettyName),
+          "dataType" -> toSQLType(dataType)
+        )
+      )
+    }
 
-    val comp = if (CodeGenerator.isPrimitiveType(keyType)) {
-      val v1 = ctx.freshName("v1")
-      val v2 = ctx.freshName("v2")
-      s"""
-         |$javaKeyType $v1 = (($boxedKeyType) $o1).${javaKeyType}Value();
-         |$javaKeyType $v2 = (($boxedKeyType) $o2).${javaKeyType}Value();
-         |int $c = ${ctx.genComp(keyType, v1, v2)};
-       """.stripMargin
-    } else {
-      s"int $c = ${ctx.genComp(keyType, s"(($javaKeyType) $o1)", s"(($javaKeyType) $o2)")};"
+    TypeCheckResult.TypeCheckSuccess
+  }
+
+  // Evaluates the expression recursively by taking into
+  // account complex types and nesting
+  def nullSafeEvalRecursive(input: Any, dataType: DataType): Any = {
+
+    dataType match {
+      // For ArrayType recursively call evaluate for
+      // all its children since MapType can be nested
+      // as array element
+      case ArrayType(elementType, _) =>

Review Comment:
   We know the type information at query plan compile time, we can always build the correct expression to handle the actual nested types. `CharVarcharUtils.stringLengthCheck` handles struct type, isn't 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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #47331:
URL: https://github.com/apache/spark/pull/47331#issuecomment-2264314742

   The failed checks are unrelated, thanks, merging to master!


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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1700400605


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,56 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case m: MapType =>
+        // Check if value type of MapType contains MapType (possibly nested)
+        // and special handle this case.
+        val mapSortExpr = if (m.valueType.existsRecursively(_.isInstanceOf[MapType])) {
+          MapFromArrays(MapKeys(e), insertMapSortRecursively(MapValues(e)))
+        }
+        else {
+          e
+        }
+
+        MapSort(mapSortExpr)
+
+      case StructType(fields) =>
+        val struct = CreateNamedStruct(fields.zipWithIndex.flatMap { case (f, i) =>
+          Seq(Literal(f.name), insertMapSortRecursively(
+            GetStructField(e, i, Some(f.name))))
+        }.toImmutableArraySeq)
+        if (struct.valExprs.forall(_.isInstanceOf[GetStructField])) {
+          // No field needs MapSort processing, just return the original expression.
+          e
+        } else if (e.nullable) {
+          If(IsNull(e), Literal(null, struct.dataType), struct)
+        } else {
+          struct
+        }
+
+      case ArrayType(et, containsNull) =>
+        val param = NamedLambdaVariable("x", et, containsNull)
+        val funcBody = insertMapSortRecursively(param)
+
+        ArrayTransform(e, LambdaFunction(funcBody, Seq(param)))

Review Comment:
   Done, for both StructType and ArrayType, please take a look and should be ready for merge :)



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1698745722


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   Again, `CharVarcharUtils.stringLengthCheck` is a good example to follow



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1698151619


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   Hm, can you explain a bit, not sure what do you mean by add MapSort to value array since it works with expressions? 



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1698745030


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   map is basically two arrays for keys and values. We can use `MapKeys` and `MapValues` to get the two arrays, and `MapFromArrays` to rebuild the map. For arrays, we already know how to add `MapSort`



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1696267761


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {

Review Comment:
   instead of doing two recursive checks ahead, we can simply skip adding `MapSort` in `insertMapSortRecursively` if the expression is already `MapSort`.



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1697879637


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   I think we can add map sort to the map value array and rebuild the map



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1695369011


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -892,132 +892,108 @@ case class MapFromEntries(child: Expression)
     copy(child = newChild)
 }
 
+// Sorts all MapType expressions based on the ordering of their keys.
+// This is used when GROUP BY is done with a MapType (possibly nested) column.
 case class MapSort(base: Expression)
-  extends UnaryExpression with NullIntolerant with QueryErrorsBase {
+  extends UnaryExpression with NullIntolerant with QueryErrorsBase with CodegenFallback {
 
-  val keyType: DataType = base.dataType.asInstanceOf[MapType].keyType
-  val valueType: DataType = base.dataType.asInstanceOf[MapType].valueType
+  override lazy val canonicalized: Expression = base.canonicalized
+
+  override lazy val deterministic: Boolean = base.deterministic
 
   override def child: Expression = base
 
   override def dataType: DataType = base.dataType
 
-  override def checkInputDataTypes(): TypeCheckResult = base.dataType match {
-    case m: MapType if RowOrdering.isOrderable(m.keyType) =>
-        TypeCheckResult.TypeCheckSuccess
+  def recursiveCheckDataTypes(dataType: DataType): TypeCheckResult = dataType match {
+    case a: ArrayType => recursiveCheckDataTypes(a.elementType)
+    case StructType(fields) =>
+      fields.collect(sf => recursiveCheckDataTypes(sf.dataType)).filter(_.isFailure).headOption
+        .getOrElse(TypeCheckResult.TypeCheckSuccess)
+    case m: MapType if RowOrdering.isOrderable(m.keyType) => TypeCheckResult.TypeCheckSuccess
     case _: MapType =>
       DataTypeMismatch(
         errorSubClass = "INVALID_ORDERING_TYPE",
         messageParameters = Map(
           "functionName" -> toSQLId(prettyName),
-          "dataType" -> toSQLType(base.dataType)
+          "dataType" -> toSQLType(dataType)
         )
       )
-    case _ =>
-      DataTypeMismatch(
+    case _ => TypeCheckResult.TypeCheckSuccess
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (!dataType.existsRecursively(_.isInstanceOf[MapType])) {
+      return DataTypeMismatch(
         errorSubClass = "UNEXPECTED_INPUT_TYPE",
         messageParameters = Map(
           "paramIndex" -> ordinalNumber(0),
           "requiredType" -> toSQLType(MapType),
           "inputSql" -> toSQLExpr(base),
           "inputType" -> toSQLType(base.dataType))
       )
-  }
-
-  override def nullSafeEval(array: Any): Any = {
-    // put keys and their respective values inside a tuple and sort them
-    // according to the key ordering. Extract the new sorted k/v pairs to form a sorted map
-
-    val mapData = array.asInstanceOf[MapData]
-    val numElements = mapData.numElements()
-    val keys = mapData.keyArray()
-    val values = mapData.valueArray()
-
-    val ordering = PhysicalDataType.ordering(keyType)
-
-    val sortedMap = Array
-      .tabulate(numElements)(i => (keys.get(i, keyType).asInstanceOf[Any],
-        values.get(i, valueType).asInstanceOf[Any]))
-      .sortBy(_._1)(ordering)
-
-    new ArrayBasedMapData(new GenericArrayData(sortedMap.map(_._1)),
-      new GenericArrayData(sortedMap.map(_._2)))
-  }
-
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    nullSafeCodeGen(ctx, ev, b => sortCodegen(ctx, ev, b))
-  }
-
-  private def sortCodegen(ctx: CodegenContext, ev: ExprCode,
-      base: String): String = {
-
-    val arrayBasedMapData = classOf[ArrayBasedMapData].getName
-    val genericArrayData = classOf[GenericArrayData].getName
-
-    val numElements = ctx.freshName("numElements")
-    val keys = ctx.freshName("keys")
-    val values = ctx.freshName("values")
-    val sortArray = ctx.freshName("sortArray")
-    val i = ctx.freshName("i")
-    val o1 = ctx.freshName("o1")
-    val o1entry = ctx.freshName("o1entry")
-    val o2 = ctx.freshName("o2")
-    val o2entry = ctx.freshName("o2entry")
-    val c = ctx.freshName("c")
-    val newKeys = ctx.freshName("newKeys")
-    val newValues = ctx.freshName("newValues")
-
-    val boxedKeyType = CodeGenerator.boxedType(keyType)
-    val boxedValueType = CodeGenerator.boxedType(valueType)
-    val javaKeyType = CodeGenerator.javaType(keyType)
+    }
 
-    val simpleEntryType = s"java.util.AbstractMap.SimpleEntry<$boxedKeyType, $boxedValueType>"
+    if (dataType.existsRecursively(dt =>
+      dt.isInstanceOf[MapType] && !RowOrdering.isOrderable(dt.asInstanceOf[MapType].keyType))) {
+      DataTypeMismatch(
+        errorSubClass = "INVALID_ORDERING_TYPE",
+        messageParameters = Map(
+          "functionName" -> toSQLId(prettyName),
+          "dataType" -> toSQLType(dataType)
+        )
+      )
+    }
 
-    val comp = if (CodeGenerator.isPrimitiveType(keyType)) {
-      val v1 = ctx.freshName("v1")
-      val v2 = ctx.freshName("v2")
-      s"""
-         |$javaKeyType $v1 = (($boxedKeyType) $o1).${javaKeyType}Value();
-         |$javaKeyType $v2 = (($boxedKeyType) $o2).${javaKeyType}Value();
-         |int $c = ${ctx.genComp(keyType, v1, v2)};
-       """.stripMargin
-    } else {
-      s"int $c = ${ctx.genComp(keyType, s"(($javaKeyType) $o1)", s"(($javaKeyType) $o2)")};"
+    TypeCheckResult.TypeCheckSuccess
+  }
+
+  // Evaluates the expression recursively by taking into
+  // account complex types and nesting
+  def nullSafeEvalRecursive(input: Any, dataType: DataType): Any = {
+
+    dataType match {
+      // For ArrayType recursively call evaluate for
+      // all its children since MapType can be nested
+      // as array element
+      case ArrayType(elementType, _) =>

Review Comment:
   Modified changes per suggestion, thanks for pointing out, wasn't aware of this option! :)



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1696278866


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   what if the key or value contains map type?



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


Re: [PR] [SC-170296] GROUP BY with MapType nested inside complex type [spark]

Posted by "nebojsa-db (via GitHub)" <gi...@apache.org>.
nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1679332626


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -892,132 +892,108 @@ case class MapFromEntries(child: Expression)
     copy(child = newChild)
 }
 
+// Sorts all MapType expressions based on the ordering of their keys.
+// This is used when GROUP BY is done with a MapType (possibly nested) column.
 case class MapSort(base: Expression)
-  extends UnaryExpression with NullIntolerant with QueryErrorsBase {
+  extends UnaryExpression with NullIntolerant with QueryErrorsBase with CodegenFallback {
 
-  val keyType: DataType = base.dataType.asInstanceOf[MapType].keyType
-  val valueType: DataType = base.dataType.asInstanceOf[MapType].valueType
+  override lazy val canonicalized: Expression = base.canonicalized
+
+  override lazy val deterministic: Boolean = base.deterministic
 
   override def child: Expression = base
 
   override def dataType: DataType = base.dataType
 
-  override def checkInputDataTypes(): TypeCheckResult = base.dataType match {
-    case m: MapType if RowOrdering.isOrderable(m.keyType) =>
-        TypeCheckResult.TypeCheckSuccess
+  def recursiveCheckDataTypes(dataType: DataType): TypeCheckResult = dataType match {
+    case a: ArrayType => recursiveCheckDataTypes(a.elementType)
+    case StructType(fields) =>
+      fields.collect(sf => recursiveCheckDataTypes(sf.dataType)).filter(_.isFailure).headOption
+        .getOrElse(TypeCheckResult.TypeCheckSuccess)
+    case m: MapType if RowOrdering.isOrderable(m.keyType) => TypeCheckResult.TypeCheckSuccess
     case _: MapType =>
       DataTypeMismatch(
         errorSubClass = "INVALID_ORDERING_TYPE",
         messageParameters = Map(
           "functionName" -> toSQLId(prettyName),
-          "dataType" -> toSQLType(base.dataType)
+          "dataType" -> toSQLType(dataType)
         )
       )
-    case _ =>
-      DataTypeMismatch(
+    case _ => TypeCheckResult.TypeCheckSuccess
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (!dataType.existsRecursively(_.isInstanceOf[MapType])) {
+      return DataTypeMismatch(
         errorSubClass = "UNEXPECTED_INPUT_TYPE",
         messageParameters = Map(
           "paramIndex" -> ordinalNumber(0),
           "requiredType" -> toSQLType(MapType),
           "inputSql" -> toSQLExpr(base),
           "inputType" -> toSQLType(base.dataType))
       )
-  }
-
-  override def nullSafeEval(array: Any): Any = {
-    // put keys and their respective values inside a tuple and sort them
-    // according to the key ordering. Extract the new sorted k/v pairs to form a sorted map
-
-    val mapData = array.asInstanceOf[MapData]
-    val numElements = mapData.numElements()
-    val keys = mapData.keyArray()
-    val values = mapData.valueArray()
-
-    val ordering = PhysicalDataType.ordering(keyType)
-
-    val sortedMap = Array
-      .tabulate(numElements)(i => (keys.get(i, keyType).asInstanceOf[Any],
-        values.get(i, valueType).asInstanceOf[Any]))
-      .sortBy(_._1)(ordering)
-
-    new ArrayBasedMapData(new GenericArrayData(sortedMap.map(_._1)),
-      new GenericArrayData(sortedMap.map(_._2)))
-  }
-
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    nullSafeCodeGen(ctx, ev, b => sortCodegen(ctx, ev, b))
-  }
-
-  private def sortCodegen(ctx: CodegenContext, ev: ExprCode,
-      base: String): String = {
-
-    val arrayBasedMapData = classOf[ArrayBasedMapData].getName
-    val genericArrayData = classOf[GenericArrayData].getName
-
-    val numElements = ctx.freshName("numElements")
-    val keys = ctx.freshName("keys")
-    val values = ctx.freshName("values")
-    val sortArray = ctx.freshName("sortArray")
-    val i = ctx.freshName("i")
-    val o1 = ctx.freshName("o1")
-    val o1entry = ctx.freshName("o1entry")
-    val o2 = ctx.freshName("o2")
-    val o2entry = ctx.freshName("o2entry")
-    val c = ctx.freshName("c")
-    val newKeys = ctx.freshName("newKeys")
-    val newValues = ctx.freshName("newValues")
-
-    val boxedKeyType = CodeGenerator.boxedType(keyType)
-    val boxedValueType = CodeGenerator.boxedType(valueType)
-    val javaKeyType = CodeGenerator.javaType(keyType)
+    }
 
-    val simpleEntryType = s"java.util.AbstractMap.SimpleEntry<$boxedKeyType, $boxedValueType>"
+    if (dataType.existsRecursively(dt =>
+      dt.isInstanceOf[MapType] && !RowOrdering.isOrderable(dt.asInstanceOf[MapType].keyType))) {
+      DataTypeMismatch(
+        errorSubClass = "INVALID_ORDERING_TYPE",
+        messageParameters = Map(
+          "functionName" -> toSQLId(prettyName),
+          "dataType" -> toSQLType(dataType)
+        )
+      )
+    }
 
-    val comp = if (CodeGenerator.isPrimitiveType(keyType)) {
-      val v1 = ctx.freshName("v1")
-      val v2 = ctx.freshName("v2")
-      s"""
-         |$javaKeyType $v1 = (($boxedKeyType) $o1).${javaKeyType}Value();
-         |$javaKeyType $v2 = (($boxedKeyType) $o2).${javaKeyType}Value();
-         |int $c = ${ctx.genComp(keyType, v1, v2)};
-       """.stripMargin
-    } else {
-      s"int $c = ${ctx.genComp(keyType, s"(($javaKeyType) $o1)", s"(($javaKeyType) $o2)")};"
+    TypeCheckResult.TypeCheckSuccess
+  }
+
+  // Evaluates the expression recursively by taking into
+  // account complex types and nesting
+  def nullSafeEvalRecursive(input: Any, dataType: DataType): Any = {
+
+    dataType match {
+      // For ArrayType recursively call evaluate for
+      // all its children since MapType can be nested
+      // as array element
+      case ArrayType(elementType, _) =>

Review Comment:
   We are working here with input data, but ArrayTransform is expecting expression as an input, is this doable? 



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