Skip to content

Commit

Permalink
[FLINK-26474][hive] fix the issue of failing to call some hive udf re…
Browse files Browse the repository at this point in the history
…quired constant parameters with implicit constant passed.
  • Loading branch information
luoyuxia committed Mar 4, 2022
1 parent 6fc2933 commit ab66505
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,8 @@ private RelNode genSelectLogicalPlan(
} else {
// Case when this is an expression
HiveParserTypeCheckCtx typeCheckCtx =
new HiveParserTypeCheckCtx(inputRR, frameworkConfig, cluster);
new HiveParserTypeCheckCtx(
inputRR, true, true, frameworkConfig, cluster);
// We allow stateful functions in the SELECT list (but nowhere else)
typeCheckCtx.setAllowStatefulFunctions(true);
if (!qbp.getDestToGroupBy().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFNvl;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPAnd;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPEqual;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPNegative;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPNot;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPOr;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFWhen;
Expand Down Expand Up @@ -1231,7 +1232,6 @@ protected ExprNodeDesc getXpathOrFuncExprNodeDesc(
// If the function is deterministic and the children are constants,
// we try to fold the expression to remove e.g. cast on constant
if (ctx.isFoldExpr()
&& desc instanceof ExprNodeGenericFuncDesc
&& FunctionRegistry.isDeterministic(genericUDF)
&& HiveParserExprNodeDescUtils.isAllConstants(children)) {
ExprNodeDesc constantExpr =
Expand All @@ -1248,11 +1248,36 @@ protected ExprNodeDesc getXpathOrFuncExprNodeDesc(
if (FunctionRegistry.isOpPositive(desc)) {
assert (desc.getChildren().size() == 1);
desc = desc.getChildren().get(0);
} else if (getGenericUDFClassFromExprDesc(desc) == GenericUDFOPNegative.class) {
// UDFOPNegative should always be folded.
assert (desc.getChildren().size() == 1);
ExprNodeDesc input = desc.getChildren().get(0);
if (input instanceof ExprNodeConstantDesc) {
ExprNodeDesc constantExpr = foldExpr(desc);
if (constantExpr != null) {
desc = constantExpr;
}
}
}
assert (desc != null);
return desc;
}

private Class<? extends GenericUDF> getGenericUDFClassFromExprDesc(ExprNodeDesc desc) {
if (!(desc instanceof ExprNodeGenericFuncDesc)) {
return null;
}
ExprNodeGenericFuncDesc genericFuncDesc = (ExprNodeGenericFuncDesc) desc;
return genericFuncDesc.getGenericUDF().getClass();
}

private ExprNodeDesc foldExpr(ExprNodeDesc expr) {
if (expr instanceof ExprNodeGenericFuncDesc) {
return ConstantPropagateProcFactory.foldExpr((ExprNodeGenericFuncDesc) expr);
}
return expr;
}

// try to create an ExprNodeDesc with a SqlOperator
private ExprNodeDesc convertSqlOperator(
String funcText, List<ExprNodeDesc> children, HiveParserTypeCheckCtx ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- SORT_QUERY_RESULTS

select bround(55.0, -1);

[+I[60]]

select round(123.45, -2);

[+I[100]]

select sha2('ABC', cast(null as int));

[+I[null]]

0 comments on commit ab66505

Please sign in to comment.