Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-49743][SQL] OptimizeCsvJsonExpr should not change schema fields when pruning GetArrayStructFields #48190

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ object OptimizeCsvJsonExprs extends Rule[LogicalPlan] {
val prunedSchema = StructType(Array(schema(ordinal)))
g.copy(child = j.copy(schema = prunedSchema), ordinal = 0)

case g @ GetArrayStructFields(j @ JsonToStructs(schema: ArrayType, _, _, _), _, _, _, _)
if schema.elementType.asInstanceOf[StructType].length > 1 && j.options.isEmpty =>
val prunedSchema = ArrayType(StructType(Array(g.field)), g.containsNull)
case g @ GetArrayStructFields(j @ JsonToStructs(ArrayType(schema: StructType, _),
_, _, _), _, ordinal, _, _) if schema.length > 1 && j.options.isEmpty =>
// Obtain the pruned schema by picking the `ordinal` field of the struct.
val prunedSchema = ArrayType(StructType(Array(schema(ordinal))), g.containsNull)
g.copy(child = j.copy(schema = prunedSchema), ordinal = 0, numFields = 1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,21 @@ class OptimizeJsonExprsSuite extends PlanTest with ExpressionEvalHelper {
comparePlans(optimized, query.analyze)
}
}

test("SPARK-49743: prune unnecessary columns from GetArrayStructFields does not change schema") {
val options = Map.empty[String, String]
val schema1 = ArrayType(StructType.fromDDL("a int, b int"), containsNull = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just schema? I don't see there are schema2, schema3...etc.


val field1 = StructField("A", IntegerType) // Instead of "a", use "A" to test case sensitivity.
val query1 = testRelation2
.select(GetArrayStructFields(
JsonToStructs(schema1, options, $"json"), field1, 0, 2, true).as("a"))
val optimized1 = Optimizer.execute(query1.analyze)

val prunedSchema1 = ArrayType(StructType.fromDDL("a int"), containsNull = true)
val expected1 = testRelation2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

.select(GetArrayStructFields(
JsonToStructs(prunedSchema1, options, $"json"), field1, 0, 1, true).as("a")).analyze
comparePlans(optimized1, expected1)
}
}