Skip to content

Commit

Permalink
use string
Browse files Browse the repository at this point in the history
  • Loading branch information
yaooqinn committed Mar 11, 2021
1 parent 423796f commit 6bc1614
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion python/pyspark/sql/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def tables(self, dbName=None):
>>> sqlContext.registerDataFrameAsTable(df, "table1")
>>> df2 = sqlContext.tables()
>>> df2.filter("tableName = 'table1'").first()
Row(namespace='', tableName='table1', isTemporary=True)
Row(namespace='', tableName='table1', isTemporary=True, tableType='TABLE')
"""
if dbName is None:
return DataFrame(self._ssql_ctx.tables(), self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,13 @@ object CatalogTableType {
val VIEW = new CatalogTableType("VIEW")

val tableTypes = Seq(EXTERNAL, MANAGED, VIEW)

def classicTableTypeString(tableType: CatalogTableType): String = tableType match {
case EXTERNAL | MANAGED => "TABLE"
case VIEW => "VIEW"
case t =>
throw new IllegalArgumentException(s"Unknown table type is found: $t")
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,21 @@ case class ShowTables(
override def children: Seq[LogicalPlan] = Seq(namespace)
}

object ShowTables {
trait ShowTablesLegacyHelper {
def getOutputAttrs: Seq[Attribute]

def getLegacyOutputAttrs: Seq[Attribute] = {
val output = getOutputAttrs
output.head.withName("database") +: output.slice(1, output.length - 1)
}
}

object ShowTables extends ShowTablesLegacyHelper {
def getOutputAttrs: Seq[Attribute] = Seq(
AttributeReference("namespace", StringType, nullable = false)(),
AttributeReference("tableName", StringType, nullable = false)(),
AttributeReference("isTemporary", BooleanType, nullable = false)(),
AttributeReference("isView", BooleanType, nullable = false)())
AttributeReference("tableType", StringType, nullable = false)())
}

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

object ShowTableExtended {
object ShowTableExtended extends ShowTablesLegacyHelper {
def getOutputAttrs: Seq[Attribute] = Seq(
AttributeReference("namespace", StringType, nullable = false)(),
AttributeReference("tableName", StringType, nullable = false)(),
AttributeReference("isTemporary", BooleanType, nullable = false)(),
AttributeReference("information", StringType, nullable = false)(),
AttributeReference("isView", BooleanType, nullable = false)())
AttributeReference("tableType", StringType, nullable = false)())
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class ResolveSessionCatalog(val catalogManager: CatalogManager)
case ShowTables(DatabaseInSessionCatalog(db), pattern, output) =>
val newOutput = if (conf.getConf(SQLConf.LEGACY_KEEP_COMMAND_OUTPUT_SCHEMA)) {
assert(output.length == 4)
output.head.withName("database") +: output.slice(1, 3)
ShowTables.getLegacyOutputAttrs
} else {
output
}
Expand All @@ -367,12 +367,12 @@ class ResolveSessionCatalog(val catalogManager: CatalogManager)
output) =>
val newOutput = if (conf.getConf(SQLConf.LEGACY_KEEP_COMMAND_OUTPUT_SCHEMA)) {
assert(output.length == 5)
output.head.withName("database") +: output.slice(1, 4)
ShowTableExtended.getLegacyOutputAttrs
} else {
output
}
val tablePartitionSpec = partitionSpec.map(_.asInstanceOf[UnresolvedPartitionSpec].spec)
ShowTablesCommand(Some(db), Some(pattern), newOutput, isExtended = true, tablePartitionSpec)
ShowTablesCommand(Some(db), Some(pattern), newOutput, true, tablePartitionSpec)

// ANALYZE TABLE works on permanent views if the views are cached.
case AnalyzeTable(ResolvedV1TableOrViewIdentifier(ident), partitionSpec, noScan) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,17 +844,17 @@ case class ShowTablesCommand(
val tableName = tableIdent.table
val isTemp = catalog.isTempView(tableIdent)
val catalogTable = catalog.getTempViewOrPermanentTableMetadata(tableIdent)
val isView = catalogTable.tableType == CatalogTableType.VIEW
val tableType = classicTableTypeString(catalogTable.tableType)
if (isExtended) {
val information = catalogTable.simpleString
if (output.size == 5) {
Row(database, tableName, isTemp, s"$information\n", isView)
Row(database, tableName, isTemp, s"$information\n", tableType)
} else {
Row(database, tableName, isTemp, s"$information\n")
}
} else {
if (output.size == 4) {
Row(database, tableName, isTemp, isView)
Row(database, tableName, isTemp, tableType)
} else {
Row(database, tableName, isTemp)
}
Expand Down Expand Up @@ -882,8 +882,8 @@ case class ShowTablesCommand(
val isTemp = catalog.isTempView(tableIdent)
val information = partition.simpleString
if (output.size == 5) {
val isView = table.tableType == CatalogTableType.VIEW
Seq(Row(database, tableName, isTemp, s"$information\n", isView))
val tableType = classicTableTypeString(table.tableType)
Seq(Row(database, tableName, isTemp, s"$information\n", tableType))
} else {
Seq(Row(database, tableName, isTemp, s"$information\n"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ case class ShowTablesExec(
val tables = catalog.listTables(namespace.toArray)
tables.map { table =>
if (pattern.map(StringUtils.filterPattern(Seq(table.name()), _).nonEmpty).getOrElse(true)) {
rows += toCatalystRow(table.namespace().quoted, table.name(), false, false)
rows += toCatalystRow(table.namespace().quoted, table.name(), false, "TABLE")
}
}

Expand Down
24 changes: 12 additions & 12 deletions sql/core/src/test/resources/sql-tests/results/show-tables.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct<>
-- !query
SHOW TABLES
-- !query schema
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
-- !query output
show_t1
show_t2
Expand All @@ -71,7 +71,7 @@ show_t3
-- !query
SHOW TABLES IN showdb
-- !query schema
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
-- !query output
show_t1
show_t2
Expand All @@ -81,7 +81,7 @@ show_t3
-- !query
SHOW TABLES 'show_t*'
-- !query schema
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
-- !query output
show_t1
show_t2
Expand All @@ -91,7 +91,7 @@ show_t3
-- !query
SHOW TABLES LIKE 'show_t1*|show_t2*'
-- !query schema
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
-- !query output
show_t1
show_t2
Expand All @@ -100,7 +100,7 @@ show_t2
-- !query
SHOW TABLES IN showdb 'show_t*'
-- !query schema
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
-- !query output
show_t1
show_t2
Expand All @@ -110,7 +110,7 @@ show_t3
-- !query
SHOW TABLES IN showdb LIKE 'show_t*'
-- !query schema
struct<namespace:string,tableName:string,isTemporary:boolean,isView:boolean>
struct<namespace:string,tableName:string,isTemporary:boolean,tableType:string>
-- !query output
show_t1
show_t2
Expand All @@ -120,7 +120,7 @@ show_t3
-- !query
SHOW TABLE EXTENDED LIKE 'show_t*'
-- !query schema
struct<namespace:string,tableName:string,isTemporary:boolean,information:string,isView:boolean>
struct<namespace:string,tableName:string,isTemporary:boolean,information:string,tableType:string>
-- !query output
show_t3 true Table: show_t3
Created Time [not included in comparison]
Expand All @@ -130,7 +130,7 @@ Type: VIEW
Schema: root
|-- e: integer (nullable = true)

true
VIEW
showdb show_t1 false Database: showdb
Table: show_t1
Created Time [not included in comparison]
Expand All @@ -147,7 +147,7 @@ Schema: root
|-- c: string (nullable = true)
|-- d: string (nullable = true)

false
TABLE
showdb show_t2 false Database: showdb
Table: show_t2
Created Time [not included in comparison]
Expand All @@ -160,7 +160,7 @@ Schema: root
|-- b: string (nullable = true)
|-- d: integer (nullable = true)

false
TABLE


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


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {
test("show an existing table") {
withNamespaceAndTable("ns", "table") { t =>
sql(s"CREATE TABLE $t (name STRING, id INT) $defaultUsing")
runShowTablesSql(s"SHOW TABLES IN $catalog.ns", Seq(Row("ns", "table", false, false)))
runShowTablesSql(s"SHOW TABLES IN $catalog.ns", Seq(Row("ns", "table", false, "TABLE")))
}
}

Expand Down Expand Up @@ -72,25 +72,25 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {
runShowTablesSql(
s"SHOW TABLES FROM $catalog.ns1",
Seq(
Row("ns1", "table", false, false),
Row("ns1", "table_name_1a", false, false),
Row("ns1", "table_name_2b", false, false)))
Row("ns1", "table", false, "TABLE"),
Row("ns1", "table_name_1a", false, "TABLE"),
Row("ns1", "table_name_2b", false, "TABLE")))

runShowTablesSql(
s"SHOW TABLES FROM $catalog.ns1 LIKE '*name*'",
Seq(
Row("ns1", "table_name_1a", false, false),
Row("ns1", "table_name_2b", false, false)))
Row("ns1", "table_name_1a", false, "TABLE"),
Row("ns1", "table_name_2b", false, "TABLE")))

runShowTablesSql(
s"SHOW TABLES FROM $catalog.ns1 LIKE 'table_name_1*|table_name_2*'",
Seq(
Row("ns1", "table_name_1a", false, false),
Row("ns1", "table_name_2b", false, false)))
Row("ns1", "table_name_1a", false, "TABLE"),
Row("ns1", "table_name_2b", false, "TABLE")))

runShowTablesSql(
s"SHOW TABLES FROM $catalog.ns1 LIKE '*2b'",
Seq(Row("ns1", "table_name_2b", false, false)))
Seq(Row("ns1", "table_name_2b", false, "TABLE")))
}
}
}
Expand All @@ -101,7 +101,7 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {
withTable(tblName) {
sql(s"CREATE TABLE $tblName (name STRING, id INT) $defaultUsing")
val ns = defaultNamespace.mkString(".")
runShowTablesSql("SHOW TABLES", Seq(Row(ns, "table", false, false)))
runShowTablesSql("SHOW TABLES", Seq(Row(ns, "table", false, "TABLE")))
}
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ trait ShowTablesSuiteBase extends QueryTest with DDLCommandTestUtils {

// Update the current namespace to match "ns.tbl".
sql(s"USE $catalog.ns")
runShowTablesSql("SHOW TABLES", Seq(Row("ns", "table", false, false)))
runShowTablesSql("SHOW TABLES", Seq(Row("ns", "table", false, "TABLE")))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ trait ShowTablesSuiteBase extends command.ShowTablesSuiteBase {
withSourceViews {
runShowTablesSql(
"SHOW TABLES FROM default",
Seq(Row("", "source", true, true), Row("", "source2", true, true)))
Seq(Row("", "source", true, "VIEW"), Row("", "source2", true, "VIEW")))
}
}

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

test("SHOW TABLE EXTENDED from default") {
withSourceViews {
val expected = Seq(Row("", "source", true, true), Row("", "source2", true, true))
val expected = Seq(Row("", "source", true, "VIEW"), Row("", "source2", true, "VIEW"))

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

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

// Keep the legacy output schema
withSQLConf(SQLConf.LEGACY_KEEP_COMMAND_OUTPUT_SCHEMA.key -> "true") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ShowTablesSuite extends command.ShowTablesSuiteBase with CommandSuiteBase
spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) $defaultUsing")
runShowTablesSql(
s"SHOW TABLES FROM $catalog.n1.n2.db",
Seq(Row("n1.n2.db", "table_name", false, false)))
Seq(Row("n1.n2.db", "table_name", false, "TABLE")))
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ private[hive] trait SparkOperation extends Operation with Logging {
}
}

def tableTypeString(tableType: CatalogTableType): String = tableType match {
case EXTERNAL | MANAGED => "TABLE"
case VIEW => "VIEW"
case t =>
throw new IllegalArgumentException(s"Unknown table type is found: $t")
def tableTypeString(tableType: CatalogTableType): String = {
CatalogTableType.classicTableTypeString(tableType)
}

protected def onError(): PartialFunction[Throwable, Unit] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class ListTablesSuite extends QueryTest with TestHiveSingleton with BeforeAndAft
// We are using default DB.
checkAnswer(
allTables.filter("tableName = 'listtablessuitetable'"),
Row("", "listtablessuitetable", true))
Row("", "listtablessuitetable", true, "VIEW"))
checkAnswer(
allTables.filter("tableName = 'hivelisttablessuitetable'"),
Row("default", "hivelisttablessuitetable", false))
Row("default", "hivelisttablessuitetable", false, "TABLE"))
assert(allTables.filter("tableName = 'hiveindblisttablessuitetable'").count() === 0)
}
}
Expand All @@ -71,11 +71,11 @@ class ListTablesSuite extends QueryTest with TestHiveSingleton with BeforeAndAft
case allTables =>
checkAnswer(
allTables.filter("tableName = 'listtablessuitetable'"),
Row("", "listtablessuitetable", true))
Row("", "listtablessuitetable", true, "VIEW"))
assert(allTables.filter("tableName = 'hivelisttablessuitetable'").count() === 0)
checkAnswer(
allTables.filter("tableName = 'hiveindblisttablessuitetable'"),
Row("listtablessuitedb", "hiveindblisttablessuitetable", false))
Row("listtablessuitedb", "hiveindblisttablessuitetable", false, "TABLE"))
}
}
}
Loading

0 comments on commit 6bc1614

Please sign in to comment.