Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1476,7 +1476,7 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
/**
* Create top level table schema.
*/
protected def createSchema(ctx: ColTypeListContext): StructType = {
def createSchema(ctx: ColTypeListContext): StructType = {
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to be a little bit careful here. I think we may need to add a rule similar to singleDataType to the grammar and expose its implementation. This to prevent us from silently dropping trailing input, or having a weird error on trailing input; it seems we get a somewhat vague error ATM...

StructType(Option(ctx).toSeq.flatMap(visitColTypeList))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.trees.Origin
import org.apache.spark.sql.types.DataType
import org.apache.spark.sql.types.{DataType, StructType}

/**
* Base SQL parsing infrastructure.
Expand All @@ -49,6 +49,11 @@ abstract class AbstractSqlParser extends ParserInterface with Logging {
astBuilder.visitSingleTableIdentifier(parser.singleTableIdentifier())
}

/** Creates StructType for a given SQL string. */
override def parseTableSchema(sqlText: String): StructType = parse(sqlText) { parser =>
astBuilder.createSchema(parser.colTypeList())
}

/** Creates LogicalPlan for a given SQL string. */
override def parsePlan(sqlText: String): LogicalPlan = parse(sqlText) { parser =>
astBuilder.visitSingleStatement(parser.singleStatement()) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.parser
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.types.StructType

/**
* Interface for a parser.
Expand All @@ -33,4 +34,7 @@ trait ParserInterface {

/** Creates TableIdentifier for a given SQL string. */
def parseTableIdentifier(sqlText: String): TableIdentifier

/** Creates StructType for a given SQL string. */
def parseTableSchema(sqlText: String): StructType
Copy link
Contributor

Choose a reason for hiding this comment

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

So the difference between the parseDataType and the parseTableSchema functions is that the latter allows you to parse a comma separated list of field definitions which will preserve the correct Hive metadata? Could you document this?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.catalyst.parser

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.types._

class TableSchemaParserSuite extends SparkFunSuite {

def parse(sql: String): DataType = CatalystSqlParser.parseTableSchema(sql)

def checkTableSchema(tableSchemaString: String, expectedDataType: DataType): Unit = {
test(s"parse ${tableSchemaString.replace("\n", "")}") {
Copy link
Contributor

Choose a reason for hiding this comment

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

seems tableSchemaString will never contains \n?

assert(parse(tableSchemaString) === expectedDataType)
}
}

checkTableSchema("a int", (new StructType).add("a", "int"))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: new StructType().add...

checkTableSchema("A int", (new StructType).add("A", "int"))
checkTableSchema("a INT", (new StructType).add("a", "int"))
checkTableSchema("`!@#$%.^&*()` string", (new StructType).add("!@#$%.^&*()", "string"))
checkTableSchema("a int, b long", (new StructType).add("a", "int").add("b", "long"))
checkTableSchema("a STRUCT<intType: int, ts:timestamp>",
StructType(
StructField("a", StructType(
StructField("intType", IntegerType) ::
StructField("ts", TimestampType) :: Nil)) :: Nil))

checkTableSchema(
"a int comment 'test'",
(new StructType).add("a", "int", nullable = true, "test"))

test("complex hive type") {
val tableSchemaString =
"""
|complexStructCol struct<
|struct:struct<deciMal:DECimal, anotherDecimal:decimAL(5,2)>,
|MAP:Map<timestamp, varchar(10)>,
|arrAy:Array<double>,
|anotherArray:Array<char(9)>>
""".stripMargin.replace("\n", "")

val builder = new MetadataBuilder
builder.putString(HIVE_TYPE_STRING,
"struct<struct:struct<deciMal:decimal(10,0),anotherDecimal:decimal(5,2)>," +
"MAP:map<timestamp,varchar(10)>,arrAy:array<double>,anotherArray:array<char(9)>>")

val expectedDataType =
StructType(
StructField("complexStructCol", StructType(
StructField("struct",
StructType(
StructField("deciMal", DecimalType.USER_DEFAULT) ::
StructField("anotherDecimal", DecimalType(5, 2)) :: Nil)) ::
StructField("MAP", MapType(TimestampType, StringType)) ::
StructField("arrAy", ArrayType(DoubleType)) ::
StructField("anotherArray", ArrayType(StringType)) :: Nil),
nullable = true,
builder.build()) :: Nil)

assert(parse(tableSchemaString) === expectedDataType)
}

test("illegal col types") {
val e = intercept[ParseException] {
CatalystSqlParser.parseTableSchema("a INT b long")
}.getMessage
assert(e.contains("mismatched input 'b' expecting {<EOF>, '(', ',', 'COMMENT'}"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure we should check this. This is an ANTLR error message.

}
}