hvanhovell commented on code in PR #48958: URL: https://github.com/apache/spark/pull/48958#discussion_r1866892694
########## connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/SQLContext.scala: ########## @@ -0,0 +1,350 @@ +/* + * 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 scala.jdk.CollectionConverters.PropertiesHasAsScala +import scala.reflect.runtime.universe.TypeTag + +import _root_.java.util.{List => JavaList, Map => JavaMap, Properties} + +import org.apache.spark.SparkContext +import org.apache.spark.annotation.Stable +import org.apache.spark.api.java.JavaRDD +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.connect.ConnectClientUnsupportedErrors +import org.apache.spark.sql.connect.ConnectConversions._ +import org.apache.spark.sql.sources.BaseRelation +import org.apache.spark.sql.streaming.{DataStreamReader, StreamingQueryManager} +import org.apache.spark.sql.types.StructType +import org.apache.spark.sql.util.ExecutionListenerManager + +@Stable +class SQLContext private[sql] (override val sparkSession: SparkSession) + extends api.SQLContext(sparkSession) { + + /** @inheritdoc */ + def newSession(): SQLContext = sparkSession.newSession().sqlContext + + /** @inheritdoc */ + def listenerManager: ExecutionListenerManager = sparkSession.listenerManager + + /** @inheritdoc */ + def setConf(props: Properties): Unit = sparkSession.conf.synchronized { + props.asScala.foreach { case (k, v) => sparkSession.conf.set(k, v) } + } + + /** @inheritdoc */ + def experimental: ExperimentalMethods = sparkSession.experimental + + /** @inheritdoc */ + def udf: UDFRegistration = sparkSession.udf + + // scalastyle:off + // Disable style checker so "implicits" object can start with lowercase i + + /** @inheritdoc */ + object implicits extends SQLImplicits(this.sparkSession) + + // scalastyle:on + + /** @inheritdoc */ + def read: DataFrameReader = sparkSession.read + + /** @inheritdoc */ + def readStream: DataStreamReader = sparkSession.readStream + + /** @inheritdoc */ + def tables(): Dataset[Row] = { + sparkSession.catalog + .listTables() + .map(t => ListTableRow(t.database, t.name, t.isTemporary))(Encoders.product[ListTableRow]) + .toDF() + } + + /** @inheritdoc */ + def tables(databaseName: String): Dataset[Row] = { + sparkSession.catalog + .listTables(databaseName) + .map(t => ListTableRow(t.database, t.name, t.isTemporary))(Encoders.product[ListTableRow]) + .toDF() + } + + /** A case class to mimic the output schema of Spark Classic's `SQLContext.tables()` API. */ + private case class ListTableRow(database: String, tableName: String, isTemporary: Boolean) + + /** + * Returns a `StreamingQueryManager` that allows managing all the + * [[org.apache.spark.sql.streaming.StreamingQuery StreamingQueries]] active on `this` context. + * + * @since 4.0.0 + */ + def streams: StreamingQueryManager = sparkSession.streams + + /** @inheritdoc */ + def tableNames(databaseName: String): Array[String] = { + sparkSession.catalog.listTables(databaseName).map(_.name)(Encoders.STRING).collect() Review Comment: Do `sparkSession.catalog.listTables(databaseName).select(col("name)).as(Encoders.STRING).collect()` instead. Map is a fairly slow operation, whereas this particular select gets optimized away. -- 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]
