-
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
- Loading branch information
Showing
6 changed files
with
477 additions
and
7 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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
/* | ||
* 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.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 isAtomicTypeChangeSupported(fromType: AtomicType, toType: AtomicType): Boolean = | ||
(fromType, toType) match { | ||
case (from, to) if from == to => true | ||
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.