Skip to content

Commit

Permalink
Merge branch 'master' into grace_shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
bestwoody authored Mar 16, 2022
2 parents ad3601d + 921dd4d commit c1bb01f
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dbms/src/Flash/Coprocessor/DAGUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ const std::unordered_map<tipb::ScalarFuncSig, String> scalar_func_map({

{tipb::ScalarFuncSig::DayName, "toDayName"},
{tipb::ScalarFuncSig::DayOfMonth, "toDayOfMonth"},
//{tipb::ScalarFuncSig::DayOfWeek, "cast"},
//{tipb::ScalarFuncSig::DayOfYear, "cast"},
{tipb::ScalarFuncSig::DayOfWeek, "toDayOfWeek"},
{tipb::ScalarFuncSig::DayOfYear, "toDayOfYear"},

//{tipb::ScalarFuncSig::WeekWithMode, "cast"},
//{tipb::ScalarFuncSig::WeekWithoutMode, "cast"},
Expand Down
1 change: 1 addition & 0 deletions dbms/src/Functions/FunctionsDateTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void registerFunctionsDateTime(FunctionFactory & factory)
factory.registerFunction<FunctionToMonth>();
factory.registerFunction<FunctionToDayOfMonth>();
factory.registerFunction<FunctionToDayOfWeek>();
factory.registerFunction<FunctionToDayOfYear>();
factory.registerFunction<FunctionToHour>();
factory.registerFunction<FunctionToMinute>();
factory.registerFunction<FunctionToSecond>();
Expand Down
27 changes: 25 additions & 2 deletions dbms/src/Functions/FunctionsDateTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,36 @@ struct ToDayOfWeekImpl
{
return time_zone.toDayOfWeek(DayNum(d));
}
static inline UInt8 execute(UInt64, const DateLUTImpl &)
static inline UInt8 execute(UInt64 d, const DateLUTImpl &)
{
throw Exception("Illegal type MyTime of argument for function toDayOfWeek", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
MyDateTime my_time(d);
return UInt8(my_time.weekDay() + 1);
}

using FactorTransform = ToMondayImpl;
};

struct ToDayOfYearImpl
{
static constexpr auto name = "toDayOfYear";

static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone)
{
return time_zone.toDayOfYear(t);
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toDayOfYear(DayNum(d));
}
static inline UInt16 execute(UInt64 d, const DateLUTImpl &)
{
MyDateTime my_time(d);
return UInt16(my_time.yearDay());
}

using FactorTransform = ToStartOfYearImpl;
};

struct ToHourImpl
{
static constexpr auto name = "toHour";
Expand Down Expand Up @@ -3373,6 +3395,7 @@ using FunctionToQuarter = FunctionDateOrDateTimeToSomething<DataTypeUInt8, ToQua
using FunctionToMonth = FunctionDateOrDateTimeToSomething<DataTypeUInt8, ToMonthImpl>;
using FunctionToDayOfMonth = FunctionDateOrDateTimeToSomething<DataTypeUInt8, ToDayOfMonthImpl>;
using FunctionToDayOfWeek = FunctionDateOrDateTimeToSomething<DataTypeUInt8, ToDayOfWeekImpl>;
using FunctionToDayOfYear = FunctionDateOrDateTimeToSomething<DataTypeUInt16, ToDayOfYearImpl>;
using FunctionToHour = FunctionDateOrDateTimeToSomething<DataTypeUInt8, ToHourImpl>;
using FunctionToMinute = FunctionDateOrDateTimeToSomething<DataTypeUInt8, ToMinuteImpl>;
using FunctionToSecond = FunctionDateOrDateTimeToSomething<DataTypeUInt8, ToSecondImpl>;
Expand Down
196 changes: 196 additions & 0 deletions dbms/src/Functions/tests/gtest_dayofweekyear.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright 2022 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <Common/MyTime.h>
#include <Functions/FunctionFactory.h>
#include <TestUtils/FunctionTestUtils.h>
#include <TestUtils/TiFlashTestBasic.h>

#include <string>
#include <vector>

namespace DB::tests
{
class TestDayOfWeekYear : public DB::tests::FunctionTest
{
};

TEST_F(TestDayOfWeekYear, TestDayOfWeek)
try
{
/// ColumnVector(nullable)
const String func_name = "toDayOfWeek";
static auto const nullable_datetime_type_ptr = makeNullable(std::make_shared<DataTypeMyDateTime>(6));
static auto const datetime_type_ptr = std::make_shared<DataTypeMyDateTime>(6);
static auto const date_type_ptr = std::make_shared<DataTypeMyDate>();
auto data_col_ptr = createColumn<Nullable<DataTypeMyDateTime::FieldType>>(
{
{}, // Null
// FIXME: https://github.com/pingcap/tiflash/issues/4186
// MyDateTime(2022, 12, 0, 1, 1, 1, 1).toPackedUInt(),
// MyDateTime(2022, 13, 31, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(1969, 1, 2, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2022, 3, 13, 6, 7, 8, 9).toPackedUInt(),
MyDateTime(2022, 3, 14, 9, 8, 7, 6).toPackedUInt(),
MyDateTime(2022, 3, 15, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 16, 1, 2, 3, 4).toPackedUInt(),
MyDateTime(2022, 3, 17, 4, 3, 2, 1).toPackedUInt(),
MyDateTime(2022, 3, 18, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt(),
})
.column;
auto input_col = ColumnWithTypeAndName(data_col_ptr, nullable_datetime_type_ptr, "input");
auto output_col = createColumn<Nullable<UInt8>>({{}, 5, 1, 2, 3, 4, 5, 6, 7});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnVector(non-null)
data_col_ptr = createColumn<DataTypeMyDateTime::FieldType>(
{
MyDateTime(1969, 1, 2, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2022, 3, 13, 6, 7, 8, 9).toPackedUInt(),
MyDateTime(2022, 3, 14, 9, 8, 7, 6).toPackedUInt(),
MyDateTime(2022, 3, 15, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 16, 1, 2, 3, 4).toPackedUInt(),
MyDateTime(2022, 3, 17, 4, 3, 2, 1).toPackedUInt(),
MyDateTime(2022, 3, 18, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt(),
})
.column;
input_col = ColumnWithTypeAndName(data_col_ptr, datetime_type_ptr, "input");
output_col = createColumn<UInt8>({5, 1, 2, 3, 4, 5, 6, 7});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnConst(non-null)
input_col = ColumnWithTypeAndName(createConstColumn<DataTypeMyDateTime::FieldType>(1, MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt()).column, datetime_type_ptr, "input");
output_col = createConstColumn<UInt8>(1, {7});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnConst(nullable)
input_col = ColumnWithTypeAndName(createConstColumn<Nullable<DataTypeMyDateTime::FieldType>>(1, MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt()).column, nullable_datetime_type_ptr, "input");
output_col = createConstColumn<Nullable<UInt8>>(1, {7});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnConst(nullable(null))
input_col = ColumnWithTypeAndName(createConstColumn<Nullable<DataTypeMyDateTime::FieldType>>(1, {}).column, nullable_datetime_type_ptr, "input");
output_col = createConstColumn<Nullable<UInt8>>(1, {});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// MyDate ColumnVector(non-null)
data_col_ptr = createColumn<DataTypeMyDate::FieldType>(
{
MyDate(1969, 1, 2).toPackedUInt(),
MyDate(2022, 3, 13).toPackedUInt(),
MyDate(2022, 3, 14).toPackedUInt(),
MyDate(2022, 3, 15).toPackedUInt(),
MyDate(2022, 3, 16).toPackedUInt(),
MyDate(2022, 3, 17).toPackedUInt(),
MyDate(2022, 3, 18).toPackedUInt(),
MyDate(2022, 3, 19).toPackedUInt(),
})
.column;
input_col = ColumnWithTypeAndName(data_col_ptr, date_type_ptr, "input");
output_col = createColumn<UInt8>({5, 1, 2, 3, 4, 5, 6, 7});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));
}
CATCH

TEST_F(TestDayOfWeekYear, TestDayOfYear)
try
{
/// ColumnVector(nullable)
const String func_name = "toDayOfYear";
static auto const nullable_datetime_type_ptr = makeNullable(std::make_shared<DataTypeMyDateTime>(6));
static auto const datetime_type_ptr = std::make_shared<DataTypeMyDateTime>(6);
static auto const date_type_ptr = std::make_shared<DataTypeMyDate>();
auto data_col_ptr = createColumn<Nullable<DataTypeMyDateTime::FieldType>>(
{
{}, // Null
// FIXME: https://github.com/pingcap/tiflash/issues/4186
// MyDateTime(2022, 12, 0, 1, 1, 1, 1).toPackedUInt(),
// MyDateTime(2022, 13, 31, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(1969, 1, 2, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2022, 3, 13, 6, 7, 8, 9).toPackedUInt(),
MyDateTime(2022, 3, 14, 9, 8, 7, 6).toPackedUInt(),
MyDateTime(2022, 3, 15, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 16, 1, 2, 3, 4).toPackedUInt(),
MyDateTime(2022, 3, 17, 4, 3, 2, 1).toPackedUInt(),
MyDateTime(2022, 3, 18, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(1900, 12, 31, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2020, 12, 31, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2022, 12, 31, 1, 1, 1, 1).toPackedUInt(),
})
.column;
auto input_col = ColumnWithTypeAndName(data_col_ptr, nullable_datetime_type_ptr, "input");
auto output_col = createColumn<Nullable<UInt16>>({{}, 2, 72, 73, 74, 75, 76, 77, 78, 365, 366, 365});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnVector(non-null)
data_col_ptr = createColumn<DataTypeMyDateTime::FieldType>(
{
MyDateTime(1969, 1, 2, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2022, 3, 13, 6, 7, 8, 9).toPackedUInt(),
MyDateTime(2022, 3, 14, 9, 8, 7, 6).toPackedUInt(),
MyDateTime(2022, 3, 15, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 16, 1, 2, 3, 4).toPackedUInt(),
MyDateTime(2022, 3, 17, 4, 3, 2, 1).toPackedUInt(),
MyDateTime(2022, 3, 18, 0, 0, 0, 0).toPackedUInt(),
MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(1900, 12, 31, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2020, 12, 31, 1, 1, 1, 1).toPackedUInt(),
MyDateTime(2022, 12, 31, 1, 1, 1, 1).toPackedUInt(),
})
.column;
input_col = ColumnWithTypeAndName(data_col_ptr, datetime_type_ptr, "input");
output_col = createColumn<UInt16>({2, 72, 73, 74, 75, 76, 77, 78, 365, 366, 365});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnConst(non-null)
input_col = ColumnWithTypeAndName(createConstColumn<DataTypeMyDateTime::FieldType>(1, MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt()).column, datetime_type_ptr, "input");
output_col = createConstColumn<UInt16>(1, {78});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnConst(nullable)
input_col = ColumnWithTypeAndName(createConstColumn<Nullable<DataTypeMyDateTime::FieldType>>(1, MyDateTime(2022, 3, 19, 1, 1, 1, 1).toPackedUInt()).column, nullable_datetime_type_ptr, "input");
output_col = createConstColumn<Nullable<UInt16>>(1, {78});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// ColumnConst(nullable(null))
input_col = ColumnWithTypeAndName(createConstColumn<Nullable<DataTypeMyDateTime::FieldType>>(1, {}).column, nullable_datetime_type_ptr, "input");
output_col = createConstColumn<Nullable<UInt16>>(1, {});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));

/// MyDate ColumnVector(non-null)
data_col_ptr = createColumn<DataTypeMyDate::FieldType>(
{
MyDate(1969, 1, 2).toPackedUInt(),
MyDate(2022, 3, 13).toPackedUInt(),
MyDate(2022, 3, 14).toPackedUInt(),
MyDate(2022, 3, 15).toPackedUInt(),
MyDate(2022, 3, 16).toPackedUInt(),
MyDate(2022, 3, 17).toPackedUInt(),
MyDate(2022, 3, 18).toPackedUInt(),
MyDate(2022, 3, 19).toPackedUInt(),
MyDate(1900, 12, 31).toPackedUInt(),
MyDate(2020, 12, 31).toPackedUInt(),
MyDate(2022, 12, 31).toPackedUInt(),
})
.column;
input_col = ColumnWithTypeAndName(data_col_ptr, date_type_ptr, "input");
output_col = createColumn<UInt16>({2, 72, 73, 74, 75, 76, 77, 78, 365, 366, 365});
ASSERT_COLUMN_EQ(output_col, executeFunction(func_name, input_col));
}
CATCH

} // namespace DB::tests
42 changes: 42 additions & 0 deletions tests/fullstack-test/expr/day_of_weekyear.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2022 PingCAP, Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

mysql> drop table if exists test.t;
mysql> create table test.t(a char(20), b datetime, c date);
mysql> insert into test.t values('', '1970-1-1 12:12:12', '1970-1-1');
mysql> insert into test.t values('123', '1989-6-6 12:12:12', '1989-6-6');
mysql> insert into test.t values('2022-3-10', '2000-3-4 12:12:12', '2000-3-4');
mysql> alter table test.t set tiflash replica 1;

func> wait_table test t

mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; select dayofweek(a), dayofweek(b), dayofweek(c) from test.t;
+--------------+--------------+--------------+
| dayofweek(a) | dayofweek(b) | dayofweek(c) |
+--------------+--------------+--------------+
| NULL | 5 | 5 |
| NULL | 3 | 3 |
| 5 | 7 | 7 |
+--------------+--------------+--------------+

mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; select dayofyear(a), dayofyear(b), dayofyear(c) from test.t;
+--------------+--------------+--------------+
| dayofyear(a) | dayofyear(b) | dayofyear(c) |
+--------------+--------------+--------------+
| NULL | 1 | 1 |
| NULL | 157 | 157 |
| 69 | 64 | 64 |
+--------------+--------------+--------------+

mysql> drop table if exists test.t;

0 comments on commit c1bb01f

Please sign in to comment.