Skip to content

Commit

Permalink
support function timestamp_xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
taiyang-li committed Sep 4, 2024
1 parent 61460d0 commit b93718a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ object CHExpressionUtil {
REGR_SXY -> DefaultValidator(),
TO_UTC_TIMESTAMP -> UtcTimestampValidator(),
FROM_UTC_TIMESTAMP -> UtcTimestampValidator(),
TIMESTAMP_MILLIS -> DefaultValidator(),
TIMESTAMP_MICROS -> DefaultValidator(),
STACK -> DefaultValidator(),
TRANSFORM_KEYS -> DefaultValidator(),
TRANSFORM_VALUES -> DefaultValidator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,4 +780,16 @@ class GlutenFunctionValidateSuite extends GlutenClickHouseWholeStageTransformerS
|""".stripMargin
runQueryAndCompare(sql)(checkGlutenOperatorMatch[ProjectExecTransformer])
}

test("test function timestamp_seconds/timestamp_millis/timestamp_micros") {
val sql = """
|SELECT
| id,
| timestamp_seconds(1725453790 + id) as ts_seconds,
| timestamp_millis(1725453790123 + id) as ts_millis,
| timestamp_micros(1725453790123456 + id) as ts_micros
|from range(10);
|""".stripMargin
runQueryAndCompare(sql)(checkGlutenOperatorMatch[ProjectExecTransformer])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ REGISTER_COMMON_SCALAR_FUNCTION_PARSER(UnixSeconds, unix_seconds, toUnixTimestam
REGISTER_COMMON_SCALAR_FUNCTION_PARSER(UnixDate, unix_date, toInt32);
REGISTER_COMMON_SCALAR_FUNCTION_PARSER(UnixMillis, unix_millis, toUnixTimestamp64Milli);
REGISTER_COMMON_SCALAR_FUNCTION_PARSER(UnixMicros, unix_micros, toUnixTimestamp64Micro);
REGISTER_COMMON_SCALAR_FUNCTION_PARSER(TimestampMillis, timestamp_millis, fromUnixTimestamp64Milli);
REGISTER_COMMON_SCALAR_FUNCTION_PARSER(TimestampMicros, timestamp_micros, fromUnixTimestamp64Micro);

// array functions
REGISTER_COMMON_SCALAR_FUNCTION_PARSER(Array, array, array);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 <DataTypes/IDataType.h>
#include <Parser/FunctionParser.h>

namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
}

namespace local_engine
{

class FunctionParserTimestampSeconds : public FunctionParser
{
public:
explicit FunctionParserTimestampSeconds(SerializedPlanParser * plan_parser_) : FunctionParser(plan_parser_) { }
~FunctionParserTimestampSeconds() override = default;

static constexpr auto name = "timestamp_seconds";

String getName() const override { return name; }

const ActionsDAG::Node * parse(const substrait::Expression_ScalarFunction & substrait_func, ActionsDAG & actions_dag) const override
{
/// Parse timestamp_seconds(expr) as toDateTime64(expr, 6)
auto parsed_args = parseFunctionArguments(substrait_func, actions_dag);
if (parsed_args.size() != 1)
throw Exception(DB::ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires exactly one argument", getName());

const auto * arg = parsed_args[0];
const auto * precision_node = addColumnToActionsDAG(actions_dag, std::make_shared<DataTypeUInt8>(), 6);
const auto * toDateTime64_node = toFunctionNode(actions_dag, "toDateTime64", {arg, precision_node});
return convertNodeTypeIfNeeded(substrait_func, toDateTime64_node, actions_dag);
}
};

static FunctionParserRegister<FunctionParserTimestampSeconds> register_timestamp_seconds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ object ExpressionMappings {
Sig[UnixSeconds](UNIX_SECONDS),
Sig[UnixMillis](UNIX_MILLIS),
Sig[UnixMicros](UNIX_MICROS),
Sig[SecondsToTimestamp](TIMESTAMP_SECONDS),
Sig[MillisToTimestamp](TIMESTAMP_MILLIS),
Sig[MicrosToTimestamp](TIMESTAMP_MICROS),
Sig[PreciseTimestampConversion](PRECYSE_TIMESTAMP_CONVERSION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,6 @@ class ClickHouseTestSettings extends BackendTestSettings {
.exclude("SPARK-31896: Handle am-pm timestamp parsing when hour is missing")
.exclude("DATE_FROM_UNIX_DATE")
.exclude("UNIX_SECONDS")
.exclude("TIMESTAMP_SECONDS")
.exclude("TIMESTAMP_MILLIS")
.exclude("TIMESTAMP_MICROS")
.exclude("SPARK-33498: GetTimestamp,UnixTimestamp,ToUnixTimestamp with parseError")
.exclude("SPARK-34739,SPARK-35889: add a year-month interval to a timestamp")
.exclude("SPARK-34761,SPARK-35889: add a day-time interval to a timestamp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,6 @@ class ClickHouseTestSettings extends BackendTestSettings {
.exclude("SPARK-31896: Handle am-pm timestamp parsing when hour is missing")
.exclude("DATE_FROM_UNIX_DATE")
.exclude("UNIX_SECONDS")
.exclude("TIMESTAMP_SECONDS")
.exclude("TIMESTAMP_MILLIS")
.exclude("TIMESTAMP_MICROS")
.exclude("SPARK-33498: GetTimestamp,UnixTimestamp,ToUnixTimestamp with parseError")
.exclude("SPARK-34739,SPARK-35889: add a year-month interval to a timestamp")
.exclude("SPARK-34761,SPARK-35889: add a day-time interval to a timestamp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,6 @@ class ClickHouseTestSettings extends BackendTestSettings {
.exclude("SPARK-31896: Handle am-pm timestamp parsing when hour is missing")
.exclude("DATE_FROM_UNIX_DATE")
.exclude("UNIX_SECONDS")
.exclude("TIMESTAMP_SECONDS")
.exclude("TIMESTAMP_MILLIS")
.exclude("TIMESTAMP_MICROS")
.exclude("SPARK-33498: GetTimestamp,UnixTimestamp,ToUnixTimestamp with parseError")
.exclude("SPARK-34739,SPARK-35889: add a year-month interval to a timestamp")
.exclude("SPARK-34761,SPARK-35889: add a day-time interval to a timestamp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,6 @@ class ClickHouseTestSettings extends BackendTestSettings {
.exclude("SPARK-31896: Handle am-pm timestamp parsing when hour is missing")
.exclude("DATE_FROM_UNIX_DATE")
.exclude("UNIX_SECONDS")
.exclude("TIMESTAMP_SECONDS")
.exclude("TIMESTAMP_MILLIS")
.exclude("TIMESTAMP_MICROS")
.exclude("SPARK-33498: GetTimestamp,UnixTimestamp,ToUnixTimestamp with parseError")
.exclude("SPARK-34739,SPARK-35889: add a year-month interval to a timestamp")
.exclude("SPARK-34761,SPARK-35889: add a day-time interval to a timestamp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ object ExpressionNames {
final val UNIX_SECONDS = "unix_seconds"
final val UNIX_MILLIS = "unix_millis"
final val UNIX_MICROS = "unix_micros"
final val TIMESTAMP_SECONDS = "timestamp_seconds"
final val TIMESTAMP_MILLIS = "timestamp_millis"
final val TIMESTAMP_MICROS = "timestamp_micros"
final val PRECYSE_TIMESTAMP_CONVERSION = "precise_timestamp_conversion"
Expand Down

0 comments on commit b93718a

Please sign in to comment.