Skip to content

Commit 3b8a13f

Browse files
authored
Add CurrentDate, CurrentTime and CurrentTimestamp Functions Part1 (#2500)
### What problem does this PR solve? _Add CurrentDate, CurrentTime and CurrentTimestamp functions. More features will be implemented in subsequent PRs._ Issue link:(#2033) ### Type of change - [x] New Feature (non-breaking change which adds functionality) - [x] Test cases
1 parent 7be45cc commit 3b8a13f

10 files changed

+673
-0
lines changed

src/function/builtin_functions.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ import like;
4848
import minus;
4949
import modulo;
5050
import multiply;
51+
import current_date;
52+
import current_time;
53+
import current_timestamp;
5154
import century;
5255
import year;
5356
import month;
@@ -166,6 +169,9 @@ void BuiltinFunctions::RegisterScalarFunction() {
166169
RegisterPositionFunction(catalog_ptr_);
167170

168171
// date and time functions
172+
RegisterCurrentDateFunction(catalog_ptr_);
173+
RegisterCurrentTimeFunction(catalog_ptr_);
174+
RegisterCurrentTimestampFunction(catalog_ptr_);
169175
RegisterCenturyFunction(catalog_ptr_);
170176
RegisterYearFunction(catalog_ptr_);
171177
RegisterMonthFunction(catalog_ptr_);

src/function/scalar/current_date.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
module;
15+
#include <chrono>
16+
module current_date;
17+
import stl;
18+
import catalog;
19+
import status;
20+
import logical_type;
21+
import infinity_exception;
22+
import scalar_function;
23+
import scalar_function_set;
24+
import third_party;
25+
import internal_types;
26+
import data_type;
27+
import column_vector;
28+
29+
namespace infinity {
30+
using namespace std::chrono;
31+
struct CurrentDateFunction {
32+
const char* defaultTZ = "Asia/Shanghai";
33+
template <typename TA, typename TB>
34+
static inline void Run(TA &left, TB &result) {
35+
Status status = Status::NotSupport("Not implemented");
36+
RecoverableError(status);
37+
return;
38+
}
39+
static inline void TimeZoneConvertHelper(VarcharT &left) {
40+
const char* tzValue = std::getenv("TZ");
41+
const std::string str = left.ToString();
42+
const char* newTZ = str.c_str();
43+
if ( tzValue == newTZ) {
44+
return;
45+
}
46+
if (setenv("TZ", newTZ, 1) != 0) {
47+
const char* newTZ = CurrentDateFunction().defaultTZ;
48+
setenv("TZ", newTZ, 1);
49+
}
50+
tzset();
51+
return;
52+
}
53+
static inline void TimeZoneResetHelper() {
54+
const char* tzValue = std::getenv("TZ");
55+
if (tzValue == CurrentDateFunction().defaultTZ) {
56+
return;
57+
}
58+
setenv("TZ", CurrentDateFunction().defaultTZ, 1);
59+
tzset();
60+
return;
61+
}
62+
};
63+
64+
template <>
65+
inline void CurrentDateFunction::Run(VarcharT &left, DateT &result) {
66+
TimeZoneConvertHelper(left);
67+
auto now = system_clock::now();
68+
auto sys_days = std::chrono::floor<std::chrono::days>(now);
69+
result.value = sys_days.time_since_epoch().count();
70+
TimeZoneResetHelper();
71+
}
72+
73+
void RegisterCurrentDateFunction(const UniquePtr<Catalog> &catalog_ptr) {
74+
String func_name = "currentdate";
75+
76+
SharedPtr<ScalarFunctionSet> function_set_ptr = MakeShared<ScalarFunctionSet>(func_name);
77+
78+
ScalarFunction current_date_function(func_name,
79+
{DataType(LogicalType::kVarchar)},
80+
DataType(LogicalType::kDate),
81+
&ScalarFunction::UnaryFunction<VarcharT, DateT, CurrentDateFunction>);
82+
function_set_ptr->AddFunction(current_date_function);
83+
84+
Catalog::AddFunctionSet(catalog_ptr.get(), function_set_ptr);
85+
}
86+
87+
} // namespace infinity

src/function/scalar/current_date.cppm

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
module;
16+
17+
export module current_date;
18+
19+
import stl;
20+
21+
namespace infinity {
22+
23+
class Catalog;
24+
25+
export void RegisterCurrentDateFunction(const UniquePtr<Catalog> &catalog_ptr);
26+
27+
} // namespace infinity

src/function/scalar/current_time.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
module;
15+
#include <chrono>
16+
module current_time;
17+
import stl;
18+
import catalog;
19+
import status;
20+
import logical_type;
21+
import infinity_exception;
22+
import scalar_function;
23+
import scalar_function_set;
24+
import third_party;
25+
import internal_types;
26+
import data_type;
27+
import column_vector;
28+
29+
namespace infinity {
30+
using namespace std::chrono;
31+
struct CurrentTimeFunction {
32+
const char* defaultTZ = "Asia/Shanghai";
33+
template <typename TA, typename TB>
34+
static inline void Run(TA &left, TB &result) {
35+
Status status = Status::NotSupport("Not implemented");
36+
RecoverableError(status);
37+
}
38+
static inline void TimeZoneConvertHelper(VarcharT &left) {
39+
const char* tzValue = std::getenv("TZ");
40+
const std::string str = left.ToString();
41+
const char* newTZ = str.c_str();
42+
if ( tzValue == newTZ) {
43+
return;
44+
}
45+
if (setenv("TZ", newTZ, 1) != 0) {
46+
const char* newTZ = "Asia/Shanghai";
47+
setenv("TZ", newTZ, 1);
48+
}
49+
tzset();
50+
return;
51+
}
52+
53+
static inline void TimeZoneResetHelper() {
54+
const char* tzValue = std::getenv("TZ");
55+
if (tzValue == CurrentTimeFunction().defaultTZ) {
56+
return;
57+
}
58+
setenv("TZ", CurrentTimeFunction().defaultTZ, 1);
59+
tzset();
60+
return;
61+
}
62+
};
63+
64+
template <>
65+
inline void CurrentTimeFunction::Run(VarcharT &left, TimeT &result) {
66+
TimeZoneConvertHelper(left);
67+
auto now = system_clock::now();
68+
auto sys_days = std::chrono::floor<std::chrono::days>(now);
69+
auto sys_secs = std::chrono::floor<std::chrono::seconds>(now);
70+
result.value = static_cast<i32>(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count());
71+
TimeZoneResetHelper();
72+
}
73+
74+
void RegisterCurrentTimeFunction(const UniquePtr<Catalog> &catalog_ptr) {
75+
String func_name = "currenttime";
76+
77+
SharedPtr<ScalarFunctionSet> function_set_ptr = MakeShared<ScalarFunctionSet>(func_name);
78+
79+
ScalarFunction current_time_function(func_name,
80+
{DataType(LogicalType::kVarchar)},
81+
DataType(LogicalType::kTime),
82+
&ScalarFunction::UnaryFunction<VarcharT, TimeT, CurrentTimeFunction>);
83+
function_set_ptr->AddFunction(current_time_function);
84+
85+
Catalog::AddFunctionSet(catalog_ptr.get(), function_set_ptr);
86+
}
87+
88+
} // namespace infinity

src/function/scalar/current_time.cppm

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
module;
16+
17+
export module current_time;
18+
19+
import stl;
20+
21+
namespace infinity {
22+
23+
class Catalog;
24+
25+
export void RegisterCurrentTimeFunction(const UniquePtr<Catalog> &catalog_ptr);
26+
27+
} // namespace infinity
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
module;
15+
#include <chrono>
16+
module current_timestamp;
17+
import stl;
18+
import catalog;
19+
import status;
20+
import logical_type;
21+
import infinity_exception;
22+
import scalar_function;
23+
import scalar_function_set;
24+
import third_party;
25+
import internal_types;
26+
import data_type;
27+
import column_vector;
28+
29+
namespace infinity {
30+
using namespace std::chrono;
31+
struct CurrentTimestampFunction {
32+
const char* defaultTZ = "Asia/Shanghai";
33+
template <typename TA, typename TB>
34+
static inline void Run(TA &left, TB &result) {
35+
Status status = Status::NotSupport("Not implemented");
36+
RecoverableError(status);
37+
}
38+
static inline void TimeZoneConvertHelper(VarcharT &left) {
39+
const char* tzValue = std::getenv("TZ");
40+
const std::string str = left.ToString();
41+
const char* newTZ = str.c_str();
42+
if ( tzValue == newTZ) {
43+
return;
44+
}
45+
if (setenv("TZ", newTZ, 1) != 0) {
46+
const char* newTZ = "Asia/Shanghai";
47+
setenv("TZ", newTZ, 1);
48+
}
49+
tzset();
50+
return;
51+
}
52+
53+
static inline void TimeZoneResetHelper() {
54+
const char* tzValue = std::getenv("TZ");
55+
if (tzValue == CurrentTimestampFunction().defaultTZ) {
56+
return;
57+
}
58+
setenv("TZ", CurrentTimestampFunction().defaultTZ, 1);
59+
tzset();
60+
return;
61+
}
62+
};
63+
64+
template <>
65+
inline void CurrentTimestampFunction::Run(VarcharT &left, TimestampT &result) {
66+
TimeZoneConvertHelper(left);
67+
auto now = system_clock::now();
68+
auto sys_days = std::chrono::floor<std::chrono::days>(now);
69+
auto sys_secs = std::chrono::floor<std::chrono::seconds>(now);
70+
result.time.value = static_cast<i32>(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count());
71+
result.date.value = static_cast<i32>(sys_days.time_since_epoch().count());
72+
TimeZoneResetHelper();
73+
}
74+
75+
void RegisterCurrentTimestampFunction(const UniquePtr<Catalog> &catalog_ptr) {
76+
String func_name = "currenttimestamp";
77+
78+
SharedPtr<ScalarFunctionSet> function_set_ptr = MakeShared<ScalarFunctionSet>(func_name);
79+
80+
ScalarFunction current_timestamp_function(func_name,
81+
{DataType(LogicalType::kVarchar)},
82+
DataType(LogicalType::kTimestamp),
83+
&ScalarFunction::UnaryFunction<VarcharT, TimestampT, CurrentTimestampFunction>);
84+
function_set_ptr->AddFunction(current_timestamp_function);
85+
86+
Catalog::AddFunctionSet(catalog_ptr.get(), function_set_ptr);
87+
}
88+
89+
} // namespace infinity
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
module;
16+
17+
export module current_timestamp;
18+
19+
import stl;
20+
21+
namespace infinity {
22+
23+
class Catalog;
24+
25+
export void RegisterCurrentTimestampFunction(const UniquePtr<Catalog> &catalog_ptr);
26+
27+
} // namespace infinity

0 commit comments

Comments
 (0)