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

Minor refactoring of formatDateTime() #48627

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/Functions/formatDateTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,15 +978,15 @@ class FunctionFormatDateTimeImpl : public IFunction
instructions.push_back(std::move(instruction));
};

auto add_extra_shift_or_literal_instruction = [&](size_t amount, std::string_view literal)
auto add_extra_shift_or_literal_instruction = [&](std::string_view literal)
{
if (mysql_with_only_fixed_length_formatters)
add_extra_shift(amount);
add_extra_shift(literal.size());
else
add_literal_instruction(literal);
};

auto add_time_instruction = [&]([[maybe_unused]] typename Instruction<T>::FuncMysql && func, [[maybe_unused]] size_t amount, [[maybe_unused]] std::string_view literal)
auto add_time_instruction = [&]([[maybe_unused]] typename Instruction<T>::FuncMysql && func, [[maybe_unused]] std::string_view literal)
{
/// DateTime/DateTime64 --> insert instruction
/// Other types cannot provide the requested data --> write out template
Expand All @@ -997,7 +997,7 @@ class FunctionFormatDateTimeImpl : public IFunction
instructions.push_back(std::move(instruction));
}
else
add_extra_shift_or_literal_instruction(amount, literal);
add_extra_shift_or_literal_instruction(literal);
};

Pos pos = format.data();
Expand All @@ -1012,7 +1012,7 @@ class FunctionFormatDateTimeImpl : public IFunction
if (pos < percent_pos)
{
/// Handle characters before next %
add_extra_shift_or_literal_instruction(percent_pos - pos, std::string_view(pos, percent_pos - pos));
add_extra_shift_or_literal_instruction(std::string_view(pos, percent_pos - pos));
out_template += String(pos, percent_pos - pos);
}

Expand Down Expand Up @@ -1107,7 +1107,7 @@ class FunctionFormatDateTimeImpl : public IFunction
else
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlMinute, 2, val);
add_time_instruction(&Instruction<T>::mysqlMinute, val);
out_template += val;
}
break;
Expand Down Expand Up @@ -1260,7 +1260,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'p':
{
static constexpr std::string_view val = "AM";
add_time_instruction(&Instruction<T>::mysqlAMPM, 2, val);
add_time_instruction(&Instruction<T>::mysqlAMPM, val);
out_template += val;
break;
}
Expand All @@ -1269,7 +1269,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'r':
{
static constexpr std::string_view val = "12:00 AM";
add_time_instruction(&Instruction<T>::mysqlHHMM12, 8, val);
add_time_instruction(&Instruction<T>::mysqlHHMM12, val);
out_template += val;
break;
}
Expand All @@ -1278,7 +1278,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'R':
{
static constexpr std::string_view val = "00:00";
add_time_instruction(&Instruction<T>::mysqlHHMM24, 5, val);
add_time_instruction(&Instruction<T>::mysqlHHMM24, val);
out_template += val;
break;
}
Expand All @@ -1287,7 +1287,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 's':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlSecond, 2, val);
add_time_instruction(&Instruction<T>::mysqlSecond, val);
out_template += val;
break;
}
Expand All @@ -1296,7 +1296,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'S':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlSecond, 2, val);
add_time_instruction(&Instruction<T>::mysqlSecond, val);
out_template += val;
break;
}
Expand All @@ -1305,7 +1305,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'T':
{
static constexpr std::string_view val = "00:00:00";
add_time_instruction(&Instruction<T>::mysqlISO8601Time, 8, val);
add_time_instruction(&Instruction<T>::mysqlISO8601Time, val);
out_template += val;
break;
}
Expand All @@ -1314,7 +1314,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'h':
{
static constexpr std::string_view val = "12";
add_time_instruction(&Instruction<T>::mysqlHour12, 2, val);
add_time_instruction(&Instruction<T>::mysqlHour12, val);
out_template += val;
break;
}
Expand All @@ -1323,7 +1323,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'H':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlHour24, 2, val);
add_time_instruction(&Instruction<T>::mysqlHour24, val);
out_template += val;
break;
}
Expand All @@ -1332,7 +1332,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'i':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlMinute, 2, val);
add_time_instruction(&Instruction<T>::mysqlMinute, val);
out_template += val;
break;
}
Expand All @@ -1341,7 +1341,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'I':
{
static constexpr std::string_view val = "12";
add_time_instruction(&Instruction<T>::mysqlHour12, 2, val);
add_time_instruction(&Instruction<T>::mysqlHour12, val);
out_template += val;
break;
}
Expand All @@ -1350,7 +1350,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'k':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlHour24, 2, val);
add_time_instruction(&Instruction<T>::mysqlHour24, val);
out_template += val;
break;
}
Expand All @@ -1359,23 +1359,23 @@ class FunctionFormatDateTimeImpl : public IFunction
case 'l':
{
static constexpr std::string_view val = "12";
add_time_instruction(&Instruction<T>::mysqlHour12, 2, val);
add_time_instruction(&Instruction<T>::mysqlHour12, val);
out_template += val;
break;
}

case 't':
{
static constexpr std::string_view val = "\t";
add_extra_shift_or_literal_instruction(1, val);
add_extra_shift_or_literal_instruction(val);
out_template += val;
break;
}

case 'n':
{
static constexpr std::string_view val = "\n";
add_extra_shift_or_literal_instruction(1, val);
add_extra_shift_or_literal_instruction(val);
out_template += val;
break;
}
Expand All @@ -1384,7 +1384,7 @@ class FunctionFormatDateTimeImpl : public IFunction
case '%':
{
static constexpr std::string_view val = "%";
add_extra_shift_or_literal_instruction(1, val);
add_extra_shift_or_literal_instruction(val);
out_template += val;
break;
}
Expand All @@ -1411,7 +1411,7 @@ class FunctionFormatDateTimeImpl : public IFunction
else
{
/// Handle characters after last %
add_extra_shift_or_literal_instruction(end - pos, std::string_view(pos, end - pos));
add_extra_shift_or_literal_instruction(std::string_view(pos, end - pos));
out_template += String(pos, end - pos);
break;
}
Expand Down