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



##########
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"))

Review comment:
       it's more readable if we pass schema string directly `createTable(t1, 
"c1 int, c2 long, c3 string")`




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