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
2 changes: 2 additions & 0 deletions be/src/common/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "exprs/timestamp_functions.h"
#include "exprs/decimal_operators.h"
#include "exprs/decimalv2_operators.h"
#include "exprs/time_operators.h"
#include "exprs/utility_functions.h"
#include "exprs/json_functions.h"
#include "exprs/hll_hash_function.h"
Expand Down Expand Up @@ -260,6 +261,7 @@ void init_daemon(int argc, char** argv, const std::vector<StorePath>& paths) {
TimestampFunctions::init();
DecimalOperators::init();
DecimalV2Operators::init();
TimeOperators::init();
UtilityFunctions::init();
CompoundPredicate::init();
JsonFunctions::init();
Expand Down
1 change: 1 addition & 0 deletions be/src/exprs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ add_library(Exprs
conditional_functions_ir.cpp
decimal_operators.cpp
decimalv2_operators.cpp
time_operators.cpp
es_functions.cpp
literal.cpp
expr.cpp
Expand Down
2 changes: 2 additions & 0 deletions be/src/exprs/anyval_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ AnyVal* create_any_val(ObjectPool* pool, const TypeDescriptor& type) {
case TYPE_FLOAT:
return pool->add(new FloatVal);

case TYPE_TIME:
case TYPE_DOUBLE:
return pool->add(new DoubleVal);

Expand Down Expand Up @@ -125,6 +126,7 @@ FunctionContext::TypeDesc AnyValUtil::column_type_to_type_desc(const TypeDescrip
case TYPE_FLOAT:
out.type = FunctionContext::TYPE_FLOAT;
break;
case TYPE_TIME:
case TYPE_DOUBLE:
out.type = FunctionContext::TYPE_DOUBLE;
break;
Expand Down
1 change: 1 addition & 0 deletions be/src/exprs/anyval_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class AnyValUtil {
reinterpret_cast<doris_udf::FloatVal*>(dst)->val =
*reinterpret_cast<const float*>(slot);
return;
case TYPE_TIME:
case TYPE_DOUBLE:
reinterpret_cast<doris_udf::DoubleVal*>(dst)->val =
*reinterpret_cast<const double*>(slot);
Expand Down
5 changes: 4 additions & 1 deletion be/src/exprs/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Expr::Expr(const TypeDescriptor& type) :

case TYPE_FLOAT:
case TYPE_DOUBLE:
case TYPE_TIME:
_node_type = (TExprNodeType::FLOAT_LITERAL);
break;

Expand Down Expand Up @@ -195,6 +196,7 @@ Expr::Expr(const TypeDescriptor& type, bool is_slotref) :

case TYPE_FLOAT:
case TYPE_DOUBLE:
case TYPE_TIME:
_node_type = (TExprNodeType::FLOAT_LITERAL);
break;

Expand Down Expand Up @@ -743,7 +745,8 @@ doris_udf::AnyVal* Expr::get_const_val(ExprContext* context) {
_constant_val.reset(new FloatVal(get_float_val(context, NULL)));
break;
}
case TYPE_DOUBLE: {
case TYPE_DOUBLE:
case TYPE_TIME: {
_constant_val.reset(new DoubleVal(get_double_val(context, NULL)));
break;
}
Expand Down
1 change: 1 addition & 0 deletions be/src/exprs/expr_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ void* ExprContext::get_value(Expr* e, TupleRow* row) {
_result.float_val = v.val;
return &_result.float_val;
}
case TYPE_TIME:
case TYPE_DOUBLE: {
doris_udf::DoubleVal v = e->get_double_val(this, row);
if (v.is_null) {
Expand Down
1 change: 1 addition & 0 deletions be/src/exprs/literal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Literal::Literal(const TExprNode& node) :
_value.float_val = node.float_literal.value;
break;
case TYPE_DOUBLE:
case TYPE_TIME:
DCHECK_EQ(node.node_type, TExprNodeType::FLOAT_LITERAL);
DCHECK(node.__isset.float_literal);
_value.double_val = node.float_literal.value;
Expand Down
3 changes: 1 addition & 2 deletions be/src/exprs/scalar_fn_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ FloatVal ScalarFnCall::get_float_val(ExprContext* context, TupleRow* row) {
}

DoubleVal ScalarFnCall::get_double_val(ExprContext* context, TupleRow* row) {
DCHECK_EQ(_type.type, TYPE_DOUBLE);
DCHECK(_type.type == TYPE_DOUBLE || _type.type == TYPE_TIME);
DCHECK(context != NULL);
if (_scalar_fn_wrapper == NULL) {
return interpret_eval<DoubleVal>(context, row);
Expand Down Expand Up @@ -871,7 +871,6 @@ DecimalV2Val ScalarFnCall::get_decimalv2_val(ExprContext* context, TupleRow* row
return fn(context, row);
}


std::string ScalarFnCall::debug_string() const {
std::stringstream out;
out << "ScalarFnCall(udf_type=" << _fn.binary_type
Expand Down
75 changes: 75 additions & 0 deletions be/src/exprs/time_operators.cpp
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.

#include "exprs/time_operators.h"

#include <iomanip>
#include <sstream>
#include <math.h>

#include "exprs/anyval_util.h"
#include "exprs/case_expr.h"
#include "exprs/expr.h"
#include "runtime/tuple_row.h"
#include "util/string_parser.hpp"
#include "util/date_func.h"

namespace doris {
void TimeOperators::init() {
}

#define CAST_TIME_TO_INT(to_type, type_name) \
to_type TimeOperators::cast_to_##type_name( \
FunctionContext* context, const DoubleVal& val) { \
if (val.is_null) return to_type::null(); \
int time = (int) val.val ; \
int second = time % 60; \
int minute = time / 60 % 60; \
int hour = time / 3600; \
return to_type(hour * 10000 + minute * 100 + second); \
}

#define CAST_FROM_TIME() \
CAST_TIME_TO_INT(BooleanVal, boolean_val);\
CAST_TIME_TO_INT(TinyIntVal, tiny_int_val);\
CAST_TIME_TO_INT(SmallIntVal, small_int_val);\
CAST_TIME_TO_INT(IntVal, int_val);\
CAST_TIME_TO_INT(BigIntVal, big_int_val);\
CAST_TIME_TO_INT(LargeIntVal, large_int_val);\
CAST_TIME_TO_INT(FloatVal, float_val);\
CAST_TIME_TO_INT(DoubleVal, double_val);

CAST_FROM_TIME();

StringVal TimeOperators::cast_to_string_val(
FunctionContext* ctx, const DoubleVal& val) {
if (val.is_null) {
return StringVal::null();
}
return AnyValUtil::from_string_temp(ctx, time_str_from_int(val.val));
}

DateTimeVal TimeOperators::cast_to_datetime_val(
FunctionContext* context, const DoubleVal& val) {
return DateTimeVal::null();
}

DecimalVal TimeOperators::cast_to_decimal_val(
FunctionContext* context, const DoubleVal& val) {
return DecimalVal::null();
}
}
48 changes: 48 additions & 0 deletions be/src/exprs/time_operators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.

#ifndef DORIS_BE_SRC_EXPRS_TIME_OPERATORS_H
#define DORIS_BE_SRC_EXPRS_TIME_OPERATORS_H

#include <stdint.h>
#include "udf/udf.h"

namespace doris {
class Expr;
struct ExprValue;
class TupleRow;

/// Implementation of the time operators. These include the cast,
/// arithmetic and binary operators.
class TimeOperators {
public:
static void init();

static BooleanVal cast_to_boolean_val(FunctionContext*, const DoubleVal&);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can only support time to double and to string

static TinyIntVal cast_to_tiny_int_val(FunctionContext*, const DoubleVal&);
static SmallIntVal cast_to_small_int_val(FunctionContext*, const DoubleVal&);
static IntVal cast_to_int_val(FunctionContext*, const DoubleVal&);
static BigIntVal cast_to_big_int_val(FunctionContext*, const DoubleVal&);
static LargeIntVal cast_to_large_int_val(FunctionContext*, const DoubleVal&);
static FloatVal cast_to_float_val(FunctionContext*, const DoubleVal&);
static DoubleVal cast_to_double_val(FunctionContext*, const DoubleVal&);
static StringVal cast_to_string_val(FunctionContext*, const DoubleVal&);
static DateTimeVal cast_to_datetime_val(FunctionContext*, const DoubleVal&);
static DecimalVal cast_to_decimal_val(FunctionContext*, const DoubleVal&);
};
}
#endif
12 changes: 5 additions & 7 deletions be/src/exprs/timestamp_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,18 +463,16 @@ IntVal TimestampFunctions::to_days(
}

// TODO(dhc): implement this funciton really
DateTimeVal TimestampFunctions::time_diff(
DoubleVal TimestampFunctions::time_diff(
FunctionContext* ctx, const DateTimeVal& ts_val1, const DateTimeVal& ts_val2) {
if (ts_val1.is_null || ts_val2.is_null) {
return DateTimeVal::null();
return DoubleVal::null();
}
const DateTimeValue& ts_value1 = DateTimeValue::from_datetime_val(ts_val1);
const DateTimeValue& ts_value2 = DateTimeValue::from_datetime_val(ts_val2);
DateTimeValue ts = ts_value1 - ts_value2;
ts.cast_to_time();
DateTimeVal result;
ts.to_datetime_val(&result);
return result;
int64_t timediff = ts_value1.unix_timestamp() - ts_value2.unix_timestamp();

return DoubleVal(timediff);
}

IntVal TimestampFunctions::date_diff(
Expand Down
2 changes: 1 addition & 1 deletion be/src/exprs/timestamp_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class TimestampFunctions {
static doris_udf::IntVal date_diff(
doris_udf::FunctionContext* ctx, const doris_udf::DateTimeVal& ts_val1,
const doris_udf::DateTimeVal& ts_val2);
static doris_udf::DateTimeVal time_diff(
static doris_udf::DoubleVal time_diff(
doris_udf::FunctionContext* ctx, const doris_udf::DateTimeVal& ts_val1,
const doris_udf::DateTimeVal& ts_val2);

Expand Down
9 changes: 9 additions & 0 deletions be/src/runtime/primitive_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ PrimitiveType thrift_to_type(TPrimitiveType::type ttype) {
case TPrimitiveType::DATETIME:
return TYPE_DATETIME;

case TPrimitiveType::TIME:
return TYPE_TIME;

case TPrimitiveType::VARCHAR:
return TYPE_VARCHAR;

Expand Down Expand Up @@ -129,6 +132,9 @@ TPrimitiveType::type to_thrift(PrimitiveType ptype) {
case TYPE_DATETIME:
return TPrimitiveType::DATETIME;

case TYPE_TIME:
return TPrimitiveType::TIME;

case TYPE_VARCHAR:
return TPrimitiveType::VARCHAR;

Expand Down Expand Up @@ -190,6 +196,9 @@ std::string type_to_string(PrimitiveType t) {
case TYPE_DATETIME:
return "DATETIME";

case TYPE_TIME:
return "TIME";

case TYPE_VARCHAR:
return "VARCHAR";

Expand Down
6 changes: 5 additions & 1 deletion be/src/runtime/primitive_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ enum PrimitiveType {
TYPE_ARRAY, /* 17 */
TYPE_MAP, /* 18 */
TYPE_HLL, /* 19 */
TYPE_DECIMALV2 /* 20 */
TYPE_DECIMALV2, /* 20 */

TYPE_TIME, /* 21 */
};

inline bool is_enumeration_type(PrimitiveType type) {
Expand Down Expand Up @@ -114,6 +116,7 @@ inline int get_byte_size(PrimitiveType type) {
return 4;

case TYPE_BIGINT:
case TYPE_TIME:
case TYPE_DOUBLE:
return 8;

Expand Down Expand Up @@ -153,6 +156,7 @@ inline int get_real_byte_size(PrimitiveType type) {
return 4;

case TYPE_BIGINT:
case TYPE_TIME:
case TYPE_DOUBLE:
return 8;

Expand Down
1 change: 1 addition & 0 deletions be/src/runtime/raw_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ void RawValue::write(const void* value, void* dst, const TypeDescriptor& type, M
break;
}

case TYPE_TIME:
case TYPE_DOUBLE: {
*reinterpret_cast<double*>(dst) = *reinterpret_cast<const double*>(value);
break;
Expand Down
8 changes: 8 additions & 0 deletions be/src/runtime/result_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "runtime/buffer_control_block.h"
#include "util/mysql_row_buffer.h"
#include "util/types.h"
#include "util/date_func.h"

#include "gen_cpp/PaloInternalService_types.h"

Expand Down Expand Up @@ -104,6 +105,13 @@ Status ResultWriter::add_one_row(TupleRow* row) {
buf_ret = _row_buffer->push_double(*static_cast<double*>(item));
break;

case TYPE_TIME: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can write a function to do this to avoid write this many times

double time = *static_cast<double *>(item);
std::string time_str = time_str_from_int((int) time);
buf_ret = _row_buffer->push_string(time_str.c_str(), time_str.size());
break;
}

case TYPE_DATE:
case TYPE_DATETIME: {
char buf[64];
Expand Down
1 change: 1 addition & 0 deletions be/src/runtime/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ struct TypeDescriptor {

case TYPE_BIGINT:
case TYPE_DOUBLE:
case TYPE_TIME:
return 8;

case TYPE_LARGEINT:
Expand Down
16 changes: 16 additions & 0 deletions be/src/util/date_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ static uint24_t timestamp_from_date(const std::string& date_str) {
return uint24_t(value);
}

static std::string time_str_from_int(int time) {
std::stringstream time_ss;
if (time < 0) {
time_ss << "-";
time = -time;
}
int hour = time / 60 / 60;
int minute = time / 60 % 60;
int second = time % 60;

time_ss << std::setw(2) << std::setfill('0') << hour
<< ":" << std::setw(2) << std::setfill('0') << minute
<< ":" << std::setw(2) << std::setfill('0') << second;
return time_ss.str();
}

} // namespace doris

#endif // DORIS_BE_SRC_UTIL_DATE_FUNC_H
1 change: 1 addition & 0 deletions be/src/util/symbols_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ static void append_any_val_type(
case TYPE_FLOAT:
append_mangled_token("FloatVal", s);
break;
case TYPE_TIME:
case TYPE_DOUBLE:
append_mangled_token("DoubleVal", s);
break;
Expand Down
Loading