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 @@ -784,7 +784,19 @@ class SessionCatalog(
* Note that, if the specified database is global temporary view database, we will list global
* temporary views.
*/
def listTables(db: String, pattern: String): Seq[TableIdentifier] = {
def listTables(db: String, pattern: String): Seq[TableIdentifier] = listTables(db, pattern, true)

/**
* List all matching tables in the specified database, including local temporary views
* if includeLocalTempViews is enabled.
*
* Note that, if the specified database is global temporary view database, we will list global
* temporary views.
*/
def listTables(
db: String,
pattern: String,
includeLocalTempViews: Boolean): Seq[TableIdentifier] = {
val dbName = formatDatabaseName(db)
val dbTables = if (dbName == globalTempViewManager.database) {
globalTempViewManager.listViewNames(pattern).map { name =>
Expand All @@ -796,12 +808,23 @@ class SessionCatalog(
TableIdentifier(name, Some(dbName))
}
}
val localTempViews = synchronized {

if (includeLocalTempViews) {
dbTables ++ listLocalTempViews(pattern)
} else {
dbTables
}
}

/**
* List all matching local temporary views.
*/
def listLocalTempViews(pattern: String): Seq[TableIdentifier] = {
synchronized {
StringUtils.filterPattern(tempViews.keys.toSeq, pattern).map { name =>
TableIdentifier(name)
}
}
dbTables ++ localTempViews
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,71 @@ abstract class SessionCatalogSuite extends AnalysisTest {
}
}

test("list tables with pattern and includeLocalTempViews") {
withEmptyCatalog { catalog =>
catalog.createDatabase(newDb("mydb"), ignoreIfExists = false)
catalog.createTable(newTable("tbl1", "mydb"), ignoreIfExists = false)
catalog.createTable(newTable("tbl2", "mydb"), ignoreIfExists = false)
val tempTable = Range(1, 10, 2, 10)
catalog.createTempView("temp_view1", tempTable, overrideIfExists = false)
catalog.createTempView("temp_view4", tempTable, overrideIfExists = false)

assert(catalog.listTables("mydb").toSet == catalog.listTables("mydb", "*").toSet)
assert(catalog.listTables("mydb").toSet == catalog.listTables("mydb", "*", true).toSet)
assert(catalog.listTables("mydb").toSet ==
catalog.listTables("mydb", "*", false).toSet ++ catalog.listLocalTempViews("*"))
assert(catalog.listTables("mydb", "*", true).toSet ==
Set(TableIdentifier("tbl1", Some("mydb")),
TableIdentifier("tbl2", Some("mydb")),
TableIdentifier("temp_view1"),
TableIdentifier("temp_view4")))
assert(catalog.listTables("mydb", "*", false).toSet ==
Set(TableIdentifier("tbl1", Some("mydb")), TableIdentifier("tbl2", Some("mydb"))))
assert(catalog.listTables("mydb", "tbl*", true).toSet ==
Set(TableIdentifier("tbl1", Some("mydb")), TableIdentifier("tbl2", Some("mydb"))))
assert(catalog.listTables("mydb", "tbl*", false).toSet ==
Set(TableIdentifier("tbl1", Some("mydb")), TableIdentifier("tbl2", Some("mydb"))))
assert(catalog.listTables("mydb", "temp_view*", true).toSet ==
Set(TableIdentifier("temp_view1"), TableIdentifier("temp_view4")))
assert(catalog.listTables("mydb", "temp_view*", false).toSet == Set.empty)
}
}

test("list temporary view with pattern") {
withBasicCatalog { catalog =>
val tempTable = Range(1, 10, 2, 10)
catalog.createTempView("temp_view1", tempTable, overrideIfExists = false)
catalog.createTempView("temp_view4", tempTable, overrideIfExists = false)
assert(catalog.listLocalTempViews("*").toSet ==
Set(TableIdentifier("temp_view1"), TableIdentifier("temp_view4")))
assert(catalog.listLocalTempViews("temp_view*").toSet ==
Set(TableIdentifier("temp_view1"), TableIdentifier("temp_view4")))
assert(catalog.listLocalTempViews("*1").toSet == Set(TableIdentifier("temp_view1")))
assert(catalog.listLocalTempViews("does_not_exist").toSet == Set.empty)
}
}

test("list global temporary view and local temporary view with pattern") {
withBasicCatalog { catalog =>
val tempTable = Range(1, 10, 2, 10)
catalog.createTempView("temp_view1", tempTable, overrideIfExists = false)
catalog.createTempView("temp_view4", tempTable, overrideIfExists = false)
catalog.globalTempViewManager.create("global_temp_view1", tempTable, overrideIfExists = false)
catalog.globalTempViewManager.create("global_temp_view2", tempTable, overrideIfExists = false)
assert(catalog.listTables(catalog.globalTempViewManager.database, "*").toSet ==
Set(TableIdentifier("temp_view1"),
TableIdentifier("temp_view4"),
TableIdentifier("global_temp_view1", Some(catalog.globalTempViewManager.database)),
TableIdentifier("global_temp_view2", Some(catalog.globalTempViewManager.database))))
assert(catalog.listTables(catalog.globalTempViewManager.database, "*temp_view1").toSet ==
Set(TableIdentifier("temp_view1"),
TableIdentifier("global_temp_view1", Some(catalog.globalTempViewManager.database))))
assert(catalog.listTables(catalog.globalTempViewManager.database, "global*").toSet ==
Set(TableIdentifier("global_temp_view1", Some(catalog.globalTempViewManager.database)),
TableIdentifier("global_temp_view2", Some(catalog.globalTempViewManager.database))))
}
}

// --------------------------------------------------------------------------
// Partitions
// --------------------------------------------------------------------------
Expand Down