Skip to content

Commit 1b56f1d

Browse files
OopsOutOfMemorymarmbrus
authored andcommitted
[SPARK-5196][SQL] Support comment in Create Table Field DDL
Support `comment` in create a table field. __CREATE TEMPORARY TABLE people(name string `comment` "the name of a person")__ Author: OopsOutOfMemory <[email protected]> Closes #3999 from OopsOutOfMemory/meta_comment and squashes the following commits: 39150d4 [OopsOutOfMemory] add comment and refine test suite
1 parent 7712ed5 commit 1b56f1d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

sql/core/src/main/scala/org/apache/spark/sql/sources/ddl.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import org.apache.spark.sql.execution.RunnableCommand
2727
import org.apache.spark.sql.types._
2828
import org.apache.spark.util.Utils
2929

30-
3130
/**
3231
* A parser for foreign DDL commands.
3332
*/
@@ -59,6 +58,7 @@ private[sql] class DDLParser extends AbstractSparkSQLParser with Logging {
5958
protected val TABLE = Keyword("TABLE")
6059
protected val USING = Keyword("USING")
6160
protected val OPTIONS = Keyword("OPTIONS")
61+
protected val COMMENT = Keyword("COMMENT")
6262

6363
// Data types.
6464
protected val STRING = Keyword("STRING")
@@ -111,8 +111,13 @@ private[sql] class DDLParser extends AbstractSparkSQLParser with Logging {
111111
protected lazy val pair: Parser[(String, String)] = ident ~ stringLit ^^ { case k ~ v => (k,v) }
112112

113113
protected lazy val column: Parser[StructField] =
114-
ident ~ dataType ^^ { case columnName ~ typ =>
115-
StructField(columnName, typ)
114+
ident ~ dataType ~ (COMMENT ~> stringLit).? ^^ { case columnName ~ typ ~ cm =>
115+
val meta = cm match {
116+
case Some(comment) =>
117+
new MetadataBuilder().putString(COMMENT.str.toLowerCase(), comment).build()
118+
case None => Metadata.empty
119+
}
120+
StructField(columnName, typ, true, meta)
116121
}
117122

118123
protected lazy val primitiveType: Parser[DataType] =

sql/core/src/test/scala/org/apache/spark/sql/sources/TableScanSuite.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,24 @@ class TableScanSuite extends DataSourceTest {
344344
}
345345
assert(schemaNeeded.getMessage.contains("A schema needs to be specified when using"))
346346
}
347+
348+
test("SPARK-5196 schema field with comment") {
349+
sql(
350+
"""
351+
|CREATE TEMPORARY TABLE student(name string comment "SN", age int comment "SA", grade int)
352+
|USING org.apache.spark.sql.sources.AllDataTypesScanSource
353+
|OPTIONS (
354+
| from '1',
355+
| to '10'
356+
|)
357+
""".stripMargin)
358+
359+
val planned = sql("SELECT * FROM student").queryExecution.executedPlan
360+
val comments = planned.schema.fields.map { field =>
361+
if (field.metadata.contains("comment")) field.metadata.getString("comment")
362+
else "NO_COMMENT"
363+
}.mkString(",")
364+
365+
assert(comments === "SN,SA,NO_COMMENT")
366+
}
347367
}

0 commit comments

Comments
 (0)