Skip to content

Commit

Permalink
Add tableNames.
Browse files Browse the repository at this point in the history
  • Loading branch information
yhuai committed Feb 12, 2015
1 parent acbb281 commit 6c8f92e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
18 changes: 17 additions & 1 deletion python/pyspark/sql/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def table(self, tableName):
return DataFrame(self._ssql_ctx.table(tableName), self)

def tables(self, dbName=None):
"""Returns a DataFrame containing names of table in the given database.
"""Returns a DataFrame containing names of tables in the given database.
If `dbName` is not specified, the current database will be used.
Expand All @@ -639,6 +639,22 @@ def tables(self, dbName=None):
else:
return DataFrame(self._ssql_ctx.tables(dbName), self)

def tableNames(self, dbName=None):
"""Returns a list of names of tables in the database `dbName`.
If `dbName` is not specified, the current database will be used.
>>> sqlCtx.registerRDDAsTable(df, "table1")
>>> "table1" in sqlCtx.tableNames()
True
>>> "table1" in sqlCtx.tableNames("db")
True
"""
if dbName is None:
return [name for name in self._ssql_ctx.tableNames()]
else:
return [name for name in self._ssql_ctx.tableNames(dbName)]

def cacheTable(self, tableName):
"""Caches the specified table in-memory."""
self._ssql_ctx.cacheTable(tableName)
Expand Down
28 changes: 23 additions & 5 deletions sql/core/src/main/scala/org/apache/spark/sql/SQLContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,15 @@ 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
* 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
Expand All @@ -744,12 +753,21 @@ class SQLContext(@transient val sparkContext: SparkContext)
}

/**
* 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
* indicating if a table is a temporary one or not).
* Returns an array of names of tables in the current database.
*/
def tables(): DataFrame = {
createDataFrame(catalog.getTables(None)).toDataFrame("tableName", "isTemporary")
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 {
Expand Down

0 comments on commit 6c8f92e

Please sign in to comment.