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 @@ -413,38 +413,36 @@ case class DescribeTableCommand(table: TableIdentifier, isExtended: Boolean, isF
} else {
val metadata = catalog.getTableMetadata(table)

if (DDLUtils.isDatasourceTable(metadata)) {
DDLUtils.getSchemaFromTableProperties(metadata) match {
case Some(userSpecifiedSchema) => describeSchema(userSpecifiedSchema, result)
case None => describeSchema(catalog.lookupRelation(table).schema, result)
}
} else {
describeSchema(metadata.schema, result)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

How about we try to put these into describeSchema? Of, maybe we can add a describeSchema(tableName, result)? Seems it is weird that describeExtended and describeFormatted do not contain the code for describing the schema.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. Let me do it now

BTW, previously, describeExtended and describeFormatted also contain the schema. Both call the original function describe.

Copy link
Member Author

Choose a reason for hiding this comment

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

@yhuai I just did a try. We have to pass CatalogTable for avoiding another call of getTableMetadata. We also need to pass SessionCatalog for calling lookupRelation. Do you like this function? or keep the existing one? Thanks!

  private def describeSchema(
      tableDesc: CatalogTable,
      catalog: SessionCatalog,
      buffer: ArrayBuffer[Row]): Unit = {
    if (DDLUtils.isDatasourceTable(tableDesc)) {
      DDLUtils.getSchemaFromTableProperties(tableDesc) match {
        case Some(userSpecifiedSchema) => describeSchema(userSpecifiedSchema, buffer)
        case None => describeSchema(catalog.lookupRelation(table).schema, buffer)
      }
    } else {
      describeSchema(tableDesc.schema, buffer)
    }
  }


if (isExtended) {
describeExtended(metadata, result)
} else if (isFormatted) {
describeFormatted(metadata, result)
} else {
describe(metadata, result)
describePartitionInfo(metadata, result)
}
}

result
}

// Shows data columns and partitioned columns (if any)
private def describe(table: CatalogTable, buffer: ArrayBuffer[Row]): Unit = {
private def describePartitionInfo(table: CatalogTable, buffer: ArrayBuffer[Row]): Unit = {
if (DDLUtils.isDatasourceTable(table)) {
val schema = DDLUtils.getSchemaFromTableProperties(table)

if (schema.isEmpty) {
append(buffer, "# Schema of this table is inferred at runtime", "", "")
} else {
schema.foreach(describeSchema(_, buffer))
}

val partCols = DDLUtils.getPartitionColumnsFromTableProperties(table)
if (partCols.nonEmpty) {
append(buffer, "# Partition Information", "", "")
append(buffer, s"# ${output.head.name}", "", "")
partCols.foreach(col => append(buffer, col, "", ""))
}
} else {
describeSchema(table.schema, buffer)

if (table.partitionColumns.nonEmpty) {
append(buffer, "# Partition Information", "", "")
append(buffer, s"# ${output.head.name}", output(1).name, output(2).name)
Expand All @@ -454,14 +452,14 @@ case class DescribeTableCommand(table: TableIdentifier, isExtended: Boolean, isF
}

private def describeExtended(table: CatalogTable, buffer: ArrayBuffer[Row]): Unit = {
describe(table, buffer)
describePartitionInfo(table, buffer)

append(buffer, "", "", "")
append(buffer, "# Detailed Table Information", table.toString, "")
}

private def describeFormatted(table: CatalogTable, buffer: ArrayBuffer[Row]): Unit = {
describe(table, buffer)
describePartitionInfo(table, buffer)

append(buffer, "", "", "")
append(buffer, "# Detailed Table Information", "", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,15 +610,17 @@ class HiveDDLSuite
}

test("desc table for data source table - no user-defined schema") {
withTable("t1") {
withTempPath { dir =>
val path = dir.getCanonicalPath
spark.range(1).write.parquet(path)
sql(s"CREATE TABLE t1 USING parquet OPTIONS (PATH '$path')")
Seq("parquet", "json", "orc").foreach { fileFormat =>
withTable("t1") {
withTempPath { dir =>
val path = dir.getCanonicalPath
spark.range(1).write.format(fileFormat).save(path)
sql(s"CREATE TABLE t1 USING $fileFormat OPTIONS (PATH '$path')")

val desc = sql("DESC FORMATTED t1").collect().toSeq
val desc = sql("DESC FORMATTED t1").collect().toSeq

assert(desc.contains(Row("# Schema of this table is inferred at runtime", "", "")))
assert(desc.contains(Row("id", "bigint", "")))
}
}
}
}
Expand Down