-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19115] [SQL] Supporting Create External Table Like Location #16638
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
Changes from 1 commit
adde008
713ca97
b80f8e6
71f1d12
9e59fb4
bb3660a
5dd21b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1140,14 +1140,18 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
| * | ||
| * For example: | ||
| * {{{ | ||
| * CREATE TABLE [IF NOT EXISTS] [db_name.]table_name | ||
| * LIKE [other_db_name.]existing_table_name | ||
| * CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name | ||
| * LIKE [other_db_name.]existing_table_name [locationSpec] | ||
| * }}} | ||
| */ | ||
| override def visitCreateTableLike(ctx: CreateTableLikeContext): LogicalPlan = withOrigin(ctx) { | ||
| val targetTable = visitTableIdentifier(ctx.target) | ||
| val sourceTable = visitTableIdentifier(ctx.source) | ||
| CreateTableLikeCommand(targetTable, sourceTable, ctx.EXISTS != null) | ||
| val location = Option(ctx.locationSpec).map(visitLocationSpec) | ||
| if (ctx.EXTERNAL != null && location.isEmpty) { | ||
|
||
| operationNotAllowed("CREATE EXTERNAL TABLE LIKE must be accompanied by LOCATION", ctx) | ||
| } | ||
|
||
| CreateTableLikeCommand(targetTable, sourceTable, location, ctx.EXISTS != null) | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ import org.apache.spark.util.Utils | |
| case class CreateTableLikeCommand( | ||
|
Member
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. Please update the comment of this class.
Author
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. ok,i will update it later,Thanks! |
||
| targetTable: TableIdentifier, | ||
| sourceTable: TableIdentifier, | ||
| location: Option[String], | ||
| ifNotExists: Boolean) extends RunnableCommand { | ||
|
|
||
| override def run(sparkSession: SparkSession): Seq[Row] = { | ||
|
|
@@ -70,12 +71,18 @@ case class CreateTableLikeCommand( | |
| sourceTableDesc.provider | ||
| } | ||
|
|
||
| val tblType = if (location.isEmpty) { | ||
| CatalogTableType.MANAGED | ||
| } else { | ||
| CatalogTableType.EXTERNAL | ||
| } | ||
|
|
||
| val newTableDesc = | ||
| CatalogTable( | ||
| identifier = targetTable, | ||
| tableType = CatalogTableType.MANAGED, | ||
| tableType = tblType, | ||
| // We are creating a new managed table, which should not have custom table location. | ||
| storage = sourceTableDesc.storage.copy(locationUri = None), | ||
| storage = sourceTableDesc.storage.copy(locationUri = location), | ||
| schema = sourceTableDesc.schema, | ||
| provider = newProvider, | ||
| partitionColumnNames = sourceTableDesc.partitionColumnNames, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -518,8 +518,8 @@ class HiveDDLCommandSuite extends PlanTest with SQLTestUtils with TestHiveSingle | |
|
|
||
| test("create table like") { | ||
| val v1 = "CREATE TABLE table1 LIKE table2" | ||
| val (target, source, exists) = parser.parsePlan(v1).collect { | ||
| case CreateTableLikeCommand(t, s, allowExisting) => (t, s, allowExisting) | ||
| val (target, source, location, exists) = parser.parsePlan(v1).collect { | ||
|
Contributor
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. add an assert to check |
||
| case CreateTableLikeCommand(t, s, l, allowExisting) => (t, s, l, allowExisting) | ||
| }.head | ||
| assert(exists == false) | ||
| assert(target.database.isEmpty) | ||
|
|
@@ -528,8 +528,8 @@ class HiveDDLCommandSuite extends PlanTest with SQLTestUtils with TestHiveSingle | |
| assert(source.table == "table2") | ||
|
|
||
| val v2 = "CREATE TABLE IF NOT EXISTS table1 LIKE table2" | ||
|
Contributor
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. add one more test case to check CREATE TABLE LIKE with location |
||
| val (target2, source2, exists2) = parser.parsePlan(v2).collect { | ||
| case CreateTableLikeCommand(t, s, allowExisting) => (t, s, allowExisting) | ||
| val (target2, source2, location2, exists2) = parser.parsePlan(v2).collect { | ||
| case CreateTableLikeCommand(t, s, l, allowExisting) => (t, s, l, allowExisting) | ||
| }.head | ||
| assert(exists2) | ||
| assert(target2.database.isEmpty) | ||
|
|
||
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.
since Spark 2.2, we wanna hide the manage/external concept from users. It looks reasonable to add a
LOCATIONstatement inCREATE TABLE LIKE, but do we really need theEXTERNALkeyword? We don't need to be exactly same with hive.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.
I am fine
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.
ok then let's simplify the logic: if
locationis specified, we create an external table internally. Else, create managed table.