forked from delta-io/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Spark] ALTER TABLE SYNC IDENTITY SQL support
- Loading branch information
1 parent
2c450fe
commit 88d27a6
Showing
8 changed files
with
270 additions
and
6 deletions.
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
44 changes: 44 additions & 0 deletions
44
spark/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/SyncIdentity.scala
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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.plans.logical | ||
|
||
import org.apache.spark.sql.catalyst.analysis.FieldName | ||
import org.apache.spark.sql.connector.catalog.TableChange | ||
import org.apache.spark.sql.connector.catalog.TableChange.ColumnChange | ||
|
||
/** | ||
* A `ColumnChange` to model `ALTER TABLE ALTER COLUMN SYNC IDENTITY` command. | ||
* | ||
* @param fieldNames The (potentially nested) column name. | ||
*/ | ||
case class SyncIdentity(fieldNames: Array[String]) extends ColumnChange { | ||
require(fieldNames.size == 1, "IDENTITY column cannot be a nested column.") | ||
} | ||
|
||
case class AlterColumnSyncIdentity( | ||
table: LogicalPlan, | ||
column: FieldName) | ||
extends AlterTableCommand { | ||
override def changes: Seq[TableChange] = { | ||
require(column.resolved, "FieldName should be resolved before it's converted to TableChange.") | ||
val colName = column.name.toArray | ||
Seq(SyncIdentity(colName)) | ||
} | ||
|
||
override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = | ||
copy(table = newChild) | ||
} |
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
116 changes: 116 additions & 0 deletions
116
spark/src/test/scala/org/apache/spark/sql/delta/IdentityColumnSyncSuite.scala
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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.delta | ||
|
||
import org.apache.spark.sql.delta.GeneratedAsIdentityType.GeneratedByDefault | ||
import org.apache.spark.sql.delta.sources.DeltaSourceUtils | ||
|
||
import org.apache.spark.sql.{AnalysisException, Row} | ||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.types._ | ||
|
||
|
||
/** | ||
* Identity Column test suite for the SYNC IDENTITY command. | ||
*/ | ||
trait IdentityColumnSyncSuiteBase | ||
extends IdentityColumnTestUtils { | ||
private val tblName = "identity_test" | ||
|
||
/** | ||
* Create and manage a table with a single identity column "id" generated by default and a single | ||
* String "value" column. | ||
*/ | ||
private def withSimpleGeneratedByDefaultTable( | ||
startsWith: Long, incrementBy: Long)(f: => Unit): Unit = { | ||
withTable(tblName) { | ||
createTable( | ||
tblName, | ||
Seq( | ||
IdentityColumnSpec( | ||
GeneratedByDefault, | ||
startsWith = Some(startsWith), | ||
incrementBy = Some(incrementBy)), | ||
TestColumnSpec(colName = "value", dataType = StringType) | ||
) | ||
) | ||
|
||
f | ||
} | ||
} | ||
|
||
test("alter table sync identity delta") { | ||
val starts = Seq(-1, 1) | ||
val steps = Seq(-3, 3) | ||
for (start <- starts; step <- steps) { | ||
withSimpleGeneratedByDefaultTable(start, step) { | ||
// Test empty table. | ||
val oldSchema = DeltaLog.forTable(spark, TableIdentifier(tblName)).snapshot.schema | ||
sql(s"ALTER TABLE $tblName ALTER COLUMN id SYNC IDENTITY") | ||
assert(DeltaLog.forTable(spark, TableIdentifier(tblName)).snapshot.schema == oldSchema) | ||
|
||
// Test a series of values that are not all following start and step configurations. | ||
for (i <- start to (start + step * 10)) { | ||
sql(s"INSERT INTO $tblName VALUES($i, 'v')") | ||
sql(s"ALTER TABLE $tblName ALTER COLUMN id SYNC IDENTITY") | ||
val expected = start + (((i - start) + (step - 1)) / step) * step | ||
val schema = DeltaLog.forTable(spark, TableIdentifier(tblName)).snapshot.schema | ||
assert(schema("id").metadata.getLong(DeltaSourceUtils.IDENTITY_INFO_HIGHWATERMARK) == | ||
expected) | ||
} | ||
} | ||
} | ||
} | ||
|
||
test("alter table sync identity overflow") { | ||
withSimpleGeneratedByDefaultTable(startsWith = 1L, incrementBy = 10L) { | ||
sql(s"INSERT INTO $tblName VALUES (${Long.MaxValue}, 'a')") | ||
intercept[ArithmeticException](sql(s"ALTER TABLE $tblName ALTER COLUMN id SYNC IDENTITY")) | ||
} | ||
} | ||
|
||
|
||
test("alter table sync identity non identity column") { | ||
withTable(tblName) { | ||
createTable( | ||
tblName, | ||
Seq( | ||
TestColumnSpec(colName = "id", dataType = LongType), | ||
TestColumnSpec(colName = "value", dataType = IntegerType) | ||
) | ||
) | ||
val ex = intercept[AnalysisException] { | ||
sql(s"ALTER TABLE $tblName ALTER COLUMN id SYNC IDENTITY") | ||
} | ||
assert(ex.getMessage.contains("ALTER TABLE ALTER COLUMN SYNC IDENTITY cannot be called")) | ||
} | ||
} | ||
} | ||
|
||
class IdentityColumnSyncScalaSuite | ||
extends IdentityColumnSyncSuiteBase | ||
with ScalaDDLTestUtils | ||
|
||
class IdentityColumnSyncScalaIdColumnMappingSuite | ||
extends IdentityColumnSyncSuiteBase | ||
with ScalaDDLTestUtils | ||
with DeltaColumnMappingEnableIdMode | ||
|
||
class IdentityColumnSyncScalaNameColumnMappingSuite | ||
extends IdentityColumnSyncSuiteBase | ||
with ScalaDDLTestUtils | ||
with DeltaColumnMappingEnableNameMode |