Skip to content

Commit 36218ce

Browse files
committed
YQL-18303: Introduce Split and MakeTz* overloads
commit_hash:16a38d1b1de0cc97c5cbf97176331ea6691e23be (cherry picked from commit 6b6e4dc)
1 parent de89917 commit 36218ce

File tree

56 files changed

+4006
-1151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4006
-1151
lines changed

yql/essentials/core/type_ann/type_ann_core.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,17 @@ namespace NTypeAnnImpl {
368368
.Build(), ctx.MakeType<TDataExprType>(EDataSlot::TzTimestamp) };
369369
}
370370

371+
if (resType->GetTag() == "DateTime2.TM64") {
372+
return { ctx.Builder(input->Pos())
373+
.Callable("Apply")
374+
.Callable(0, "Udf")
375+
.Atom(0, "DateTime2.MakeTzTimestamp64", TNodeFlags::Default)
376+
.Seal()
377+
.Add(1, input)
378+
.Seal()
379+
.Build(), ctx.MakeType<TDataExprType>(EDataSlot::TzTimestamp64) };
380+
}
381+
371382
if (resType->GetTag() == "JsonNode") {
372383
return { ctx.Builder(input->Pos())
373384
.Callable("Apply")

yql/essentials/core/type_ann/type_ann_list.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,11 +2400,13 @@ namespace {
24002400
case EDataSlot::Date:
24012401
case EDataSlot::TzDate:
24022402
case EDataSlot::Date32:
2403+
case EDataSlot::TzDate32:
24032404
value = ctx.Expr.NewAtom(input->Pos(), "86400000000", TNodeFlags::Default);
24042405
break;
24052406
case EDataSlot::Datetime:
24062407
case EDataSlot::TzDatetime:
24072408
case EDataSlot::Datetime64:
2409+
case EDataSlot::TzDatetime64:
24082410
value = ctx.Expr.NewAtom(input->Pos(), "1000000", TNodeFlags::Default);
24092411
break;
24102412
default:

yql/essentials/core/yql_expr_type_annotation.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,21 +335,26 @@ IGraphTransformer::TStatus TryConvertToImpl(TExprContext& ctx, TExprNode::TPtr&
335335
} else if (from == EDataSlot::Date && (
336336
to == EDataSlot::Date32 ||
337337
to == EDataSlot::TzDate ||
338+
to == EDataSlot::TzDate32 ||
338339
to == EDataSlot::Datetime ||
339340
to == EDataSlot::Timestamp ||
340341
to == EDataSlot::TzDatetime ||
341342
to == EDataSlot::TzTimestamp ||
342343
to == EDataSlot::Datetime64 ||
343-
to == EDataSlot::Timestamp64))
344+
to == EDataSlot::Timestamp64 ||
345+
to == EDataSlot::TzDatetime64 ||
346+
to == EDataSlot::TzTimestamp64))
344347
{
345348
allow = true;
346349
useCast = true;
347350
} else if (from == EDataSlot::Datetime && (
348351
to == EDataSlot::Datetime64 ||
349352
to == EDataSlot::TzDatetime ||
353+
to == EDataSlot::TzDatetime64 ||
350354
to == EDataSlot::Timestamp ||
351355
to == EDataSlot::TzTimestamp ||
352-
to == EDataSlot::Timestamp64))
356+
to == EDataSlot::Timestamp64 ||
357+
to == EDataSlot::TzTimestamp64))
353358
{
354359
allow = true;
355360
useCast = true;
@@ -365,9 +370,15 @@ IGraphTransformer::TStatus TryConvertToImpl(TExprContext& ctx, TExprNode::TPtr&
365370
} else if (from == EDataSlot::Date32 && (to == EDataSlot::Datetime64 || to == EDataSlot::Timestamp64)) {
366371
allow = true;
367372
useCast = true;
373+
} else if (from == EDataSlot::TzDate32 && (to == EDataSlot::TzDatetime64 || to == EDataSlot::TzTimestamp64)) {
374+
allow = true;
375+
useCast = true;
368376
} else if (from == EDataSlot::Datetime64 && (to == EDataSlot::Timestamp64)) {
369377
allow = true;
370378
useCast = true;
379+
} else if (from == EDataSlot::TzDatetime64 && to == EDataSlot::TzTimestamp64) {
380+
allow = true;
381+
useCast = true;
371382
} else if (from == EDataSlot::Interval && (to == EDataSlot::Interval64)) {
372383
allow = true;
373384
useCast = true;

yql/essentials/minikql/datetime/datetime64.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,26 @@ struct TTM64Storage {
106106
Microsecond = value - datetime * 1000000ll;
107107
}
108108

109-
i32 ToDate32(const NUdf::IDateBuilder& builder) const {
110-
i32 date;
111-
if (!builder.MakeTzDate32(Year, Month, Day, date, TimezoneId)) {
112-
ythrow yexception() << "Error in MakeTzDate32 tzId " << TimezoneId
113-
<< " " << Year << "-" << Month << "-" << Day;
109+
i32 ToDate32(const NUdf::IDateBuilder& builder, bool local) const {
110+
if (!IsUniversal(TimezoneId)) {
111+
i64 datetime;
112+
ui32 hour = local ? 0 : Hour;
113+
ui32 minute = local ? 0 : Minute;
114+
ui32 second = local ? 0 : Second;
115+
if (!builder.MakeTzDatetime64(Year, Month, Day, hour, minute, second, datetime, TimezoneId)) {
116+
ythrow yexception() << "Error in MakeTzDatetime64 tzId "
117+
<< TimezoneId << " " << Year << "-" << Month << "-" << Day
118+
<< "T" << hour << ":" << minute << ":" << second;
119+
}
120+
return datetime / 86400u;
121+
} else {
122+
i32 date;
123+
if (!builder.MakeTzDate32(Year, Month, Day, date, TimezoneId)) {
124+
ythrow yexception() << "Error in MakeTzDate32 tzId "
125+
<< TimezoneId << " " << Year << "-" << Month << "-" << Day;
126+
}
127+
return date;
114128
}
115-
return date;
116129
}
117130

118131
i64 ToDatetime64(const NUdf::IDateBuilder& builder) const {
@@ -160,6 +173,13 @@ struct TTM64Storage {
160173
inline ui64 ToTimeOfDay() const {
161174
return ((Hour * 60ull + Minute) * 60ull + Second) * 1000000ull + Microsecond;
162175
}
176+
177+
const TString ToString() const {
178+
const auto& tzName = NUdf::GetTimezones()[TimezoneId];
179+
return Sprintf("%8d-%02d-%02dT%02d:%02d:%02d.%06d,%.*s",
180+
Year, Month, Day, Hour, Minute, Second, Microsecond,
181+
static_cast<int>(tzName.size()), tzName.data());
182+
}
163183
};
164184

165185
}

yql/essentials/minikql/mkql_type_ops.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,7 @@ class TDateTable {
894894
{
895895
if (tzId) {
896896
ui32 hour, min, sec;
897-
i64 utcSeconds = (date >= 0) ? ((date + 1) * 86400ll - 1) : (date * 86400ll);
898-
ToLocalTime64(utcSeconds, tzId, year, month, day, hour, min, sec);
897+
ToLocalTime64(86400ll * ++date - 1, tzId, year, month, day, hour, min, sec);
899898
if (year <= 0) {
900899
year--;
901900
}
@@ -924,7 +923,7 @@ class TDateTable {
924923
if (year <= 0) {
925924
year--;
926925
}
927-
if (!MakeDate32(year, month, day, date)) {
926+
if (!GetDate32Offset(year, month, day, date)) {
928927
return false;
929928
}
930929
SplitDate32(date, year, month, day, dayOfYear, weekOfYear, weekOfYearIso8601, dayOfWeek);
@@ -986,8 +985,12 @@ class TDateTable {
986985
return true;
987986
}
988987

989-
bool MakeDate32(i32 year, ui32 month, ui32 day, i32& value) const {
990-
if (Y_UNLIKELY(year == 0 || year < NUdf::MIN_YEAR32 || year >= NUdf::MAX_YEAR32)) {
988+
bool GetDate32Offset(i32 year, ui32 month, ui32 day, i32& value) const {
989+
if (Y_UNLIKELY(year < NUdf::MIN_YEAR32 - 1 || year > NUdf::MAX_YEAR32
990+
|| (year == NUdf::MAX_YEAR32 && (day > 1U || month > 1U))
991+
|| (year == NUdf::MIN_YEAR32 - 1 && (day < 31U || month < 12U))
992+
|| year == 0))
993+
{
991994
return false;
992995
}
993996
auto isLeap = IsLeapYear(year);
@@ -1015,6 +1018,16 @@ class TDateTable {
10151018
return true;
10161019
}
10171020

1021+
bool MakeDate32(i32 year, ui32 month, ui32 day, i32& value) const {
1022+
if (Y_UNLIKELY(year == 0 || year < NUdf::MIN_YEAR32 || year >= NUdf::MAX_YEAR32)) {
1023+
return false;
1024+
}
1025+
if (Y_UNLIKELY(!GetDate32Offset(year, month, day, value))) {
1026+
return false;
1027+
}
1028+
return true;
1029+
}
1030+
10181031
bool MakeTzDate32(i32 year, ui32 month, ui32 day, i32& value, ui16 tzId) const {
10191032
if (tzId == 0) {
10201033
return MakeDate32(year, month, day, value);
@@ -1766,7 +1779,7 @@ NUdf::TUnboxedValuePod ParseDatetime(NUdf::TStringRef buf) {
17661779
}
17671780

17681781
bool waiting_for_z = true;
1769-
1782+
17701783
ui32 offset_hours = 0;
17711784
ui32 offset_minutes = 0;
17721785
bool is_offset_negative = false;
@@ -1776,12 +1789,12 @@ NUdf::TUnboxedValuePod ParseDatetime(NUdf::TStringRef buf) {
17761789
// Skip sign
17771790
++pos;
17781791

1779-
if (!ParseNumber(pos, buf, offset_hours, 2) ||
1792+
if (!ParseNumber(pos, buf, offset_hours, 2) ||
17801793
pos == buf.Size() || buf.Data()[pos] != ':')
17811794
{
17821795
return NUdf::TUnboxedValuePod();
17831796
}
1784-
1797+
17851798
// Skip ':'
17861799
++pos;
17871800

@@ -2049,12 +2062,12 @@ NUdf::TUnboxedValuePod ParseTimestamp(NUdf::TStringRef buf) {
20492062
// Skip sign
20502063
++pos;
20512064

2052-
if (!ParseNumber(pos, buf, offset_hours, 2) ||
2065+
if (!ParseNumber(pos, buf, offset_hours, 2) ||
20532066
pos == buf.Size() || buf.Data()[pos] != ':')
20542067
{
20552068
return NUdf::TUnboxedValuePod();
20562069
}
2057-
2070+
20582071
// Skip ':'
20592072
++pos;
20602073

@@ -2085,7 +2098,7 @@ NUdf::TUnboxedValuePod ParseTimestamp(NUdf::TStringRef buf) {
20852098
}
20862099

20872100
ui64 value = dateValue * 86400000000ull + timeValue * 1000000ull + microseconds;
2088-
2101+
20892102
if (is_offset_negative) {
20902103
if (UINT64_MAX - value < offset_value) {
20912104
return NUdf::TUnboxedValuePod();

0 commit comments

Comments
 (0)