Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "vec/functions/function_date_or_datetime_computation.h"

#include "runtime/define_primitive_type.h"
#include "vec/functions/simple_function_factory.h"

namespace doris::vectorized {
Expand Down Expand Up @@ -58,7 +59,9 @@ struct CurTimeFunctionName {
};

using FunctionCurTime = FunctionCurrentDateOrDateTime<CurrentTimeImpl<CurTimeFunctionName>>;
using FunctionUtcTimeStamp = FunctionCurrentDateOrDateTime<UtcTimestampImpl>;
using FunctionUtcTimeStamp = FunctionCurrentDateOrDateTime<UtcImpl<PrimitiveType::TYPE_DATETIMEV2>>;
using FunctionUtcDate = FunctionCurrentDateOrDateTime<UtcImpl<PrimitiveType::TYPE_DATEV2>>;
using FunctionUtcTime = FunctionCurrentDateOrDateTime<UtcImpl<PrimitiveType::TYPE_TIMEV2>>;
using FunctionTimeToSec = FunctionCurrentDateOrDateTime<TimeToSecImpl>;
using FunctionSecToTime = FunctionCurrentDateOrDateTime<SecToTimeImpl>;
using FunctionMicroSecToDateTime = TimestampToDateTime<MicroSec>;
Expand Down Expand Up @@ -86,6 +89,8 @@ void register_function_date_time_computation(SimpleFunctionFactory& factory) {
factory.register_function(CurDateFunctionName::name, &createCurDateFunctionBuilderFunction);
factory.register_function<FunctionCurTime>();
factory.register_function<FunctionUtcTimeStamp>();
factory.register_function<FunctionUtcDate>();
factory.register_function<FunctionUtcTime>();
factory.register_function<FunctionTimeToSec>();
factory.register_function<FunctionSecToTime>();
factory.register_function<FunctionMicroSecToDateTime>();
Expand Down
48 changes: 40 additions & 8 deletions be/src/vec/functions/function_date_or_datetime_computation.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,18 +971,50 @@ struct TimestampToDateTime : IFunction {
}
};

struct UtcTimestampImpl {
static constexpr PrimitiveType ReturnType = TYPE_DATETIME;
static constexpr auto name = "utc_timestamp";
template <PrimitiveType UTCType>
struct UtcImpl {
static constexpr PrimitiveType ReturnType = UTCType;

static constexpr const char* get_function_name() {
if constexpr (ReturnType == TYPE_DATETIMEV2 || ReturnType == TYPE_DATETIME) {
return "utc_timestamp";
} else if constexpr (ReturnType == TYPE_DATEV2 || ReturnType == TYPE_DATE) {
return "utc_date";
} else if constexpr (ReturnType == TYPE_TIMEV2) {
return "utc_time";
}
}

static constexpr auto name = get_function_name();

static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) {
auto col_to = ColumnDateTimeV2::create();
int scale = 0;
if (arguments.size() == 1) {
// the precision must be const, which is checked in fe.
const auto* col = assert_cast<const ColumnInt32*>(
block.get_by_position(arguments[0]).column.get());
scale = col->get_element(0);
}
auto col_to = PrimitiveTypeTraits<ReturnType>::ColumnType::create();
DateV2Value<DateTimeV2ValueType> dtv;
if (dtv.from_unixtime(context->state()->timestamp_ms() / 1000, "+00:00")) {
auto date_packed_int = binary_cast<DateV2Value<DateTimeV2ValueType>, UInt64>(dtv);
col_to->insert_data(reinterpret_cast<char*>(&date_packed_int), 0);
if (dtv.from_unixtime(context->state()->timestamp_ms() / 1000,
context->state()->nano_seconds(), "+00:00", scale)) {
if constexpr (ReturnType == TYPE_DATETIMEV2) {
auto date_packed_int = binary_cast<DateV2Value<DateTimeV2ValueType>, UInt64>(dtv);
col_to->insert_data(reinterpret_cast<char*>(&date_packed_int), 0);
} else if constexpr (ReturnType == TYPE_DATEV2) {
DateV2Value<DateV2ValueType> dv;
dv.assign_from(dtv);
auto date_packed_int = binary_cast<DateV2Value<DateV2ValueType>, UInt32>(dv);
col_to->insert_data(reinterpret_cast<char*>(&date_packed_int), 0);
} else if constexpr (ReturnType == TYPE_TIMEV2) {
double time = TimeValue::make_time(dtv.hour(), dtv.minute(), dtv.second(),
dtv.microsecond());
col_to->insert_data(reinterpret_cast<char*>(&time), 0);
}
} else {
uint64_t invalid_val = 0;
typename PrimitiveTypeTraits<ReturnType>::ColumnItemType invalid_val = 0;
col_to->insert_data(reinterpret_cast<char*>(&invalid_val), 0);
}
block.get_by_position(result).column =
Expand Down
1 change: 1 addition & 0 deletions be/test/testutil/function_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ FunctionUtils::FunctionUtils(const vectorized::DataTypePtr& return_type,
globals.__set_now_string("2019-08-06 01:38:57");
globals.__set_timestamp_ms(1565026737805);
globals.__set_time_zone("Asia/Shanghai");
globals.__set_nano_seconds(805000000);

_state = std::make_unique<MockRuntimeState>(globals);
_fn_ctx = FunctionContext::create_context(_state.get(), return_type, arg_types);
Expand Down
70 changes: 70 additions & 0 deletions be/test/vec/function/function_time_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "vec/data_types/data_type_number.h"
#include "vec/data_types/data_type_string.h"
#include "vec/data_types/data_type_time.h"
#include "vec/functions/function_date_or_datetime_computation.h"
#include "vec/runtime/time_value.h"
#include "vec/runtime/vdatetime_value.h"

Expand Down Expand Up @@ -1641,4 +1642,73 @@ TEST(VTimestampFunctionsTest, curtime_test) {
}
}

// Test UTC functions
// FunctionUtils sets fixed time: 2019-08-06 01:38:57.805000 Asia/Shanghai (UTC+8)
// Corresponding UTC time: 2019-08-05 17:38:57.805
TEST(VTimestampFunctionsTest, utc_timestamp_test) {
std::string func_name = "utc_timestamp";
TimezoneUtils::load_timezones_to_cache();

{
InputTypeSet input_types = {};
DataSet data_set = {
{{}, std::string("2019-08-05 17:38:57")},
};
static_cast<void>(check_function<DataTypeDateTimeV2>(func_name, input_types, data_set));
}

{
InputTypeSet input_types = {PrimitiveType::TYPE_INT};
DataSet data_set = {
{{int32_t(3)}, std::string("2019-08-05 17:38:57.805")},
};
static_cast<void>(
check_function<DataTypeDateTimeV2, true>(func_name, input_types, data_set, 3));
}
}

TEST(VTimestampFunctionsTest, utc_date_test) {
std::string func_name = "utc_date";
TimezoneUtils::load_timezones_to_cache();

{
InputTypeSet input_types = {};
DataSet data_set = {
{{}, std::string("2019-08-05")},
};
static_cast<void>(check_function<DataTypeDateV2>(func_name, input_types, data_set));
}
}

TEST(VTimestampFunctionsTest, utc_time_test) {
std::string func_name = "utc_time";
TimezoneUtils::load_timezones_to_cache();

{
InputTypeSet input_types = {};
DataSet data_set = {
{{}, std::string("17:38:57")},
};
static_cast<void>(check_function<DataTypeTimeV2>(func_name, input_types, data_set));
}

{
InputTypeSet input_types = {PrimitiveType::TYPE_INT};
DataSet data_set = {
{{int32_t(3)}, std::string("17:38:57.805")},
};
static_cast<void>(
check_function<DataTypeTimeV2, true>(func_name, input_types, data_set, 3));
}
}

TEST(VTimestampFunctionsTest, utc_impl_function_name_test) {
EXPECT_STREQ("utc_timestamp", UtcImpl<PrimitiveType::TYPE_DATETIMEV2>::get_function_name());
EXPECT_STREQ("utc_date", UtcImpl<PrimitiveType::TYPE_DATEV2>::get_function_name());
EXPECT_STREQ("utc_time", UtcImpl<PrimitiveType::TYPE_TIMEV2>::get_function_name());
EXPECT_STREQ("utc_timestamp", UtcImpl<PrimitiveType::TYPE_DATETIMEV2>::name);
EXPECT_STREQ("utc_date", UtcImpl<PrimitiveType::TYPE_DATEV2>::name);
EXPECT_STREQ("utc_time", UtcImpl<PrimitiveType::TYPE_TIMEV2>::name);
}

} // namespace doris::vectorized
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.LeafExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DateV2Type;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* ScalarFunction 'utc_date'. This class is generated by GenerateFunction.
*/
public class UtcDate extends ScalarFunction
implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateV2Type.INSTANCE).args()
);

/**
* constructor with 0 argument.
*/
public UtcDate() {
super("utc_date");
}

/** constructor for withChildren and reuse signature */
private UtcDate(ScalarFunctionParams functionParams) {
super(functionParams);
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public Expression withChildren(List<Expression> children) {
Preconditions.checkArgument(children.isEmpty());
return new UtcDate(getFunctionParams(children));
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitUtcDate(this, context);
}

@Override
public boolean isDeterministic() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.shape.LeafExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.TimeV2Type;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* ScalarFunction 'utc_time'. This class is generated by GenerateFunction.
*/
public class UtcTime extends ScalarFunction
implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(TimeV2Type.INSTANCE).args(),
FunctionSignature.ret(TimeV2Type.INSTANCE).args(IntegerType.INSTANCE)
);

/**
* constructor with 0 argument.
*/
public UtcTime() {
super("utc_time");
}

/**
* constructor with 1 argument.
*/
public UtcTime(Expression arg) {
super("utc_time", arg);
}

/** constructor for withChildren and reuse signature */
private UtcTime(ScalarFunctionParams functionParams) {
super(functionParams);
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public FunctionSignature computeSignature(FunctionSignature signature) {
signature = super.computeSignature(signature);
if (arity() == 1 && getArgument(0) instanceof IntegerLiteral) {
int scale = ((IntegerLiteral) getArgument(0)).getValue();
if (scale < 0 || scale > 6) {
throw new AnalysisException("scale must be between 0 and 6");
}
return signature.withReturnType(TimeV2Type.of(scale));
}

return signature;
}

@Override
public void checkLegalityAfterRewrite() {
if (arity() == 1 && !child(0).isLiteral()) {
throw new AnalysisException("UTC_TIME scale argument must be a constant literal.");
}
}

@Override
public void checkLegalityBeforeTypeCoercion() {
checkLegalityAfterRewrite();
}

@Override
public Expression withChildren(List<Expression> children) {
Preconditions.checkArgument(children.isEmpty() || arity() == 1);
return new UtcTime(getFunctionParams(children));
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitUtcTime(this, context);
}

@Override
public boolean isDeterministic() {
return false;
}
}
Loading
Loading