Skip to content

Commit

Permalink
AL-6084 in Cast for method of canCast, when DecimalType cast to Doubl…
Browse files Browse the repository at this point in the history
…eType add transformable logic (Kyligence#542)

* AL-6084 in Cast for method of canCast, when DecimalType cast DecimalType to DoubleType add suit logical
  • Loading branch information
songzhxlh-max authored and hellozepp committed Aug 10, 2023
1 parent e37ed33 commit 51bd242
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ case class DecimalType(precision: Int, scale: Int) extends FractionalType {
(precision - scale) <= (dt.precision - dt.scale) && scale <= dt.scale
case dt: IntegralType =>
isTighterThanInternal(DecimalType.forType(dt))
case dt: DoubleType =>
isTighterThanInternal(DecimalType(precision, scale))
case _ => false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class StrictDataTypeWriteCompatibilitySuite extends DataTypeWriteCompatibilityBa
}
}

test("Check cast: decimalType to doubleType allowed") {
assert(canCast(DecimalType(30, 15).defaultConcreteType, DoubleType))
assert(canCast(DecimalType(38, 18).defaultConcreteType, DoubleType))
assert(canCast(DecimalType(38, 6).defaultConcreteType, DoubleType))
}

test("Check array types: unsafe casts are not allowed") {
val arrayOfLong = ArrayType(LongType)
val arrayOfInt = ArrayType(IntegerType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1255,13 +1255,9 @@ class JDBCV2Suite extends QueryTest with SharedSparkSession with ExplainSuiteHel

val df4 = spark.table("h2.test.employee")
.filter(($"salary" > 1000d).and($"salary" < 12000d))
checkFiltersRemoved(df4, ansiMode)
val expectedPlanFragment4 = if (ansiMode) {
"PushedFilters: [SALARY IS NOT NULL, " +
"CAST(SALARY AS double) > 1000.0, CAST(SALARY AS double) < 12000.0], "
} else {
"PushedFilters: [SALARY IS NOT NULL], "
}
checkFiltersRemoved(df4)
val expectedPlanFragment4 = "PushedFilters: [SALARY IS NOT NULL, " +
"SALARY > 1000.00, SALARY < 12000.00], "
checkPushedInfo(df4, expectedPlanFragment4)
checkAnswer(df4, Seq(Row(1, "amy", 10000, 1000, true),
Row(1, "cathy", 9000, 1200, false), Row(2, "david", 10000, 1300, true)))
Expand All @@ -1274,7 +1270,8 @@ class JDBCV2Suite extends QueryTest with SharedSparkSession with ExplainSuiteHel
"PushedFilters: [DEPT IS NOT NULL, ABS(DEPT - 3) > 1, " +
"(COALESCE(CAST(SALARY AS double), BONUS)) > 2000.0]"
} else {
"PushedFilters: [DEPT IS NOT NULL]"
"PushedFilters: [DEPT IS NOT NULL, " +
"(COALESCE(CAST(SALARY AS double), BONUS)) > 2000.0]"
}
checkPushedInfo(df5, expectedPlanFragment5)
checkAnswer(df5, Seq(Row(1, "amy", 10000, 1000, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,6 @@ class InsertSuite extends DataSourceTest with SharedSparkSession {
}.getMessage
assert(msg.contains("Cannot safely cast 'i': bigint to int"))

msg = intercept[AnalysisException] {
sql("insert into t select 1, 2.0")
}.getMessage
assert(msg.contains("Cannot safely cast 'd': decimal(2,1) to double"))

msg = intercept[AnalysisException] {
sql("insert into t select 1, 2.0D, 3")
}.getMessage
Expand Down

0 comments on commit 51bd242

Please sign in to comment.