You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Acfboy (via GitHub)" <gi...@apache.org> on 2026/02/16 14:06:09 UTC
[PR] fix: negation operator escapes type checking at logical planning phase [datafusion]
Acfboy opened a new pull request, #20383:
URL: https://github.com/apache/datafusion/pull/20383
## 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 #20355 .
## 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.
-->
While generating logical plan, it invokes `Expr::to_field to check types and get filed of each expressions in query . However, in the `Expr::Negative` branch it only returns the field of its inner expression without checking.
## 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.
-->
Added type validation logic to Expr::to_field and Expr::get_type for Expr::Negative.
## 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)?
-->
Yes. All tests passed.
## Are there any user-facing changes?
Yes. Users will now receive type error reports at the logical planning phase rather than during execution or physical planning, which is the expected behavior.
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
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: negation operator escapes type checking at logical planning phase [datafusion]
Posted by "Acfboy (via GitHub)" <gi...@apache.org>.
Acfboy commented on code in PR #20383:
URL: https://github.com/apache/datafusion/pull/20383#discussion_r2813128777
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -697,6 +716,13 @@ pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subq
})
}
+/// Determine whether the given data type `dt` supports the negation operator.
+///
+/// Negation is supported for numeric, interval, timestamp types, and null.
+pub fn is_negatable(dt: &DataType) -> bool {
+ dt.is_numeric() || is_interval(dt) || is_timestamp(dt) || dt.is_null()
Review Comment:
This is to ensure consistency with the current codebase, as several tests (e.g., test_simplify_negated_bitwise_and in the simplifier) already cover negation on unsigned types. I'm still evaluating if this behavior is intentional or an oversight. It appears there may be some issues with how negation is handled in the optimizer; specifically, it seems the negation operator might also be representing a bitwise NOT during the planning phase. For example:
https://github.com/apache/datafusion/blob/b0566c57cc0b9ee2db8465f53fbd0d6e9e08798c/datafusion/optimizer/src/simplify_expressions/utils.rs#L254-L257
For now, I've kept the logic consistent with the previous behavior.
--
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: negation operator escapes type checking at logical planning phase [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #20383: fix: negation operator escapes type checking at logical planning phase
URL: https://github.com/apache/datafusion/pull/20383
--
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: negation operator escapes type checking at logical planning phase [datafusion]
Posted by "Acfboy (via GitHub)" <gi...@apache.org>.
Acfboy commented on code in PR #20383:
URL: https://github.com/apache/datafusion/pull/20383#discussion_r2813136893
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -697,6 +716,13 @@ pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subq
})
}
+/// Determine whether the given data type `dt` supports the negation operator.
+///
+/// Negation is supported for numeric, interval, timestamp types, and null.
+pub fn is_negatable(dt: &DataType) -> bool {
Review Comment:
Sorry for my oversight.
--
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: negation operator escapes type checking at logical planning phase [datafusion]
Posted by "Acfboy (via GitHub)" <gi...@apache.org>.
Acfboy commented on code in PR #20383:
URL: https://github.com/apache/datafusion/pull/20383#discussion_r2813128777
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -697,6 +716,13 @@ pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subq
})
}
+/// Determine whether the given data type `dt` supports the negation operator.
+///
+/// Negation is supported for numeric, interval, timestamp types, and null.
+pub fn is_negatable(dt: &DataType) -> bool {
+ dt.is_numeric() || is_interval(dt) || is_timestamp(dt) || dt.is_null()
Review Comment:
To ensure consistency with the current codebase, as several tests (e.g., test_simplify_negated_bitwise_and in the simplifier) already cover negation on unsigned types. I'm still evaluating if this behavior is intentional or an oversight. It appears there may be some issues with how negation is handled in the optimizer; specifically, it seems the negation operator might also be representing a bitwise NOT during the planning phase. For example:
https://github.com/apache/datafusion/blob/b0566c57cc0b9ee2db8465f53fbd0d6e9e08798c/datafusion/optimizer/src/simplify_expressions/utils.rs#L254-L257
For now, I've kept the logic consistent with the previous behavior.
--
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: negation operator escapes type checking at logical planning phase [datafusion]
Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on code in PR #20383:
URL: https://github.com/apache/datafusion/pull/20383#discussion_r2813019236
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -697,6 +716,13 @@ pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subq
})
}
+/// Determine whether the given data type `dt` supports the negation operator.
+///
+/// Negation is supported for numeric, interval, timestamp types, and null.
+pub fn is_negatable(dt: &DataType) -> bool {
+ dt.is_numeric() || is_interval(dt) || is_timestamp(dt) || dt.is_null()
Review Comment:
When creating the physical expr, we don't allow unsigned numerics https://github.com/apache/datafusion/blob/7d217b1ec3bd4d447961e84bc3fe7f7708546b2d/datafusion/physical-expr/src/expressions/negative.rs#L195-L199
Seems like these two checks ought to be consistent with one another.
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -697,6 +716,13 @@ pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subq
})
}
+/// Determine whether the given data type `dt` supports the negation operator.
+///
+/// Negation is supported for numeric, interval, timestamp types, and null.
+pub fn is_negatable(dt: &DataType) -> bool {
Review Comment:
Why is this `pub`?
--
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: negation operator escapes type checking at logical planning phase [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20383:
URL: https://github.com/apache/datafusion/pull/20383#issuecomment-4309999633
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.
--
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