MaxGekk commented on a change in pull request #30377:
URL: https://github.com/apache/spark/pull/30377#discussion_r524002484



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowPartitionsSuite.scala
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.execution.command.v1
+
+import org.apache.spark.sql.{AnalysisException, Row, SaveMode}
+import org.apache.spark.sql.catalyst.analysis.NoSuchTableException
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import org.apache.spark.sql.execution.command
+import org.apache.spark.sql.test.SharedSparkSession
+
+trait ShowPartitionsSuiteBase extends command.ShowPartitionsSuiteBase {
+  override def version: String = "V1"
+  override def catalog: String = CatalogManager.SESSION_CATALOG_NAME
+  override def defaultNamespace: Seq[String] = Seq("default")
+  override def defaultUsing: String = "USING parquet"
+
+  protected def createDateTable(table: String): Unit = {
+    sql(s"""
+      |CREATE TABLE $table (price int, qty int, year int, month int)
+      |$defaultUsing
+      |partitioned by (year, month)""".stripMargin)
+  }
+
+  protected def fillDateTable(table: String): Unit = {
+    sql(s"INSERT INTO $table PARTITION(year = 2015, month = 1) SELECT 1, 1")
+    sql(s"INSERT INTO $table PARTITION(year = 2015, month = 2) SELECT 2, 2")
+    sql(s"INSERT INTO $table PARTITION(year = 2016, month = 2) SELECT 3, 3")
+    sql(s"INSERT INTO $table PARTITION(year = 2016, month = 3) SELECT 3, 3")
+  }
+
+  protected def createWideTable(table: String): Unit = {
+    sql(s"""
+      |CREATE TABLE $table (
+      |  price int, qty int,
+      |  year int, month int, hour int, minute int, sec int, extra int)
+      |$defaultUsing
+      |PARTITIONED BY (year, month, hour, minute, sec, extra)""".stripMargin)
+  }
+
+  protected def fillWideTable(table: String): Unit = {
+    sql(s"""
+      |INSERT INTO $table
+      |PARTITION(year = 2016, month = 3, hour = 10, minute = 10, sec = 10, 
extra = 1) SELECT 3, 3
+      """.stripMargin)
+    sql(s"""
+      |INSERT INTO $table
+      |PARTITION(year = 2016, month = 4, hour = 10, minute = 10, sec = 10, 
extra = 1) SELECT 3, 3
+      """.stripMargin)
+  }
+
+  test("show everything") {
+    val table = "dateTable"
+    withTable(table) {
+      createDateTable(table)
+      fillDateTable(table)
+      checkAnswer(
+        sql(s"show partitions $table"),
+        Row("year=2015/month=1") ::
+          Row("year=2015/month=2") ::
+          Row("year=2016/month=2") ::
+          Row("year=2016/month=3") :: Nil)
+
+      checkAnswer(
+        sql(s"show partitions default.$table"),
+        Row("year=2015/month=1") ::
+          Row("year=2015/month=2") ::
+          Row("year=2016/month=2") ::
+          Row("year=2016/month=3") :: Nil)
+    }
+  }
+
+  test("filter by partitions") {
+    val table = "dateTable"
+    withTable(table) {
+      createDateTable(table)
+      fillDateTable(table)
+      checkAnswer(
+        sql(s"show partitions default.$table PARTITION(year=2015)"),
+        Row("year=2015/month=1") ::
+          Row("year=2015/month=2") :: Nil)
+      checkAnswer(
+        sql(s"show partitions default.$table PARTITION(year=2015, month=1)"),
+        Row("year=2015/month=1") :: Nil)
+      checkAnswer(
+        sql(s"show partitions default.$table PARTITION(month=2)"),
+        Row("year=2015/month=2") ::
+          Row("year=2016/month=2") :: Nil)
+    }
+  }
+
+  test("show everything more than 5 part keys") {
+    val table = "wideTable"
+    withTable(table) {
+      createWideTable(table)
+      fillWideTable(table)
+      checkAnswer(
+        sql(s"show partitions $table"),
+        Row("year=2016/month=3/hour=10/minute=10/sec=10/extra=1") ::
+          Row("year=2016/month=4/hour=10/minute=10/sec=10/extra=1") :: Nil)
+    }
+  }
+
+  test("non-partitioning columns") {
+    val table = "dateTable"
+    withTable(table) {
+      createDateTable(table)
+      fillDateTable(table)
+      val errMsg = intercept[AnalysisException] {
+        sql(s"SHOW PARTITIONS $table PARTITION(abcd=2015, xyz=1)")
+      }.getMessage
+      assert(errMsg.contains("Non-partitioning column(s) [abcd, xyz] are 
specified"))
+    }
+  }
+
+  test("show partitions of not partitioned table") {

Review comment:
       There are a few places with `not partitioned`:
   ```
   $ find . -name '*.scala' -print0|xargs -0 grep -i -n 'not partitioned'
   ./core/src/test/scala/org/apache/spark/rdd/SortingSuite.scala:138:  
test("get a range of elements in an array not partitioned by a range 
partitioner") {
   ./mllib/src/main/scala/org/apache/spark/ml/recommendation/ALS.scala:925:   * 
Note that the term "rating block" is a bit of a misnomer, as the ratings are 
not partitioned by
   
./streaming/src/main/scala/org/apache/spark/streaming/dstream/MapWithStateDStream.scala:134:
          // If the RDD is not partitioned the right way, let us repartition it 
using the
   ./sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala:556:   
 // One side of join is not partitioned in the desired way. Need to shuffle one 
side.
   ./sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala:590:   
 // One side of join is not partitioned in the desired way. Since the number of 
partitions of
   ./sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala:591:   
 // the side that has already partitioned is smaller than the side that is not 
partitioned,
   
./sql/core/src/test/scala/org/apache/spark/sql/DataFrameWriterV2Suite.scala:308:
  test("OverwritePartitions: overwrite all rows if not partitioned") {
   
./sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowPartitionsSuite.scala:130:
  test("show partitions of not partitioned table") {
   
./sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowPartitionsSuite.scala:137:
      assert(errMsg.contains("not allowed on a table that is not partitioned"))
   
./sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala:1982:
        // not supported since the table is not partitioned
   
./sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileIndex.scala:73:
  /** Schema of the partitioning columns, or the empty schema if the table is 
not partitioned. */
   
./sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DescribeTableExec.scala:79:
      rows += toCatalystRow("Not partitioned", "", "")
   
./sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala:533:
            failAnalysis(s"Insert into a partition is not allowed because $l is 
not partitioned.")
   
./sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PartitioningUtils.scala:149:
      // This dataset is not partitioned.
   
./sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala:462:
        s"for tables that are not partitioned: $tableIdentWithDB")
   
./sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala:989:
     * 1. If the table is not partitioned.
   
./sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala:999:
        s"SHOW PARTITIONS is not allowed on a table that is not partitioned: 
$tableIdentWithDB")
   
./sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala:1755:
        // not supported since the table is not partitioned
   ```
   I will change the title of this test but I am not sure about other places. I 
will leave them AS IS so far. @janekdb If you want, you can open a PR and fix 
them when it makes sense.




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