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 @@ -20,9 +20,10 @@ package org.apache.spark.sql.execution
import org.apache.spark.Logging
import org.apache.spark.annotation.DeveloperApi
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.StringType
import org.apache.spark.sql.{SchemaRDD, SQLConf, SQLContext}
import org.apache.spark.sql.catalyst.errors.TreeNodeException
import org.apache.spark.sql.catalyst.expressions.{Row, Attribute}
import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Row, Attribute}
import org.apache.spark.sql.catalyst.plans.logical
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan

Expand Down Expand Up @@ -171,10 +172,16 @@ case class UncacheTableCommand(tableName: String) extends RunnableCommand {
*/
@DeveloperApi
case class DescribeCommand(
child: SparkPlan,
override val output: Seq[Attribute]) extends RunnableCommand {
table: LogicalPlan,
isExtended: Boolean) extends RunnableCommand {

override def run(sqlContext: SQLContext) = {
child.output.map(field => Row(field.name, field.dataType.toString, null))
val plan = sqlContext.executePlan(table).analyzed
plan.output.map(field => Row(field.name, field.dataType.toString, null))
}

override val output: Seq[Attribute] = Seq(
AttributeReference("col_name", StringType, nullable = false)(),
AttributeReference("data_type", StringType, nullable = false)(),
AttributeReference("comment", StringType, nullable = true)())
}
15 changes: 13 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/sources/ddl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package org.apache.spark.sql.sources

import org.apache.spark.sql.catalyst.analysis.UnresolvedRelation

import scala.language.implicitConversions

import org.apache.spark.Logging
import org.apache.spark.sql.{SchemaRDD, SQLContext}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.AbstractSparkSQLParser
import org.apache.spark.sql.execution.RunnableCommand
import org.apache.spark.sql.execution.{DescribeCommand, RunnableCommand}
import org.apache.spark.sql.types._
import org.apache.spark.util.Utils

Expand Down Expand Up @@ -59,6 +61,8 @@ private[sql] class DDLParser extends AbstractSparkSQLParser with Logging {
protected val TABLE = Keyword("TABLE")
protected val USING = Keyword("USING")
protected val OPTIONS = Keyword("OPTIONS")
protected val DESCRIBE = Keyword("DESCRIBE")
protected val EXTENDED = Keyword("EXTENDED")

// Data types.
protected val STRING = Keyword("STRING")
Expand All @@ -80,7 +84,7 @@ private[sql] class DDLParser extends AbstractSparkSQLParser with Logging {

protected lazy val ddl: Parser[LogicalPlan] = createTable

protected def start: Parser[LogicalPlan] = ddl
protected def start: Parser[LogicalPlan] = ddl | describe

/**
* `CREATE [TEMPORARY] TABLE avroTable
Expand Down Expand Up @@ -164,6 +168,13 @@ private[sql] class DDLParser extends AbstractSparkSQLParser with Logging {
mapType |
structType |
primitiveType

private lazy val describe: Parser[LogicalPlan] =
DESCRIBE ~> EXTENDED.? ~ rep1sep(ident, ".") ^^ {
case isExtended ~ tableIdent =>
DescribeCommand((UnresolvedRelation(tableIdent,None)),false)
}

}

object ResolvedDataSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,4 +1034,11 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
rdd.registerTempTable("distinctData")
checkAnswer(sql("SELECT COUNT(DISTINCT key,value) FROM distinctData"), Row(2))
}

test("describe table") {
checkAnswer(sql("DESCRIBE EXTENDED testData"),Seq(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXTENDED ?
This seems no different with describe.
I think we'd better do this later after the discusstion.

Row("key","IntegerType",null), Row("value","StringType",null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntegerType and StringType ? should be int, string

))
}

}
17 changes: 1 addition & 16 deletions sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans._
import org.apache.spark.sql.catalyst.plans.logical
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.execution.DescribeCommand
import org.apache.spark.sql.execution.ExplainCommand
import org.apache.spark.sql.hive.execution.{HiveNativeCommand, DropTable, AnalyzeTable}
import org.apache.spark.sql.types._
Expand All @@ -46,22 +47,6 @@ import scala.collection.JavaConversions._
*/
private[hive] case object NativePlaceholder extends Command

/**
* Returned for the "DESCRIBE [EXTENDED] [dbName.]tableName" command.
* @param table The table to be described.
* @param isExtended True if "DESCRIBE EXTENDED" is used. Otherwise, false.
* It is effective only when the table is a Hive table.
*/
case class DescribeCommand(
table: LogicalPlan,
isExtended: Boolean) extends Command {
override def output = Seq(
// Column names are based on Hive.
AttributeReference("col_name", StringType, nullable = false)(),
AttributeReference("data_type", StringType, nullable = false)(),
AttributeReference("comment", StringType, nullable = false)())
}

/** Provides a mapping from HiveQL statements to catalyst logical plans and expression trees. */
private[hive] object HiveQl {
protected val nativeCommands = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private[hive] trait HiveStrategies {
ExecutedCommand(
DescribeHiveTableCommand(t, describe.output, describe.isExtended)) :: Nil
case o: LogicalPlan =>
ExecutedCommand(RunnableDescribeCommand(planLater(o), describe.output)) :: Nil
ExecutedCommand(RunnableDescribeCommand(o, describe.isExtended)) :: Nil
}

case _ => Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.scalatest.{BeforeAndAfterAll, FunSuite, GivenWhenThen}

import org.apache.spark.Logging
import org.apache.spark.sql.execution.{SetCommand, ExplainCommand}
import org.apache.spark.sql.hive.DescribeCommand
import org.apache.spark.sql.execution.DescribeCommand
import org.apache.spark.sql.catalyst.planning.PhysicalOperation
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.util._
Expand Down