-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: Return Int for Date - Date instead of duration #19563
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,7 +260,25 @@ impl<'a> BinaryTypeCoercer<'a> { | |
| ) | ||
| }) | ||
| } | ||
| Minus if is_date_minus_date(lhs, rhs) => { | ||
| return Ok(Signature { | ||
| lhs: lhs.clone(), | ||
| rhs: rhs.clone(), | ||
| ret: Int64, | ||
| }); | ||
| } | ||
| Plus | Minus | Multiply | Divide | Modulo => { | ||
| // Special case: Date - Date should return Int64 (days difference) | ||
| // This aligns with PostgreSQL, DuckDB, and MySQL behavior | ||
| // See: https://www.postgresql.org/docs/current/functions-datetime.html | ||
| if matches!(self.op, Minus) && is_date_minus_date(lhs, rhs) { | ||
|
Contributor
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. This needs to be removed if the above suggestion was applied
Contributor
Author
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. Yes, I forgot |
||
| return Ok(Signature { | ||
| lhs: lhs.clone(), | ||
| rhs: rhs.clone(), | ||
| ret: Int64, | ||
| }); | ||
| } | ||
|
|
||
| if let Ok(ret) = self.get_result(lhs, rhs) { | ||
| // Temporal arithmetic, e.g. Date32 + Interval | ||
| Ok(Signature{ | ||
|
|
@@ -281,6 +299,7 @@ impl<'a> BinaryTypeCoercer<'a> { | |
| ret, | ||
| }) | ||
| } else if let Some(coerced) = temporal_coercion_strict_timezone(lhs, rhs) { | ||
|
|
||
| // Temporal arithmetic by first coercing to a common time representation | ||
| // e.g. Date32 - Timestamp | ||
| let ret = self.get_result(&coerced, &coerced).map_err(|e| { | ||
|
|
@@ -351,6 +370,15 @@ fn is_decimal(data_type: &DataType) -> bool { | |
| ) | ||
| } | ||
|
|
||
| /// Returns true if both operands are Date types (Date32 or Date64) | ||
| /// Used to detect Date - Date operations which should return Int64 (days difference) | ||
| fn is_date_minus_date(lhs: &DataType, rhs: &DataType) -> bool { | ||
| matches!( | ||
| (lhs, rhs), | ||
| (DataType::Date32, DataType::Date32) | (DataType::Date64, DataType::Date64) | ||
| ) | ||
| } | ||
|
|
||
| /// Coercion rules for mathematics operators between decimal and non-decimal types. | ||
| fn math_decimal_coercion( | ||
| lhs_type: &DataType, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
datafusion/sqllogictest/test_files/datetime/arith_date_date.slt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,15 @@ | ||
| # date - date → integer | ||
| # Subtract dates, producing the number of days elapsed | ||
| # date '2001-10-01' - date '2001-09-28' → 3 | ||
| # This aligns with PostgreSQL, DuckDB, and MySQL behavior | ||
| # Resolved by: https://github.com/apache/datafusion/issues/19528 | ||
|
|
||
| # note that datafusion returns Duration whereas postgres returns an int | ||
| # Tracking issue: https://github.com/apache/datafusion/issues/19528 | ||
|
|
||
| query ? | ||
| query I | ||
| SELECT '2001-10-01'::date - '2001-09-28'::date | ||
| ---- | ||
| 3 days 0 hours 0 mins 0 secs | ||
| 3 | ||
|
|
||
| query T | ||
| SELECT arrow_typeof('2001-10-01'::date - '2001-09-28'::date) | ||
| ---- | ||
| Duration(s) | ||
| Int64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.