Skip to content
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

fix: built-in TIMESTAMP(str) function does not respect nano precision in language options #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions zetasql/public/functions/date_time_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,17 @@ absl::Status ConvertStringToTimestamp(absl::string_view str,
return ConvertStringToTimestamp(str, timezone, scale, timestamp);
}

absl::Status ConvertStringToTimestamp(absl::string_view str,
absl::string_view default_timezone_string,
TimestampScale scale,
bool allow_tz_in_str,
absl::Time* output) {
absl::TimeZone timezone;
ZETASQL_RETURN_IF_ERROR(MakeTimeZone(default_timezone_string, &timezone));
return ConvertStringToTimestamp(str, timezone, scale, allow_tz_in_str,
output);
}

absl::Status ConvertStringToTimestamp(absl::string_view str,
absl::TimeZone default_timezone,
TimestampScale scale,
Expand Down
10 changes: 10 additions & 0 deletions zetasql/public/functions/date_time_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,16 @@ absl::Status ConvertStringToTimestamp(absl::string_view str,
TimestampScale scale,
bool allow_tz_in_str, int64_t* timestamp);

// If the time zone is not included in the string, then
// <default_timezone_string> is applied for the conversion. Uses the
// canonical timestamp string format.
// If the time zone is included in the string and allow_tz_in_str is set to
// false, an error will be returned.
absl::Status ConvertStringToTimestamp(absl::string_view str,
absl::string_view default_timezone_string,
TimestampScale scale,
bool allow_tz_in_str, absl::Time* output)

// Populate the <output> with the values from input if the values on all fields
// are valid, return error otherwise.
absl::Status ConstructDate(int year, int month, int day, int32_t* output);
Expand Down
10 changes: 6 additions & 4 deletions zetasql/reference_impl/function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9231,19 +9231,21 @@ absl::StatusOr<Value> TimestampConversionFunction::Eval(
}
return Value::Timestamp(timestamp);
} else if (!args.empty() && args[0].type()->IsString()) {
int64_t timestamp_micros;
absl::Time timestamp;
if (args.size() == 2 && args[1].type()->IsString()) {
ZETASQL_RETURN_IF_ERROR(functions::ConvertStringToTimestamp(
args[0].string_value(), args[1].string_value(),
functions::kMicroseconds, false, &timestamp_micros));
GetTimestampScale(context->GetLanguageOptions()), false, &timestamp));
} else if (args.size() == 1) {
ZETASQL_RETURN_IF_ERROR(functions::ConvertStringToTimestamp(
args[0].string_value(), context->GetDefaultTimeZone(),
functions::kMicroseconds, true, &timestamp_micros));
GetTimestampScale(context->GetLanguageOptions()), true, &timestamp));
} else {
return MakeEvalError() << "Unsupported function: " << debug_name();
}
return Value::TimestampFromUnixMicros(timestamp_micros);
const Value result = Value::Timestamp(timestamp);
ZETASQL_RETURN_IF_ERROR(ValidateMicrosPrecision(result, context));
return result;
} else if (!args.empty() && args[0].type()->IsDate()) {
int64_t timestamp_micros;
if (args.size() == 2 && args[1].type()->IsString()) {
Expand Down