Skip to content

fix: div and rem by negative zero#960

Merged
kazuyukitanimura merged 6 commits intoapache:mainfrom
kazuyukitanimura:fix-neative-zero-div
Sep 24, 2024
Merged

fix: div and rem by negative zero#960
kazuyukitanimura merged 6 commits intoapache:mainfrom
kazuyukitanimura:fix-neative-zero-div

Conversation

@kazuyukitanimura
Copy link
Copy Markdown
Contributor

@kazuyukitanimura kazuyukitanimura commented Sep 23, 2024

Which issue does this PR close?

Closes #521 and #665.

Rationale for this change

What changes are included in this PR?

This PR normalize the right hand of div and rem

How are these changes tested?

Test updated

@kazuyukitanimura
Copy link
Copy Markdown
Contributor Author

Copy link
Copy Markdown
Contributor

@mbutrovich mbutrovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just comments and questions from me.

```sh
./mvnw -pl spark -Dsuites="org.apache.spark.sql.comet.CometTPCDSV1_4_PlanStabilitySuite" test
./mvnw -pl spark -Dsuites="org.apache.spark.sql.comet.CometTPCDSV1_4_PlanStabilitySuite" -nsu test
./mvnw -pl spark -Dsuites="org.apache.spark.sql.comet.CometTPCDSV1_4_PlanStabilitySuite" -Pspark-3.5 -nsu test
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you added these steps to the testing commands, does it makes sense to duplicate them below for the generation commands?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.

def normalizeNaNAndZero(expr: Expression): Expression = {
expr match {
case _: KnownFloatingPointNormalized => expr
case FloatLiteral(f) if !f.equals(-0.0f) => expr
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just clarifying: this is saying that as long as the literal isn't -0.0, we don't have to normalize it? And it doesn't look like there's any way to define a literal NaN, so we don't handle that case?

Copy link
Copy Markdown
Contributor Author

@kazuyukitanimura kazuyukitanimura Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just clarifying: this is saying that as long as the literal isn't -0.0, we don't have to normalize it?

Exactly

And it doesn't look like there's any way to define a literal NaN, so we don't handle that case?

We can create a literal NaN, but Spark NormalizeNaNAndZero is not doing much for literal NaN. I think NaN normalization matters only when some expression results are NaN https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NormalizeFloatingNumbers.scala#L194


```sh
./mvnw -pl spark -Dsuites="org.apache.spark.sql.comet.CometTPCDSV1_4_PlanStabilitySuite" test
./mvnw -pl spark -Dsuites="org.apache.spark.sql.comet.CometTPCDSV1_4_PlanStabilitySuite" -nsu test
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add -nsu?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It helps for faster turnaround

val zero = Literal.default(expression.dataType)
If(EqualTo(expression, zero), Literal.create(null, expression.dataType), expression)
expression match {
case _: Literal if expression != zero => expression
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indirectly related, this will avoid to put EqualTo for non zero literals for divisions

@kazuyukitanimura
Copy link
Copy Markdown
Contributor Author

kazuyukitanimura commented Sep 24, 2024

Closes #585

@kazuyukitanimura kazuyukitanimura merged commit 5663fc2 into apache:main Sep 24, 2024
@kazuyukitanimura
Copy link
Copy Markdown
Contributor Author

Merged Thank you @mbutrovich @viirya

coderfender pushed a commit to coderfender/datafusion-comet that referenced this pull request Dec 13, 2025
## 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 apache#123` indicates that this PR will close issue apache#123.
-->

N/A

## 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.
-->

Apply OSS 0.3.0 changes.

## 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.
-->

```
84cccf7 docs: Add notes for IntelliJ code size limits for code inspections. (apache#985)
dcc4a8a fix: The spilled_bytes metric of CometSortExec should be size instead of time (apache#984)
f64553b chore: fix compatibility guide (apache#978)
0ee7df8 chore: Enable additional CreateArray tests (apache#928)
a690e9d perf: Remove one redundant CopyExec for SMJ (apache#962)
a8156b5 chore: update rem expression guide (apache#976)
317a534 fix: Use the number of rows from underlying arrays instead of logical row count from RecordBatch (apache#972)
22561c4 doc: add documentation interlinks (apache#975)
b4de8e0 chore: Update benchmarks results based on 0.3.0-rc1 (apache#969)
94093f3 chore: fix publish-to-maven script (apache#966)
f31f6cc Generate changelog for 0.3.0 release (apache#964)
5663fc2 fix: div and rem by negative zero (apache#960)
50517f6 perf: Optimize decimal precision check in decimal aggregates (sum and avg) (apache#952)
5b3f7bc fix: CometScanExec on Spark 3.5.2 (apache#915)
8410c71 chore: clarify tarball installation (apache#959)
459b2b0 fix: window function range offset should be long instead of int (apache#733)
```


## 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)?
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modulus can produce incorrect results in some cases

3 participants