|
| 1 | +/* |
| 2 | + * Copyright (2021) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.spark.sql.delta |
| 18 | + |
| 19 | +import org.apache.spark.sql.delta.actions.{Metadata, Protocol, TableFeatureProtocolUtils} |
| 20 | + |
| 21 | +import org.apache.spark.sql.catalyst.expressions.Cast |
| 22 | +import org.apache.spark.sql.types._ |
| 23 | + |
| 24 | +object TypeWidening { |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns whether the protocol version supports the Type Widening table feature. |
| 28 | + */ |
| 29 | + def isSupported(protocol: Protocol): Boolean = |
| 30 | + protocol.isFeatureSupported(TypeWideningTableFeature) |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns whether Type Widening is enabled on this table version. Checks that Type Widening is |
| 34 | + * supported, which is a pre-requisite for enabling Type Widening, throws an error if |
| 35 | + * not. When Type Widening is enabled, the type of existing columns or fields can be widened |
| 36 | + * using ALTER TABLE CHANGE COLUMN. |
| 37 | + */ |
| 38 | + def isEnabled(protocol: Protocol, metadata: Metadata): Boolean = { |
| 39 | + val isEnabled = DeltaConfigs.ENABLE_TYPE_WIDENING.fromMetaData(metadata) |
| 40 | + if (isEnabled && !isSupported(protocol)) { |
| 41 | + throw new IllegalStateException( |
| 42 | + s"Table property '${DeltaConfigs.ENABLE_TYPE_WIDENING.key}' is " + |
| 43 | + s"set on the table but this table version doesn't support table feature " + |
| 44 | + s"'${TableFeatureProtocolUtils.propertyKey(TypeWideningTableFeature)}'.") |
| 45 | + } |
| 46 | + isEnabled |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns whether the given type change is eligible for widening. This only checks atomic types. |
| 51 | + * It is the responsibility of the caller to recurse into structs, maps and arrays. |
| 52 | + */ |
| 53 | + def isTypeChangeSupported(fromType: AtomicType, toType: AtomicType): Boolean = |
| 54 | + (fromType, toType) match { |
| 55 | + case (from, to) if from == to => true |
| 56 | + // All supported type changes below are supposed to be widening, but to be safe, reject any |
| 57 | + // non-widening change upfront. |
| 58 | + case (from, to) if !Cast.canUpCast(from, to) => false |
| 59 | + case (ByteType, ShortType) => true |
| 60 | + case (ByteType | ShortType, IntegerType) => true |
| 61 | + case _ => false |
| 62 | + } |
| 63 | +} |
0 commit comments