Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ statement
(LIKE? pattern=STRING)? #showTables
| SHOW TABLE EXTENDED ((FROM | IN) db=errorCapturingIdentifier)?
LIKE pattern=STRING partitionSpec? #showTable
| SHOW TBLPROPERTIES table=tableIdentifier
| SHOW TBLPROPERTIES table=multipartIdentifier
('(' key=tablePropertyKey ')')? #showTblProperties
| SHOW COLUMNS (FROM | IN) table=multipartIdentifier
((FROM | IN) namespace=multipartIdentifier)? #showColumns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@ class Analyzer(
.map(rel => alter.copy(table = rel))
.getOrElse(alter)

case show @ ShowTableProperties(u: UnresolvedV2Relation, _) =>
CatalogV2Util.loadRelation(u.catalog, u.tableName)
.map(rel => show.copy(table = rel))
.getOrElse(show)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it '.getOrElse(u)' instead of '.getOrElse(show)' here ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, yes good catch!!

case u: UnresolvedV2Relation =>
CatalogV2Util.loadRelation(u.catalog, u.tableName).getOrElse(u)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ class ResolveCatalogs(val catalogManager: CatalogManager)

case ShowCurrentNamespaceStatement() =>
ShowCurrentNamespace(catalogManager)

case ShowTablePropertiesStatement(
nameParts @ NonSessionCatalog(catalog, tableName), propertyKey) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 4 space indentation

val r = UnresolvedV2Relation(nameParts, catalog.asTableCatalog, tableName.asIdentifier)
ShowTableProperties(r, propertyKey)
}

object NonSessionCatalog {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3193,4 +3193,20 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
originalText = source(ctx.query),
query = plan(ctx.query))
}

/**
* A command for users to list the properties for a table. If propertyKey is specified, the value
* for the propertyKey is returned. If propertyKey is not specified, all the keys and their
* corresponding values are returned.
* The syntax of using this command in SQL is:
* {{{
* SHOW TBLPROPERTIES multi_part_name[('propertyKey')];
* }}}
*/
override def visitShowTblProperties(
ctx: ShowTblPropertiesContext): LogicalPlan = withOrigin(ctx) {
ShowTablePropertiesStatement(
visitMultipartIdentifier(ctx.table),
Option(ctx.key).map(visitTablePropertyKey))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,10 @@ case class ShowColumnsStatement(
* A SHOW CURRENT NAMESPACE statement, as parsed from SQL
*/
case class ShowCurrentNamespaceStatement() extends ParsedStatement

/**
* A SHOW TBLPROPERTIES statement, as parsed from SQL
*/
case class ShowTablePropertiesStatement(
tableName: Seq[String],
propertyKey: Option[String]) extends ParsedStatement
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,14 @@ case class ShowCurrentNamespace(catalogManager: CatalogManager) extends Command
AttributeReference("catalog", StringType, nullable = false)(),
AttributeReference("namespace", StringType, nullable = false)())
}

/**
* The logical plan of the SHOW TBLPROPERTIES command that works for v2 catalogs.
*/
case class ShowTableProperties(
table: NamedRelation,
propertyKey: Option[String]) extends Command{
override val output: Seq[Attribute] = Seq(
AttributeReference("key", StringType, nullable = false)(),
AttributeReference("value", StringType, nullable = false)())
}
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,16 @@ class DDLParserSuite extends AnalysisTest {
comparePlans(parsed, expected)
}

test("SHOW TBLPROPERTIES table") {
comparePlans(
parsePlan("SHOW TBLPROPERTIES a.b.c"),
ShowTablePropertiesStatement(Seq("a", "b", "c"), None))

comparePlans(
parsePlan("SHOW TBLPROPERTIES a.b.c('propKey1')"),
ShowTablePropertiesStatement(Seq("a", "b", "c"), Some("propKey1")))
}

private case class TableSpec(
name: Seq[String],
schema: Option[StructType],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ class ResolveSessionCatalog(
v1TableName.asTableIdentifier,
originalText,
query)

case ShowTablePropertiesStatement(SessionCatalog(_, tableName), propertyKey) =>
ShowTablePropertiesCommand(
tableName.asTableIdentifier,
propertyKey)
}

private def parseV1Table(tableName: Seq[String], sql: String): Seq[String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,6 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder(conf) {
partitionSpec = partitionSpec)
}

/**
* A command for users to list the properties for a table. If propertyKey is specified, the value
* for the propertyKey is returned. If propertyKey is not specified, all the keys and their
* corresponding values are returned.
* The syntax of using this command in SQL is:
* {{{
* SHOW TBLPROPERTIES table_name[('propertyKey')];
* }}}
*/
override def visitShowTblProperties(
ctx: ShowTblPropertiesContext): LogicalPlan = withOrigin(ctx) {
ShowTablePropertiesCommand(
visitTableIdentifier(ctx.tableIdentifier),
Option(ctx.key).map(visitTablePropertyKey))
}

/**
* Create a [[RefreshResource]] logical plan.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import scala.collection.JavaConverters._
import org.apache.spark.sql.{AnalysisException, Strategy}
import org.apache.spark.sql.catalyst.expressions.{And, PredicateHelper, SubqueryExpression}
import org.apache.spark.sql.catalyst.planning.PhysicalOperation
import org.apache.spark.sql.catalyst.plans.logical.{AlterTable, AppendData, CreateNamespace, CreateTableAsSelect, CreateV2Table, DeleteFromTable, DescribeTable, DropNamespace, DropTable, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic, RefreshTable, Repartition, ReplaceTable, ReplaceTableAsSelect, SetCatalogAndNamespace, ShowCurrentNamespace, ShowNamespaces, ShowTables}
import org.apache.spark.sql.catalyst.plans.logical.{AlterTable, AppendData, CreateNamespace, CreateTableAsSelect, CreateV2Table, DeleteFromTable, DescribeTable, DropNamespace, DropTable, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic, RefreshTable, Repartition, ReplaceTable, ReplaceTableAsSelect, SetCatalogAndNamespace, ShowCurrentNamespace, ShowNamespaces, ShowTableProperties, ShowTables}
import org.apache.spark.sql.connector.catalog.{StagingTableCatalog, TableCapability}
import org.apache.spark.sql.connector.read.streaming.{ContinuousStream, MicroBatchStream}
import org.apache.spark.sql.execution.{FilterExec, ProjectExec, SparkPlan}
Expand Down Expand Up @@ -213,6 +213,9 @@ object DataSourceV2Strategy extends Strategy with PredicateHelper {
case r: ShowCurrentNamespace =>
ShowCurrentNamespaceExec(r.output, r.catalogManager) :: Nil

case r @ ShowTableProperties(DataSourceV2Relation(table, _, _), propertyKey) =>
ShowTablePropertiesExec(r.output, table, propertyKey) :: Nil

case _ => Nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.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

/**
* Physical plan node for showing table properties.
*/
case class ShowTablePropertiesExec(
output: Seq[Attribute],
catalogTable: Table,
propertyKey: Option[String]) extends V2CommandExec {

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

val properties = catalogTable.properties.asScala
propertyKey match {
case Some(p) =>
val propValue = properties
.getOrElse(p, s"Table ${catalogTable.name} does not have property: $p")
Seq(encoder.toRow(new GenericRowWithSchema(Array(p, propValue), schema)).copy())
case None =>
properties.keys.map(k =>
encoder.toRow(new GenericRowWithSchema(Array(k, properties(k)), schema)).copy()).toSeq
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,63 @@ class DataSourceV2SQLSuite
assert(e.message.contains("ALTER VIEW QUERY is only supported with v1 tables"))
}

test("SHOW TBLPROPERTIES: v2 table") {
val t = "testcat.ns1.ns2.tbl"
withTable(t) {
val owner = "andrew"
val status = "new"
val provider = "foo"
spark.sql(s"CREATE TABLE $t (id bigint, data string) USING $provider " +
s"TBLPROPERTIES ('owner'='$owner', 'status'='$status')")

val properties = sql(s"SHOW TBLPROPERTIES $t")

val schema = new StructType()
.add("key", StringType, nullable = false)
.add("value", StringType, nullable = false)

val expected = Seq(
Row("owner", owner),
Row("provider", provider),
Row("status", status))

assert(properties.schema === schema)
assert(expected === properties.collect())
}
}

test("SHOW TBLPROPERTIES(key): v2 table") {
val t = "testcat.ns1.ns2.tbl"
withTable(t) {
val owner = "andrew"
val status = "new"
val provider = "foo"
spark.sql(s"CREATE TABLE $t (id bigint, data string) USING $provider " +
s"TBLPROPERTIES ('owner'='$owner', 'status'='$status')")

val properties = sql(s"SHOW TBLPROPERTIES $t ('status')")

val expected = Seq(Row("status", status))

assert(expected === properties.collect())
}
}

test("SHOW TBLPROPERTIES(key): v2 table, key not found") {
val t = "testcat.ns1.ns2.tbl"
withTable(t) {
val nonExistingKey = "nonExistingKey"
spark.sql(s"CREATE TABLE $t (id bigint, data string) USING foo " +
s"TBLPROPERTIES ('owner'='andrew', 'status'='new')")

val properties = sql(s"SHOW TBLPROPERTIES $t ('$nonExistingKey')")

val expected = Seq(Row(nonExistingKey, s"Table $t does not have property: $nonExistingKey"))

assert(expected === properties.collect())
}
}

private def testV1Command(sqlCommand: String, sqlParams: String): Unit = {
val e = intercept[AnalysisException] {
sql(s"$sqlCommand $sqlParams")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,6 @@ class DDLParserSuite extends AnalysisTest with SharedSparkSession {
""".stripMargin)
}

test("show tblproperties") {
val parsed1 = parser.parsePlan("SHOW TBLPROPERTIES tab1")
val expected1 = ShowTablePropertiesCommand(TableIdentifier("tab1", None), None)
val parsed2 = parser.parsePlan("SHOW TBLPROPERTIES tab1('propKey1')")
val expected2 = ShowTablePropertiesCommand(TableIdentifier("tab1", None), Some("propKey1"))
comparePlans(parsed1, expected1)
comparePlans(parsed2, expected2)
}

test("SPARK-14383: DISTRIBUTE and UNSET as non-keywords") {
val sql = "SELECT distribute, unset FROM x"
val parsed = parser.parsePlan(sql)
Expand Down