Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.

[Time Conversion] Timestamp to time #482

Merged
merged 1 commit into from
Jun 15, 2023
Merged
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
4 changes: 3 additions & 1 deletion omniscidb/IR/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ bool isCastAllowed(const Type* old_type, const Type* new_type) {
// can cast from date to timestamp
} else if (old_type->isDate() && new_type->isTimestamp()) {
return true;
} else if (old_type->isTimestamp() && new_type->isDate()) {
} else if (old_type->isTimestamp() && (new_type->isDate() || new_type->isTime())) {
return true;
} else if (old_type->isTime() && new_type->isInteger()) {
return true;
} else if (old_type->isBoolean() && new_type->isNumber()) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions omniscidb/QueryBuilder/QueryBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,11 @@ BuilderExpr BuilderExpr::cast(const Type* new_type) const {
return {builder_, expr_->cast(new_type), "", true};
}
} else if (expr_->type()->isTime()) {
if (new_type->isTime()) {
if (new_type->isInteger() || new_type->isTime()) {
return {builder_, expr_->cast(new_type), "", true};
}
} else if (expr_->type()->isTimestamp()) {
if (new_type->isNumber() || new_type->isDate() || new_type->isTimestamp()) {
if (new_type->isInteger() || new_type->isDateTime()) {
return {builder_, expr_->cast(new_type), "", true};
}
}
Expand Down
73 changes: 66 additions & 7 deletions omniscidb/QueryEngine/ArrowResultSetConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,19 @@ void append_scalar_value_and_validity(const ScalarTargetValue& value,
}
break;
case hdk::ir::Type::kTime:
create_or_append_value<int32_t, int64_t>(value, values, max_size);
create_or_append_validity<int64_t>(value, type, null_bitmap, max_size);
switch (type->size()) {
case 4:
create_or_append_value<int32_t, int64_t>(value, values, max_size);
create_or_append_validity<int64_t>(value, type, null_bitmap, max_size);
break;
case 8:
create_or_append_value<int64_t, int64_t>(value, values, max_size);
create_or_append_validity<int64_t>(value, type, null_bitmap, max_size);
break;
default:
throw std::runtime_error(type->toString() +
" is not supported in Arrow result sets.");
}
break;
case hdk::ir::Type::kDate:
device_type == ExecutorDeviceType::GPU
Expand Down Expand Up @@ -1566,8 +1577,22 @@ std::shared_ptr<arrow::DataType> get_arrow_type(const hdk::ir::Type* type,
CHECK_EQ(type->size(), 8);
return arrow::decimal(std::min(type->as<hdk::ir::DecimalType>()->precision(), 38),
type->as<hdk::ir::DecimalType>()->scale());
case hdk::ir::Type::kTime:
return time32(arrow::TimeUnit::SECOND);
case hdk::ir::Type::kTime: {
auto unit = type->as<hdk::ir::TimeType>()->unit();
switch (unit) {
case hdk::ir::TimeUnit::kSecond:
return time32(arrow::TimeUnit::SECOND);
case hdk::ir::TimeUnit::kMilli:
return time32(arrow::TimeUnit::MILLI);
case hdk::ir::TimeUnit::kMicro:
return time64(arrow::TimeUnit::MICRO);
case hdk::ir::TimeUnit::kNano:
return time64(arrow::TimeUnit::NANO);
default:
throw std::runtime_error("Unsupported time precision for Arrow result sets: " +
toString(unit));
}
}
case hdk::ir::Type::kDate: {
// TODO(wamsi) : Remove date64() once date32() support is added in cuDF. date32()
// Currently support for date32() is missing in cuDF.Hence, if client requests for
Expand Down Expand Up @@ -1839,6 +1864,25 @@ void appendToColumnBuilder(ArrowResultSetConverter::ColumnBuilder& column_builde
}
}

template <>
void appendToColumnBuilder<arrow::Time32Builder, int64_t>(
ArrowResultSetConverter::ColumnBuilder& column_builder,
arrow::ArrayBuilder* arrow_builder,
const ValueArray& values,
const std::shared_ptr<std::vector<bool>>& is_valid) {
std::vector<int64_t> val_raw = boost::get<std::vector<int64_t>>(values);
std::vector<int32_t> vals(val_raw.begin(), val_raw.end());

auto typed_builder = dynamic_cast<arrow::Time32Builder*>(arrow_builder);
CHECK(typed_builder);
if (column_builder.field->nullable()) {
CHECK(is_valid.get());
ARROW_THROW_NOT_OK(typed_builder->AppendValues(vals, *is_valid));
} else {
ARROW_THROW_NOT_OK(typed_builder->AppendValues(vals));
}
}

template <>
void appendToColumnBuilder<arrow::Decimal128Builder, int64_t>(
ArrowResultSetConverter::ColumnBuilder& column_builder,
Expand Down Expand Up @@ -2005,10 +2049,25 @@ void ArrowResultSetConverter::append(
" is not supported in Arrow result sets.");
}
break;
case hdk::ir::Type::kTime:
appendToColumnBuilder<arrow::Time32Builder, int32_t>(
column_builder, elem_builder, values, is_valid);
case hdk::ir::Type::kTime: {
auto unit = elem_type->as<hdk::ir::TimeType>()->unit();
switch (unit) {
case hdk::ir::TimeUnit::kSecond:
case hdk::ir::TimeUnit::kMilli:
appendToColumnBuilder<arrow::Time32Builder, int64_t>(
column_builder, elem_builder, values, is_valid);
break;
case hdk::ir::TimeUnit::kMicro:
case hdk::ir::TimeUnit::kNano:
appendToColumnBuilder<arrow::Time64Builder, int64_t>(
column_builder, elem_builder, values, is_valid);
break;
default:
throw std::runtime_error("Unsupported time precision for Arrow result sets: " +
toString(unit));
}
break;
}
case hdk::ir::Type::kTimestamp:
appendToColumnBuilder<arrow::TimestampBuilder, int64_t>(
column_builder, elem_builder, values, is_valid);
Expand Down
144 changes: 93 additions & 51 deletions omniscidb/QueryEngine/CastIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,58 +73,58 @@ llvm::Value* CodeGenerator::codegenCast(llvm::Value* operand_lv,
return cgen_state_->ir_builder_.CreatePointerCast(operand_lv,
byte_array_type->getPointerTo());
}
if (operand_lv->getType()->isIntegerTy()) {
if (operand_type->isString() || operand_type->isExtDictionary()) {
return codegenCastFromString(
operand_lv, operand_type, type, operand_is_const, is_dict_intersection, co);
}
CHECK(operand_type->isInteger() || operand_type->isDecimal() ||
operand_type->isDateTime() || operand_type->isBoolean());
if (operand_type->isBoolean()) {
// cast boolean to int8
CHECK(operand_lv->getType()->isIntegerTy(1) ||
operand_lv->getType()->isIntegerTy(8));
if (operand_lv->getType()->isIntegerTy(1)) {
operand_lv = cgen_state_->castToTypeIn(operand_lv, 8);
}
if (type->isBoolean()) {
return operand_lv;
}
}
if (operand_type->isInteger() && operand_lv->getType()->isIntegerTy(8) &&
type->isBoolean()) {
// cast int8 to boolean
return codegenCastBetweenIntTypes(operand_lv, operand_type, type);
}
if (operand_type->isTimestamp() && type->isDate()) {
// Maybe we should instead generate DateTruncExpr directly from RelAlgTranslator
// for this pattern. However, DateTruncExpr is supposed to return a timestamp,
// whereas this cast returns a date. The underlying type for both is still the same,
// but it still doesn't look like a good idea to misuse DateTruncExpr.
// Date will have default precision of day, but TIMESTAMP dimension would
// matter but while converting date through seconds
return codegenCastTimestampToDate(
operand_lv,
operand_type->as<hdk::ir::TimestampType>()->unit(),
type->nullable());
if (!operand_lv->getType()->isIntegerTy()) {
return codegenCastFromFp(operand_lv, operand_type, type);
}

if (operand_type->isString() || operand_type->isExtDictionary()) {
return codegenCastFromString(
operand_lv, operand_type, type, operand_is_const, is_dict_intersection, co);
}
CHECK(operand_type->isInteger() || operand_type->isDecimal() ||
operand_type->isDateTime() || operand_type->isBoolean());
if (operand_type->isBoolean()) {
// cast boolean to int8
CHECK(operand_lv->getType()->isIntegerTy(1) || operand_lv->getType()->isIntegerTy(8));
if (operand_lv->getType()->isIntegerTy(1)) {
operand_lv = cgen_state_->castToTypeIn(operand_lv, 8);
}
if ((operand_type->isTimestamp() || operand_type->isDate()) && type->isTimestamp()) {
const auto operand_unit = (operand_type->isTimestamp())
? operand_type->as<hdk::ir::TimestampType>()->unit()
: hdk::ir::TimeUnit::kSecond;
if (operand_unit != type->as<hdk::ir::TimestampType>()->unit()) {
return codegenCastBetweenTimestamps(
operand_lv, operand_type, type, type->nullable());
}
if (type->isBoolean()) {
return operand_lv;
}
if (type->isInteger() || type->isDecimal() || type->isDateTime()) {
return codegenCastBetweenIntTypes(operand_lv, operand_type, type);
} else {
return codegenCastToFp(operand_lv, operand_type, type);
}
if (operand_type->isInteger() && operand_lv->getType()->isIntegerTy(8) &&
type->isBoolean()) {
// cast int8 to boolean
return codegenCastBetweenIntTypes(operand_lv, operand_type, type);
}
if (operand_type->isTimestamp() && type->isDate()) {
// Maybe we should instead generate DateTruncExpr directly from RelAlgTranslator
// for this pattern. However, DateTruncExpr is supposed to return a timestamp,
// whereas this cast returns a date. The underlying type for both is still the same,
// but it still doesn't look like a good idea to misuse DateTruncExpr.
// Date will have default precision of day, but TIMESTAMP dimension would
// matter but while converting date through seconds
return codegenCastTimestampToDate(
operand_lv, operand_type->as<hdk::ir::TimestampType>()->unit(), type->nullable());
}
if (operand_type->isTimestamp() && type->isTime()) {
return codegenCastTimestampToTime(operand_lv, operand_type, type);
}
if ((operand_type->isTimestamp() || operand_type->isDate()) && type->isTimestamp()) {
const auto operand_unit = (operand_type->isTimestamp())
? operand_type->as<hdk::ir::TimestampType>()->unit()
: hdk::ir::TimeUnit::kSecond;
if (operand_unit != type->as<hdk::ir::TimestampType>()->unit()) {
return codegenCastBetweenTimestamps(operand_lv, operand_type, type);
}
}
if (type->isInteger() || type->isDecimal() || type->isDateTime()) {
return codegenCastBetweenIntTypes(operand_lv, operand_type, type);
} else {
return codegenCastFromFp(operand_lv, operand_type, type);
return codegenCastToFp(operand_lv, operand_type, type);
}

CHECK(false);
return nullptr;
}
Expand Down Expand Up @@ -159,11 +159,53 @@ llvm::Value* CodeGenerator::codegenCastTimestampToDate(llvm::Value* ts_lv,
return ret;
}

llvm::Value* CodeGenerator::codegenCastTimestampToTime(llvm::Value* ts_lv,
const hdk::ir::Type* operand_type,
const hdk::ir::Type* target_type) {
AUTOMATIC_IR_METADATA(cgen_state_);
const auto operand_unit = operand_type->as<hdk::ir::TimestampType>()->unit();
const auto target_unit = target_type->as<hdk::ir::TimeType>()->unit();

CHECK(ts_lv->getType()->isIntegerTy(64));
if (operand_unit < target_unit) {
const auto target_sec_scale = hdk::ir::unitsPerSecond(target_unit);
const auto scale =
hdk::ir::unitsPerSecond(target_unit) / hdk::ir::unitsPerSecond(operand_unit);
if (!operand_type->nullable()) {
return cgen_state_->emitCall(
"TimestampToTimeUpscale",
{{ts_lv, cgen_state_->llInt(scale), cgen_state_->llInt(target_sec_scale)}});
}
// Timestamp, Time32. Time64 have int64 inline int value, but logically corect to pass
// target and source null values.
return cgen_state_->emitCall("TimestampToTimeUpscaleNullable",
{{ts_lv,
cgen_state_->llInt(scale),
cgen_state_->llInt(target_sec_scale),
cgen_state_->inlineIntNull(operand_type),
cgen_state_->inlineIntNull(target_type)}});
}
// On downscale we at first cut of date part, than cut of unused unit precision
const auto operand_sec_scale = hdk::ir::unitsPerSecond(operand_unit);
const auto scale =
hdk::ir::unitsPerSecond(operand_unit) / hdk::ir::unitsPerSecond(target_unit);
if (!operand_type->nullable()) {
return cgen_state_->emitCall(
"TimestampToTimeDownscale",
{{ts_lv, cgen_state_->llInt(scale), cgen_state_->llInt(operand_sec_scale)}});
}
return cgen_state_->emitCall("TimestampToTimeDownscaleNullable",
{{ts_lv,
cgen_state_->llInt(scale),
cgen_state_->llInt(operand_sec_scale),
cgen_state_->inlineIntNull(operand_type),
cgen_state_->inlineIntNull(target_type)}});
}

llvm::Value* CodeGenerator::codegenCastBetweenTimestamps(
llvm::Value* ts_lv,
const hdk::ir::Type* operand_type,
const hdk::ir::Type* target_type,
const bool nullable) {
const hdk::ir::Type* target_type) {
AUTOMATIC_IR_METADATA(cgen_state_);
const auto operand_unit = operand_type->isTimestamp()
? operand_type->as<hdk::ir::TimestampType>()->unit()
Expand All @@ -179,7 +221,7 @@ llvm::Value* CodeGenerator::codegenCastBetweenTimestamps(
const auto scale =
hdk::ir::unitsPerSecond(target_unit) / hdk::ir::unitsPerSecond(operand_unit);
codegenCastBetweenIntTypesOverflowChecks(ts_lv, operand_type, target_type, scale);
return nullable
return target_type->nullable()
? cgen_state_->emitCall("mul_int64_t_nullable_lhs",
{ts_lv,
cgen_state_->llInt(static_cast<int64_t>(scale)),
Expand All @@ -189,7 +231,7 @@ llvm::Value* CodeGenerator::codegenCastBetweenTimestamps(
}
const auto scale =
hdk::ir::unitsPerSecond(operand_unit) / hdk::ir::unitsPerSecond(target_unit);
return nullable
return target_type->nullable()
? cgen_state_->emitCall("floor_div_nullable_lhs",
{ts_lv,
cgen_state_->llInt(static_cast<int64_t>(scale)),
Expand Down
7 changes: 5 additions & 2 deletions omniscidb/QueryEngine/CodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,13 @@ class CodeGenerator {
const hdk::ir::TimeUnit unit,
const bool nullable);

llvm::Value* codegenCastTimestampToTime(llvm::Value* ts_lv,
const hdk::ir::Type* operand_type,
const hdk::ir::Type* target_type);

llvm::Value* codegenCastBetweenTimestamps(llvm::Value* ts_lv,
const hdk::ir::Type* operand_type,
const hdk::ir::Type* target_type,
const bool nullable);
const hdk::ir::Type* target_type);

llvm::Value* codegenCastFromString(llvm::Value* operand_lv,
const hdk::ir::Type* operand_type,
Expand Down
60 changes: 60 additions & 0 deletions omniscidb/QueryEngine/DateTruncate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,66 @@ DateTruncateHighPrecisionToDateNullable(const int64_t timeval,
return truncate_high_precision_timestamp_to_date(timeval, scale);
}

namespace {
// scale is 10^{3,6,9}
inline DEVICE int64_t
timestamp_to_time_truncation_upscale(const int64_t timeval,
const int64_t conversion_scale,
const int64_t target_sec_scale) {
return unsigned_mod(timeval * conversion_scale, target_sec_scale * kSecsPerDay);
}

// scale is 10^{3,6,9}
inline DEVICE int64_t
timestamp_to_time_truncation_downscale(const int64_t timeval,
const int64_t conversion_scale,
const int64_t target_sec_scale) {
return unsigned_mod(timeval, target_sec_scale * kSecsPerDay) / conversion_scale;
}
} // namespace

extern "C" DEVICE RUNTIME_EXPORT ALWAYS_INLINE int64_t
TimestampToTimeUpscale(const int64_t timeval,
const int64_t conversion_scale,
const int64_t target_sec_scale) {
return timestamp_to_time_truncation_upscale(
timeval, conversion_scale, target_sec_scale);
}

extern "C" DEVICE RUNTIME_EXPORT ALWAYS_INLINE int64_t
TimestampToTimeDownscale(const int64_t timeval,
const int64_t conversion_scale,
const int64_t target_sec_scale) {
return timestamp_to_time_truncation_downscale(
timeval, conversion_scale, target_sec_scale);
}

extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
TimestampToTimeUpscaleNullable(const int64_t timeval,
const int64_t conversion_scale,
const int64_t target_sec_scale,
const int64_t source_null_val,
const int64_t target_null_val) {
if (timeval == source_null_val) {
return target_null_val;
}
return timestamp_to_time_truncation_upscale(
timeval, conversion_scale, target_sec_scale);
}

extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
TimestampToTimeDownscaleNullable(const int64_t timeval,
const int64_t conversion_scale,
const int64_t target_sec_scale,
const int64_t source_null_val,
const int64_t target_null_val) {
if (timeval == source_null_val) {
return target_null_val;
}
return timestamp_to_time_truncation_downscale(
timeval, conversion_scale, target_sec_scale);
}

namespace {

struct EraTime {
Expand Down
Loading