Skip to content

Commit

Permalink
Basics of implicit time/timestamp cast
Browse files Browse the repository at this point in the history
  • Loading branch information
pdet committed Apr 19, 2024
1 parent 1601d94 commit 239a1d1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
43 changes: 23 additions & 20 deletions src/execution/operator/csv_scanner/scanner/string_value_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,32 @@ void StringValueResult::AddValueToVector(const char *value_ptr, const idx_t size
false, state_machine.options.decimal_separator[0]);
break;
case LogicalTypeId::DATE: {
idx_t pos;
bool special;
success = Date::TryConvertDate(value_ptr, size, pos,
static_cast<date_t *>(vector_ptr[chunk_col_id])[number_of_rows], special, false);
if (!state_machine.dialect_options.date_format.find(LogicalTypeId::DATE)->second.GetValue().Empty()) {
string error_message;
success = state_machine.dialect_options.date_format.find(LogicalTypeId::DATE)
->second.GetValue()
.TryParseDate(value_ptr, size,
static_cast<date_t *>(vector_ptr[chunk_col_id])[number_of_rows], error_message);
} else {
idx_t pos;
bool special;
success = Date::TryConvertDate(
value_ptr, size, pos, static_cast<date_t *>(vector_ptr[chunk_col_id])[number_of_rows], special, false);
}
break;
}
case LogicalTypeId::TIMESTAMP: {
success = Timestamp::TryConvertTimestamp(
value_ptr, size, static_cast<timestamp_t *>(vector_ptr[chunk_col_id])[number_of_rows]) ==
TimestampCastResult::SUCCESS;
if (!state_machine.dialect_options.date_format.find(LogicalTypeId::TIMESTAMP)->second.GetValue().Empty()) {
timestamp_t result;
string error_message;
// success = state_machine.dialect_options.date_format.find(LogicalTypeId::TIMESTAMP)
// ->second.GetValue()
// .TryParseTimestamp(value, result, error_message);
} else {
success = Timestamp::TryConvertTimestamp(
value_ptr, size, static_cast<timestamp_t *>(vector_ptr[chunk_col_id])[number_of_rows]) ==
TimestampCastResult::SUCCESS;
}
break;
}
default: {
Expand Down Expand Up @@ -1185,7 +1201,6 @@ bool StringValueScanner::CanDirectlyCast(const LogicalType &type,
const map<LogicalTypeId, CSVOption<StrpTimeFormat>> &format_options) {

switch (type.id()) {
// All Integers (Except HugeInt)
case LogicalTypeId::TINYINT:
case LogicalTypeId::SMALLINT:
case LogicalTypeId::INTEGER:
Expand All @@ -1196,20 +1211,8 @@ bool StringValueScanner::CanDirectlyCast(const LogicalType &type,
case LogicalTypeId::UBIGINT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::FLOAT:
return true;
case LogicalTypeId::DATE:
// We can only internally cast YYYY-MM-DD
if (format_options.at(LogicalTypeId::DATE).GetValue().format_specifier == "%Y-%m-%d") {
return true;
} else {
return false;
}
case LogicalTypeId::TIMESTAMP:
if (format_options.at(LogicalTypeId::TIMESTAMP).GetValue().format_specifier == "%Y-%m-%d %H:%M:%S") {
return true;
} else {
return false;
}
case LogicalType::VARCHAR:
return true;
default:
Expand Down
23 changes: 16 additions & 7 deletions src/function/scalar/strftime_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,7 @@ int32_t StrpTimeFormat::TryParseCollection(const char *data, idx_t &pos, idx_t s
return -1;
}

//! Parses a timestamp using the given specifier
bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
bool StrpTimeFormat::Parse(const char *data, size_t size, ParseResult &result) const {
auto &result_data = result.data;
auto &error_message = result.error_message;
auto &error_position = result.error_position;
Expand All @@ -755,15 +754,11 @@ bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
result_data[5] = 0;
result_data[6] = 0;
result_data[7] = 0;

auto data = str.GetData();
idx_t size = str.GetSize();
// skip leading spaces
while (StringUtil::CharacterIsSpace(*data)) {
data++;
size--;
}

// Check for specials
// Precheck for alphas for performance.
idx_t pos = 0;
Expand Down Expand Up @@ -1067,7 +1062,6 @@ bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
case StrTimeSpecifier::YEAR_DECIMAL:
// Just validate, don't use
break;
break;
case StrTimeSpecifier::WEEKDAY_DECIMAL:
// First offset specifier
offset_specifier = specifiers[i];
Expand Down Expand Up @@ -1324,6 +1318,13 @@ bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
return true;
}

//! Parses a timestamp using the given specifier
bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
auto data = str.GetData();
idx_t size = str.GetSize();
return Parse(data, size, result);
}

StrpTimeFormat::ParseResult StrpTimeFormat::Parse(const string &format_string, const string &text) {
StrpTimeFormat format;
format.format_specifier = format_string;
Expand Down Expand Up @@ -1413,6 +1414,14 @@ bool StrpTimeFormat::TryParseDate(string_t input, date_t &result, string &error_
return parse_result.TryToDate(result);
}

bool StrpTimeFormat::TryParseDate(const char *data, size_t size, date_t &result, string &error_message) const {
ParseResult parse_result;
if (!Parse(data, size, parse_result)) {
return false;
}
return parse_result.TryToDate(result);
}

bool StrpTimeFormat::TryParseTime(string_t input, dtime_t &result, string &error_message) const {
ParseResult parse_result;
if (!Parse(input, parse_result)) {
Expand Down
4 changes: 4 additions & 0 deletions src/include/duckdb/function/scalar/strftime_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ struct StrpTimeFormat : public StrTimeFormat { // NOLINT: work-around bug in cla

DUCKDB_API bool Parse(string_t str, ParseResult &result) const;

DUCKDB_API bool Parse(const char *data, size_t size, ParseResult &result) const;

DUCKDB_API bool TryParseDate(const char *data, size_t size, date_t &result, string &error_message) const;

DUCKDB_API bool TryParseDate(string_t str, date_t &result, string &error_message) const;
DUCKDB_API bool TryParseTime(string_t str, dtime_t &result, string &error_message) const;
DUCKDB_API bool TryParseTimestamp(string_t str, timestamp_t &result, string &error_message) const;
Expand Down

0 comments on commit 239a1d1

Please sign in to comment.