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

GH-37110: [C++] Expression: SmallestTypeFor lost tz for Scalar #37135

Merged
merged 4 commits into from
Aug 14, 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
12 changes: 6 additions & 6 deletions cpp/src/arrow/compute/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -480,26 +480,26 @@ TypeHolder SmallestTypeFor(const arrow::Datum& value) {
return value.type();
case TimeUnit::MILLI:
if (ts % 1000 == 0) {
return timestamp(TimeUnit::SECOND);
return timestamp(TimeUnit::SECOND, ts_type->timezone());
}
return value.type();
case TimeUnit::MICRO:
if (ts % 1000000 == 0) {
return timestamp(TimeUnit::SECOND);
return timestamp(TimeUnit::SECOND, ts_type->timezone());
}
if (ts % 1000 == 0) {
return timestamp(TimeUnit::MILLI);
return timestamp(TimeUnit::MILLI, ts_type->timezone());
}
return value.type();
case TimeUnit::NANO:
if (ts % 1000000000 == 0) {
return timestamp(TimeUnit::SECOND);
return timestamp(TimeUnit::SECOND, ts_type->timezone());
}
if (ts % 1000000 == 0) {
return timestamp(TimeUnit::MILLI);
return timestamp(TimeUnit::MILLI, ts_type->timezone());
}
if (ts % 1000 == 0) {
return timestamp(TimeUnit::MICRO);
return timestamp(TimeUnit::MICRO, ts_type->timezone());
}
return value.type();
default:
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/compute/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const std::shared_ptr<Schema> kBoringSchema = schema({
field("ts_ns", timestamp(TimeUnit::NANO)),
field("ts_s", timestamp(TimeUnit::SECOND)),
field("binary", binary()),
field("ts_s_utc", timestamp(TimeUnit::SECOND, "UTC")),
});

#define EXPECT_OK ARROW_EXPECT_OK
Expand Down Expand Up @@ -630,6 +631,18 @@ TEST(Expression, BindWithImplicitCasts) {
literal(std::make_shared<TimestampScalar>(0, TimeUnit::NANO))),
cmp(field_ref("ts_s"),
literal(std::make_shared<TimestampScalar>(0, TimeUnit::SECOND))));
// GH-37110
ExpectBindsTo(
cmp(field_ref("ts_s_utc"),
literal(std::make_shared<TimestampScalar>(0, TimeUnit::NANO, "UTC"))),
cmp(field_ref("ts_s_utc"),
literal(std::make_shared<TimestampScalar>(0, TimeUnit::SECOND, "UTC"))));
Copy link
Member

Choose a reason for hiding this comment

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

Since we are at it and the original PR didn't add many tests, can we also test non-zero values? For example (untested):

Suggested change
literal(std::make_shared<TimestampScalar>(0, TimeUnit::SECOND, "UTC"))));
literal(std::make_shared<TimestampScalar>(0, TimeUnit::SECOND, "UTC"))));
ExpectBindsTo(
cmp(field_ref("ts_s"),
literal(std::make_shared<TimestampScalar>(123000, TimeUnit::NANO, "UTC"))),
cmp(field_ref("ts_s"),
literal(std::make_shared<TimestampScalar>(123, TimeUnit::MICRO, "UTC"))));

Copy link
Member Author

@mapleFU mapleFU Aug 14, 2023

Choose a reason for hiding this comment

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

Added, I also found that we need a UTC type here.

field("ts_s_utc", timestamp(TimeUnit::SECOND, "UTC"))

ExpectBindsTo(
cmp(field_ref("ts_s_utc"),
literal(std::make_shared<TimestampScalar>(123000, TimeUnit::NANO, "UTC"))),
cmp(field_ref("ts_s_utc"),
literal(std::make_shared<TimestampScalar>(123, TimeUnit::MICRO, "UTC"))));

ExpectBindsTo(
cmp(field_ref("binary"), literal(std::make_shared<LargeBinaryScalar>("foo"))),
cmp(field_ref("binary"), literal(std::make_shared<BinaryScalar>("foo"))));
Expand Down
Loading