Date_Time functions PART 1

This commit is contained in:
HeenaBansal2009 2022-08-09 09:40:35 -07:00 committed by Yong Wang
parent 7b2fc8c3b7
commit 232e4c73f9
4 changed files with 120 additions and 47 deletions

View File

@ -63,23 +63,23 @@ bool DatetimeDiff::convertImpl(String &out,IParser::Pos &pos)
bool DayOfMonth::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
return directMapping(out, pos, "toDayOfMonth");
}
bool DayOfWeek::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;
++pos;
out = std::format("toDayOfWeek() + %7");
return true;
}
bool DayOfYear::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
return directMapping(out, pos, "toDayOfYear");
}
bool EndOfDay::convertImpl(String &out,IParser::Pos &pos)
@ -119,23 +119,17 @@ bool FormatTimeSpan::convertImpl(String &out,IParser::Pos &pos)
bool GetMonth::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
return directMapping(out, pos, "toMonth");
}
bool GetYear::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
return directMapping(out, pos, "toYear");
}
bool HoursOfDay::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
return directMapping(out, pos, "toHour");
}
bool MakeTimeSpan::convertImpl(String &out,IParser::Pos &pos)
@ -162,39 +156,91 @@ bool Now::convertImpl(String &out,IParser::Pos &pos)
if (pos->type != TokenType::ClosingRoundBracket)
{
const auto offset = getConvertedArgument(fn_name, pos);
out = std::format("now('UTC') + {}", offset);
out = std::format("now64(9,'UTC') + {}", offset);
}
else
out = "now('UTC')";
out = "now64(9,'UTC')";
return true;
}
bool StartOfDay::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;
++pos;
const String datetime_str = getConvertedArgument(fn_name, pos);
String offset;
if (pos->type == TokenType::Comma)
{
++pos;
offset = getConvertedArgument(fn_name, pos);
}
out = std::format("date_add(DAY,{}, toDateTime64((toStartOfDay({})) , 9 , 'UTC')) ", offset, datetime_str);
return true;
}
bool StartOfMonth::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;
++pos;
const String datetime_str = getConvertedArgument(fn_name, pos);
String offset;
if (pos->type == TokenType::Comma)
{
++pos;
offset = getConvertedArgument(fn_name, pos);
}
out = std::format("date_add(MONTH,{}, toDateTime64((toStartOfMonth({})) , 9 , 'UTC')) ", offset, datetime_str);
return true;
}
bool StartOfWeek::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;
++pos;
const String datetime_str = getConvertedArgument(fn_name, pos);
String offset;
if (pos->type == TokenType::Comma)
{
++pos;
offset = getConvertedArgument(fn_name, pos);
}
out = std::format("date_add(Week,{}, toDateTime64((toStartOfWeek({})) , 9 , 'UTC')) ", offset, datetime_str);
return true;
}
bool StartOfYear::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;
++pos;
const String datetime_str = getConvertedArgument(fn_name, pos);
String offset ;
if (pos->type == TokenType::Comma)
{
++pos;
offset = getConvertedArgument(fn_name, pos);
}
out = std::format("date_add(YEAR,{}, toDateTime64((toStartOfYear({}, 'UTC')) , 9 , 'UTC'))", offset, datetime_str);
return true;
}
bool UnixTimeMicrosecondsToDateTime::convertImpl(String &out,IParser::Pos &pos)
@ -220,16 +266,32 @@ bool UnixTimeNanosecondsToDateTime::convertImpl(String &out,IParser::Pos &pos)
bool UnixTimeSecondsToDateTime::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;
++pos;
const String value = getConvertedArgument(fn_name, pos);
out = std::format("toDateTime64({},9,'UTC')", value);
return true;
}
bool WeekOfYear::convertImpl(String &out,IParser::Pos &pos)
{
String res = String(pos->begin,pos->end);
out = res;
return false;
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;
++pos;
const String time_str = getConvertedArgument(fn_name, pos);
out = std::format("toWeek({},3,'UTC')", time_str);
return true;
}
bool MonthOfYear::convertImpl(String &out,IParser::Pos &pos)
{
return directMapping(out, pos, "toMonth");
}
}

View File

@ -120,7 +120,7 @@ protected:
class HoursOfDay : public IParserKQLFunction
{
protected:
const char * getName() const override { return "hoursofday()"; }
const char * getName() const override { return "hourofday()"; }
bool convertImpl(String &out,IParser::Pos &pos) override;
};
@ -204,7 +204,14 @@ protected:
class WeekOfYear : public IParserKQLFunction
{
protected:
const char * getName() const override { return "weekofyear()"; }
const char * getName() const override { return "week_of_year()"; }
bool convertImpl(String &out,IParser::Pos &pos) override;
};
class MonthOfYear : public IParserKQLFunction
{
protected:
const char * getName() const override { return "monthofyear()"; }
bool convertImpl(String &out,IParser::Pos &pos) override;
};

View File

@ -35,7 +35,7 @@ namespace DB
{"format_timespan", KQLFunctionValue::format_timespan},
{"getmonth", KQLFunctionValue::getmonth},
{"getyear", KQLFunctionValue::getyear},
{"hoursofday", KQLFunctionValue::hoursofday},
{"hourofday", KQLFunctionValue::hourofday},
{"make_timespan", KQLFunctionValue::make_timespan},
{"make_datetime", KQLFunctionValue::make_datetime},
{"now", KQLFunctionValue::now},
@ -49,8 +49,8 @@ namespace DB
{"unixtime_milliseconds_todatetime", KQLFunctionValue::unixtime_milliseconds_todatetime},
{"unixtime_nanoseconds_todatetime", KQLFunctionValue::unixtime_nanoseconds_todatetime},
{"unixtime_seconds_todatetime", KQLFunctionValue::unixtime_seconds_todatetime},
{"weekofyear", KQLFunctionValue::weekofyear},
{"week_of_year", KQLFunctionValue::week_of_year},
{"monthofyear", KQLFunctionValue::monthofyear},
{"base64_encode_tostring", KQLFunctionValue::base64_encode_tostring},
{"base64_encode_fromguid", KQLFunctionValue::base64_encode_fromguid},
{"base64_decode_tostring", KQLFunctionValue::base64_decode_tostring},
@ -269,6 +269,9 @@ std::unique_ptr<IParserKQLFunction> KQLFunctionFactory::get(String &kql_function
case KQLFunctionValue::endofyear:
return std::make_unique<EndOfYear>();
case KQLFunctionValue::monthofyear:
return std::make_unique<MonthOfYear>();
case KQLFunctionValue::format_datetime:
return std::make_unique<FormatDateTime>();
@ -282,7 +285,7 @@ std::unique_ptr<IParserKQLFunction> KQLFunctionFactory::get(String &kql_function
case KQLFunctionValue::getyear:
return std::make_unique<GetYear>();
case KQLFunctionValue::hoursofday:
case KQLFunctionValue::hourofday:
return std::make_unique<HoursOfDay>();
case KQLFunctionValue::make_timespan:
@ -318,7 +321,7 @@ std::unique_ptr<IParserKQLFunction> KQLFunctionFactory::get(String &kql_function
case KQLFunctionValue::unixtime_seconds_todatetime:
return std::make_unique<UnixTimeSecondsToDateTime>();
case KQLFunctionValue::weekofyear:
case KQLFunctionValue::week_of_year:
return std::make_unique<WeekOfYear>();
case KQLFunctionValue::base64_encode_tostring:

View File

@ -19,11 +19,12 @@ namespace DB
endofday,
endofweek,
endofyear,
monthofyear,
format_datetime,
format_timespan,
getmonth,
getyear,
hoursofday,
hourofday,
make_timespan,
make_datetime,
now,
@ -37,7 +38,7 @@ namespace DB
unixtime_milliseconds_todatetime,
unixtime_nanoseconds_todatetime,
unixtime_seconds_todatetime,
weekofyear,
week_of_year,
base64_encode_tostring,
base64_encode_fromguid,