-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Type Widening in ALTER TABLE CHANGE COLUMN #2645
Changes from all commits
9a5e7dd
73eb480
95c4fe2
0d0c90b
a227fa6
d462e32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 " + | ||
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. I guess this should be using the error framework? 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. This should never happen unless there's a bug in the implementation, so I wouldn't give it an error class. We typically wouldn't want to document that error as a user-facing error |
||
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 | ||
} | ||
} |
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 we are using -dev right now and its not ready for users using it. is it behind the isTesting flag?
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.
Yes, it is: https://github.com/databricks/runtime/pull/83562/files#diff-7b78502e6bca8772b4197ce6652fdfa72c4211c15b49e3b03ddfef7199cfcf0aR353