You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2025/01/22 22:07:12 UTC
[PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
andygrove opened a new pull request, #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes https://github.com/apache/datafusion-comet/issues/1294
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
Fix correctness issue
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
Fall back to Spark when hashing decimals with precision > 18
## How are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#discussion_r1933004117
##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -1929,19 +1929,45 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}
- test("hash functions with decimal input") {
- withTable("t1", "t2") {
- // Apache Spark: if it's a small decimal, i.e. precision <= 18, turn it into long and hash it.
- // Else, turn it into bytes and hash it.
- sql("create table t1(c1 decimal(18, 2)) using parquet")
- sql("insert into t1 values(1.23), (-1.23), (0.0), (null)")
- checkSparkAnswerAndOperator("select c1, hash(c1), xxhash64(c1) from t1 order by c1")
-
- // TODO: comet hash function is not compatible with spark for decimal with precision greater than 18.
- // https://github.com/apache/datafusion-comet/issues/1294
-// sql("create table t2(c1 decimal(20, 2)) using parquet")
-// sql("insert into t2 values(1.23), (-1.23), (0.0), (null)")
-// checkSparkAnswerAndOperator("select c1, hash(c1), xxhash64(c1) from t2 order by c1")
+ test("hash function with decimal input") {
+ val testPrecisionScales: Seq[(Int, Int)] = Seq(
+ (1, 0),
+ (17, 2),
+ (18, 2),
+ (19, 2),
+ (DecimalType.MAX_PRECISION, DecimalType.MAX_PRECISION - 1))
Review Comment:
Updated. Thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#issuecomment-2619596347
> Does this implicitly affect any data read that originated as `uint64`? I believe it gets converted to `DECIMAL(20,0)`.
Yes, in the context of a user calling the `hash` or `xxhash64` Spark SQL functions on that data. This PR does not change anything with respect to hashing as part of shuffle.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#issuecomment-2608456250
## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/1325?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
Attention: Patch coverage is `80.95238%` with `4 lines` in your changes missing coverage. Please review.
> Project coverage is 34.04%. Comparing base [(`f09f8af`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/f09f8af64c6599255e116a376f4f008f2fd63b43?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`28a8df4`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/28a8df4eb21afd418d6102e73f19624586e8bbab?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
> Report is 1 commits behind head on main.
| [Files with missing lines](https://app.codecov.io/gh/apache/datafusion-comet/pull/1325?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...k/src/main/scala/org/apache/comet/serde/hash.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/1325?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Fhash.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9oYXNoLnNjYWxh) | 69.23% | [2 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/1325?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## main #1325 +/- ##
=============================================
- Coverage 56.12% 34.04% -22.09%
- Complexity 976 979 +3
=============================================
Files 119 125 +6
Lines 11743 48130 +36387
Branches 2251 10560 +8309
=============================================
+ Hits 6591 16385 +9794
- Misses 4012 28423 +24411
- Partials 1140 3322 +2182
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/1325?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#discussion_r1933815284
##########
spark/src/main/scala/org/apache/comet/serde/hash.scala:
##########
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.comet.serde
+
+import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, Murmur3Hash, XxHash64}
+import org.apache.spark.sql.types.{DecimalType, IntegerType, LongType}
+
+import org.apache.comet.CometSparkSessionExtensions.withInfo
+import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, scalarExprToProtoWithReturnType, serializeDataType, supportedDataType}
+
+object CometXxHash64 extends CometExpressionSerde {
+ override def convert(
+ expr: Expression,
+ inputs: Seq[Attribute],
+ binding: Boolean): Option[ExprOuterClass.Expr] = {
+ if (!HashUtils.isSupportedType(expr)) {
+ return None
+ }
+ val hash = expr.asInstanceOf[XxHash64]
+ val exprs = hash.children.map(exprToProtoInternal(_, inputs, binding))
+ val seedBuilder = ExprOuterClass.Literal
+ .newBuilder()
+ .setDatatype(serializeDataType(LongType).get)
+ .setLongVal(hash.seed)
+ val seedExpr = Some(ExprOuterClass.Expr.newBuilder().setLiteral(seedBuilder).build())
+ // the seed is put at the end of the arguments
+ scalarExprToProtoWithReturnType("xxhash64", LongType, exprs :+ seedExpr: _*)
+ }
+}
+
+object CometMurmur3Hash extends CometExpressionSerde {
+ override def convert(
+ expr: Expression,
+ inputs: Seq[Attribute],
+ binding: Boolean): Option[ExprOuterClass.Expr] = {
+ if (!HashUtils.isSupportedType(expr)) {
+ return None
+ }
+ val hash = expr.asInstanceOf[Murmur3Hash]
+ val exprs = hash.children.map(exprToProtoInternal(_, inputs, binding))
+ val seedBuilder = ExprOuterClass.Literal
+ .newBuilder()
+ .setDatatype(serializeDataType(IntegerType).get)
+ .setIntVal(hash.seed)
+ val seedExpr = Some(ExprOuterClass.Expr.newBuilder().setLiteral(seedBuilder).build())
+ // the seed is put at the end of the arguments
+ scalarExprToProtoWithReturnType("murmur3_hash", IntegerType, exprs :+ seedExpr: _*)
+ }
+}
+
+private object HashUtils {
+ def isSupportedType(expr: Expression): Boolean = {
+ for (child <- expr.children) {
+ child.dataType match {
+ case dt: DecimalType if dt.precision > 18 =>
Review Comment:
There was an earlier PR that implemented the correct behavior for the case where precision is < 18.
https://github.com/apache/datafusion-comet/pull/1295
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#issuecomment-2622599915
Thanks for the review @kazuyukitanimura and @parthchandra
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#issuecomment-2619399645
@parthchandra @wForget fyi
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "kazuyukitanimura (via GitHub)" <gi...@apache.org>.
kazuyukitanimura commented on code in PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#discussion_r1932970709
##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -1929,19 +1929,45 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}
- test("hash functions with decimal input") {
- withTable("t1", "t2") {
- // Apache Spark: if it's a small decimal, i.e. precision <= 18, turn it into long and hash it.
- // Else, turn it into bytes and hash it.
- sql("create table t1(c1 decimal(18, 2)) using parquet")
- sql("insert into t1 values(1.23), (-1.23), (0.0), (null)")
- checkSparkAnswerAndOperator("select c1, hash(c1), xxhash64(c1) from t1 order by c1")
-
- // TODO: comet hash function is not compatible with spark for decimal with precision greater than 18.
- // https://github.com/apache/datafusion-comet/issues/1294
-// sql("create table t2(c1 decimal(20, 2)) using parquet")
-// sql("insert into t2 values(1.23), (-1.23), (0.0), (null)")
-// checkSparkAnswerAndOperator("select c1, hash(c1), xxhash64(c1) from t2 order by c1")
+ test("hash function with decimal input") {
+ val testPrecisionScales: Seq[(Int, Int)] = Seq(
+ (1, 0),
+ (17, 2),
+ (18, 2),
+ (19, 2),
+ (DecimalType.MAX_PRECISION, DecimalType.MAX_PRECISION - 1))
Review Comment:
nit: perhaps `MAX_SCALE - 1` instead of `DecimalType.MAX_PRECISION - 1`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#issuecomment-2619527291
Does this implicitly affect any data read that originated as `uint64`? I believe it gets converted to `DECIMAL(20,0)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: Fall back to Spark when hashing decimals with precision > 18 [datafusion-comet]
Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #1325:
URL: https://github.com/apache/datafusion-comet/pull/1325#discussion_r1933099806
##########
spark/src/main/scala/org/apache/comet/serde/hash.scala:
##########
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.comet.serde
+
+import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, Murmur3Hash, XxHash64}
+import org.apache.spark.sql.types.{DecimalType, IntegerType, LongType}
+
+import org.apache.comet.CometSparkSessionExtensions.withInfo
+import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, scalarExprToProtoWithReturnType, serializeDataType, supportedDataType}
+
+object CometXxHash64 extends CometExpressionSerde {
+ override def convert(
+ expr: Expression,
+ inputs: Seq[Attribute],
+ binding: Boolean): Option[ExprOuterClass.Expr] = {
+ if (!HashUtils.isSupportedType(expr)) {
+ return None
+ }
+ val hash = expr.asInstanceOf[XxHash64]
+ val exprs = hash.children.map(exprToProtoInternal(_, inputs, binding))
+ val seedBuilder = ExprOuterClass.Literal
+ .newBuilder()
+ .setDatatype(serializeDataType(LongType).get)
+ .setLongVal(hash.seed)
+ val seedExpr = Some(ExprOuterClass.Expr.newBuilder().setLiteral(seedBuilder).build())
+ // the seed is put at the end of the arguments
+ scalarExprToProtoWithReturnType("xxhash64", LongType, exprs :+ seedExpr: _*)
+ }
+}
+
+object CometMurmur3Hash extends CometExpressionSerde {
+ override def convert(
+ expr: Expression,
+ inputs: Seq[Attribute],
+ binding: Boolean): Option[ExprOuterClass.Expr] = {
+ if (!HashUtils.isSupportedType(expr)) {
+ return None
+ }
+ val hash = expr.asInstanceOf[Murmur3Hash]
+ val exprs = hash.children.map(exprToProtoInternal(_, inputs, binding))
+ val seedBuilder = ExprOuterClass.Literal
+ .newBuilder()
+ .setDatatype(serializeDataType(IntegerType).get)
+ .setIntVal(hash.seed)
+ val seedExpr = Some(ExprOuterClass.Expr.newBuilder().setLiteral(seedBuilder).build())
+ // the seed is put at the end of the arguments
+ scalarExprToProtoWithReturnType("murmur3_hash", IntegerType, exprs :+ seedExpr: _*)
+ }
+}
+
+private object HashUtils {
+ def isSupportedType(expr: Expression): Boolean = {
+ for (child <- expr.children) {
+ child.dataType match {
+ case dt: DecimalType if dt.precision > 18 =>
Review Comment:
From the issue it seemed that we get test failure even if the precision is less than 18. So do we want to fallback to Spark for all DecimalType values?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org