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

Conversation

Devjiu
Copy link
Contributor

@Devjiu Devjiu commented May 15, 2023

Adding timestamp to time conversion. The units is also taken into account.

Resolves: #318

Signed-off-by: Dmitrii Makarenko [email protected]

@Devjiu Devjiu force-pushed the dmitriim/tmstmp_to_time branch from de68bcd to 510ca18 Compare May 16, 2023 12:58
@Devjiu Devjiu changed the title [Date Conversion] Timestamp to time [Time Conversion] Timestamp to time May 16, 2023
@Devjiu Devjiu force-pushed the dmitriim/tmstmp_to_time branch 4 times, most recently from 586db69 to c83d743 Compare May 16, 2023 15:44
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->isDate() || new_type->isTime() ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use isDateTime here.

@@ -108,6 +108,14 @@ llvm::Value* CodeGenerator::codegenCast(llvm::Value* operand_lv,
operand_type->as<hdk::ir::TimestampType>()->unit(),
type->nullable());
}
if (operand_type->isTimestamp() && type->isTime()) {
const auto operand_unit = (operand_type->isTimestamp())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know here operand_type is a timestamp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

? operand_type->as<hdk::ir::TimestampType>()->unit()
: hdk::ir::TimeUnit::kSecond;
// if (operand_unit != type->as<hdk::ir::TimestampType>()->unit()) {
return codegenCastTimestampToTime(operand_lv, operand_type, type, type->nullable());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why nullalbe should be a separate argument if it can be extracted from types. Also, not clear why we use nullable from the target type, not the source.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed unnecessary argument.

LOG(ERROR) << " units from: " << operand_unit << " to: " << target_unit;
CHECK(ts_lv->getType()->isIntegerTy(64));
const auto target_sec_scale = hdk::ir::unitsPerSecond(target_unit) /
hdk::ir::unitsPerSecond(hdk::ir::TimeUnit::kSecond);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to divide by 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

{{ts_lv,
cgen_state_->llInt(scale),
cgen_state_->llInt(target_sec_scale),
cgen_state_->inlineIntNull(ctx.int64())}});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null arg is not required here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

{{ts_lv,
cgen_state_->llInt(scale),
cgen_state_->llInt(target_sec_scale),
cgen_state_->inlineIntNull(ctx.int64())}});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null arg is not required here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

{{ts_lv,
cgen_state_->llInt(scale),
cgen_state_->llInt(target_sec_scale),
cgen_state_->inlineIntNull(ctx.int64())}});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to use operand_type to get a proper null value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, passing source and target null values

const int64_t target_sec_scale,
const int64_t null_val) {
if (timeval == null_val) {
return null_val;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need to return the target null value, not the source one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, done


createTable("test_tmstmp",
{{"col_bi", ctx().int64()},
{"col_tmstp", ctx().timestamp(hdk::ir::TimeUnit::kSecond)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, try non-nullable types. For nullable types add some NULL values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made 2 tests, first one is non-nullable, second one is nullable.

@Devjiu Devjiu force-pushed the dmitriim/tmstmp_to_time branch 5 times, most recently from 19e5a39 to 7125f3c Compare May 25, 2023 18:24
@Devjiu Devjiu force-pushed the dmitriim/tmstmp_to_time branch 2 times, most recently from 1e60caa to a587707 Compare May 31, 2023 17:43
@Devjiu Devjiu marked this pull request as ready for review May 31, 2023 17:44
@Devjiu
Copy link
Contributor Author

Devjiu commented May 31, 2023

I am not sure, that nulls handled in correct way. Arrow array in case Time32[s] will have int32, canonical type is int64, so in ResultSet will be null int64_t, after conversion to arrow int32 null.

Copy link
Contributor

@ienkovich ienkovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good!

I just have some non-functional comments.

The only open question remaining for me is negative timestamps. I see them in tests but don't actually understand how we handle them using unsigned mod. Can you elaborate on this?

// scale is 10^{3,6,9}
inline DEVICE int64_t
timestamp_to_time_truncation_upscale(const int64_t timeval,
const int64_t conersion_scale,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conersion -> conversion in multiple places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

timestamp_to_time_truncation_upscale(const int64_t timeval,
const int64_t conersion_scale,
const int64_t target_sec_scale) {
return unsigned_mod(timeval * conersion_scale, target_sec_scale * kSecsPerDay);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what about negative values? Do we support them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, changed the order of calculation. On downscale we at first cut of date part, than cut of unused unit precision. Tests also modified to align with this behavior.

const auto scale =
hdk::ir::unitsPerSecond(target_unit) / hdk::ir::unitsPerSecond(operand_unit);

// codegenCastBetweenIntTypesOverflowChecks(ts_lv, operand_type, target_type, scale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const auto scale =
hdk::ir::unitsPerSecond(operand_unit) / hdk::ir::unitsPerSecond(target_unit);

// codegenCastBetweenIntTypesOverflowChecks(ts_lv, operand_type, target_type, scale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

oss << getStrTimeFromSeconds(boost::get<int64_t>(current_scalar));
oss << getStrTStamp(boost::get<int64_t>(current_scalar),
col_type->as<hdk::ir::TimeType>()->unit())
<< " scalar: " << current_scalar;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function is used to print result sets in PyHDK, scalar values would be really unexpected there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getStrTStamp print all time types as timestamp, it looks like a debug function. If it's for end user we should rewrite it.

@@ -519,6 +562,180 @@ TEST_F(QueryBuilderTest, Arithmetics) {
compare_res_data(res, std::vector<int64_t>({0, NULL_BIGINT}));
}

TEST_F(QueryBuilderTest, TmstmpToTime) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use unabbreviated TimestampToTime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Devjiu Devjiu force-pushed the dmitriim/tmstmp_to_time branch 2 times, most recently from 0ae014d to d14f208 Compare June 9, 2023 14:04
Adding timestamp to time conversion. The  units is also taken into account.

Resolves: #318

Signed-off-by: Dmitrii Makarenko <[email protected]>
@Devjiu Devjiu force-pushed the dmitriim/tmstmp_to_time branch from d14f208 to 48d8b98 Compare June 14, 2023 17:58
@kurapov-peter kurapov-peter merged commit c89a4ed into main Jun 15, 2023
@kurapov-peter kurapov-peter deleted the dmitriim/tmstmp_to_time branch June 15, 2023 14:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Timestamp value cannot be casted to time
3 participants