andreaschat-db commented on code in PR #55571:
URL: https://github.com/apache/spark/pull/55571#discussion_r3208143037


##########
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala:
##########
@@ -0,0 +1,661 @@
+/*
+ * 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}
+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, 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.map(_.toString()).toSet == expected.map(_.toString()).toSet,
+      s"Expected ${expected.mkString(", ")} but got ${actual.mkString(", ")}")
+  }
+
+  /** Get the catalog from the server-side session. */
+  private def serverCatalog(serverSession: classic.SparkSession): 
InMemoryTableCatalog =
+    serverSession.sessionState.catalogManager
+      .catalog("testcat")
+      .asInstanceOf[InMemoryTableCatalog]
+
+  /** Get the caching catalog from the server-side session. */
+  private def serverCachingCatalog(
+      serverSession: classic.SparkSession): CachingInMemoryTableCatalog =
+    serverSession.sessionState.catalogManager
+      .catalog("cachingcat")
+      .asInstanceOf[CachingInMemoryTableCatalog]
+
+  // Temp views with stored plans: scenarios from the DSv2 table refresh tests.
+  // Each test creates a DSv2 table with initial data, builds a temp view with 
a filter
+  // (to demonstrate that the stored plan is non-trivial), and then verifies 
the view
+  // behavior after various table modifications (session or external).
+
+  // Scenario 1.1 (session write)
+  test("[connect] temp view with stored plan reflects session write") {
+    withSession { session =>
+      session.sql(s"CREATE TABLE $T (id INT, salary INT) USING foo").collect()
+      session.sql(s"INSERT INTO $T VALUES (1, 100), (10, 1000)").collect()
+
+      session.table(T).filter("salary < 999").createOrReplaceTempView("v")
+      assertRows(session.table("v").collect(), Seq(Row(1, 100)))
+
+      session.sql(s"INSERT INTO $T VALUES (2, 200)").collect()
+
+      assertRows(session.table("v").collect(), Seq(Row(1, 100), Row(2, 200)))
+
+      session.sql("DROP VIEW IF EXISTS v").collect()

Review Comment:
   Can we create a helper here so we do not have to DROP VIEW/TABLE at each 
test? More importantly, the drops need to be in a `try {} finally {}`. That 
ensure that if a tests fails everything is still cleaned up.



##########
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala:
##########
@@ -0,0 +1,661 @@
+/*
+ * 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}
+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, 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.map(_.toString()).toSet == expected.map(_.toString()).toSet,

Review Comment:
   We could instead do: `actual.toSeq.sortBy(_.toString) == 
expected.sortBy(_.toString)` so we preserve the original type and duplicates.



##########
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala:
##########
@@ -0,0 +1,661 @@
+/*
+ * 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}
+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, 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.map(_.toString()).toSet == expected.map(_.toString()).toSet,
+      s"Expected ${expected.mkString(", ")} but got ${actual.mkString(", ")}")
+  }
+
+  /** Get the catalog from the server-side session. */
+  private def serverCatalog(serverSession: classic.SparkSession): 
InMemoryTableCatalog =

Review Comment:
   ```suggestion
     private def serverCatalog[T <: TableCatalog: ClassTag](                    
                                                                                
                                                                                
       
         serverSession: classic.SparkSession, name: String): T =                
                                                                                
                                                                                
       
       serverSession.sessionState.catalogManager.catalog(name).asInstanceOf[T]  
   ```



-- 
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]

Reply via email to