cloud-fan commented on code in PR #37758:
URL: https://github.com/apache/spark/pull/37758#discussion_r963251827


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/MetadataColumnSuite.scala:
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.connector
+
+import org.apache.spark.sql.{AnalysisException, Row}
+import org.apache.spark.sql.functions.struct
+
+class MetadataColumnSuite extends DatasourceV2SQLBase {
+  import testImplicits._
+
+  private val tbl = "testcat.t"
+
+  private def prepareTable(): Unit = {
+    sql(s"CREATE TABLE $tbl (id bigint, data string) PARTITIONED BY (bucket(4, 
id), id)")
+    sql(s"INSERT INTO $tbl VALUES (1, 'a'), (2, 'b'), (3, 'c')")
+  }
+
+  test("SPARK-31255: Project a metadata column") {
+    withTable(tbl) {
+      prepareTable()
+      val sqlQuery = sql(s"SELECT id, data, index, _partition FROM $tbl")
+      val dfQuery = spark.table(tbl).select("id", "data", "index", 
"_partition")
+
+      Seq(sqlQuery, dfQuery).foreach { query =>
+        checkAnswer(query, Seq(Row(1, "a", 0, "3/1"), Row(2, "b", 0, "0/2"), 
Row(3, "c", 0, "1/3")))
+      }
+    }
+  }
+
+  test("SPARK-31255: Projects data column when metadata column has the same 
name") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (index bigint, data string) PARTITIONED BY 
(bucket(4, index), index)")
+      sql(s"INSERT INTO $tbl VALUES (3, 'c'), (2, 'b'), (1, 'a')")
+
+      val sqlQuery = sql(s"SELECT index, data, _partition FROM $tbl")
+      val dfQuery = spark.table(tbl).select("index", "data", "_partition")
+
+      Seq(sqlQuery, dfQuery).foreach { query =>
+        checkAnswer(query, Seq(Row(3, "c", "1/3"), Row(2, "b", "0/2"), Row(1, 
"a", "3/1")))
+      }
+    }
+  }
+
+  test("SPARK-31255: * expansion does not include metadata columns") {
+    withTable(tbl) {
+      prepareTable()
+      val sqlQuery = sql(s"SELECT * FROM $tbl")
+      val dfQuery = spark.table(tbl)
+
+      Seq(sqlQuery, dfQuery).foreach { query =>
+        checkAnswer(query, Seq(Row(1, "a"), Row(2, "b"), Row(3, "c")))
+      }
+    }
+  }
+
+  test("SPARK-31255: metadata column should only be produced when necessary") {
+    withTable(tbl) {
+      prepareTable()
+      val sqlQuery = sql(s"SELECT * FROM $tbl WHERE index = 0")
+      val dfQuery = spark.table(tbl).filter("index = 0")
+
+      Seq(sqlQuery, dfQuery).foreach { query =>
+        assert(query.schema.fieldNames.toSeq == Seq("id", "data"))
+      }
+    }
+  }
+
+  test("SPARK-34547: metadata columns are resolved last") {
+    withTable(tbl) {
+      prepareTable()
+      withTempView("v") {
+        sql(s"CREATE TEMPORARY VIEW v AS SELECT * FROM " +
+          s"VALUES (1, -1), (2, -2), (3, -3) AS v(id, index)")
+
+        val sqlQuery = sql(s"SELECT $tbl.id, v.id, data, index, $tbl.index, 
v.index " +
+          s"FROM $tbl JOIN v WHERE $tbl.id = v.id")
+        val tableDf = spark.table(tbl)
+        val viewDf = spark.table("v")
+        val dfQuery = tableDf.join(viewDf, tableDf.col("id") === 
viewDf.col("id"))
+          .select(s"$tbl.id", "v.id", "data", "index", s"$tbl.index", 
"v.index")
+
+        Seq(sqlQuery, dfQuery).foreach { query =>
+          checkAnswer(query,
+            Seq(
+              Row(1, 1, "a", -1, 0, -1),
+              Row(2, 2, "b", -2, 0, -2),
+              Row(3, 3, "c", -3, 0, -3)
+            )
+          )
+        }
+      }
+    }
+  }
+
+  test("SPARK-34555: Resolve DataFrame metadata column") {
+    withTable(tbl) {
+      prepareTable()
+      val table = spark.table(tbl)
+      val dfQuery = table.select(
+        table.col("id"),
+        table.col("data"),
+        table.col("index"),
+        table.col("_partition")
+      )
+
+      checkAnswer(
+        dfQuery,
+        Seq(Row(1, "a", 0, "3/1"), Row(2, "b", 0, "0/2"), Row(3, "c", 0, 
"1/3"))
+      )
+    }
+  }
+
+  test("SPARK-34923: propagate metadata columns through Project") {

Review Comment:
   It is kept, in 
https://github.com/apache/spark/pull/37758/files#diff-0d2dd492482083c6ff24c2572eacf9eeef3f01a180f97fec4dc4c5e08e9fbfb4R203



-- 
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: [email protected]

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