|
| 1 | +/* |
| 2 | + * Copyright (2024) 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.test.SQLTestUtils |
| 20 | +import org.apache.spark.sql.types._ |
| 21 | + |
| 22 | +/** |
| 23 | + * The set of type changes supported by type widening is different between Spark 3.5 and Spark 4.0. |
| 24 | + * See [[TypeWideningShims]]. This shim splits the test cases into supported and unsupported |
| 25 | + * accordingly for delta on Spark 3.5. |
| 26 | + */ |
| 27 | +trait TypeWideningTestCasesShims { |
| 28 | + self: DeltaTypeWideningTestCases with SQLTestUtils => |
| 29 | + |
| 30 | + import testImplicits._ |
| 31 | + |
| 32 | + // Type changes that are supported by all Parquet readers. Byte, Short, Int are all stored as |
| 33 | + // INT32 in parquet so these changes are guaranteed to be supported. |
| 34 | + protected val supportedTestCases: Seq[TypeEvolutionTestCase] = Seq( |
| 35 | + SupportedTypeEvolutionTestCase(ByteType, ShortType, |
| 36 | + Seq(1, -1, Byte.MinValue, Byte.MaxValue, null.asInstanceOf[Byte]), |
| 37 | + Seq(4, -4, Short.MinValue, Short.MaxValue, null.asInstanceOf[Short])), |
| 38 | + SupportedTypeEvolutionTestCase(ByteType, IntegerType, |
| 39 | + Seq(1, -1, Byte.MinValue, Byte.MaxValue, null.asInstanceOf[Byte]), |
| 40 | + Seq(4, -4, Int.MinValue, Int.MaxValue, null.asInstanceOf[Int])), |
| 41 | + SupportedTypeEvolutionTestCase(ShortType, IntegerType, |
| 42 | + Seq(1, -1, Short.MinValue, Short.MaxValue, null.asInstanceOf[Short]), |
| 43 | + Seq(4, -4, Int.MinValue, Int.MaxValue, null.asInstanceOf[Int])) |
| 44 | + ) |
| 45 | + |
| 46 | + // Type changes that are only supported in ALTER TABLE CHANGE COLUMN TYPE but are not considered |
| 47 | + // for automatic type widening. |
| 48 | + protected val alterTableOnlySupportedTestCases: Seq[TypeEvolutionTestCase] = Seq.empty |
| 49 | + |
| 50 | + // Test type changes that aren't supported. |
| 51 | + protected val unsupportedTestCases: Seq[TypeEvolutionTestCase] = Seq( |
| 52 | + UnsupportedTypeEvolutionTestCase(IntegerType, ByteType, |
| 53 | + Seq(1, 2, Int.MinValue)), |
| 54 | + UnsupportedTypeEvolutionTestCase(LongType, IntegerType, |
| 55 | + Seq(4, 5, Long.MaxValue)), |
| 56 | + UnsupportedTypeEvolutionTestCase(DoubleType, FloatType, |
| 57 | + Seq(987654321.987654321d, Double.NaN, Double.NegativeInfinity, |
| 58 | + Double.PositiveInfinity, Double.MinPositiveValue, |
| 59 | + Double.MinValue, Double.MaxValue)), |
| 60 | + UnsupportedTypeEvolutionTestCase(ByteType, DecimalType(2, 0), |
| 61 | + Seq(1, -1, Byte.MinValue)), |
| 62 | + UnsupportedTypeEvolutionTestCase(ShortType, DecimalType(4, 0), |
| 63 | + Seq(1, -1, Short.MinValue)), |
| 64 | + UnsupportedTypeEvolutionTestCase(IntegerType, DecimalType(9, 0), |
| 65 | + Seq(1, -1, Int.MinValue)), |
| 66 | + UnsupportedTypeEvolutionTestCase(LongType, DecimalType(19, 0), |
| 67 | + Seq(1, -1, Long.MinValue)), |
| 68 | + UnsupportedTypeEvolutionTestCase(TimestampNTZType, DateType, |
| 69 | + Seq("2020-03-17 15:23:15", "2023-12-31 23:59:59", "0001-01-01 00:00:00")), |
| 70 | + // Reduce scale |
| 71 | + UnsupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_INT_DIGITS, 2), |
| 72 | + DecimalType(Decimal.MAX_INT_DIGITS, 3), |
| 73 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_INT_DIGITS - 2) + ".99"))), |
| 74 | + // Reduce precision |
| 75 | + UnsupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_INT_DIGITS, 2), |
| 76 | + DecimalType(Decimal.MAX_INT_DIGITS - 1, 2), |
| 77 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_INT_DIGITS - 2) + ".99"))), |
| 78 | + // Reduce precision & scale |
| 79 | + UnsupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_LONG_DIGITS, 2), |
| 80 | + DecimalType(Decimal.MAX_INT_DIGITS - 1, 1), |
| 81 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_LONG_DIGITS - 2) + ".99"))), |
| 82 | + // Increase scale more than precision |
| 83 | + UnsupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_INT_DIGITS, 2), |
| 84 | + DecimalType(Decimal.MAX_INT_DIGITS + 1, 4), |
| 85 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_INT_DIGITS - 2) + ".99"))), |
| 86 | + // Smaller scale and larger precision. |
| 87 | + UnsupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_LONG_DIGITS, 2), |
| 88 | + DecimalType(Decimal.MAX_INT_DIGITS + 3, 1), |
| 89 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_LONG_DIGITS - 2) + ".99"))), |
| 90 | + SupportedTypeEvolutionTestCase(IntegerType, DoubleType, |
| 91 | + Seq(1, -1, Int.MinValue, Int.MaxValue, null.asInstanceOf[Int]), |
| 92 | + Seq(987654321.987654321d, -0d, 0d, Double.NaN, Double.NegativeInfinity, |
| 93 | + Double.PositiveInfinity, Double.MinPositiveValue, Double.MinValue, Double.MaxValue, |
| 94 | + null.asInstanceOf[Double])), |
| 95 | + SupportedTypeEvolutionTestCase(ByteType, DecimalType(10, 0), |
| 96 | + Seq(1, -1, Byte.MinValue, Byte.MaxValue, null.asInstanceOf[Byte]), |
| 97 | + Seq(BigDecimal("1.23"), BigDecimal("9" * 10), null.asInstanceOf[BigDecimal])), |
| 98 | + SupportedTypeEvolutionTestCase(ShortType, DecimalType(10, 0), |
| 99 | + Seq(1, -1, Short.MinValue, Short.MaxValue, null.asInstanceOf[Short]), |
| 100 | + Seq(BigDecimal("1.23"), BigDecimal("9" * 10), null.asInstanceOf[BigDecimal])), |
| 101 | + SupportedTypeEvolutionTestCase(IntegerType, DecimalType(10, 0), |
| 102 | + Seq(1, -1, Int.MinValue, Int.MaxValue, null.asInstanceOf[Int]), |
| 103 | + Seq(BigDecimal("1.23"), BigDecimal("9" * 10), null.asInstanceOf[BigDecimal])), |
| 104 | + SupportedTypeEvolutionTestCase(LongType, DecimalType(20, 0), |
| 105 | + Seq(1L, -1L, Long.MinValue, Long.MaxValue, null.asInstanceOf[Int]), |
| 106 | + Seq(BigDecimal("1.23"), BigDecimal("9" * 20), null.asInstanceOf[BigDecimal])), |
| 107 | + SupportedTypeEvolutionTestCase(ShortType, LongType, |
| 108 | + Seq(1, -1, Short.MinValue, Short.MaxValue, null.asInstanceOf[Short]), |
| 109 | + Seq(4L, -4L, Long.MinValue, Long.MaxValue, null.asInstanceOf[Long])), |
| 110 | + SupportedTypeEvolutionTestCase(IntegerType, LongType, |
| 111 | + Seq(1, -1, Int.MinValue, Int.MaxValue, null.asInstanceOf[Int]), |
| 112 | + Seq(4L, -4L, Long.MinValue, Long.MaxValue, null.asInstanceOf[Long])), |
| 113 | + SupportedTypeEvolutionTestCase(FloatType, DoubleType, |
| 114 | + Seq(1234.56789f, -0f, 0f, Float.NaN, Float.NegativeInfinity, Float.PositiveInfinity, |
| 115 | + Float.MinPositiveValue, Float.MinValue, Float.MaxValue, null.asInstanceOf[Float]), |
| 116 | + Seq(987654321.987654321d, -0d, 0d, Double.NaN, Double.NegativeInfinity, |
| 117 | + Double.PositiveInfinity, Double.MinPositiveValue, Double.MinValue, Double.MaxValue, |
| 118 | + null.asInstanceOf[Double])), |
| 119 | + SupportedTypeEvolutionTestCase(DateType, TimestampNTZType, |
| 120 | + Seq("2020-01-01", "2024-02-29", "1312-02-27"), |
| 121 | + Seq("2020-03-17 15:23:15.123456", "2058-12-31 23:59:59.999", "0001-01-01 00:00:00")), |
| 122 | + // Larger precision. |
| 123 | + SupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_INT_DIGITS, 2), |
| 124 | + DecimalType(Decimal.MAX_LONG_DIGITS, 2), |
| 125 | + Seq(BigDecimal("1.23"), BigDecimal("10.34"), null.asInstanceOf[BigDecimal]), |
| 126 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_LONG_DIGITS - 2) + ".99"), |
| 127 | + null.asInstanceOf[BigDecimal])), |
| 128 | + // Larger precision and scale, same physical type. |
| 129 | + SupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_INT_DIGITS - 1, 2), |
| 130 | + DecimalType(Decimal.MAX_INT_DIGITS, 3), |
| 131 | + Seq(BigDecimal("1.23"), BigDecimal("10.34"), null.asInstanceOf[BigDecimal]), |
| 132 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_INT_DIGITS - 3) + ".99"), |
| 133 | + null.asInstanceOf[BigDecimal])), |
| 134 | + // Larger precision and scale, different physical types. |
| 135 | + SupportedTypeEvolutionTestCase(DecimalType(Decimal.MAX_INT_DIGITS, 2), |
| 136 | + DecimalType(Decimal.MAX_LONG_DIGITS + 1, 3), |
| 137 | + Seq(BigDecimal("1.23"), BigDecimal("10.34"), null.asInstanceOf[BigDecimal]), |
| 138 | + Seq(BigDecimal("-67.89"), BigDecimal("9" * (Decimal.MAX_LONG_DIGITS - 2) + ".99"), |
| 139 | + null.asInstanceOf[BigDecimal])) |
| 140 | + ) |
| 141 | +} |
0 commit comments