mrhhsg commented on code in PR #68579:
URL: https://github.com/apache/doris/pull/68579#discussion_r4218813161


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Sha2.java:
##########
@@ -63,15 +65,32 @@ private Sha2(ScalarFunctionParams functionParams) {
 
     @Override
     public void checkLegalityBeforeTypeCoercion() {
-        checkLegalityAfterRewrite();
+        // validate the value FE can evaluate here, because constant folding, 
e.g. of sha2(null, 1 + 2), may remove
+        // this function before checkLegalityAfterRewrite
+        
checkDigestLength(ExpressionUtils.foldConstantArgument(getArgument(1)));
     }
 
     @Override
     public void checkLegalityAfterRewrite() {
-        if (!(child(1) instanceof IntegerLikeLiteral)) {
-            throw new AnalysisException("the second parameter of sha2 must be 
a literal but got: " + child(1).toSql());
+        checkDigestLength(getArgument(1));
+    }
+
+    private void checkDigestLength(Expression digestLength) {
+        if (!digestLength.isConstant()) {
+            throw new AnalysisException("the second parameter of sha2 must be 
a constant but got: "
+                    + digestLength.toSql());
+        }
+        // the type is checked before the type coercion casts the argument to 
the INT signature, so a decimal
+        // constant is rejected like a decimal literal
+        if (!digestLength.getDataType().isIntegralType()) {
+            throw new AnalysisException("the second parameter of sha2 must be 
an integer but got: "
+                    + digestLength.toSql());
+        }
+        // the value of a constant FE cannot fold is validated by BE when it 
is evaluated
+        if (!(digestLength instanceof Literal)) {
+            return;
         }
-        final int constParam = ((IntegerLikeLiteral) child(1)).getIntValue();
+        final int constParam = ((IntegerLikeLiteral) 
digestLength).getIntValue();

Review Comment:
   Fixed in 8963623f870: FE now rejects a typed-NULL digest length with 
AnalysisException before the IntegerLikeLiteral cast. The FE unit test and SQL 
regression cover this input.



##########
be/src/exprs/function/function_other_types_to_date.cpp:
##########
@@ -481,41 +481,46 @@ struct DateTrunc {
         if (scope != FunctionContext::THREAD_LOCAL) {
             return Status::OK();
         }
+        // The time unit is a constant, but a constant expression such as an 
arithmetic one is not
+        // evaluated in open. Then the state is created from the first row in 
execute.
         if (!context->is_col_constant(DateArgIsFirst ? 1 : 0)) {
-            return Status::InvalidArgument(
-                    "date_trunc function of time unit argument must be 
constant.");
+            return Status::OK();
         }
-        const auto& data_str =
-                context->get_constant_col(DateArgIsFirst ? 1 : 
0)->column_ptr->get_data_at(0);
+        return create_state(
+                context,
+                context->get_constant_col(DateArgIsFirst ? 1 : 
0)->column_ptr->get_data_at(0));

Review Comment:
   Fixed in 8963623f870: date_trunc skips state creation in open for a NULL 
constant unit. BE unit tests cover both argument orders; the multirow INSERT 
regression now stores NULL for both.



##########
be/src/exprs/function/function_string_digest.cpp:
##########
@@ -226,9 +226,10 @@ class FunctionStringDigestSHA2 : public IFunction {
 
     Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
                         uint32_t result, size_t input_rows_count) const 
override {
-        DCHECK(!is_column_const(*block.get_by_position(arguments[0]).column));
-
-        ColumnPtr data_col = block.get_by_position(arguments[0]).column;
+        // The input is a constant too when the digest length is a constant BE 
evaluates to a full
+        // column, such as uniform(...).
+        ColumnPtr data_col =
+                
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();

Review Comment:
   Fixed in 8963623f870: SHA2 keeps a constant source compact and hashes row 0 
instead of materializing it for every row. Mixed constant-source/full-length BE 
unit and SQL regression cases pass.



-- 
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.

To unsubscribe, e-mail: [email protected]

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