cloud-fan commented on code in PR #56190: URL: https://github.com/apache/spark/pull/56190#discussion_r3463232589
########## sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SessionQueryTest.scala: ########## @@ -0,0 +1,49 @@ +/* + * 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.connect + +import scala.util.matching.Regex + +import org.apache.spark.sql + +/** + * Overrides test utils to implement 'connect variants' of suites declared in sql/core: + * {{{ + * // in sql/core + * FooSuite extends SessionQueryTest { test("") { ... } } + * + * // in sql/connect + * FooConnectSuite extends FooSuite with connect.SessionQueryTest + * }}} + * + * This trait overrides [[spark]] to use a [[SparkSession connect.SparkSession]], which executes + * via the gRPC API using an in-process connect server. + */ +trait SessionQueryTest extends sql.SessionQueryTest with SparkSessionBinder { + + private val sortOperator: Regex = "\b(?:Photon)?Sort\b".r Review Comment: This regex never matches anything, so `isDfSorted` is always `false` on Connect — every `checkAnswer` silently becomes order-insensitive (the round-4 weakening, back), and `QueryTestWithConnectSuite` will fail the inherited "demands correct result order" test in CI. Two root causes, both need fixing: 1. In a non-raw string literal `\b` is the backspace char (U+0008), not the regex word-boundary — make the literal raw (below). 2. `Regex.matches` matches the *whole* input — the call on line 44 needs a substring match. Verified in a REPL: a raw `\bSort\b` with `unanchored.matches` detects `Sort` and rejects `SortMergeJoin`/`SortAggregate`, so this also closes last round's over-match concern. ```suggestion private val sortOperator: Regex = """\b(?:Photon)?Sort\b""".r ``` Line 44 also needs `.unanchored` — see the next comment. ########## sql/core/src/test/scala/org/apache/spark/sql/CheckAnswerHelper.scala: ########## @@ -0,0 +1,215 @@ +/* + * 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 _ => + // isDfSorted should be overriden by connect so that this case can't be reached. Review Comment: ```suggestion // isDfSorted should be overridden by connect so that this case can't be reached. ``` ########## sql/connect/common/src/main/scala/org/apache/spark/sql/connect/Dataset.scala: ########## @@ -241,6 +241,29 @@ class Dataset[T] private[sql] ( // scalastyle:on println } + private[connect] def explainString(mode: String): String = { + val protoMode = mode.trim.toLowerCase(util.Locale.ROOT) match { + case "simple" => proto.AnalyzePlanRequest.Explain.ExplainMode.EXPLAIN_MODE_SIMPLE + case "extended" => proto.AnalyzePlanRequest.Explain.ExplainMode.EXPLAIN_MODE_EXTENDED + case "codegen" => proto.AnalyzePlanRequest.Explain.ExplainMode.EXPLAIN_MODE_CODEGEN + case "cost" => proto.AnalyzePlanRequest.Explain.ExplainMode.EXPLAIN_MODE_COST + case "formatted" => proto.AnalyzePlanRequest.Explain.ExplainMode.EXPLAIN_MODE_FORMATTED + case _ => throw new IllegalArgumentException("Unsupported explain mode: " + mode) + } + sparkSession + .analyze(plan, proto.AnalyzePlanRequest.AnalyzeCase.EXPLAIN, Some(protoMode)) + .getExplain + .getExplainString + } + + private[connect] def explainString(extended: Boolean): String = if (extended) { + explainString("extended") + } else { + explainString("simple") + } + + private[connect] def explainString(): String = explainString("simple") Review Comment: This no-arg overload has no callers — only the `Boolean` overload is used (by `connect.SessionQueryTest`). Worth dropping to keep the seam minimal. ########## sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SessionQueryTest.scala: ########## @@ -0,0 +1,49 @@ +/* + * 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.connect + +import scala.util.matching.Regex + +import org.apache.spark.sql + +/** + * Overrides test utils to implement 'connect variants' of suites declared in sql/core: + * {{{ + * // in sql/core + * FooSuite extends SessionQueryTest { test("") { ... } } + * + * // in sql/connect + * FooConnectSuite extends FooSuite with connect.SessionQueryTest + * }}} + * + * This trait overrides [[spark]] to use a [[SparkSession connect.SparkSession]], which executes + * via the gRPC API using an in-process connect server. + */ +trait SessionQueryTest extends sql.SessionQueryTest with SparkSessionBinder { + + private val sortOperator: Regex = "\b(?:Photon)?Sort\b".r + + /** + * Approximates [[sql.SessionQueryTest.isDfSorted]] by inspecting the explain string. + */ + override def isDfSorted(df: sql.DataFrame): Boolean = df match { + case df: DataFrame => sortOperator.matches(df.explainString(extended = false)) Review Comment: `Regex.matches` matches the entire string, so this is `false` for any multi-line explain output. `unanchored` makes `matches` behave like `find` (substring): ```suggestion case df: DataFrame => sortOperator.unanchored.matches(df.explainString(extended = false)) ``` Needs the raw-string fix on line 38 to actually match. -- 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]
