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-32906][SQL] Struct field names should not change after normalizing floats #29780

Closed
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -129,10 +129,10 @@ object NormalizeFloatingNumbers extends Rule[LogicalPlan] {
Coalesce(children.map(normalize))

case _ if expr.dataType.isInstanceOf[StructType] =>
val fields = expr.dataType.asInstanceOf[StructType].fields.indices.map { i =>
normalize(GetStructField(expr, i))
val fields = expr.dataType.asInstanceOf[StructType].fieldNames.zipWithIndex.map {
case (name, i) => Seq(Literal(name), normalize(GetStructField(expr, i)))
}
val struct = CreateStruct(fields)
val struct = CreateNamedStruct(fields.flatten.toSeq)
Copy link
Member

Choose a reason for hiding this comment

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

Looks logically fine but just to confirm that I understood correctly, it's just a cleanup fix to match the column names, right? and doesn't affect the final output?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea I think so. CreateNamedStruct allows you to specify the name for each field.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, you're right and this is not an user-facing bug. When writing code to check paln integrity for SparkPlans, I found this issue; canonicalized attributes between final/partial HaashAggregates were not equal incorrectly because of this issue (schema mismatch).

KnownFloatingPointNormalized(If(IsNull(expr), Literal(null, struct.dataType), struct))

case _ if expr.dataType.isInstanceOf[ArrayType] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,14 @@ class DataFrameAggregateSuite extends QueryTest
checkAnswer(sql(queryTemplate("FIRST")), Row(1))
checkAnswer(sql(queryTemplate("LAST")), Row(3))
}

test("SPARK-32906: struct field names should not change after normalizing floats") {
val df = Seq(Tuple1(Tuple2(-0.0d, Double.NaN)), Tuple1(Tuple2(0.0d, Double.NaN))).toDF("k")
val aggs = df.distinct().queryExecution.sparkPlan.collect { case a: HashAggregateExec => a }
assert(aggs.length == 2)
assert(aggs.head.output.map(_.dataType.simpleString).head ===
aggs.last.output.map(_.dataType.simpleString).head)
}
}

case class B(c: Option[Double])
Expand Down