Skip to content

HIVE-30201: UDFArgumentLengthException error while compiling the statement - #6895

Open
mdayakar wants to merge 5 commits into
apache:masterfrom
mdayakar:HIVE-30201_Operands_Deduplication
Open

mdayakar wants to merge 5 commits into
apache:masterfrom
mdayakar:HIVE-30201_Operands_Deduplication

Conversation

@mdayakar

Copy link
Copy Markdown
Contributor

HIVE-30201: UDFArgumentLengthException error while compiling the statement

What changes were proposed in this pull request?

As a part of HIVE-28911 some improvements done for SEARCH operator which involves the code changes in ExprNodeConverter.visitCall(), AND/OR calls get converted with ExprNodeDescUtils.and(args) / ExprNodeDescUtils.or(args) (to flatten nested ANDs/ORs produced by SearchTransformer). Those helpers internally use ExprNodeDescUtils.split(...), which also removes duplicate operands (indexOf(current, splitted) < 0).
So for the current query(mentioned in the JIRA issue), AND(=(1, 1), =(1, 1)) expression is flattened to AND(=(1,1)) but AND/OR require at least two operands (GenericUDFOPAnd/Or#initialize enforces this) so throwing org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException.
As a part of fix, without changing flattening logic, when the flattened AND/OR ends up with one operand, return that operand
directly (AND(x) == x, OR(x) == x).

Why are the changes needed?

To fix HIVE-28911 issue.

Does this PR introduce any user-facing change?

No

How was this patch tested?

Unit and q file tests are added.

@zabetak zabetak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My first attempt would be to try to fix the AND(=(1, 1), =(1, 1)) at the very source which occurs. Having simpler expressions to begin with is generally desired. Please check where the expression is created in the first place and if there is a meaningful way to fix it there. If that becomes too intrusive or complicated then we can leave it as a follow-up.

Tentatively approving this assuming that the minor suggestions are incorporated in the next commit.

return ExprNodeDescUtils.or(args);
// Flattening/deduplication may collapse the operand list;
// OR require at least two operands (GenericUDFOPOr#initialize enforces this), so unwrapping trivial cases.
return unwrapSingleOperand(ExprNodeDescUtils.or(args));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Its better to put the unwrap logic inside ExprNodeDescUtils.or and ExprNodeDescUtils.and to avoid having the same problem appearing in a different place where these methods are called. Moreover this new method is more of a utility rather than something belonging to ExprNodeConverter so the utils is a more appropriate place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here ExprNodeDescUtils.or and ExprNodeDescUtils.and are returning ExprNodeGenericFuncDesc so here we need to change the return type of these functions to ExprNodeDesc which results in more changes so if required we can do it in another PR. Also current unwrapSingleOperand API specific to AND/OR functions so moving it to ExprNodeDescUtils requires more check so currently not moving it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mdayakar I have attached in the Jira a patch that introduces the fix inside ExprNodeDescUtils.or and ExprNodeDescUtils.and, changing the return type and propagating the necessary adjustments in cascade to all relevant files (just code change, no test included); in case we want to consider this solution.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The patch attached by @rubenada makes sense and given that we have a bit more time for landing this I would say that it's worth going into this direction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Once we adopt the patch that makes the fix inside ExprNodeDescUtils all the comments about unrapping should become part of the Javadoc of the respective methods.

@zabetak zabetak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's incorporate the patch provided by @rubenada and address the few nits and then merge the fix.

return ExprNodeDescUtils.or(args);
// Flattening/deduplication may collapse the operand list;
// OR require at least two operands (GenericUDFOPOr#initialize enforces this), so unwrapping trivial cases.
return unwrapSingleOperand(ExprNodeDescUtils.or(args));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Once we adopt the patch that makes the fix inside ExprNodeDescUtils all the comments about unrapping should become part of the Javadoc of the respective methods.

}

@Test
public void testAndWithDuplicateColumnOperandsIsUnwrapped() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The name is bit misleading. The operands are duplicate but they are not columns; they are equality expressions. Compared to the previous test case just above not sure if it increases coverage.

Comment on lines +72 to +74
assertEquals(1, reduced.size());
assertTrue(reduced.get(0) instanceof RexLiteral);
assertEquals(Boolean.TRUE, ((RexLiteral) reduced.get(0)).getValueAs(Boolean.class));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Again assertion on the string representation of the result would be easier to read.

assertSpecifiedOperatorWithDuplicateConstantOperands(SqlStdOperatorTable.OR);
}

private void assertSpecifiedOperatorWithDuplicateConstantOperands(SqlBinaryOperator operator) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This very specific assert method is unlikely to be used by anyone else writing tests in this class in the future. Generalizing it a bit and taking the expression under test outside of the method would increase the likelihood of others taking advantage of it.

Suggested change
private void assertSpecifiedOperatorWithDuplicateConstantOperands(SqlBinaryOperator operator) {
private void assertReduce(String expected, RexNode node) {

@mdayakar

Copy link
Copy Markdown
Contributor Author

Let's incorporate the patch provided by @rubenada and address the few nits and then merge the fix.

Done the changes accordingly.

@rubenada

Copy link
Copy Markdown
Contributor

@mdayakar there are several "Unused import" warnings detected by Sonar which should be handled before the merge, sorry if the came from my patch

@mdayakar
mdayakar force-pushed the HIVE-30201_Operands_Deduplication branch from 276c296 to 2bd1fde Compare November 24, 2026 04:51
@rubenada

Copy link
Copy Markdown
Contributor

LGTM
Minor remark: the PR description still mentions ExprNodeConverter, which I think is no longer modified with the latest approach.

@mdayakar
mdayakar force-pushed the HIVE-30201_Operands_Deduplication branch 2 times, most recently from 1e5a2d1 to a7cbb44 Compare November 24, 2026 08:25
@mdayakar
mdayakar force-pushed the HIVE-30201_Operands_Deduplication branch from a7cbb44 to c0e2108 Compare November 24, 2026 08:54
@mdayakar

Copy link
Copy Markdown
Contributor Author

LGTM Minor remark: the PR description still mentions ExprNodeConverter, which I think is no longer modified with the latest approach.

@rubenada ExprNodeConverter is mentioned in the root cause section not in the fixed issue details.

@sonarqubecloud

Copy link
Copy Markdown

@zabetak zabetak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I understand why instanceof and casting was introduced but the new code also appears a bit shaky and possibly more risky than the original solution. I raised #6989 to test the impact of changing Kryo serde which would help to get rid of the extra checks and casts.

However, if we want to advance the resolution of this bug that is independent of Kryo and the general refactoring I am wondering if we can replace:

  • ExprNodeDescUtils#and with ExprNodeDescUtils#conjunction
  • ExprNodeDescUtils#or with ExprNodeDescUtils#disjunction
    The latter seem to handle expression flattening and also the case where input is one operand. They don't remove duplicate expressions but this may not be necessary at this point.

Comment on lines +964 to +967
ExprNodeDesc combinedExpr = ExprNodeDescUtils.and(filterExpr, pruningExpr);
if (combinedExpr instanceof ExprNodeGenericFuncDesc combinedFuncExpr) {
filterExpr = combinedFuncExpr;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the instanceof fails then we are going to ignore the pruningExpr. Not sure if its possible now but it may silently fail if something changes in the future.

Comment on lines +156 to +157
sjTargetTable.getConf()
.setFilterExpr((ExprNodeGenericFuncDesc) and(sjTargetTable.getConf().getFilterExpr(), sjPredicate));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Requiring casts increases the risk of ClassCastException so the new code does not seem better.

Comment on lines +6 to +7
explain cbo select t1.id, t2.name, (case when (t1.id = 1 or t2.id = 1) then 'One' else null end) mycase
from mytable t1 join mytable t2 on t1.name = t2.name where t1.id = 1 and t2.id = 1; No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Explain query is duplicated

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants