Skip to content
Merged
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 @@ -2952,8 +2952,9 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
protected def getSerdeInfo(
rowFormatCtx: Seq[RowFormatContext],
createFileFormatCtx: Seq[CreateFileFormatContext],
ctx: ParserRuleContext): Option[SerdeInfo] = {
validateRowFormatFileFormat(rowFormatCtx, createFileFormatCtx, ctx)
ctx: ParserRuleContext,
skipCheck: Boolean = false): Option[SerdeInfo] = {
if (!skipCheck) validateRowFormatFileFormat(rowFormatCtx, createFileFormatCtx, ctx)
val rowFormatSerdeInfo = rowFormatCtx.map(visitRowFormat)
val fileFormatSerdeInfo = createFileFormatCtx.map(visitCreateFileFormat)
(fileFormatSerdeInfo ++ rowFormatSerdeInfo).reduceLeftOption((l, r) => l.merge(r))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class ResolveSessionCatalog(
assertNoNullTypeInSchema(c.asSelect.schema)
}
val (storageFormat, provider) = getStorageFormatAndProvider(
c.provider, c.options, c.location, c.serde, ctas = false)
c.provider, c.options, c.location, c.serde, ctas = true)
Copy link
Author

Choose a reason for hiding this comment

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

this is CTAS.

if (!isV2Provider(provider)) {
val tableDesc = buildCatalogTable(tbl.asTableIdentifier, new StructType,
c.partitioning, c.bucketSpec, c.properties, provider, c.location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,23 @@ class SparkSqlAstBuilder extends AstBuilder {
checkDuplicateClauses(ctx.TBLPROPERTIES, "TBLPROPERTIES", ctx)
val provider = ctx.tableProvider.asScala.headOption.map(_.multipartIdentifier.getText)
val location = visitLocationSpecList(ctx.locationSpec())
// rowStorage used to determine CatalogStorageFormat.serde and
// CatalogStorageFormat.properties in STORED AS clause.
val serdeInfo = getSerdeInfo(ctx.rowFormat.asScala, ctx.createFileFormat.asScala, ctx)
// TODO: Do not skip serde check for CREATE TABLE LIKE.
Copy link
Author

Choose a reason for hiding this comment

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

CREATE TABLE LIKE has several inconsistencies with CREATE TABLE, I'll keep them unchanged here and fix them later.

val serdeInfo = getSerdeInfo(
ctx.rowFormat.asScala, ctx.createFileFormat.asScala, ctx, skipCheck = true)
if (provider.isDefined && serdeInfo.isDefined) {
operationNotAllowed(s"CREATE TABLE LIKE ... USING ... ${serdeInfo.get.describe}", ctx)
}

// TODO: remove this restriction as it seems unnecessary.
Copy link
Owner

Choose a reason for hiding this comment

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

If you intend to change this later, then please file an issue for it rather than adding a TODO here. This PR should preserve existing behavior.

serdeInfo match {
case Some(SerdeInfo(storedAs, formatClasses, serde, _)) =>
if (storedAs.isEmpty && formatClasses.isEmpty && serde.isDefined) {
throw new ParseException("'ROW FORMAT' must be used with 'STORED AS'", ctx)
}
case _ =>
}

// TODO: also look at `HiveSerDe.getDefaultStorage`.
val storage = toStorageFormat(location, serdeInfo, ctx)
val properties = Option(ctx.tableProps).map(visitPropertyKeyValues).getOrElse(Map.empty)
CreateTableLikeCommand(
Expand Down Expand Up @@ -603,7 +613,8 @@ class SparkSqlAstBuilder extends AstBuilder {
*/
override def visitInsertOverwriteHiveDir(
ctx: InsertOverwriteHiveDirContext): InsertDirParams = withOrigin(ctx) {
val serdeInfo = getSerdeInfo(Seq(ctx.rowFormat), Seq(ctx.createFileFormat), ctx)
val serdeInfo = getSerdeInfo(
Option(ctx.rowFormat).toSeq, Option(ctx.createFileFormat).toSeq, ctx)
Copy link
Author

Choose a reason for hiding this comment

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

to be null safe.

val path = string(ctx.path)
// The path field is required
if (path.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,7 @@ class HiveDDLSuite
|ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
""".stripMargin)
}.getMessage
assert(e.contains("'ROW FORMAT' must be used with 'STORED AS'"))
assert(e.contains("Operation not allowed: CREATE TABLE LIKE ... USING ... ROW FORMAT SERDE"))

// row format doesn't work with provider hive
e = intercept[AnalysisException] {
Expand All @@ -2791,7 +2791,7 @@ class HiveDDLSuite
|WITH SERDEPROPERTIES ('test' = 'test')
""".stripMargin)
}.getMessage
assert(e.contains("'ROW FORMAT' must be used with 'STORED AS'"))
assert(e.contains("Operation not allowed: CREATE TABLE LIKE ... USING ... ROW FORMAT SERDE"))

// row format doesn't work without 'STORED AS'
e = intercept[AnalysisException] {
Expand All @@ -2813,8 +2813,7 @@ class HiveDDLSuite
|ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
""".stripMargin)
}.getMessage
assert(e.contains(
"'INPUTFORMAT hiveFormat' and 'USING provider' should not be specified both"))
assert(e.contains("Operation not allowed: CREATE TABLE LIKE ... USING ... STORED AS"))

// row format works with STORED AS hive format (from hive table)
spark.sql(
Expand Down