Repository navigation
fix(spark): date_part SECOND returns Decimal(8,6) #26905
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
Closed
ump45nose
wants to merge
1
commit into
apache:main
from
ump45nose:fix/26336-spark-date-part-second-decimal
Closed
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ | |
| // SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership | ||
| // SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements | ||
|
|
||
| use crate::function::datetime::date_part::{date_part_seconds, is_seconds_part}; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_expr::Expr; | ||
| use datafusion_expr::expr::ScalarFunction; | ||
| use datafusion_expr::planner::{ExprPlanner, PlannerResult}; | ||
| use datafusion_expr::planner::{ExprPlanner, PlannerResult, RawScalarExpr}; | ||
|
|
||
| #[derive(Default, Debug)] | ||
| pub struct SparkFunctionPlanner; | ||
|
|
@@ -14,6 +16,20 @@ impl ExprPlanner for SparkFunctionPlanner { | |
| &self, | ||
| args: Vec<Expr>, | ||
| ) -> datafusion_common::Result<PlannerResult<Vec<Expr>>> { | ||
| // EXTRACT(SECOND FROM ts) must return DECIMAL(8, 6) like date_part('SECOND', ts). | ||
| if let Some(part) = args.first().and_then(expr_string_literal) | ||
| && is_seconds_part(&part) | ||
| { | ||
| let date_expr = args.into_iter().nth(1).ok_or_else(|| { | ||
| datafusion_common::DataFusionError::Internal( | ||
|
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. Instead of building |
||
| "EXTRACT expects a source expression".to_string(), | ||
| ) | ||
| })?; | ||
| return Ok(PlannerResult::Planned(Expr::ScalarFunction( | ||
| ScalarFunction::new_udf(date_part_seconds(), vec![date_expr]), | ||
| ))); | ||
| } | ||
|
|
||
| Ok(PlannerResult::Planned(Expr::ScalarFunction( | ||
| ScalarFunction::new_udf(crate::function::datetime::date_part(), args), | ||
| ))) | ||
|
|
@@ -27,4 +43,44 @@ impl ExprPlanner for SparkFunctionPlanner { | |
| ScalarFunction::new_udf(crate::function::string::substring(), args), | ||
| ))) | ||
| } | ||
|
|
||
| fn plan_scalar( | ||
| &self, | ||
| expr: RawScalarExpr, | ||
| _schema: &datafusion_common::DFSchema, | ||
| ) -> datafusion_common::Result<PlannerResult<RawScalarExpr>> { | ||
| // date_part('SECOND', ts) / datepart(...) → DECIMAL(8, 6) seconds UDF. | ||
| // Done here so the return type is fixed before analysis; a user-written | ||
| // cast on the part literal is invisible to return_field_from_args. | ||
| if expr.func.name() == "date_part" | ||
| && let Some(part) = expr.args.first().and_then(expr_string_literal) | ||
| && is_seconds_part(&part) | ||
| { | ||
| let date_expr = expr.args.into_iter().nth(1).ok_or_else(|| { | ||
| datafusion_common::DataFusionError::Internal( | ||
|
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. Same here |
||
| "date_part expects a date/timestamp argument".to_string(), | ||
| ) | ||
| })?; | ||
| return Ok(PlannerResult::Planned(Expr::ScalarFunction( | ||
| ScalarFunction::new_udf(date_part_seconds(), vec![date_expr]), | ||
| ))); | ||
| } | ||
|
|
||
| Ok(PlannerResult::Original(expr)) | ||
| } | ||
| } | ||
|
|
||
| /// String value of a literal, or of a cast/try_cast wrapping a string literal. | ||
| fn expr_string_literal(expr: &Expr) -> Option<String> { | ||
| match expr { | ||
| Expr::Literal(sv, _) => match sv { | ||
| ScalarValue::Utf8(Some(s)) | ||
| | ScalarValue::Utf8View(Some(s)) | ||
| | ScalarValue::LargeUtf8(Some(s)) => Some(s.clone()), | ||
| _ => None, | ||
| }, | ||
| Expr::Cast(c) => expr_string_literal(c.expr.as_ref()), | ||
| Expr::TryCast(c) => expr_string_literal(c.expr.as_ref()), | ||
| _ => None, | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
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 found one regression in this PR: the new seconds UDF is also named
date_partbut it takes only the timestamp argument. As a result the default column name is nowdate_part(t.ts)instead ofdate_part(Utf8("SECOND"),t.ts). If we have 2 seconds aliases in the same SELECT, they now collide. Example:This runs on
mainbut fails with the following error in this branch -Projections require unique expression names but the expression "date_part(t.ts)" at position 0 and "date_part(t.ts)" at position 1 have the same name.Could the rewritten call keep the part literal as its first argument, and ignored at runtime, so the expression name stays the same as before? An slt case with this query would guard against it further regressing.