andreaschat-db commented on code in PR #55571: URL: https://github.com/apache/spark/pull/55571#discussion_r3209131688
########## sql/connect/server/src/test/scala/org/apache/spark/sql/connect/DataSourceV2TempViewConnectSuite.scala: ########## @@ -0,0 +1,781 @@ +/* + * 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 withTableAndView( Review Comment: nit: `withTableAndViews` -- 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]
