-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-39552][SQL] Unify v1 and v2 DESCRIBE TABLE
#36946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ecec69b
4ffa242
c17b346
2224caa
227b99f
699e72e
c75e090
39ed298
d95ff9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,8 +21,11 @@ import scala.collection.JavaConverters._ | |
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTableType | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.connector.catalog.{CatalogV2Util, SupportsMetadataColumns, Table} | ||
| import org.apache.spark.sql.catalyst.util.quoteIfNeeded | ||
| import org.apache.spark.sql.connector.catalog.{CatalogV2Util, SupportsMetadataColumns, Table, TableCatalog} | ||
| import org.apache.spark.sql.connector.expressions.IdentityTransform | ||
|
|
||
| case class DescribeTableExec( | ||
| output: Seq[Attribute], | ||
|
|
@@ -45,11 +48,19 @@ case class DescribeTableExec( | |
| rows += toCatalystRow("# Detailed Table Information", "", "") | ||
| rows += toCatalystRow("Name", table.name(), "") | ||
|
|
||
| CatalogV2Util.TABLE_RESERVED_PROPERTIES.foreach(propKey => { | ||
| if (table.properties.containsKey(propKey)) { | ||
| rows += toCatalystRow(propKey.capitalize, table.properties.get(propKey), "") | ||
| } | ||
| }) | ||
| val tableType = if (table.properties().containsKey(TableCatalog.PROP_EXTERNAL)) { | ||
| CatalogTableType.EXTERNAL.name | ||
| } else { | ||
| CatalogTableType.MANAGED.name | ||
| } | ||
| rows += toCatalystRow("Type", tableType, "") | ||
| CatalogV2Util.TABLE_RESERVED_PROPERTIES | ||
| .filterNot(_ == TableCatalog.PROP_EXTERNAL) | ||
| .foreach(propKey => { | ||
| if (table.properties.containsKey(propKey)) { | ||
| rows += toCatalystRow(propKey.capitalize, table.properties.get(propKey), "") | ||
| } | ||
| }) | ||
| val properties = | ||
| conf.redactOptions(table.properties.asScala.toMap).toList | ||
| .filter(kv => !CatalogV2Util.TABLE_RESERVED_PROPERTIES.contains(kv._1)) | ||
|
|
@@ -62,7 +73,7 @@ case class DescribeTableExec( | |
| private def addSchema(rows: ArrayBuffer[InternalRow]): Unit = { | ||
| rows ++= table.schema.map{ column => | ||
| toCatalystRow( | ||
| column.name, column.dataType.simpleString, column.getComment().getOrElse("")) | ||
| column.name, column.dataType.simpleString, column.getComment().orNull) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -80,13 +91,31 @@ case class DescribeTableExec( | |
| } | ||
|
|
||
| private def addPartitioning(rows: ArrayBuffer[InternalRow]): Unit = { | ||
| rows += emptyRow() | ||
| rows += toCatalystRow("# Partitioning", "", "") | ||
| if (table.partitioning.isEmpty) { | ||
| rows += toCatalystRow("Not partitioned", "", "") | ||
| } else { | ||
| rows ++= table.partitioning.zipWithIndex.map { | ||
| case (transform, index) => toCatalystRow(s"Part $index", transform.describe(), "") | ||
| if (table.partitioning.nonEmpty) { | ||
| val partitionColumnsOnly = table.partitioning.forall(t => t.isInstanceOf[IdentityTransform]) | ||
| if (partitionColumnsOnly) { | ||
| rows += toCatalystRow("# Partition Information", "", "") | ||
| rows += toCatalystRow(s"# ${output(0).name}", output(1).name, output(2).name) | ||
| rows ++= table.partitioning | ||
| .map(_.asInstanceOf[IdentityTransform].ref.fieldNames()) | ||
| .map { fieldNames => | ||
| val nestedField = table.schema.findNestedField(fieldNames) | ||
| assert(nestedField.isDefined, | ||
| s"Not found the partition column ${fieldNames.map(quoteIfNeeded).mkString(".")} " + | ||
| s"in the table schema ${table.schema().catalogString}.") | ||
| nestedField.get | ||
| }.map { case (path, field) => | ||
| toCatalystRow( | ||
| (path :+ field.name).map(quoteIfNeeded(_)).mkString("."), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| field.dataType.simpleString, | ||
| field.getComment().orNull) | ||
| } | ||
| } else { | ||
| rows += emptyRow() | ||
| rows += toCatalystRow("# Partitioning", "", "") | ||
| rows ++= table.partitioning.zipWithIndex.map { | ||
| case (transform, index) => toCatalystRow(s"Part $index", transform.describe(), "") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,26 +28,6 @@ import org.apache.spark.util.Utils | |
| */ | ||
| class DescribeTableSuite extends command.DescribeTableSuiteBase with CommandSuiteBase { | ||
|
|
||
| test("DESCRIBE TABLE with non-'partitioned-by' clause") { | ||
| withNamespaceAndTable("ns", "table") { tbl => | ||
| spark.sql(s"CREATE TABLE $tbl (id bigint, data string) $defaultUsing") | ||
| val descriptionDf = spark.sql(s"DESCRIBE TABLE $tbl") | ||
| assert(descriptionDf.schema.map(field => (field.name, field.dataType)) === | ||
| Seq( | ||
| ("col_name", StringType), | ||
| ("data_type", StringType), | ||
| ("comment", StringType))) | ||
| QueryTest.checkAnswer( | ||
| descriptionDf, | ||
| Seq( | ||
| Row("data", "string", ""), | ||
| Row("id", "bigint", ""), | ||
| Row("", "", ""), | ||
| Row("# Partitioning", "", ""), | ||
| Row("Not partitioned", "", ""))) | ||
| } | ||
| } | ||
|
|
||
| test("Describing a partition is not supported") { | ||
| withNamespaceAndTable("ns", "table") { tbl => | ||
| spark.sql(s"CREATE TABLE $tbl (id bigint, data string) $defaultUsing " + | ||
|
|
@@ -59,6 +39,23 @@ class DescribeTableSuite extends command.DescribeTableSuiteBase with CommandSuit | |
| } | ||
| } | ||
|
|
||
| test("DESCRIBE TABLE of a partitioned table by nested columns") { | ||
| withNamespaceAndTable("ns", "table") { tbl => | ||
| sql(s"CREATE TABLE $tbl (s struct<id:INT, a:BIGINT>, data string) " + | ||
| s"$defaultUsing PARTITIONED BY (s.id, s.a)") | ||
| val descriptionDf = sql(s"DESCRIBE TABLE $tbl") | ||
| QueryTest.checkAnswer( | ||
| descriptionDf.filter("col_name != 'Created Time'"), | ||
| Seq( | ||
| Row("data", "string", null), | ||
| Row("s", "struct<id:int,a:bigint>", null), | ||
| Row("# Partition Information", "", ""), | ||
| Row("# col_name", "data_type", "comment"), | ||
| Row("s.id", "int", null), | ||
| Row("s.a", "bigint", null))) | ||
| } | ||
| } | ||
|
|
||
| test("DESCRIBE TABLE EXTENDED of a partitioned table") { | ||
| withNamespaceAndTable("ns", "table") { tbl => | ||
| spark.sql(s"CREATE TABLE $tbl (id bigint, data string) $defaultUsing" + | ||
|
|
@@ -74,18 +71,19 @@ class DescribeTableSuite extends command.DescribeTableSuiteBase with CommandSuit | |
| QueryTest.checkAnswer( | ||
| descriptionDf, | ||
| Seq( | ||
| Row("id", "bigint", ""), | ||
| Row("data", "string", ""), | ||
| Row("", "", ""), | ||
| Row("# Partitioning", "", ""), | ||
| Row("Part 0", "id", ""), | ||
| Row("id", "bigint", null), | ||
| Row("data", "string", null), | ||
| Row("# Partition Information", "", ""), | ||
| Row("# col_name", "data_type", "comment"), | ||
| Row("id", "bigint", null), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for curiosity: what's different between v1 and v2 DESC TABLE for this test
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v2 (after the PR): v1 in memory:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v1 (hive):
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, can we at least include
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| Row("", "", ""), | ||
| Row("# Metadata Columns", "", ""), | ||
| Row("index", "int", "Metadata column used to conflict with a data column"), | ||
| Row("_partition", "string", "Partition key used to store the row"), | ||
| Row("", "", ""), | ||
| Row("# Detailed Table Information", "", ""), | ||
| Row("Name", tbl, ""), | ||
| Row("Type", "MANAGED", ""), | ||
| Row("Comment", "this is a test table", ""), | ||
| Row("Location", "file:/tmp/testcat/table_name", ""), | ||
| Row("Provider", "_", ""), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we can reuse
MultipartIdentifierHelper.quoted