Skip to content

Commit

Permalink
[SPARK-31624] Fix SHOW TBLPROPERTIES for V2 tables that leverage the …
Browse files Browse the repository at this point in the history
…session catalog

## What changes were proposed in this pull request?

SHOW TBLPROPERTIES does not get the correct table properties for tables using the Session Catalog. This PR fixes that, by explicitly falling back to the V1 implementation if the table is in fact a V1 table. We also hide the reserved table properties for V2 tables, as users do not have control over setting these table properties. Henceforth, if they cannot be set or controlled by the user, then they shouldn't be displayed as such.

### Why are the changes needed?

Shows the incorrect table properties, i.e. only what exists in the Hive MetaStore for V2 tables that may have table properties outside of the MetaStore.

### Does this PR introduce _any_ user-facing change?

Fixes a bug

### How was this patch tested?

Regression test

Closes apache#28434 from brkyvz/ddlCommands.

Authored-by: Burak Yavuz <brkyvz@gmail.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
  • Loading branch information
brkyvz authored and huaxingao committed May 4, 2020
1 parent ffd69c6 commit 78758b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
Expand Up @@ -559,7 +559,8 @@ class ResolveSessionCatalog(
"SHOW VIEWS, only SessionCatalog supports this command.")
}

case ShowTableProperties(r: ResolvedTable, propertyKey) if isSessionCatalog(r.catalog) =>
case ShowTableProperties(
r @ ResolvedTable(_, _, _: V1Table), propertyKey) if isSessionCatalog(r.catalog) =>
ShowTablePropertiesCommand(r.identifier.asTableIdentifier, propertyKey)

case ShowTableProperties(r: ResolvedView, propertyKey) =>
Expand Down
Expand Up @@ -19,8 +19,8 @@ package org.apache.spark.sql.execution.datasources.v2

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.encoders.RowEncoder
import org.apache.spark.sql.catalyst.expressions.{Attribute, GenericRowWithSchema}
import org.apache.spark.sql.connector.catalog.Table
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, GenericRowWithSchema}
import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog}

/**
* Physical plan node for showing table properties.
Expand All @@ -30,11 +30,15 @@ case class ShowTablePropertiesExec(
catalogTable: Table,
propertyKey: Option[String]) extends V2CommandExec {

override def producedAttributes: AttributeSet = AttributeSet(output)

override protected def run(): Seq[InternalRow] = {
import scala.collection.JavaConverters._
val toRow = RowEncoder(schema).resolveAndBind().createSerializer()

// The reservered properties are accessible through DESCRIBE
val properties = catalogTable.properties.asScala
.filter { case (k, v) => !CatalogV2Util.TABLE_RESERVED_PROPERTIES.contains(k) }
propertyKey match {
case Some(p) =>
val propValue = properties
Expand Down
Expand Up @@ -17,7 +17,7 @@

package org.apache.spark.sql.connector

import org.apache.spark.sql.{DataFrame, SaveMode}
import org.apache.spark.sql.{DataFrame, Row, SaveMode}
import org.apache.spark.sql.connector.catalog.{Identifier, Table, TableCatalog}

class DataSourceV2SQLSessionCatalogSuite
Expand Down Expand Up @@ -63,4 +63,20 @@ class DataSourceV2SQLSessionCatalogSuite
}
}
}

test("SPARK-31624: SHOW TBLPROPERTIES working with V2 tables and the session catalog") {
val t1 = "tbl"
withTable(t1) {
sql(s"CREATE TABLE $t1 (id bigint, data string) USING $v2Format TBLPROPERTIES " +
"(key='v', key2='v2')")

checkAnswer(sql(s"SHOW TBLPROPERTIES $t1"), Seq(Row("key", "v"), Row("key2", "v2")))

checkAnswer(sql(s"SHOW TBLPROPERTIES $t1('key')"), Row("key", "v"))

checkAnswer(
sql(s"SHOW TBLPROPERTIES $t1('keyX')"),
Row("keyX", s"Table default.$t1 does not have property: keyX"))
}
}
}
Expand Up @@ -2122,8 +2122,6 @@ class DataSourceV2SQLSuite
.add("value", StringType, nullable = false)

val expected = Seq(
Row(TableCatalog.PROP_OWNER, defaultUser),
Row("provider", provider),
Row("status", status),
Row("user", user))

Expand Down

0 comments on commit 78758b6

Please sign in to comment.