-
Notifications
You must be signed in to change notification settings - Fork 182
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
fix: enable full decimal to decimal support #1385
base: main
Are you sure you want to change the base?
Changes from all commits
a2689a3
99727b8
42648f4
4df3e68
5cce0c4
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 |
---|---|---|
|
@@ -69,7 +69,8 @@ object GenerateDocs { | |
w.write("|-|-|-|\n".getBytes) | ||
for (fromType <- CometCast.supportedTypes) { | ||
for (toType <- CometCast.supportedTypes) { | ||
if (Cast.canCast(fromType, toType) && fromType != toType) { | ||
if (Cast.canCast(fromType, toType) && (fromType != toType || fromType.typeName | ||
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. @andygrove please check - I added this exception for decimal |
||
.contains("decimal"))) { | ||
val fromTypeName = fromType.typeName.replace("(10,2)", "") | ||
val toTypeName = toType.typeName.replace("(10,2)", "") | ||
CometCast.isSupported(fromType, toType, None, CometEvalMode.LEGACY) match { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,12 @@ import scala.util.Random | |
import scala.util.matching.Regex | ||
|
||
import org.apache.hadoop.fs.Path | ||
import org.apache.spark.sql.{CometTestBase, DataFrame, SaveMode} | ||
import org.apache.spark.sql.{CometTestBase, DataFrame, Row, SaveMode} | ||
import org.apache.spark.sql.catalyst.expressions.Cast | ||
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
import org.apache.spark.sql.functions.col | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types.{DataType, DataTypes, DecimalType} | ||
import org.apache.spark.sql.types.{DataType, DataTypes, DecimalType, StructField, StructType} | ||
|
||
import org.apache.comet.expressions.{CometCast, CometEvalMode, Compatible} | ||
|
||
|
@@ -909,12 +909,15 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper { | |
} | ||
|
||
test("cast between decimals with different precision and scale") { | ||
// 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) | ||
val rowData = Seq( | ||
Row(BigDecimal("12345.6789")), | ||
Row(BigDecimal("9876.5432")), | ||
Row(BigDecimal("123.4567"))) | ||
val df = spark.createDataFrame( | ||
spark.sparkContext.parallelize(rowData), | ||
StructType(Seq(StructField("a", DataTypes.createDecimalType(10, 4))))) | ||
|
||
castTest(df, DecimalType(6, 2)) | ||
} | ||
|
||
test("cast between decimals with higher precision than source") { | ||
|
@@ -1126,27 +1129,33 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper { | |
val cometMessage = | ||
if (cometException.getCause != null) cometException.getCause.getMessage | ||
else cometException.getMessage | ||
if (CometSparkSessionExtensions.isSpark40Plus) { | ||
// for Spark 4 we expect to sparkException carries the message | ||
assert( | ||
sparkException.getMessage | ||
.replace(".WITH_SUGGESTION] ", "]") | ||
.startsWith(cometMessage)) | ||
} else if (CometSparkSessionExtensions.isSpark34Plus) { | ||
// for Spark 3.4 we expect to reproduce the error message exactly | ||
assert(cometMessage == sparkMessage) | ||
// for comet decimal conversion throws ArrowError(string) from arrow - across spark versions the message dont match. | ||
if (sparkMessage.contains("cannot be represented as")) { | ||
cometMessage.contains("cannot be represented as") || cometMessage.contains( | ||
"too large to store") | ||
} else { | ||
Comment on lines
+1132
to
1136
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 think we still need to remove this new 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. when I removed the branch from the top, the test fails for double to decimal conversion with allow-incompatible flag, I think that is still using spark cast. Hence I had to put it back. |
||
// for Spark 3.3 we just need to strip the prefix from the Comet message | ||
// before comparing | ||
val cometMessageModified = cometMessage | ||
.replace("[CAST_INVALID_INPUT] ", "") | ||
.replace("[CAST_OVERFLOW] ", "") | ||
.replace("[NUMERIC_VALUE_OUT_OF_RANGE] ", "") | ||
|
||
if (sparkMessage.contains("cannot be represented as")) { | ||
assert(cometMessage.contains("cannot be represented as")) | ||
if (CometSparkSessionExtensions.isSpark40Plus) { | ||
// for Spark 4 we expect to sparkException carries the message | ||
assert( | ||
sparkException.getMessage | ||
.replace(".WITH_SUGGESTION] ", "]") | ||
.startsWith(cometMessage)) | ||
} else if (CometSparkSessionExtensions.isSpark34Plus) { | ||
// for Spark 3.4 we expect to reproduce the error message exactly | ||
assert(cometMessage == sparkMessage) | ||
} else { | ||
assert(cometMessageModified == sparkMessage) | ||
// for Spark 3.3 we just need to strip the prefix from the Comet message | ||
// before comparing | ||
val cometMessageModified = cometMessage | ||
.replace("[CAST_INVALID_INPUT] ", "") | ||
.replace("[CAST_OVERFLOW] ", "") | ||
.replace("[NUMERIC_VALUE_OUT_OF_RANGE] ", "") | ||
|
||
if (sparkMessage.contains("cannot be represented as")) { | ||
assert(cometMessage.contains("too large to store")) | ||
} else { | ||
assert(cometMessageModified == sparkMessage) | ||
} | ||
} | ||
} | ||
} | ||
|
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 think one can use a
default
value defined forFormatOptions
hereThere 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.
In the default CAST_OPTIONS which is replaced by this native_cast_options had two these set to
If we change it to
default
, I checkedFormatOptions::default()
implementation set theseHence kept it as it is defined inside default CAST_OPTIONS for comet.
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.
Fair enough. (The format options are used only to make the cast of timestamp to string compatible with Spark, and are not needed anywhere else) but I guess it is a good idea to be consistent everywhere.