cloud-fan commented on code in PR #56190:
URL: https://github.com/apache/spark/pull/56190#discussion_r3456225239
##########
sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala:
##########
@@ -172,9 +173,15 @@ trait QueryTestBase
}
}
- assertEmptyMissingInput(analyzedDF)
+ if (analyzedDF.isInstanceOf[classic.DataFrame]) {
+ assertEmptyMissingInput(analyzedDF)
- QueryTest.checkAnswer(analyzedDF, expectedAnswer)
+ SQLExecution.withSQLConfPropagated(analyzedDF.sparkSession) {
+ df.materializedRdd.count() // Also attempt to deserialize as an RDD
[SPARK-15791]
Review Comment:
`df` is the by-name parameter (`df: => DataFrame`), already forced once into
`analyzedDF` at the top of this method. `df.materializedRdd` re-evaluates the
thunk, so the query plan is constructed twice for every classic `checkAnswer`
(and a side-effecting by-name block would run twice). Before this PR the trait
evaluated `df` exactly once. Use the already-forced value:
```suggestion
analyzedDF.materializedRdd.count() // Also attempt to deserialize as
an RDD [SPARK-15791]
```
##########
sql/core/src/test/scala/org/apache/spark/sql/CheckAnswerHelper.scala:
##########
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql
+
+import java.util.TimeZone
+
+import org.scalatest.Assertions
+
+import org.apache.spark.annotation.Experimental
+import org.apache.spark.sql.catalyst.ExtendedAnalysisException
+import org.apache.spark.sql.catalyst.plans.logical
+import org.apache.spark.util.{SparkErrorUtils, SparkStringUtils}
+
+/**
+ * Provides [[checkAnswer]] helper for SQL- & DataFrame-API tests.
+ *
+ * TODO: should be moved to sql/api together with SessionQueryTestBase
+ */
+@Experimental
+trait CheckAnswerHelper extends Assertions {
+
+ /**
+ * Runs the plan and makes sure the answer matches the expected result.
+ *
+ * @param df the DataFrame to be executed
+ * @param expectedAnswer the expected result in a Seq of Rows.
+ */
+ protected def checkAnswer(df: => DataFrame, expectedAnswer: Seq[Row]): Unit
= {
+
+ val analyzedDF = try df catch {
+ case ae: ExtendedAnalysisException =>
+ if (ae.plan.isDefined) {
+ fail(
+ s"""
+ |Failed to analyze query: $ae
+ |${ae.plan.get}
+ |
+ |${SparkErrorUtils.stackTraceToString(ae)}
+ |""".stripMargin)
+ } else {
+ throw ae
+ }
+ }
+
+ getErrorMessageInCheckAnswer(analyzedDF, expectedAnswer) match {
+ case Some(errorMessage) => fail(errorMessage)
+ case None =>
+ }
+ }
+
+ /*
+ * Note: when moving this to sql/api, implementation should stay in sql/core
+ * (i.e. only have abstract decl in sql/api)
+ */
+ protected def isDfSorted(df: DataFrame): Boolean = {
+ df match {
+ case df: classic.DataFrame =>
+ df.logicalPlan.collectFirst { case s: logical.Sort => s }.nonEmpty
+ case _ => throw new RuntimeException(s"Cannot determine whether df is
sorted: $df")
Review Comment:
`checkAnswer` here flows to `getErrorMessageInCheckAnswer` → `sameRows(...,
isDfSorted(df))`, and this default throws for any non-`classic.DataFrame`. The
connect-safe override (`= false`) lives only in `connect.SessionQueryTest`, so
the documented `FooSuite with connect.SparkSessionBinder` swap — what
`QueryTestWithConnectSuite` does — hits this throw on every Connect
`checkAnswer`, and that suite is red in CI for exactly this reason. Same
trait-linearization issue raised on `QueryTest.scala:160`, in a method that fix
didn't reach.
Returning `false` here (matching what `connect.SessionQueryTest` already
does deliberately) makes the helper connect-safe by default, fixes the
binder-swap path, and makes the `connect.SessionQueryTest.isDfSorted` override
redundant — i.e. it consolidates the connect behavior into one mixin.
```suggestion
case _ => false
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]