HyukjinKwon commented on code in PR #39541: URL: https://github.com/apache/spark/pull/39541#discussion_r1082316930
########## connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/SparkSession.scala: ########## @@ -0,0 +1,103 @@ +/* + * 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 org.apache.arrow.memory.RootAllocator + +import org.apache.spark.connect.proto +import org.apache.spark.sql.connect.client.{SparkConnectClient, SparkResult} +import org.apache.spark.sql.connect.client.util.Cleaner + +/** + * The entry point to programming Spark with the Dataset and DataFrame API. + * + * In environments that this has been created upfront (e.g. REPL, notebooks), use the builder to + * get an existing session: + * + * {{{ + * SparkSession.builder().getOrCreate() + * }}} + * + * The builder can also be used to create a new session: + * + * {{{ + * SparkSession.builder + * .master("local") + * .appName("Word Count") + * .config("spark.some.config.option", "some-value") + * .getOrCreate() + * }}} + */ +class SparkSession(private val client: SparkConnectClient, private val cleaner: Cleaner) + extends AutoCloseable { + + private[this] val allocator = new RootAllocator() + + /** + * Executes a SQL query using Spark, returning the result as a `DataFrame`. This API eagerly + * runs DDL/DML commands, but not for SELECT queries. + * + * @since 3.4.0 + */ + def sql(query: String): Dataset = newDataset { builder => + builder.setSql(proto.SQL.newBuilder().setQuery(query)) + } + + private[sql] def newDataset(f: proto.Relation.Builder => Unit): Dataset = { + val builder = proto.Relation.newBuilder() + f(builder) + val plan = proto.Plan.newBuilder().setRoot(builder).build() + new Dataset(this, plan) + } + + private[sql] def execute(plan: proto.Plan): SparkResult = { + val value = client.execute(plan) + val result = new SparkResult(value, allocator) + cleaner.register(result) + result + } + + override def close(): Unit = { + client.shutdown() + allocator.close() + } +} + +// The minimal builder needed to create a spark session. +// TODO: implements all methods mentioned in the scaladoc of [[SparkSession]] Review Comment: Please file a JIRA for a TODO. -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org