-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-3299][SQL]Public API in SQLContext to list tables #4547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12c86df
Add tables() to SQLContext to return a DataFrame containing existing …
yhuai aba2e88
Format.
yhuai 572870d
Address comments.
yhuai 7793dcb
Fix scala test.
yhuai acbb281
Update Python test.
yhuai 6c8f92e
Add tableNames.
yhuai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -734,6 +734,42 @@ class SQLContext(@transient val sparkContext: SparkContext) | |
def table(tableName: String): DataFrame = | ||
DataFrame(this, catalog.lookupRelation(Seq(tableName))) | ||
|
||
/** | ||
* Returns a [[DataFrame]] containing names of existing tables in the given database. | ||
* The returned DataFrame has two columns, tableName and isTemporary (a column with BooleanType | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd just say |
||
* indicating if a table is a temporary one or not). | ||
*/ | ||
def tables(): DataFrame = { | ||
createDataFrame(catalog.getTables(None)).toDataFrame("tableName", "isTemporary") | ||
} | ||
|
||
/** | ||
* Returns a [[DataFrame]] containing names of existing tables in the current database. | ||
* The returned DataFrame has two columns, tableName and isTemporary (a column with BooleanType | ||
* indicating if a table is a temporary one or not). | ||
*/ | ||
def tables(databaseName: String): DataFrame = { | ||
createDataFrame(catalog.getTables(Some(databaseName))).toDataFrame("tableName", "isTemporary") | ||
} | ||
|
||
/** | ||
* Returns an array of names of tables in the current database. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns the names of all tables in the current database as an array? |
||
*/ | ||
def tableNames(): Array[String] = { | ||
catalog.getTables(None).map { | ||
case (tableName, _) => tableName | ||
}.toArray | ||
} | ||
|
||
/** | ||
* Returns an array of names of tables in the given database. | ||
*/ | ||
def tableNames(databaseName: String): Array[String] = { | ||
catalog.getTables(Some(databaseName)).map { | ||
case (tableName, _) => tableName | ||
}.toArray | ||
} | ||
|
||
protected[sql] class SparkPlanner extends SparkStrategies { | ||
val sparkContext: SparkContext = self.sparkContext | ||
|
||
|
76 changes: 76 additions & 0 deletions
76
sql/core/src/test/scala/org/apache/spark/sql/ListTablesSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql | ||
|
||
import org.scalatest.BeforeAndAfter | ||
|
||
import org.apache.spark.sql.test.TestSQLContext | ||
import org.apache.spark.sql.test.TestSQLContext._ | ||
import org.apache.spark.sql.types.{BooleanType, StringType, StructField, StructType} | ||
|
||
class ListTablesSuite extends QueryTest with BeforeAndAfter { | ||
|
||
import org.apache.spark.sql.test.TestSQLContext.implicits._ | ||
|
||
val df = | ||
sparkContext.parallelize((1 to 10).map(i => (i,s"str$i"))).toDataFrame("key", "value") | ||
|
||
before { | ||
df.registerTempTable("ListTablesSuiteTable") | ||
} | ||
|
||
after { | ||
catalog.unregisterTable(Seq("ListTablesSuiteTable")) | ||
} | ||
|
||
test("get all tables") { | ||
checkAnswer( | ||
tables().filter("tableName = 'ListTablesSuiteTable'"), | ||
Row("ListTablesSuiteTable", true)) | ||
|
||
catalog.unregisterTable(Seq("ListTablesSuiteTable")) | ||
assert(tables().filter("tableName = 'ListTablesSuiteTable'").count() === 0) | ||
} | ||
|
||
test("getting all Tables with a database name has no impact on returned table names") { | ||
checkAnswer( | ||
tables("DB").filter("tableName = 'ListTablesSuiteTable'"), | ||
Row("ListTablesSuiteTable", true)) | ||
|
||
catalog.unregisterTable(Seq("ListTablesSuiteTable")) | ||
assert(tables().filter("tableName = 'ListTablesSuiteTable'").count() === 0) | ||
} | ||
|
||
test("query the returned DataFrame of tables") { | ||
val tableDF = tables() | ||
val schema = StructType( | ||
StructField("tableName", StringType, true) :: | ||
StructField("isTemporary", BooleanType, false) :: Nil) | ||
assert(schema === tableDF.schema) | ||
|
||
tableDF.registerTempTable("tables") | ||
checkAnswer( | ||
sql("SELECT isTemporary, tableName from tables WHERE tableName = 'ListTablesSuiteTable'"), | ||
Row(true, "ListTablesSuiteTable") | ||
) | ||
checkAnswer( | ||
tables().filter("tableName = 'tables'").select("tableName", "isTemporary"), | ||
Row("tables", true)) | ||
dropTempTable("tables") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
sql/hive/src/test/scala/org/apache/spark/sql/hive/ListTablesSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.hive | ||
|
||
import org.scalatest.BeforeAndAfterAll | ||
|
||
import org.apache.spark.sql.hive.test.TestHive | ||
import org.apache.spark.sql.hive.test.TestHive._ | ||
import org.apache.spark.sql.QueryTest | ||
import org.apache.spark.sql.Row | ||
|
||
class ListTablesSuite extends QueryTest with BeforeAndAfterAll { | ||
|
||
import org.apache.spark.sql.hive.test.TestHive.implicits._ | ||
|
||
val df = | ||
sparkContext.parallelize((1 to 10).map(i => (i,s"str$i"))).toDataFrame("key", "value") | ||
|
||
override def beforeAll(): Unit = { | ||
// The catalog in HiveContext is a case insensitive one. | ||
catalog.registerTable(Seq("ListTablesSuiteTable"), df.logicalPlan) | ||
catalog.registerTable(Seq("ListTablesSuiteDB", "InDBListTablesSuiteTable"), df.logicalPlan) | ||
sql("CREATE TABLE HiveListTablesSuiteTable (key int, value string)") | ||
sql("CREATE DATABASE IF NOT EXISTS ListTablesSuiteDB") | ||
sql("CREATE TABLE ListTablesSuiteDB.HiveInDBListTablesSuiteTable (key int, value string)") | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
catalog.unregisterTable(Seq("ListTablesSuiteTable")) | ||
catalog.unregisterTable(Seq("ListTablesSuiteDB", "InDBListTablesSuiteTable")) | ||
sql("DROP TABLE IF EXISTS HiveListTablesSuiteTable") | ||
sql("DROP TABLE IF EXISTS ListTablesSuiteDB.HiveInDBListTablesSuiteTable") | ||
sql("DROP DATABASE IF EXISTS ListTablesSuiteDB") | ||
} | ||
|
||
test("get all tables of current database") { | ||
val allTables = tables() | ||
// We are using default DB. | ||
checkAnswer( | ||
allTables.filter("tableName = 'listtablessuitetable'"), | ||
Row("listtablessuitetable", true)) | ||
assert(allTables.filter("tableName = 'indblisttablessuitetable'").count() === 0) | ||
checkAnswer( | ||
allTables.filter("tableName = 'hivelisttablessuitetable'"), | ||
Row("hivelisttablessuitetable", false)) | ||
assert(allTables.filter("tableName = 'hiveindblisttablessuitetable'").count() === 0) | ||
} | ||
|
||
test("getting all tables with a database name") { | ||
val allTables = tables("ListTablesSuiteDB") | ||
checkAnswer( | ||
allTables.filter("tableName = 'listtablessuitetable'"), | ||
Row("listtablessuitetable", true)) | ||
checkAnswer( | ||
allTables.filter("tableName = 'indblisttablessuitetable'"), | ||
Row("indblisttablessuitetable", true)) | ||
assert(allTables.filter("tableName = 'hivelisttablessuitetable'").count() === 0) | ||
checkAnswer( | ||
allTables.filter("tableName = 'hiveindblisttablessuitetable'"), | ||
Row("hiveindblisttablessuitetable", false)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the javadoc is actually wrong. this one should say "current database", and next one should say "given database"