Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for leveraging StarTree index in conjunction with filtered aggregations #11886

Merged

Conversation

egalpin
Copy link
Member

@egalpin egalpin commented Oct 26, 2023

This PR aims to add support for leveraging StarTree index from filtered aggregations (i.e. SELECT COUNT(*) FILTER (WHERE foo='bar')), including filtered group-by aggregations. For queries with a mixture of star-tree-solvable aggregations and aggs which cannot be solved by star-tree, those aggs solvable via star-tree will do so iff all aggs in the same filter swimlane are solvable via star-tree (where swimlane means same FILTER WHERE expression, including lack of FILTER WHERE).

tags: [feature]


// Use star-tree to solve the query if possible
List<StarTreeV2> starTrees = _indexSegment.getStarTrees();
if (starTrees != null && !_queryContext.isSkipStarTree()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: this PR would introduce a behaviour change (bugfix?) where AggregationPlanNode included an additional predicate to check if null handling is enabled before allowing use of StarTree index to solve the query. This PR consolidates this logic to one method within OperatorUtils.java, where the null handling predicate is kept such that both GroupByPlanNode and AggregationPlanNode will ensure null handling is disabled before allowing use of StarTree index.

if (starTrees != null && !_queryContext.isSkipStarTree() && !_queryContext.isNullHandlingEnabled()) {

@egalpin egalpin marked this pull request as ready for review October 26, 2023 22:47
@egalpin
Copy link
Member Author

egalpin commented Oct 26, 2023

Marking as ready-for-review to get test results, but this should effectively be considered a draft at this point

@codecov-commenter
Copy link

codecov-commenter commented Oct 26, 2023

Codecov Report

Attention: 37 lines in your changes are missing coverage. Please review.

Comparison is base (41d1a12) 61.62% compared to head (10d1df9) 61.63%.
Report is 9 commits behind head on master.

Files Patch % Lines
.../org/apache/pinot/core/startree/StarTreeUtils.java 21.73% 17 Missing and 1 partial ⚠️
...t/core/operator/query/FilteredGroupByOperator.java 61.53% 3 Missing and 2 partials ⚠️
...ore/startree/executor/StarTreeGroupByExecutor.java 0.00% 5 Missing ⚠️
...re/operator/query/FilteredAggregationOperator.java 66.66% 2 Missing and 1 partial ⚠️
...aggregation/function/AggregationFunctionUtils.java 95.00% 1 Missing and 2 partials ⚠️
...che/pinot/core/operator/query/GroupByOperator.java 85.71% 0 Missing and 1 partial ⚠️
...rg/apache/pinot/core/plan/AggregationPlanNode.java 87.50% 0 Missing and 1 partial ⚠️
...ava/org/apache/pinot/core/plan/FilterPlanNode.java 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master   #11886   +/-   ##
=========================================
  Coverage     61.62%   61.63%           
- Complexity     1150     1152    +2     
=========================================
  Files          2385     2385           
  Lines        129275   129374   +99     
  Branches      20016    20030   +14     
=========================================
+ Hits          79668    79740   +72     
- Misses        43806    43821   +15     
- Partials       5801     5813   +12     
Flag Coverage Δ
custom-integration1 <0.01% <0.00%> (ø)
integration <0.01% <0.00%> (ø)
integration1 <0.01% <0.00%> (ø)
integration2 0.00% <0.00%> (ø)
java-11 61.59% <73.38%> (-0.01%) ⬇️
java-21 61.51% <73.38%> (+0.02%) ⬆️
skip-bytebuffers-false 61.62% <73.38%> (+<0.01%) ⬆️
skip-bytebuffers-true 61.47% <73.38%> (+0.02%) ⬆️
temurin 61.63% <73.38%> (+<0.01%) ⬆️
unittests 61.63% <73.38%> (+<0.01%) ⬆️
unittests1 46.98% <73.38%> (+<0.01%) ⬆️
unittests2 27.56% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@egalpin egalpin force-pushed the egalpin/startree-support-filtered-aggs branch from 90d91fe to e873508 Compare October 27, 2023 16:16
…e even when nullhandling enabled to check integration tests
@xiangfu0
Copy link
Contributor

not sure if this is related: #11899

@egalpin
Copy link
Member Author

egalpin commented Oct 28, 2023

@xiangfu0 yep! The goal of this PR would be to support exactly what’s described in #11899. Happy to collaborate, just let me know!

@xiangfu0
Copy link
Contributor

@xiangfu0 yep! The goal of this PR would be to support exactly what’s described in #11899. Happy to collaborate, just let me know!

Great!

@egalpin
Copy link
Member Author

egalpin commented Nov 7, 2023

@xiangfu0 @Jackie-Jiang This can now be considered ready for review, as it's operationally functional. I'll add tests, but wanted to flag that this is ready for review in case that helps with velocity etc. See screen shots below for examples.

I noticed that this feature also provides an additional enhancement: being able to abuse FILTER aggs to allow queries to solve any agg which might be solvable via ST index even if one of the aggs in the query must be solved without the ST. This isn't currently possible; currently, all aggs must be solvable by the ST in order to proceed with its use.

Take for example the airlineStats example data. The following query would utilize ST:

select
  AirlineID,
  MAX(ArrDelay) as max_delay
FROM airlineStats
GROUP BY AirlineID

However, this would not (because MIN is not in the ST):

select
  AirlineID,
  MAX(ArrDelay) as max_delay,
  MIN(ArrDelay)
FROM airlineStats
GROUP BY AirlineID

After this change, one could rewrite the query like so:

select
  AirlineID,
  MAX(ArrDelay) FILTER (WHERE AirlineID != -1) AS max_delay,
  MIN(ArrDelay)
FROM airlineStats
GROUP BY AirlineID

This would allow the MAX to leverage the ST. (note that the FILTER WHERE could be placed on either aggregation, the key element is that adding a filter to the agg places the 2 aggs in different swimlanes which changes the decision making around whether or not ST can be used to answer the query)

@egalpin
Copy link
Member Author

egalpin commented Nov 7, 2023

Contrived example to show the working of FILTER (only has a result for the one row that matches AirlineID). Note the 2 FILTER_STARTREE_INDEX in the plan.

Screenshot 2023-11-06 at 16 26 40 Screenshot 2023-11-06 at 16 27 18

@egalpin
Copy link
Member Author

egalpin commented Nov 8, 2023

@Jackie-Jiang @xiangfu0 this is now complete with tests and ready for review. Thanks!

Copy link
Contributor

@Jackie-Jiang Jackie-Jiang left a comment

Choose a reason for hiding this comment

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

Nice to see it fits to the current filtered aggregation architecture!

import org.apache.pinot.core.operator.BaseProjectOperator;


public interface ProjectionPlanNode extends PlanNode {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest not adding this interface. We can do cast if necessary


public CombinedFilteredAggregationContext(BaseFilterOperator baseFilterOperator,
List<Pair<Predicate, PredicateEvaluator>> predicateEvaluators, @Nullable FilterContext mainFilterContext,
@Nonnull FilterContext subFilterContext, List<AggregationFunction> aggregationFunctions) {
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor, convention) We don't usually use @Nonnull, but only annotate @Nullable and assume everything else as non-null

// Otherwise, there is no way to tell whether the 1st reload on server side is finished,
// which may hit the race condition that the 1st reload finishes after the 2nd reload is fully done.
// 10 seconds are still better than hitting race condition which will time out after 10 minutes.
Thread.sleep(10_000L);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should avoid this kind of wait because it will waste resource on fast machine but flaky on slow machine. We don't need to add test in star-tree triggering test because the intention for this test is to just verify star-tree is created. We should add more tests into the StarTreeClusterIntegrationTest

@@ -38,7 +38,9 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.junit.Assert.assertFalse;
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong import

// Prevent instantiation, make checkstyle happy
}

public static ProjectionPlanNode maybeGetStartreeProjectionOperator(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest moving this into the StarTreeUtils as

  @Nullable
  public static BaseProjectOperator<?> createStarTreeBasedProjectOperator(...)

It returns null when star-tree cannot be used. This way we don't need to worry about using instanceof to identify whether it is star-tree based, and we don't need to create PlanNode out of BaseFilterOperator which should be avoided

Copy link
Member Author

Choose a reason for hiding this comment

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

In that case would you put the onus of creating the ProjectPlanNode on the caller of createStarTreeBasedProjectOperator if it returned null?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. Note that I'm suggesting directly creating operator instead of plan node, so the caller is responsible for creating the BaseProjectOperator if star-tree cannot be used

import org.apache.pinot.core.query.aggregation.function.AggregationFunction;


public class CombinedFilteredAggregationContext {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is used as a inner helper wrapper class for AggregationFunctionUtils.buildFilteredAggregateProjectOperators(). Suggest making it a private inner class, and we can directly access the fields within the class

@@ -82,6 +83,16 @@ private String generateAggregation(String metricColumn) {
metricColumn);
}

private String generateFilteredAggregation(String metricColumn) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@Jackie-Jiang these tests seem to consistently fail when run using the multi-stage engine. Are filtered aggregations supported in multi-stage, or not yet?

Copy link
Member Author

Choose a reason for hiding this comment

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

attempted fix in 990bf5a

@Jackie-Jiang Jackie-Jiang added the release-notes Referenced by PRs that need attention when compiling the next release notes label Nov 18, 2023
@@ -214,46 +214,69 @@ public static Object getConvertedFinalResult(DataTable dataTable, ColumnDataType
}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Much better, I was not liking the nested Pair usage haha. Thanks!

@Jackie-Jiang Jackie-Jiang merged commit f7f8260 into apache:master Nov 18, 2023
19 checks passed
@egalpin egalpin deleted the egalpin/startree-support-filtered-aggs branch November 19, 2023 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature release-notes Referenced by PRs that need attention when compiling the next release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants