-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type Widening in ALTER TABLE CHANGE COLUMN
## Description This change introduces the `typeWidening` delta table feature, allowing to widen the type of existing columns and fields in a delta table using the `ALTER TABLE CHANGE COLUMN TYPE` or `ALTER TABLE REPLACE COLUMNS` commands. The table feature is introduced as `typeWidening-dev` during implementation and is available in testing only. For now, only byte -> short -> int are supported. Other changes will require support in the Spark parquet reader that will be introduced in Spark 4.0 Type widening feature request: #2622 Type Widening protocol RFC: #2624 A new test suite `DeltaTypeWideningSuite` is created, containing: - `DeltaTypeWideningAlterTableTests`: Covers applying supported and unsupported type changes on partitioned columns, non-partitioned columns and nested fields - `DeltaTypeWideningTableFeatureTests`: Covers adding the `typeWidening` table feature ## This PR introduces the following *user-facing* changes The table feature is available in testing only, there's no user-facing changes as of now. The type widening table feature will introduce the following changes: - Adding the `typeWidening` via a table property: ``` ALTER TABLE t SET TBLPROPERTIES (‘delta.enableTypeWidening' = true) ``` - Apply a widening type change: ``` ALTER TABLE t CHANGE COLUMN int_col TYPE long ``` or ``` ALTER TABLE t REPLACE COLUMNS int_col TYPE long ``` Note: both ALTER TABLE commands reuse the existing syntax for setting a table property and applying a type change, no new SQL syntax is being introduced by this feature. Closes #2645 GitOrigin-RevId: 2ca0e6b22ec24b304241460553547d0d4c6026a2
- Loading branch information
1 parent
eb59d4a
commit 9b3fa0a
Showing
6 changed files
with
586 additions
and
4 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
63 changes: 63 additions & 0 deletions
63
spark/src/main/scala/org/apache/spark/sql/delta/TypeWidening.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,63 @@ | ||
/* | ||
* 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.actions.{Metadata, Protocol, TableFeatureProtocolUtils} | ||
|
||
import org.apache.spark.sql.catalyst.expressions.Cast | ||
import org.apache.spark.sql.types._ | ||
|
||
object TypeWidening { | ||
|
||
/** | ||
* Returns whether the protocol version supports the Type Widening table feature. | ||
*/ | ||
def isSupported(protocol: Protocol): Boolean = | ||
protocol.isFeatureSupported(TypeWideningTableFeature) | ||
|
||
/** | ||
* Returns whether Type Widening is enabled on this table version. Checks that Type Widening is | ||
* supported, which is a pre-requisite for enabling Type Widening, throws an error if | ||
* not. When Type Widening is enabled, the type of existing columns or fields can be widened | ||
* using ALTER TABLE CHANGE COLUMN. | ||
*/ | ||
def isEnabled(protocol: Protocol, metadata: Metadata): Boolean = { | ||
val isEnabled = DeltaConfigs.ENABLE_TYPE_WIDENING.fromMetaData(metadata) | ||
if (isEnabled && !isSupported(protocol)) { | ||
throw new IllegalStateException( | ||
s"Table property '${DeltaConfigs.ENABLE_TYPE_WIDENING.key}' is " + | ||
s"set on the table but this table version doesn't support table feature " + | ||
s"'${TableFeatureProtocolUtils.propertyKey(TypeWideningTableFeature)}'.") | ||
} | ||
isEnabled | ||
} | ||
|
||
/** | ||
* Returns whether the given type change is eligible for widening. This only checks atomic types. | ||
* It is the responsibility of the caller to recurse into structs, maps and arrays. | ||
*/ | ||
def isTypeChangeSupported(fromType: AtomicType, toType: AtomicType): Boolean = | ||
(fromType, toType) match { | ||
case (from, to) if from == to => true | ||
// All supported type changes below are supposed to be widening, but to be safe, reject any | ||
// non-widening change upfront. | ||
case (from, to) if !Cast.canUpCast(from, to) => false | ||
case (ByteType, ShortType) => true | ||
case (ByteType | ShortType, IntegerType) => true | ||
case _ => false | ||
} | ||
} |
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.