Skip to content

Commit 6bc1614

Browse files
committed
use string
1 parent 423796f commit 6bc1614

File tree

14 files changed

+69
-56
lines changed

14 files changed

+69
-56
lines changed

python/pyspark/sql/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def tables(self, dbName=None):
478478
>>> sqlContext.registerDataFrameAsTable(df, "table1")
479479
>>> df2 = sqlContext.tables()
480480
>>> df2.filter("tableName = 'table1'").first()
481-
Row(namespace='', tableName='table1', isTemporary=True)
481+
Row(namespace='', tableName='table1', isTemporary=True, tableType='TABLE')
482482
"""
483483
if dbName is None:
484484
return DataFrame(self._ssql_ctx.tables(), self)

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ object CatalogTableType {
742742
val VIEW = new CatalogTableType("VIEW")
743743

744744
val tableTypes = Seq(EXTERNAL, MANAGED, VIEW)
745+
746+
def classicTableTypeString(tableType: CatalogTableType): String = tableType match {
747+
case EXTERNAL | MANAGED => "TABLE"
748+
case VIEW => "VIEW"
749+
case t =>
750+
throw new IllegalArgumentException(s"Unknown table type is found: $t")
751+
}
745752
}
746753

747754

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2Commands.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,21 @@ case class ShowTables(
510510
override def children: Seq[LogicalPlan] = Seq(namespace)
511511
}
512512

513-
object ShowTables {
513+
trait ShowTablesLegacyHelper {
514+
def getOutputAttrs: Seq[Attribute]
515+
516+
def getLegacyOutputAttrs: Seq[Attribute] = {
517+
val output = getOutputAttrs
518+
output.head.withName("database") +: output.slice(1, output.length - 1)
519+
}
520+
}
521+
522+
object ShowTables extends ShowTablesLegacyHelper {
514523
def getOutputAttrs: Seq[Attribute] = Seq(
515524
AttributeReference("namespace", StringType, nullable = false)(),
516525
AttributeReference("tableName", StringType, nullable = false)(),
517526
AttributeReference("isTemporary", BooleanType, nullable = false)(),
518-
AttributeReference("isView", BooleanType, nullable = false)())
527+
AttributeReference("tableType", StringType, nullable = false)())
519528
}
520529

521530
/**
@@ -529,13 +538,13 @@ case class ShowTableExtended(
529538
override def children: Seq[LogicalPlan] = namespace :: Nil
530539
}
531540

532-
object ShowTableExtended {
541+
object ShowTableExtended extends ShowTablesLegacyHelper {
533542
def getOutputAttrs: Seq[Attribute] = Seq(
534543
AttributeReference("namespace", StringType, nullable = false)(),
535544
AttributeReference("tableName", StringType, nullable = false)(),
536545
AttributeReference("isTemporary", BooleanType, nullable = false)(),
537546
AttributeReference("information", StringType, nullable = false)(),
538-
AttributeReference("isView", BooleanType, nullable = false)())
547+
AttributeReference("tableType", StringType, nullable = false)())
539548
}
540549

541550
/**

sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ class ResolveSessionCatalog(val catalogManager: CatalogManager)
354354
case ShowTables(DatabaseInSessionCatalog(db), pattern, output) =>
355355
val newOutput = if (conf.getConf(SQLConf.LEGACY_KEEP_COMMAND_OUTPUT_SCHEMA)) {
356356
assert(output.length == 4)
357-
output.head.withName("database") +: output.slice(1, 3)
357+
ShowTables.getLegacyOutputAttrs
358358
} else {
359359
output
360360
}
@@ -367,12 +367,12 @@ class ResolveSessionCatalog(val catalogManager: CatalogManager)
367367
output) =>
368368
val newOutput = if (conf.getConf(SQLConf.LEGACY_KEEP_COMMAND_OUTPUT_SCHEMA)) {
369369
assert(output.length == 5)
370-
output.head.withName("database") +: output.slice(1, 4)
370+
ShowTableExtended.getLegacyOutputAttrs
371371
} else {
372372
output
373373
}
374374
val tablePartitionSpec = partitionSpec.map(_.asInstanceOf[UnresolvedPartitionSpec].spec)
375-
ShowTablesCommand(Some(db), Some(pattern), newOutput, isExtended = true, tablePartitionSpec)
375+
ShowTablesCommand(Some(db), Some(pattern), newOutput, true, tablePartitionSpec)
376376

377377
// ANALYZE TABLE works on permanent views if the views are cached.
378378
case AnalyzeTable(ResolvedV1TableOrViewIdentifier(ident), partitionSpec, noScan) =>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,17 +844,17 @@ case class ShowTablesCommand(
844844
val tableName = tableIdent.table
845845
val isTemp = catalog.isTempView(tableIdent)
846846
val catalogTable = catalog.getTempViewOrPermanentTableMetadata(tableIdent)
847-
val isView = catalogTable.tableType == CatalogTableType.VIEW
847+
val tableType = classicTableTypeString(catalogTable.tableType)
848848
if (isExtended) {
849849
val information = catalogTable.simpleString
850850
if (output.size == 5) {
851-
Row(database, tableName, isTemp, s"$information\n", isView)
851+
Row(database, tableName, isTemp, s"$information\n", tableType)
852852
} else {
853853
Row(database, tableName, isTemp, s"$information\n")
854854
}
855855
} else {
856856
if (output.size == 4) {
857-
Row(database, tableName, isTemp, isView)
857+
Row(database, tableName, isTemp, tableType)
858858
} else {
859859
Row(database, tableName, isTemp)
860860
}
@@ -882,8 +882,8 @@ case class ShowTablesCommand(
882882
val isTemp = catalog.isTempView(tableIdent)
883883
val information = partition.simpleString
884884
if (output.size == 5) {
885-
val isView = table.tableType == CatalogTableType.VIEW
886-
Seq(Row(database, tableName, isTemp, s"$information\n", isView))
885+
val tableType = classicTableTypeString(table.tableType)
886+
Seq(Row(database, tableName, isTemp, s"$information\n", tableType))
887887
} else {
888888
Seq(Row(database, tableName, isTemp, s"$information\n"))
889889
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ case class ShowTablesExec(
4040
val tables = catalog.listTables(namespace.toArray)
4141
tables.map { table =>
4242
if (pattern.map(StringUtils.filterPattern(Seq(table.name()), _).nonEmpty).getOrElse(true)) {
43-
rows += toCatalystRow(table.namespace().quoted, table.name(), false, false)
43+
rows += toCatalystRow(table.namespace().quoted, table.name(), false, "TABLE")
4444
}
4545
}
4646

sql/core/src/test/resources/sql-tests/results/show-tables.sql.out

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct<>
6161
-- !query
6262
SHOW TABLES
6363
-- !query schema
64-
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
64+
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
6565
-- !query output
6666
show_t1
6767
show_t2
@@ -71,7 +71,7 @@ show_t3
7171
-- !query
7272
SHOW TABLES IN showdb
7373
-- !query schema
74-
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
74+
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
7575
-- !query output
7676
show_t1
7777
show_t2
@@ -81,7 +81,7 @@ show_t3
8181
-- !query
8282
SHOW TABLES 'show_t*'
8383
-- !query schema
84-
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
84+
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
8585
-- !query output
8686
show_t1
8787
show_t2
@@ -91,7 +91,7 @@ show_t3
9191
-- !query
9292
SHOW TABLES LIKE 'show_t1*|show_t2*'
9393
-- !query schema
94-
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
94+
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
9595
-- !query output
9696
show_t1
9797
show_t2
@@ -100,7 +100,7 @@ show_t2
100100
-- !query
101101
SHOW TABLES IN showdb 'show_t*'
102102
-- !query schema
103-
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
103+
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
104104
-- !query output
105105
show_t1
106106
show_t2
@@ -110,7 +110,7 @@ show_t3
110110
-- !query
111111
SHOW TABLES IN showdb LIKE 'show_t*'
112112
-- !query schema
113-
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
113+
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
114114
-- !query output
115115
show_t1
116116
show_t2
@@ -120,7 +120,7 @@ show_t3
120120
-- !query
121121
SHOW TABLE EXTENDED LIKE 'show_t*'
122122
-- !query schema
123-
struct<namespace:string,tableName:string,isTemporary:boolean,information:string,isView:boolean>
123+
struct<namespace:string,tableName:string,isTemporary:boolean,information:string,tableType:string>
124124
-- !query output
125125
show_t3 true Table: show_t3
126126
Created Time [not included in comparison]
@@ -130,7 +130,7 @@ Type: VIEW
130130
Schema: root
131131
|-- e: integer (nullable = true)
132132

133-
true
133+
VIEW
134134
showdb show_t1 false Database: showdb
135135
Table: show_t1
136136
Created Time [not included in comparison]
@@ -147,7 +147,7 @@ Schema: root
147147
|-- c: string (nullable = true)
148148
|-- d: string (nullable = true)
149149

150-
false
150+
TABLE
151151
showdb show_t2 false Database: showdb
152152
Table: show_t2
153153
Created Time [not included in comparison]
@@ -160,7 +160,7 @@ Schema: root
160160
|-- b: string (nullable = true)
161161
|-- d: integer (nullable = true)
162162

163-
false
163+
TABLE
164164

165165

166166
-- !query
@@ -180,13 +180,13 @@ SHOW TABLE EXTENDED
180180
-- !query
181181
SHOW TABLE EXTENDED LIKE 'show_t1' PARTITION(c='Us', d=1)
182182
-- !query schema
183-
struct<namespace:string,tableName:string,isTemporary:boolean,information:string,isView:boolean>
183+
struct<namespace:string,tableName:string,isTemporary:boolean,information:string,tableType:string>
184184
-- !query output
185185
showdb show_t1 false Partition Values: [c=Us, d=1]
186186
Location [not included in comparison]/{warehouse_dir}/showdb.db/show_t1/c=Us/d=1
187187
Created Time [not included in comparison]
188188
Last Access [not included in comparison]
189-
false
189+
TABLE
190190

191191

192192
-- !query

sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuiteBase.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {
4444
test("show an existing table") {
4545
withNamespaceAndTable("ns", "table") { t =>
4646
sql(s"CREATE TABLE $t (name STRING, id INT) $defaultUsing")
47-
runShowTablesSql(s"SHOW TABLES IN $catalog.ns", Seq(Row("ns", "table", false, false)))
47+
runShowTablesSql(s"SHOW TABLES IN $catalog.ns", Seq(Row("ns", "table", false, "TABLE")))
4848
}
4949
}
5050

@@ -72,25 +72,25 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {
7272
runShowTablesSql(
7373
s"SHOW TABLES FROM $catalog.ns1",
7474
Seq(
75-
Row("ns1", "table", false, false),
76-
Row("ns1", "table_name_1a", false, false),
77-
Row("ns1", "table_name_2b", false, false)))
75+
Row("ns1", "table", false, "TABLE"),
76+
Row("ns1", "table_name_1a", false, "TABLE"),
77+
Row("ns1", "table_name_2b", false, "TABLE")))
7878

7979
runShowTablesSql(
8080
s"SHOW TABLES FROM $catalog.ns1 LIKE '*name*'",
8181
Seq(
82-
Row("ns1", "table_name_1a", false, false),
83-
Row("ns1", "table_name_2b", false, false)))
82+
Row("ns1", "table_name_1a", false, "TABLE"),
83+
Row("ns1", "table_name_2b", false, "TABLE")))
8484

8585
runShowTablesSql(
8686
s"SHOW TABLES FROM $catalog.ns1 LIKE 'table_name_1*|table_name_2*'",
8787
Seq(
88-
Row("ns1", "table_name_1a", false, false),
89-
Row("ns1", "table_name_2b", false, false)))
88+
Row("ns1", "table_name_1a", false, "TABLE"),
89+
Row("ns1", "table_name_2b", false, "TABLE")))
9090

9191
runShowTablesSql(
9292
s"SHOW TABLES FROM $catalog.ns1 LIKE '*2b'",
93-
Seq(Row("ns1", "table_name_2b", false, false)))
93+
Seq(Row("ns1", "table_name_2b", false, "TABLE")))
9494
}
9595
}
9696
}
@@ -101,7 +101,7 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {
101101
withTable(tblName) {
102102
sql(s"CREATE TABLE $tblName (name STRING, id INT) $defaultUsing")
103103
val ns = defaultNamespace.mkString(".")
104-
runShowTablesSql("SHOW TABLES", Seq(Row(ns, "table", false, false)))
104+
runShowTablesSql("SHOW TABLES", Seq(Row(ns, "table", false, "TABLE")))
105105
}
106106
}
107107
}
@@ -129,7 +129,7 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {
129129

130130
// Update the current namespace to match "ns.tbl".
131131
sql(s"USE $catalog.ns")
132-
runShowTablesSql("SHOW TABLES", Seq(Row("ns", "table", false, false)))
132+
runShowTablesSql("SHOW TABLES", Seq(Row("ns", "table", false, "TABLE")))
133133
}
134134
}
135135
}

sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ trait ShowTablesSuiteBase extends command.ShowTablesSuiteBase {
4747
withSourceViews {
4848
runShowTablesSql(
4949
"SHOW TABLES FROM default",
50-
Seq(Row("", "source", true, true), Row("", "source2", true, true)))
50+
Seq(Row("", "source", true, "VIEW"), Row("", "source2", true, "VIEW")))
5151
}
5252
}
5353

@@ -60,12 +60,12 @@ trait ShowTablesSuiteBase extends command.ShowTablesSuiteBase {
6060

6161
test("SHOW TABLE EXTENDED from default") {
6262
withSourceViews {
63-
val expected = Seq(Row("", "source", true, true), Row("", "source2", true, true))
63+
val expected = Seq(Row("", "source", true, "VIEW"), Row("", "source2", true, "VIEW"))
6464

6565
val df = sql("SHOW TABLE EXTENDED FROM default LIKE '*source*'")
6666
val result = df.collect()
6767
val resultWithoutInfo = result.map {
68-
case Row(db, table, temp, _, isView) => Row(db, table, temp, isView)
68+
case Row(db, table, temp, _, tableType) => Row(db, table, temp, tableType)
6969
}
7070

7171
assert(resultWithoutInfo === expected)
@@ -110,12 +110,12 @@ trait ShowTablesSuiteBase extends command.ShowTablesSuiteBase {
110110
sql(s"USE $catalog.ns")
111111
withTable("tbl") {
112112
sql("CREATE TABLE tbl(col1 int, col2 string) USING parquet")
113-
checkAnswer(sql("show tables"), Row("ns", "tbl", false, false))
113+
checkAnswer(sql("show tables"), Row("ns", "tbl", false, "TABLE"))
114114
assert(sql("show tables").schema.fieldNames ===
115-
Seq("namespace", "tableName", "isTemporary", "isView"))
115+
Seq("namespace", "tableName", "isTemporary", "tableType"))
116116
assert(sql("show table extended like 'tbl'").collect()(0).length == 5)
117117
assert(sql("show table extended like 'tbl'").schema.fieldNames ===
118-
Seq("namespace", "tableName", "isTemporary", "information", "isView"))
118+
Seq("namespace", "tableName", "isTemporary", "information", "tableType"))
119119

120120
// Keep the legacy output schema
121121
withSQLConf(SQLConf.LEGACY_KEEP_COMMAND_OUTPUT_SCHEMA.key -> "true") {

sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ShowTablesSuite extends command.ShowTablesSuiteBase with CommandSuiteBase
3434
spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) $defaultUsing")
3535
runShowTablesSql(
3636
s"SHOW TABLES FROM $catalog.n1.n2.db",
37-
Seq(Row("n1.n2.db", "table_name", false, false)))
37+
Seq(Row("n1.n2.db", "table_name", false, "TABLE")))
3838
}
3939
}
4040

@@ -44,7 +44,7 @@ class ShowTablesSuite extends command.ShowTablesSuiteBase with CommandSuiteBase
4444
test("using v2 catalog with empty namespace") {
4545
withTable(s"$catalog.table") {
4646
spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) $defaultUsing")
47-
runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(Row("", "table", false, false)))
47+
runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(Row("", "table", false, "TABLE")))
4848
}
4949
}
5050

0 commit comments

Comments
 (0)