Skip to content

Conversation

@wecharyu
Copy link
Contributor

@wecharyu wecharyu commented Nov 12, 2025

What changes were proposed in this pull request?

  1. Convert adjacent AND LeafNodes to a MultiAndLeafNode
  2. Concatenate partition name filter in MultiAndLeafNode
  3. Refactor NonAcidBenchmarks: supports multi test cases in a benchmark.

Why are the changes needed?

Match longer index prefix in partition filter to improve performance.

For example, a table has partition key: a, b, c, d, and a filter is a=1 and b=1 and c=1, when visit as a binary tree AND(AND(a=1, b=1), c=1), the filter is:

PART_NAME like 'a=1/%' and PART_NAME like '%/b=2/%' and PART_NAME like '%/c=3/%'

If we combine the adjacent AND conditions, the tree will be MultiAnd(a=1, b=2, c=3), and the filte can be:

PART_NAME like 'a=1/b=2/c=3/%'

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Add unit tests and run benchmark test:

java -jar hmsbench.jar -H localhost --savedata /tmp/benchdata --sanitize -N 100 -N 10000 -N 1000000 -o bench_results.csv -C -d testbench_http --params=100 -M 'getPartitionsByFilter.*' --runMode NONACID
- Before this patch
Operation                      Mean     Med      Min      Max      Err%
getPartitionsByFilter#multiAnd.100 5.789    5.653    4.691    8.888    11.89
getPartitionsByFilter#multiAnd.10000 7.805    7.765    6.946    10.30    6.825
getPartitionsByFilter#multiAnd.1000000 276.2    274.1    271.8    360.5    3.426
getPartitionsByFilter#multiOr.100 6.443    6.399    5.289    8.888    9.664
getPartitionsByFilter#multiOr.10000 8.962    8.838    8.204    17.77    10.81
getPartitionsByFilter#multiOr.1000000 379.9    377.4    372.2    425.8    2.044
getPartitionsByFilter#simple.100 7.883    7.609    6.362    10.83    11.31
getPartitionsByFilter#simple.10000 7.163    7.093    6.624    8.400    4.653
getPartitionsByFilter#simple.1000000 242.0    236.2    233.1    287.4    4.468

- After this patch
Operation                      Mean     Med      Min      Max      Err%
getPartitionsByFilter#multiAnd.100 5.649    5.399    4.523    8.767    16.05
getPartitionsByFilter#multiAnd.10000 6.802    6.786    6.411    7.670    3.358
getPartitionsByFilter#multiAnd.1000000 235.6    233.1    228.7    303.8    3.833
getPartitionsByFilter#multiOr.100 6.176    6.051    5.373    8.610    9.339
getPartitionsByFilter#multiOr.10000 8.245    8.181    7.709    9.801    4.121
getPartitionsByFilter#multiOr.1000000 376.1    368.8    365.0    457.6    4.887
getPartitionsByFilter#simple.100 7.729    7.842    5.273    10.55    16.74
getPartitionsByFilter#simple.10000 6.846    6.731    6.422    8.648    5.709
getPartitionsByFilter#simple.1000000 234.2    231.2    229.6    264.5    2.835

@wecharyu
Copy link
Contributor Author

@zhangbutao @deniskuzZ @dengzhhu653 could you help review this PR?

@sonarqubecloud
Copy link

}

@Override
public void visit(MultiAndLeafNode node) throws MetaException {
Copy link
Member

Choose a reason for hiding this comment

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

wonder if Calcite rules could be used for such predicate optimization

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calcite's seems not have built-in api to combine AND expressions, for example the flattenAnd does not handle OR:
https://github.com/apache/calcite/blob/96b05ee12f936ed057265072ff6a2de8ea0a249e/core/src/main/java/org/apache/calcite/rex/RexUtil.java#L1296-L1325

Copy link
Member

@deniskuzZ deniskuzZ Nov 26, 2025

Choose a reason for hiding this comment

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

why not add it then to Calcite rules? it might optimize Hive queries as well
cc @zabetak, @kasakrisz

Copy link
Member

Choose a reason for hiding this comment

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

The RexUtil#flattenAnd does exactly what is described in this PR. I don't understand the argument that it doesn't handle OR. How does OR come into the picture? I don't see any mention of OR in the description of this PR/JIRA.

As part of the CBO phase we already use flattenAnd method so in most cases the predicates should be of the form AND(p1,p2,p3,...,pn) and not AND(AND(AND(p1,p2),...,pn). However, these expressions are send as string/bytes from HS2 to HMS and the latter parses them again so not sure how relevant are the optimizations that happen in the HS2 side.

In addition, there may be direct calls to HMS APIs (outside of HS2) and in that case I suppose the expressions can be of any form.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The RexUtil#flattenAnd does exactly what is described in this PR.

@zabetak you are right, the OR will be add in addAnd function, so it does the same thing like this PR: https://github.com/apache/calcite/blob/96b05ee12f936ed057265072ff6a2de8ea0a249e/core/src/main/java/org/apache/calcite/rex/RexUtil.java#L1322

If we change to use Calcite's rule, we need replace the TreeNode with RexCall.

@deniskuzZ
Copy link
Member

cc @dengzhhu653

Copy link
Member

@zabetak zabetak left a comment

Choose a reason for hiding this comment

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

First of all, I would like to understand a bit better the motivation behind this change to see if the performance improvement justifies the added complexity. I left a question under the JIRA ticket.

@NotNull List<String> arguments,
int npartitions) throws TException {
Table table = client.getTable(dbName, tableName);
client.addPartitions(createManyPartitions(table, parameters, arguments, npartitions));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need add partitions in batch because it may hit the max thrift message size for huge partition size.

@wecharyu
Copy link
Contributor Author

wecharyu commented Jan 3, 2026

@deniskuzZ @zabetak @dengzhhu653 could you take a look again? The benchmark test getPartitionsByFilter#multiAnd shows it could improve around 14.7% for 1 million partitions.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 3, 2026

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