Skip to content

Commit e13d47a

Browse files
yaooqinncloud-fan
authored andcommitted
[SPARK-33892][SQL] Display char/varchar in DESC and SHOW CREATE TABLE
### What changes were proposed in this pull request? Display char/varchar in - DESC table - DESC column - SHOW CREATE TABLE ### Why are the changes needed? show the correct definition for users ### Does this PR introduce _any_ user-facing change? yes, char/varchar column's will print char/varchar instead of string ### How was this patch tested? new tests Closes #30908 from yaooqinn/SPARK-33892. Authored-by: Kent Yao <[email protected]> Signed-off-by: Wenchen Fan <[email protected]> (cherry picked from commit 29cca68) Signed-off-by: Wenchen Fan <[email protected]>
1 parent 8da8e60 commit e13d47a

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
3535
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference}
3636
import org.apache.spark.sql.catalyst.plans.DescribeCommandSchema
3737
import org.apache.spark.sql.catalyst.plans.logical._
38-
import org.apache.spark.sql.catalyst.util.{escapeSingleQuotedString, quoteIdentifier, CaseInsensitiveMap}
38+
import org.apache.spark.sql.catalyst.util.{escapeSingleQuotedString, quoteIdentifier, CaseInsensitiveMap, CharVarcharUtils}
3939
import org.apache.spark.sql.execution.datasources.DataSource
4040
import org.apache.spark.sql.execution.datasources.csv.CSVFileFormat
4141
import org.apache.spark.sql.execution.datasources.json.JsonFileFormat
@@ -638,7 +638,7 @@ case class DescribeTableCommand(
638638
}
639639
describeSchema(catalog.lookupRelation(table).schema, result, header = false)
640640
} else {
641-
val metadata = catalog.getTableMetadata(table)
641+
val metadata = catalog.getTableRawMetadata(table)
642642
if (metadata.schema.isEmpty) {
643643
// In older version(prior to 2.1) of Spark, the table schema can be empty and should be
644644
// inferred at runtime. We should still support it.
@@ -789,9 +789,11 @@ case class DescribeColumnCommand(
789789
None
790790
}
791791

792+
val dataType = CharVarcharUtils.getRawType(field.metadata)
793+
.getOrElse(field.dataType).catalogString
792794
val buffer = ArrayBuffer[Row](
793795
Row("col_name", field.name),
794-
Row("data_type", field.dataType.catalogString),
796+
Row("data_type", dataType),
795797
Row("comment", comment.getOrElse("NULL"))
796798
)
797799
if (isExtended) {
@@ -1122,7 +1124,7 @@ case class ShowCreateTableCommand(table: TableIdentifier)
11221124
throw new AnalysisException(
11231125
s"SHOW CREATE TABLE is not supported on a temporary view: ${table.identifier}")
11241126
} else {
1125-
val tableMetadata = catalog.getTableMetadata(table)
1127+
val tableMetadata = catalog.getTableRawMetadata(table)
11261128

11271129
// TODO: [SPARK-28692] unify this after we unify the
11281130
// CREATE TABLE syntax for hive serde and data source table.
@@ -1273,7 +1275,7 @@ case class ShowCreateTableAsSerdeCommand(table: TableIdentifier)
12731275

12741276
override def run(sparkSession: SparkSession): Seq[Row] = {
12751277
val catalog = sparkSession.sessionState.catalog
1276-
val tableMetadata = catalog.getTableMetadata(table)
1278+
val tableMetadata = catalog.getTableRawMetadata(table)
12771279

12781280
val stmt = if (DDLUtils.isDatasourceTable(tableMetadata)) {
12791281
throw new AnalysisException(

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowTablePropertiesExec.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package org.apache.spark.sql.execution.datasources.v2
1919

2020
import org.apache.spark.sql.catalyst.InternalRow
2121
import org.apache.spark.sql.catalyst.encoders.RowEncoder
22-
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, GenericRowWithSchema}
22+
import org.apache.spark.sql.catalyst.expressions.{Attribute, GenericRowWithSchema}
2323
import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table}
2424

2525
/**
@@ -30,8 +30,6 @@ case class ShowTablePropertiesExec(
3030
catalogTable: Table,
3131
propertyKey: Option[String]) extends V2CommandExec {
3232

33-
override def producedAttributes: AttributeSet = AttributeSet(output)
34-
3533
override protected def run(): Seq[InternalRow] = {
3634
import scala.collection.JavaConverters._
3735
val toRow = RowEncoder(schema).resolveAndBind().createSerializer()

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2CommandExec.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.apache.spark.sql.execution.datasources.v2
1919

2020
import org.apache.spark.rdd.RDD
2121
import org.apache.spark.sql.catalyst.InternalRow
22+
import org.apache.spark.sql.catalyst.expressions.AttributeSet
2223
import org.apache.spark.sql.execution.SparkPlan
2324

2425
/**
@@ -55,4 +56,7 @@ abstract class V2CommandExec extends SparkPlan {
5556
}
5657

5758
override def children: Seq[SparkPlan] = Nil
59+
60+
override def producedAttributes: AttributeSet = outputSet
61+
5862
}

sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,14 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
443443
("c1 IN (c2)", true)))
444444
}
445445
}
446+
447+
test("SPARK-33892: DESCRIBE TABLE w/ char/varchar") {
448+
withTable("t") {
449+
sql(s"CREATE TABLE t(v VARCHAR(3), c CHAR(5)) USING $format")
450+
checkAnswer(sql("desc t").selectExpr("data_type").where("data_type like '%char%'"),
451+
Seq(Row("char(5)"), Row("varchar(3)")))
452+
}
453+
}
446454
}
447455

448456
// Some basic char/varchar tests which doesn't rely on table implementation.
@@ -603,6 +611,27 @@ class FileSourceCharVarcharTestSuite extends CharVarcharTestSuite with SharedSpa
603611
}
604612
}
605613
}
614+
615+
// TODO(SPARK-33875): Move these tests to super after DESCRIBE COLUMN v2 implemented
616+
test("SPARK-33892: DESCRIBE COLUMN w/ char/varchar") {
617+
withTable("t") {
618+
sql(s"CREATE TABLE t(v VARCHAR(3), c CHAR(5)) USING $format")
619+
checkAnswer(sql("desc t v").selectExpr("info_value").where("info_value like '%char%'"),
620+
Row("varchar(3)"))
621+
checkAnswer(sql("desc t c").selectExpr("info_value").where("info_value like '%char%'"),
622+
Row("char(5)"))
623+
}
624+
}
625+
626+
// TODO(SPARK-33898): Move these tests to super after SHOW CREATE TABLE for v2 implemented
627+
test("SPARK-33892: SHOW CREATE TABLE w/ char/varchar") {
628+
withTable("t") {
629+
sql(s"CREATE TABLE t(v VARCHAR(3), c CHAR(5)) USING $format")
630+
val rest = sql("SHOW CREATE TABLE t").head().getString(0)
631+
assert(rest.contains("VARCHAR(3)"))
632+
assert(rest.contains("CHAR(5)"))
633+
}
634+
}
606635
}
607636

608637
class DSV2CharVarcharTestSuite extends CharVarcharTestSuite

sql/hive/src/test/scala/org/apache/spark/sql/HiveCharVarcharTestSuite.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ class HiveCharVarcharTestSuite extends CharVarcharTestSuite with TestHiveSinglet
4141
}
4242
super.afterAll()
4343
}
44+
45+
test("SPARK-33892: SHOW CREATE TABLE AS SERDE w/ char/varchar") {
46+
withTable("t") {
47+
sql(s"CREATE TABLE t(v VARCHAR(3), c CHAR(5)) USING $format")
48+
val rest = sql("SHOW CREATE TABLE t AS SERDE").head().getString(0)
49+
assert(rest.contains("VARCHAR(3)"))
50+
assert(rest.contains("CHAR(5)"))
51+
}
52+
}
4453
}
4554

4655
class HiveCharVarcharDDLTestSuite extends CharVarcharDDLTestBase with TestHiveSingleton {

0 commit comments

Comments
 (0)