maropu commented on a change in pull request #24158: [SPARK-26847][SQL] Pruning nested serializers from object serializers: MapType support URL: https://github.com/apache/spark/pull/24158#discussion_r268032776
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/objects.scala ########## @@ -132,49 +130,74 @@ object ObjectSerializerPruning extends Rule[LogicalPlan] { fields.map(f => collectStructType(f.dataType, structs)) case ArrayType(elementType, _) => collectStructType(elementType, structs) + case MapType(_, valueType, _) => + // Because we can't select a field from struct in key, so we skip key type. + collectStructType(valueType, structs) case _ => } structs } + /** + * This method returns pruned `CreateNamedStruct` expression given an original `CreateNamedStruct` + * and a pruned `StructType`. + */ + private def pruneNamedStruct(struct: CreateNamedStruct, prunedType: StructType) = { + // Filters out the pruned fields. + val prunedFields = struct.nameExprs.zip(struct.valExprs).filter { case (nameExpr, _) => + val name = nameExpr.eval(EmptyRow).toString + prunedType.fieldNames.exists { fieldName => + if (SQLConf.get.caseSensitiveAnalysis) { + fieldName.equals(name) + } else { + fieldName.equalsIgnoreCase(name) + } + } + }.flatMap(pair => Seq(pair._1, pair._2)) + + CreateNamedStruct(prunedFields) + } + + /** + * When we change nested serializer data type, `If` expression will be unresolved because + * literal null's data type doesn't match now. We need to align it with new data type. + * Note: we should do `transformUp` explicitly to change data types. + */ + private def alignNullTypeInIf(expr: Expression) = expr.transformUp { + case i @ If(_: IsNull, Literal(null, dt), ser) if !dt.sameType(ser.dataType) => + i.copy(trueValue = Literal(null, ser.dataType)) + } + /** * This method prunes given serializer expression by given pruned data type. For example, * given a serializer creating struct(a int, b int) and pruned data type struct(a int), - * this method returns pruned serializer creating struct(a int). For now it supports to - * prune nested fields in struct and array of struct. - * TODO(SPARK-26847): support to prune nested fields in key and value of map type. + * this method returns pruned serializer creating struct(a int). */ def pruneSerializer( serializer: NamedExpression, prunedDataType: DataType): NamedExpression = { val prunedStructTypes = collectStructType(prunedDataType, ArrayBuffer.empty[StructType]) var structTypeIndex = 0 - val prunedSerializer = serializer.transformDown { + val transformedSerializer = serializer.transformDown { + case m: ExternalMapToCatalyst => + val valueStructs = m.valueConverter.collect { Review comment: `valueStructs` not used? ---------------------------------------------------------------- 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 With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org