cloud-fan commented on code in PR #55571: URL: https://github.com/apache/spark/pull/55571#discussion_r3280856607
########## sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala: ########## @@ -0,0 +1,748 @@ +/* + * 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 java.util + +import org.apache.spark.SparkConf +import org.apache.spark.sql.{AnalysisException, Row, SparkSession} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.classic +import org.apache.spark.sql.connector.catalog.{BufferedRows, CachingInMemoryTableCatalog, Column, Identifier, InMemoryBaseTable, InMemoryTableCatalog, TableCatalog, TableChange, TableInfo, TableWritePrivilege} +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType} + +/** + * DSv2 temp view with stored plan tests for Spark Connect, mirroring the classic + * DataSourceV2DataFrameSuite temp view scenarios. + * + * Uses an in-process Connect server ([[SparkConnectServerTest]]) so that the test can access the + * server's catalog directly for external changes. All data reads go through the Connect client + * session to simulate the real client experience. + */ +class DataSourceV2TempViewConnectSuite extends SparkConnectServerTest { + + override def sparkConf: SparkConf = super.sparkConf + .set("spark.sql.catalog.testcat", classOf[InMemoryTableCatalog].getName) + .set("spark.sql.catalog.testcat.copyOnLoad", "true") + .set("spark.sql.catalog.cachingcat", classOf[CachingInMemoryTableCatalog].getName) + .set("spark.sql.catalog.cachingcat.copyOnLoad", "true") + + private val T = "testcat.ns1.ns2.tbl" + private val CT = "cachingcat.ns1.ns2.tbl" + private val ident = Identifier.of(Array("ns1", "ns2"), "tbl") + + /** + * Assert that rows collected through the Connect client match expected rows (order-agnostic). + */ + private def assertRows(actual: Array[Row], expected: Seq[Row]): Unit = { + assert( + actual.toSeq.sortBy(_.toString()) == expected.sortBy(_.toString()), + s"Expected ${expected.mkString(", ")} but got ${actual.mkString(", ")}") + } + + /** Get a catalog from the server-side session by name. */ + private def serverCatalog[T <: TableCatalog]( + serverSession: classic.SparkSession, + name: String): T = + serverSession.sessionState.catalogManager.catalog(name).asInstanceOf[T] Review Comment: @andreaschat-db's earlier suggestion to add `: ClassTag` on the type parameter wasn't picked up in the follow-up commits — worth keeping it, since `asInstanceOf[T]` at this boundary is a no-op under erasure (the cast really happens at the use site, producing a confusing classcast on a downstream method call instead of failing here at the helper boundary). ########## sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala: ########## @@ -0,0 +1,748 @@ +/* + * 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 java.util + +import org.apache.spark.SparkConf +import org.apache.spark.sql.{AnalysisException, Row, SparkSession} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.classic +import org.apache.spark.sql.connector.catalog.{BufferedRows, CachingInMemoryTableCatalog, Column, Identifier, InMemoryBaseTable, InMemoryTableCatalog, TableCatalog, TableChange, TableInfo, TableWritePrivilege} +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType} + +/** + * DSv2 temp view with stored plan tests for Spark Connect, mirroring the classic + * DataSourceV2DataFrameSuite temp view scenarios. + * + * Uses an in-process Connect server ([[SparkConnectServerTest]]) so that the test can access the + * server's catalog directly for external changes. All data reads go through the Connect client + * session to simulate the real client experience. + */ +class DataSourceV2TempViewConnectSuite extends SparkConnectServerTest { + + override def sparkConf: SparkConf = super.sparkConf + .set("spark.sql.catalog.testcat", classOf[InMemoryTableCatalog].getName) + .set("spark.sql.catalog.testcat.copyOnLoad", "true") + .set("spark.sql.catalog.cachingcat", classOf[CachingInMemoryTableCatalog].getName) + .set("spark.sql.catalog.cachingcat.copyOnLoad", "true") + + private val T = "testcat.ns1.ns2.tbl" + private val CT = "cachingcat.ns1.ns2.tbl" + private val ident = Identifier.of(Array("ns1", "ns2"), "tbl") + + /** + * Assert that rows collected through the Connect client match expected rows (order-agnostic). + */ + private def assertRows(actual: Array[Row], expected: Seq[Row]): Unit = { + assert( + actual.toSeq.sortBy(_.toString()) == expected.sortBy(_.toString()), Review Comment: Sorting and comparing by `_.toString()` is type-blind — two rows that stringify identically would compare equal regardless of underlying value types. `QueryTest.checkAnswer` is usable from connect server tests (it's already called statically in `sql/connect/server/src/test/scala/org/apache/spark/sql/connect/pipelines/PipelineRefreshFunctionalSuite.scala:351`) and would give value-based, order-agnostic comparison matching what the analogue suite uses. Worth wiring it in here rather than this custom helper. ########## sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala: ########## @@ -0,0 +1,748 @@ +/* + * 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 java.util + +import org.apache.spark.SparkConf +import org.apache.spark.sql.{AnalysisException, Row, SparkSession} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.classic +import org.apache.spark.sql.connector.catalog.{BufferedRows, CachingInMemoryTableCatalog, Column, Identifier, InMemoryBaseTable, InMemoryTableCatalog, TableCatalog, TableChange, TableInfo, TableWritePrivilege} +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType} + +/** + * DSv2 temp view with stored plan tests for Spark Connect, mirroring the classic + * DataSourceV2DataFrameSuite temp view scenarios. + * + * Uses an in-process Connect server ([[SparkConnectServerTest]]) so that the test can access the + * server's catalog directly for external changes. All data reads go through the Connect client + * session to simulate the real client experience. + */ +class DataSourceV2TempViewConnectSuite extends SparkConnectServerTest { + + override def sparkConf: SparkConf = super.sparkConf + .set("spark.sql.catalog.testcat", classOf[InMemoryTableCatalog].getName) + .set("spark.sql.catalog.testcat.copyOnLoad", "true") + .set("spark.sql.catalog.cachingcat", classOf[CachingInMemoryTableCatalog].getName) + .set("spark.sql.catalog.cachingcat.copyOnLoad", "true") + + private val T = "testcat.ns1.ns2.tbl" + private val CT = "cachingcat.ns1.ns2.tbl" + private val ident = Identifier.of(Array("ns1", "ns2"), "tbl") + + /** + * Assert that rows collected through the Connect client match expected rows (order-agnostic). + */ + private def assertRows(actual: Array[Row], expected: Seq[Row]): Unit = { + assert( + actual.toSeq.sortBy(_.toString()) == expected.sortBy(_.toString()), + s"Expected ${expected.mkString(", ")} but got ${actual.mkString(", ")}") + } + + /** Get a catalog from the server-side session by name. */ + private def serverCatalog[T <: TableCatalog]( + serverSession: classic.SparkSession, + name: String): T = + serverSession.sessionState.catalogManager.catalog(name).asInstanceOf[T] + + /** Appends a row to a DSv2 table via the catalog API, bypassing the session. */ + private def externalAppend( + cat: TableCatalog, + ident: Identifier, + schema: StructType, + row: InternalRow): Unit = { + val extTable = cat + .loadTable(ident, util.Set.of(TableWritePrivilege.INSERT)) + .asInstanceOf[InMemoryBaseTable] + extTable.withData(Array(new BufferedRows(Seq.empty, schema).withRow(row))) + } + + /** Ensure views and table are dropped even if the test body throws. */ + private def withTableAndViews( + session: SparkSession, + table: String, + views: Seq[String], + clearCachingCatalog: Boolean = false)(fn: => Unit): Unit = { + try { + fn + } finally { + views.foreach(v => session.sql(s"DROP VIEW IF EXISTS $v").collect()) + session.sql(s"DROP TABLE IF EXISTS $table").collect() + if (clearCachingCatalog) { + val serverSession = getServerSession(session) + serverCatalog[CachingInMemoryTableCatalog](serverSession, "cachingcat").clearCache() + } Review Comment: Each `withSession` server-side gets a freshly isolated `SparkSession` (`SparkConnectSessionManager.newIsolatedSession` → `baseSession.newSession()`, which creates an independent `SessionState` and `CatalogManager`), and `afterEach` invalidates all sessions. The `CachingInMemoryTableCatalog` instance is therefore per-test, so `clearCache()` here is clearing state that wouldn't have survived anyway — the auto-review's compile-error fix on `0b76175` paid the complexity cost of a parameter + server-session lookup + generic cast for no observable behavior change. Two options: (a) drop the `clearCachingCatalog` parameter and the conditional entirely, or (b) keep it but add a one-line comment explaining "defensive — even though sessions are isolated, ..." so the next reader doesn't have to re-derive the analysis. Worth confirming this isn't relying on some intra-test reuse I haven't spotted. ########## sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala: ########## @@ -0,0 +1,748 @@ +/* + * 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 java.util + +import org.apache.spark.SparkConf +import org.apache.spark.sql.{AnalysisException, Row, SparkSession} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.classic +import org.apache.spark.sql.connector.catalog.{BufferedRows, CachingInMemoryTableCatalog, Column, Identifier, InMemoryBaseTable, InMemoryTableCatalog, TableCatalog, TableChange, TableInfo, TableWritePrivilege} +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType} + +/** + * DSv2 temp view with stored plan tests for Spark Connect, mirroring the classic + * DataSourceV2DataFrameSuite temp view scenarios. + * + * Uses an in-process Connect server ([[SparkConnectServerTest]]) so that the test can access the + * server's catalog directly for external changes. All data reads go through the Connect client + * session to simulate the real client experience. + */ +class DataSourceV2TempViewConnectSuite extends SparkConnectServerTest { + + override def sparkConf: SparkConf = super.sparkConf + .set("spark.sql.catalog.testcat", classOf[InMemoryTableCatalog].getName) + .set("spark.sql.catalog.testcat.copyOnLoad", "true") + .set("spark.sql.catalog.cachingcat", classOf[CachingInMemoryTableCatalog].getName) + .set("spark.sql.catalog.cachingcat.copyOnLoad", "true") + + private val T = "testcat.ns1.ns2.tbl" + private val CT = "cachingcat.ns1.ns2.tbl" + private val ident = Identifier.of(Array("ns1", "ns2"), "tbl") + + /** + * Assert that rows collected through the Connect client match expected rows (order-agnostic). + */ + private def assertRows(actual: Array[Row], expected: Seq[Row]): Unit = { + assert( + actual.toSeq.sortBy(_.toString()) == expected.sortBy(_.toString()), + s"Expected ${expected.mkString(", ")} but got ${actual.mkString(", ")}") + } + + /** Get a catalog from the server-side session by name. */ + private def serverCatalog[T <: TableCatalog]( + serverSession: classic.SparkSession, + name: String): T = + serverSession.sessionState.catalogManager.catalog(name).asInstanceOf[T] + + /** Appends a row to a DSv2 table via the catalog API, bypassing the session. */ + private def externalAppend( + cat: TableCatalog, + ident: Identifier, + schema: StructType, + row: InternalRow): Unit = { + val extTable = cat + .loadTable(ident, util.Set.of(TableWritePrivilege.INSERT)) + .asInstanceOf[InMemoryBaseTable] + extTable.withData(Array(new BufferedRows(Seq.empty, schema).withRow(row))) + } Review Comment: The analogue's `externalAppend` (`DataSourceV2DataFrameSuite.scala:2997`) derives the schema from the loaded table via `CatalogV2Util.v2ColumnsToStructType(extTable.columns())`. Here every call site duplicates the schema as a DDL string (`StructType.fromDDL("id INT, salary INT[, new_column INT]")` at lines 132, 159, 211, 242), opening the door to a caller's DDL silently diverging from the table's actual schema if a future scenario reshapes the table. Suggest dropping the `schema` parameter and computing it from `extTable.columns()` here, matching the analogue. -- 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]
