-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: Use dynamic timezone in now() function for accurate timestamp #18017
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
base: main
Are you sure you want to change the base?
Changes from all commits
0e75f6a
33732c5
b81ebf6
c396d63
9f1b55f
048a6f1
e7a6ab9
ed73c86
391ecf7
589cddd
cab15c0
179607a
82a643e
d5a9f95
d8e4905
77ca3b6
c2b8b5d
f9fbf67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,14 @@ use arrow::datatypes::DataType::Timestamp; | |
use arrow::datatypes::TimeUnit::Nanosecond; | ||
use arrow::datatypes::{DataType, Field, FieldRef}; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use datafusion_common::config::ConfigOptions; | ||
use datafusion_common::{internal_err, Result, ScalarValue}; | ||
use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo}; | ||
use datafusion_expr::{ | ||
ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarUDFImpl, Signature, | ||
Volatility, | ||
ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarUDF, ScalarUDFImpl, | ||
Signature, Volatility, | ||
}; | ||
use datafusion_macros::user_doc; | ||
|
||
|
@@ -41,19 +43,30 @@ The `now()` return value is determined at query time and will return the same ti | |
pub struct NowFunc { | ||
signature: Signature, | ||
aliases: Vec<String>, | ||
timezone: Option<Arc<str>>, | ||
} | ||
|
||
impl Default for NowFunc { | ||
fn default() -> Self { | ||
Self::new() | ||
Self::new_with_config(&ConfigOptions::default()) | ||
} | ||
} | ||
|
||
impl NowFunc { | ||
#[deprecated(since = "50.2.0", note = "use `new_with_config` instead")] | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::nullary(Volatility::Stable), | ||
aliases: vec!["current_timestamp".to_string()], | ||
timezone: Some(Arc::from("+00")), | ||
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. NowFunc::new() (and therefore Default) now hardcodes the timezone as "+00". 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. I think we could remove 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. Can someone explain the implications of this change on downstream crates? Does it mean that that 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. Update: Yes, this is the implication 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. @alamb With this implementation, yes. To me the logical solution is to allow the time_zone config option to be an Option (and default to None) and fix any existing usage to handle that ... or to detect when the time_zone config option is '' and handle that in the same manner ('' -> None tz) 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 agree |
||
} | ||
} | ||
|
||
pub fn new_with_config(config: &ConfigOptions) -> Self { | ||
Self { | ||
signature: Signature::nullary(Volatility::Stable), | ||
aliases: vec!["current_timestamp".to_string()], | ||
timezone: Some(Arc::from(config.execution.time_zone.as_str())), | ||
} | ||
} | ||
} | ||
|
@@ -77,10 +90,14 @@ impl ScalarUDFImpl for NowFunc { | |
&self.signature | ||
} | ||
|
||
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> { | ||
Some(Self::new_with_config(config).into()) | ||
} | ||
|
||
fn return_field_from_args(&self, _args: ReturnFieldArgs) -> Result<FieldRef> { | ||
Ok(Field::new( | ||
self.name(), | ||
Timestamp(Nanosecond, Some("+00:00".into())), | ||
Timestamp(Nanosecond, self.timezone.clone()), | ||
false, | ||
) | ||
.into()) | ||
|
@@ -106,8 +123,9 @@ impl ScalarUDFImpl for NowFunc { | |
.execution_props() | ||
.query_execution_start_time | ||
.timestamp_nanos_opt(); | ||
|
||
Ok(ExprSimplifyResult::Simplified(Expr::Literal( | ||
ScalarValue::TimestampNanosecond(now_ts, Some("+00:00".into())), | ||
ScalarValue::TimestampNanosecond(now_ts, self.timezone.clone()), | ||
None, | ||
))) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.