viirya commented on a change in pull request #29942:
URL: https://github.com/apache/spark/pull/29942#discussion_r499945084
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeJsonExprsSuite.scala
##########
@@ -199,4 +199,51 @@ class OptimizeJsonExprsSuite extends PlanTest with
ExpressionEvalHelper {
JsonToStructs(prunedSchema2, options, 'json), field2, 0, 1,
false).as("b")).analyze
comparePlans(optimized2, expected2)
}
+
+ test("SPARK-33007: simplify named_struct + from_json") {
+ val options = Map.empty[String, String]
+ val schema = StructType.fromDDL("a int, b int, c long, d string")
+
+ val query1 = testRelation2
+ .select(namedStruct(
+ "a", GetStructField(JsonToStructs(schema, options, 'json), 0),
+ "b", GetStructField(JsonToStructs(schema, options, 'json),
1)).as("struct"))
+ val optimized1 = Optimizer.execute(query1.analyze)
+
+ val prunedSchema1 = StructType.fromDDL("a int, b int")
+ val nullStruct = namedStruct("a", Literal(null, IntegerType), "b",
Literal(null, IntegerType))
+ val expected1 = testRelation2
+ .select(
+ If(IsNull('json),
+ nullStruct,
+ KnownNotNull(JsonToStructs(prunedSchema1, options,
'json))).as("struct")).analyze
+ comparePlans(optimized1, expected1)
+
+ // Skip it if `namedStruct` aliases field name.
+ val field1 = StructType.fromDDL("a int")
+ val field2 = StructType.fromDDL("b int")
+ val query2 = testRelation2
+ .select(namedStruct(
+ "a1", GetStructField(JsonToStructs(schema, options, 'json), 0),
+ "b", GetStructField(JsonToStructs(schema, options, 'json),
1)).as("struct"))
+ val optimized2 = Optimizer.execute(query2.analyze)
Review comment:
Ok.
----------------------------------------------------------------
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]