cloud-fan commented on a change in pull request #29893:
URL: https://github.com/apache/spark/pull/29893#discussion_r531421673



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.spark.SparkConf
+import org.apache.spark.sql.connector.InMemoryPartitionTableCatalog
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.{SharedSparkSession, SQLTestUtils}
+
+/**
+ * The base trait for DML - insert syntax
+ */
+trait SQLInsertTestSuite extends QueryTest with SQLTestUtils {
+
+  import testImplicits._
+
+  def format: String
+
+  protected def createTable(
+      table: String,
+      cols: Seq[String],
+      colTypes: Seq[String],
+      partCols: Seq[String] = Nil): Unit = {
+    val values = cols.zip(colTypes).map(tuple => tuple._1 + " " + 
tuple._2).mkString("(", ", ", ")")
+    val partitionSpec = if (partCols.nonEmpty) {
+      partCols.mkString("PARTITIONED BY (", ",", ")")
+    } else ""
+    sql(s"CREATE TABLE $table$values USING $format $partitionSpec")
+  }
+
+  protected def processInsert(
+      tableName: String,
+      insert: DataFrame,
+      cols: Seq[String] = Nil,
+      partitionExprs: Seq[String] = Nil,
+      mode: SaveMode): Unit = {
+    val tmpView = "tmp_view"
+    val columnList = if (cols.nonEmpty) cols.mkString("(", ",", ")") else ""
+    val partitionList = if (partitionExprs.nonEmpty) {
+      partitionExprs.mkString("PARTITION (", ",", ")")
+    } else ""
+    withTempView(tmpView) {
+      insert.createOrReplaceTempView(tmpView)
+      val overwrite = if (mode == SaveMode.Overwrite) "OVERWRITE" else "INTO"
+      sql(s"INSERT $overwrite TABLE $tableName $partitionList $columnList 
SELECT * FROM $tmpView")
+    }
+  }
+
+  protected def verifyTable(tableName: String, expected: DataFrame): Unit = {
+    checkAnswer(spark.table(tableName), expected)
+  }
+
+  test("insert with column list - follow table output order") {
+    val t1 = "t1"
+    withTable(t1) {
+      val df = Seq((1, 2L, "3")).toDF()
+      val cols = Seq("c1", "c2", "c3")
+      createTable(t1, cols, Seq("int", "long", "string"))
+      Seq(SaveMode.Append, SaveMode.Overwrite).foreach { m =>
+        processInsert(t1, df, cols, mode = m)
+        verifyTable(t1, df)
+      }
+    }
+
+    val cols = Seq("c1", "c2", "c3", "c4")
+    val df = Seq((1, 2, 3, 4)).toDF(cols: _*)
+    withTable(t1) {
+      createTable(t1, cols, Seq("int", "int", "int", "int"), cols.takeRight(2))
+      Seq(SaveMode.Append, SaveMode.Overwrite).foreach { m =>
+        processInsert(t1, df, cols, mode = m)
+        verifyTable(t1, df)
+      }
+    }
+
+    withTable(t1) {

Review comment:
       ditto, `insert with column list - follow table output order + 
partitioned table and write with static partitions`




----------------------------------------------------------------
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to