morningman commented on a change in pull request #3947:
URL: https://github.com/apache/incubator-doris/pull/3947#discussion_r446194550



##########
File path: be/src/exprs/timestamp_functions.cpp
##########
@@ -451,20 +451,79 @@ BigIntVal 
TimestampFunctions::timestamp_diff(FunctionContext* ctx, const DateTim
     }
 }
 
+void TimestampFunctions::format_prepare(
+        doris_udf::FunctionContext* context,
+        doris_udf::FunctionContext::FunctionStateScope scope) {
+
+    if (scope != FunctionContext::FRAGMENT_LOCAL
+            || context->get_num_args() < 2
+            || context->get_arg_type(1)->type != 
doris_udf::FunctionContext::Type::TYPE_VARCHAR
+            || !context->is_arg_constant(1)) {
+        VLOG(10) << "format_prepare returned";
+        return;
+    }
+
+    FormatCtx* fc = new FormatCtx();
+    context->set_function_state(scope, fc);
+
+    StringVal* format = 
reinterpret_cast<StringVal*>(context->get_constant_arg(1));
+    if (UNLIKELY(format->is_null)) {
+        fc->is_valid = false;
+        return;
+    }
+
+    fc->fmt = convert_format(context, *format);
+    int format_len = DateTimeValue::compute_format_len((const char*) 
fc->fmt.ptr, fc->fmt.len);
+    if (UNLIKELY(format_len >= 128)) {
+        fc->is_valid = false;
+        return;
+    }
+
+    fc->is_valid = true;
+    return;
+}
+
+void TimestampFunctions::format_close(
+        doris_udf::FunctionContext* context,
+        doris_udf::FunctionContext::FunctionStateScope scope) {
+    if (scope != FunctionContext::FRAGMENT_LOCAL) {
+        return;
+    }
+
+    FormatCtx* fc = 
reinterpret_cast<FormatCtx*>(context->get_function_state(FunctionContext::FRAGMENT_LOCAL));
+    if (fc != nullptr) {
+        delete fc;
+    }
+}
+
 StringVal TimestampFunctions::date_format(
         FunctionContext* ctx, const DateTimeVal& ts_val, const StringVal& 
format) {
     if (ts_val.is_null || format.is_null) {
         return StringVal::null();
     }
+
     DateTimeValue ts_value = DateTimeValue::from_datetime_val(ts_val);
-    if (ts_value.compute_format_len((const char*)format.ptr, format.len) >= 
128) {
-        return StringVal::null();
+    FormatCtx* fc = 
reinterpret_cast<FormatCtx*>(ctx->get_function_state(FunctionContext::FRAGMENT_LOCAL));
+    if (UNLIKELY(fc == nullptr)) {
+        // prepare phase failed, calculate at runtime
+        StringVal new_fmt = convert_format(ctx, format);
+        if (DateTimeValue::compute_format_len((const char*) new_fmt.ptr, 
new_fmt.len) >= 128) {

Review comment:
       Doesn't matter, the old code's order is incorrect, but it doesn't 
matter, because `compute_format_len` will return < 128 in most cases.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to