-
Notifications
You must be signed in to change notification settings - Fork 206
feat: enable decimal to decimal cast of different precision and scale #1086
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
Changes from all commits
471c2a7
3062de2
b8bf29e
d4ee388
647ad01
cdae3a2
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 |
---|---|---|
|
@@ -895,6 +895,34 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper { | |
} | ||
} | ||
|
||
test("cast between decimals with different precision and scale") { | ||
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. It's great to see that some basic tests now pass. I assume there must have been improvements in DataFusion since we started this project. I'd like to see the tests cover more scenarios, such as:
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. +1 for more scenarios
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’m adding more cases, negative scale throws scale exception in spark as well. |
||
// cast between default Decimal(38, 18) to Decimal(6,2) | ||
val values = Seq(BigDecimal("12345.6789"), BigDecimal("9876.5432"), BigDecimal("123.4567")) | ||
val df = withNulls(values) | ||
.toDF("b") | ||
.withColumn("a", col("b").cast(DecimalType(6, 2))) | ||
checkSparkAnswer(df) | ||
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. FYI, ideally this should be checkSparkAnswerAndOperator once precision change gets supported |
||
} | ||
|
||
test("cast between decimals with higher precision than source") { | ||
// cast between Decimal(10, 2) to Decimal(10,4) | ||
castTest(generateDecimalsPrecision10Scale2(), DataTypes.createDecimalType(10, 4)) | ||
} | ||
|
||
test("cast between decimals with negative precision") { | ||
// cast to negative scale | ||
checkSparkMaybeThrows( | ||
spark.sql("select a, cast(a as DECIMAL(10,-4)) from t order by a")) match { | ||
case (expected, actual) => | ||
assert(expected.contains("PARSE_SYNTAX_ERROR") === actual.contains("PARSE_SYNTAX_ERROR")) | ||
} | ||
} | ||
|
||
test("cast between decimals with zero precision") { | ||
// cast between Decimal(10, 2) to Decimal(10,0) | ||
castTest(generateDecimalsPrecision10Scale2(), DataTypes.createDecimalType(10, 0)) | ||
} | ||
|
||
private def generateFloats(): DataFrame = { | ||
withNulls(gen.generateFloats(dataSize)).toDF("a") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,11 +231,9 @@ abstract class CometTestBase | |
df: => DataFrame): (Option[Throwable], Option[Throwable]) = { | ||
var expected: Option[Throwable] = None | ||
withSQLConf(CometConf.COMET_ENABLED.key -> "false") { | ||
val dfSpark = Dataset.ofRows(spark, df.logicalPlan) | ||
expected = Try(dfSpark.collect()).failed.toOption | ||
expected = Try(Dataset.ofRows(spark, df.logicalPlan).collect()).failed.toOption | ||
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. if the plan parsing encounters a problem like 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. so the change here is just formatting: changing from 2 lines to 1? 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. Earlier only df.collect() part was inside the Try, I also included df.logicalPlan - this includes any exception during parsing, in this example, it was throwing parsing error for this select a, cast(a as DECIMAL(10,-4)) from t order by a |
||
} | ||
val dfComet = Dataset.ofRows(spark, df.logicalPlan) | ||
val actual = Try(dfComet.collect()).failed.toOption | ||
val actual = Try(Dataset.ofRows(spark, df.logicalPlan).collect()).failed.toOption | ||
(expected, actual) | ||
} | ||
|
||
|
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.
higher to lower precision conversion in Datafusion changing the integer part, hence marked it as incompatible().
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.
I will change the PR description to
part of
#375 instead ofclosing
...