-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-18885][SQL] unify CREATE TABLE syntax for data source and hive serde tables #16296
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1dbf61
unify CREATE TABLE syntax for data source and hive serde tables
cloud-fan 25af2cb
address comments
cloud-fan ec4ab37
Merge remote-tracking branch 'origin/master' into create-table
cloud-fan 561b925
document serde properties
cloud-fan 08ec4a7
address comments
cloud-fan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -342,42 +342,46 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
} | ||
|
||
/** | ||
* Create a data source table, returning a [[CreateTable]] logical plan. | ||
* Create a table, returning a [[CreateTable]] logical plan. | ||
* | ||
* Expected format: | ||
* {{{ | ||
* CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name | ||
* CREATE [TEMPORARY] TABLE [IF NOT EXISTS] [db_name.]table_name | ||
* USING table_provider | ||
* [OPTIONS table_property_list] | ||
* [PARTITIONED BY (col_name, col_name, ...)] | ||
* [CLUSTERED BY (col_name, col_name, ...) | ||
* [SORTED BY (col_name [ASC|DESC], ...)] | ||
* INTO num_buckets BUCKETS | ||
* ] | ||
* [LOCATION path] | ||
* [COMMENT table_comment] | ||
* [AS select_statement]; | ||
* }}} | ||
*/ | ||
override def visitCreateTableUsing(ctx: CreateTableUsingContext): LogicalPlan = withOrigin(ctx) { | ||
override def visitCreateTable(ctx: CreateTableContext): LogicalPlan = withOrigin(ctx) { | ||
val (table, temp, ifNotExists, external) = visitCreateTableHeader(ctx.createTableHeader) | ||
if (external) { | ||
operationNotAllowed("CREATE EXTERNAL TABLE ... USING", ctx) | ||
} | ||
val options = Option(ctx.tablePropertyList).map(visitPropertyKeyValues).getOrElse(Map.empty) | ||
val options = Option(ctx.options).map(visitPropertyKeyValues).getOrElse(Map.empty) | ||
val provider = ctx.tableProvider.qualifiedName.getText | ||
if (provider.toLowerCase == DDLUtils.HIVE_PROVIDER) { | ||
throw new AnalysisException("Cannot create hive serde table with CREATE TABLE USING") | ||
} | ||
val schema = Option(ctx.colTypeList()).map(createSchema) | ||
val partitionColumnNames = | ||
Option(ctx.partitionColumnNames) | ||
.map(visitIdentifierList(_).toArray) | ||
.getOrElse(Array.empty[String]) | ||
val bucketSpec = Option(ctx.bucketSpec()).map(visitBucketSpec) | ||
|
||
// TODO: this may be wrong for non file-based data source like JDBC, which should be external | ||
// even there is no `path` in options. We should consider allow the EXTERNAL keyword. | ||
val location = Option(ctx.locationSpec).map(visitLocationSpec) | ||
val storage = DataSource.buildStorageFormatFromOptions(options) | ||
val tableType = if (storage.locationUri.isDefined) { | ||
|
||
if (location.isDefined && storage.locationUri.isDefined) { | ||
throw new ParseException("Cannot specify LOCATION when there is 'path' in OPTIONS.", ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's be more specific at here. These two approaches are the same and we only want users to use one, right? |
||
} | ||
val customLocation = storage.locationUri.orElse(location) | ||
|
||
val tableType = if (customLocation.isDefined) { | ||
CatalogTableType.EXTERNAL | ||
} else { | ||
CatalogTableType.MANAGED | ||
|
@@ -386,12 +390,12 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
val tableDesc = CatalogTable( | ||
identifier = table, | ||
tableType = tableType, | ||
storage = storage, | ||
storage = storage.copy(locationUri = customLocation), | ||
schema = schema.getOrElse(new StructType), | ||
provider = Some(provider), | ||
partitionColumnNames = partitionColumnNames, | ||
bucketSpec = bucketSpec | ||
) | ||
bucketSpec = bucketSpec, | ||
comment = Option(ctx.comment).map(string)) | ||
|
||
// Determine the storage mode. | ||
val mode = if (ifNotExists) SaveMode.Ignore else SaveMode.ErrorIfExists | ||
|
@@ -1011,10 +1015,10 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
} | ||
|
||
/** | ||
* Create a table, returning a [[CreateTable]] logical plan. | ||
* Create a Hive serde table, returning a [[CreateTable]] logical plan. | ||
* | ||
* This is not used to create datasource tables, which is handled through | ||
* "CREATE TABLE ... USING ...". | ||
* This is a legacy syntax for Hive compatibility, we recommend users to use the Spark SQL | ||
* CREATE TABLE syntax to create Hive serde table, e.g. "CREATE TABLE ... USING hive ..." | ||
* | ||
* Note: several features are currently not supported - temporary tables, bucketing, | ||
* skewed columns and storage handlers (STORED BY). | ||
|
@@ -1032,7 +1036,7 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
* [AS select_statement]; | ||
* }}} | ||
*/ | ||
override def visitCreateTable(ctx: CreateTableContext): LogicalPlan = withOrigin(ctx) { | ||
override def visitCreateHiveTable(ctx: CreateHiveTableContext): LogicalPlan = withOrigin(ctx) { | ||
val (name, temp, ifNotExists, external) = visitCreateTableHeader(ctx.createTableHeader) | ||
// TODO: implement temporary tables | ||
if (temp) { | ||
|
@@ -1046,7 +1050,6 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
if (ctx.bucketSpec != null) { | ||
operationNotAllowed("CREATE TABLE ... CLUSTERED BY", ctx) | ||
} | ||
val comment = Option(ctx.STRING).map(string) | ||
val dataCols = Option(ctx.columns).map(visitColTypeList).getOrElse(Nil) | ||
val partitionCols = Option(ctx.partitionColumns).map(visitColTypeList).getOrElse(Nil) | ||
val properties = Option(ctx.tablePropertyList).map(visitPropertyKeyValues).getOrElse(Map.empty) | ||
|
@@ -1104,7 +1107,7 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
provider = Some(DDLUtils.HIVE_PROVIDER), | ||
partitionColumnNames = partitionCols.map(_.name), | ||
properties = properties, | ||
comment = comment) | ||
comment = Option(ctx.comment).map(string)) | ||
|
||
val mode = if (ifNotExists) SaveMode.Ignore else SaveMode.ErrorIfExists | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was
external
a typo at here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh it is a typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup