HeartSaVioR commented on a change in pull request #30521:
URL: https://github.com/apache/spark/pull/30521#discussion_r535882516



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/streaming/DataStreamWriter.scala
##########
@@ -304,46 +308,68 @@ final class DataStreamWriter[T] private[sql](ds: 
Dataset[T]) {
    * @since 3.1.0
    */
   @throws[TimeoutException]
-  def saveAsTable(tableName: String): StreamingQuery = {
-    this.source = SOURCE_NAME_TABLE
+  def table(tableName: String): StreamingQuery = {
     this.tableName = tableName
-    startInternal(None)
-  }
 
-  private def startInternal(path: Option[String]): StreamingQuery = {
-    if (source.toLowerCase(Locale.ROOT) == DDLUtils.HIVE_PROVIDER) {
-      throw new AnalysisException("Hive data source can only be used with 
tables, you can not " +
-        "write files of Hive data source directly.")
-    }
+    import df.sparkSession.sessionState.analyzer.CatalogAndIdentifier
 
-    if (source == SOURCE_NAME_TABLE) {
-      assertNotPartitioned(SOURCE_NAME_TABLE)
+    import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+    val originalMultipartIdentifier = df.sparkSession.sessionState.sqlParser
+      .parseMultipartIdentifier(tableName)
+    val CatalogAndIdentifier(catalog, identifier) = originalMultipartIdentifier
 
-      import df.sparkSession.sessionState.analyzer.CatalogAndIdentifier
+    // Currently we don't create a logical streaming writer node in logical 
plan, so cannot rely
+    // on analyzer to resolve it. Directly lookup only for temp view to 
provide clearer message.
+    // TODO (SPARK-27484): we should add the writing node before the plan is 
analyzed.
+    if 
(df.sparkSession.sessionState.catalog.isTempView(originalMultipartIdentifier)) {
+      throw new AnalysisException(s"Temporary view $tableName doesn't support 
streaming write")
+    }
 
+    if (!catalog.asTableCatalog.tableExists(identifier)) {
       import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
-      val originalMultipartIdentifier = df.sparkSession.sessionState.sqlParser
-        .parseMultipartIdentifier(tableName)
-      val CatalogAndIdentifier(catalog, identifier) = 
originalMultipartIdentifier
-
-      // Currently we don't create a logical streaming writer node in logical 
plan, so cannot rely
-      // on analyzer to resolve it. Directly lookup only for temp view to 
provide clearer message.
-      // TODO (SPARK-27484): we should add the writing node before the plan is 
analyzed.
-      if 
(df.sparkSession.sessionState.catalog.isTempView(originalMultipartIdentifier)) {
-        throw new AnalysisException(s"Temporary view $tableName doesn't 
support streaming write")
-      }
+      val cmd = CreateTableStatement(
+        originalMultipartIdentifier,
+        df.schema.asNullable,
+        partitioningColumns.getOrElse(Nil).asTransforms.toSeq,
+        None,
+        Map.empty[String, String],
+        Some(source),
+        Map.empty[String, String],
+        extraOptions.get("path"),
+        None,
+        None,
+        external = false,
+        ifNotExists = false)
+      Dataset.ofRows(df.sparkSession, cmd)
+    }
 
-      val tableInstance = catalog.asTableCatalog.loadTable(identifier)
+    val tableInstance = catalog.asTableCatalog.loadTable(identifier)
 
-      import 
org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits._
-      val sink = tableInstance match {
-        case t: SupportsWrite if t.supports(STREAMING_WRITE) => t
-        case t => throw new AnalysisException(s"Table $tableName doesn't 
support streaming " +
-          s"write - $t")
-      }
+    def writeToV1Table(table: CatalogTable): StreamingQuery = {
+      require(table.tableType != CatalogTableType.VIEW, "Streaming into views 
is not supported.")
+      format(table.provider.get)

Review comment:
       Sorry I missed this. I'm not sure we provide the both into data source, 
but data source is probably able to know about the partitioning (as table 
partitioning is given by data source), so consider this as minor and make a 
follow-up if necessary. In anyway you'll want to check this to achieve my 
review comments on documentation.




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

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