You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "nozjkoitop (via GitHub)" <gi...@apache.org> on 2024/04/09 08:28:58 UTC

[PR] Feature configurable calcite bloat (druid)

nozjkoitop opened a new pull request, #16248:
URL: https://github.com/apache/druid/pull/16248

   ### Description
   
   This pull request introduces a configurable `druid.sql.planner.bloat` parameter in CalciteRulesManager.
   
   #### Fixed the problem when complicated nested sql queries which where working before Druid 29.0.0 (1.21.0 calcite version) are being rejected by calcite planner.
   
   With exception:
   `There are not enough rules to produce a node with desired properties: convention=DRUID, sort=[]`
   
   Appears that the exceptiom is being thrown, when the root `RelNode` which initially is instance of `AbstractConverter` is being treated as `ProjectMergeRule`, which counts the result of merge not optimal and reject merging, which means that transformation of AbstractConverter [is not happening](https://github.com/apache/calcite/blob/75750b78b5ac692caa654f506fc1515d4d3991d6/core/src/main/java/org/apache/calcite/rel/rules/ProjectMergeRule.java#L129,), so at the end of the day this `AbstractConverter` instance keeps skipping bestCost initialization, because it returns default [infinite value](https://github.com/apache/calcite/blob/75750b78b5ac692caa654f506fc1515d4d3991d6/core/src/main/java/org/apache/calcite/plan/volcano/AbstractConverter.java#L75)  which is being skipped by  [VolcanoPlanner](https://github.com/apache/calcite/blob/75750b78b5ac692caa654f506fc1515d4d3991d6/core/src/main/java/org/apache/calcite/plan/volcano/VolcanoPlanner.java#L984). That's why bestCost to root relNode 
 is never set which is causing [exception](https://github.com/apache/calcite/blob/fe0da064dc3081b140e3c84e109aed63f202a4af/core/src/main/java/org/apache/calcite/plan/volcano/RelSubset.java#L550.) 
   This issue is probably caused by lots of optimizations in `VolcanoPlanner `and `ProjectMergeRule` and looks like as a calcite bug.
   
   
   #### Release note
   Created workaround: configurable `druid.sql.planner.bloat` parameter, which should be set on broker `jvm.conf`,  it will adjust the limits of `ProjectMergeRule` result cost and could allow running complex queries(default value 100, suggested 1000, but it depends on data to be processed). 
   
   <hr>
   
   ##### Key changed/added classes in this PR
    * `CalciteRulesManager`
   
   <hr>
   
   This PR has:
   
   - [x] been self-reviewed.
   - [ ] added documentation for new or modified features or behaviors.
   - [x] a release note entry in the PR description.
   - [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
   - [x] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
   - [ ] added integration tests.
   - [x] been tested in a test Druid cluster.


-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "nozjkoitop (via GitHub)" <gi...@apache.org>.
nozjkoitop commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1559635309


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@ public class CalciteRulesManager
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(
+      System.getProperty(BLOAT_PROPERTY, "100")

Review Comment:
   Hi, considering that  this is primarily a workaround for a potential Calcite problem, I'm hesitant to increase the default value that much. However, I agree that raising the default to at least 1000 would be a good compromise.



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1557255062


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(
+      System.getProperty(BLOAT_PROPERTY, "100")
+  );

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/7234)



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1562325407


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -95,12 +100,16 @@ public class CalciteRulesManager
    * and {@link CoreRules#FILTER_INTO_JOIN}, which are part of {@link #FANCY_JOIN_RULES}.
    * 4) {@link CoreRules#PROJECT_FILTER_TRANSPOSE} because PartialDruidQuery would like to have the Project on top of the Filter -
    * this rule could create a lot of non-useful plans.
+   *
+   * {@link CoreRules#PROJECT_MERGE} includes configurable bloat parameter, as a workaround for Calcite exception
+   * (there are not enough rules to produce a node with desired properties) thrown while running complex sql-queries with
+   * big amount of subqueries. `druid.sql.planner.bloat` should be set in broker's `jvm.config` file.
    */
   private static final List<RelOptRule> BASE_RULES =
       ImmutableList.of(
           CoreRules.AGGREGATE_STAR_TABLE,
           CoreRules.AGGREGATE_PROJECT_STAR_TABLE,
-          CoreRules.PROJECT_MERGE,
+          ProjectMergeRule.Config.DEFAULT.withBloat(BLOAT).toRule(),

Review Comment:
   How do users know when to set this context parameter ?
   I think we should change the error message so that they can be a bit more helpful for the end users. 



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "kgyrtkirk (via GitHub)" <gi...@apache.org>.
kgyrtkirk commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1557559476


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@ public class CalciteRulesManager
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(
+      System.getProperty(BLOAT_PROPERTY, "100")

Review Comment:
   I think the druid planner we'll not like at all if multiple projects are kep on top of eachother...
   
   the [default](https://github.com/apache/calcite/blob/4e6a320bf476c4b3c313d86f8ff3c6edf4c2a578/core/src/main/java/org/apache/calcite/rel/rules/ProjectMergeRule.java#L49) seems to be the same (`100`) - what do you think about raising it to a bigger value; like a few millions - so that its less likely to hit this issue out of the box ?



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "nozjkoitop (via GitHub)" <gi...@apache.org>.
nozjkoitop commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1560492042


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@ public class CalciteRulesManager
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(

Review Comment:
   Good point, thanks!  I will have a look 



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "nozjkoitop (via GitHub)" <gi...@apache.org>.
nozjkoitop commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1574793790


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@ public class CalciteRulesManager
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(

Review Comment:
   Updated to use context parameter



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "kgyrtkirk (via GitHub)" <gi...@apache.org>.
kgyrtkirk commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1559676135


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@ public class CalciteRulesManager
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(
+      System.getProperty(BLOAT_PROPERTY, "100")

Review Comment:
   yeah - I agree...an off switch would be better than tweaking an integer;
   it depends on the execution engine how it evaluates a set of expressions which may share common subexpressions ...it might not cause much problem in case it could avoid recomputations.
   



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1559689231


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(
+      System.getProperty(BLOAT_PROPERTY, "1000")
+  );

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/7256)



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "kgyrtkirk (via GitHub)" <gi...@apache.org>.
kgyrtkirk commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1557559476


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@ public class CalciteRulesManager
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(
+      System.getProperty(BLOAT_PROPERTY, "100")

Review Comment:
   I think the druid planner we'll not like at all if multiple projects are kep on top of eachother...
   
   the [default](https://github.com/apache/calcite/blob/4e6a320bf476c4b3c313d86f8ff3c6edf4c2a578/core/src/main/java/org/apache/calcite/rel/rules/ProjectMergeRule.java#L49) seems to be the same (`100`) - what do you think about raising it to a bigger value; like a few millions - so that its less likely to hit this issue?



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "abhishekagarwal87 (via GitHub)" <gi...@apache.org>.
abhishekagarwal87 commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1559715883


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -82,6 +83,10 @@ public class CalciteRulesManager
   private static final int HEP_DEFAULT_MATCH_LIMIT = Integer.parseInt(
       System.getProperty(HEP_DEFAULT_MATCH_LIMIT_CONFIG_STRING, "1200")
   );
+  private static final String BLOAT_PROPERTY = "druid.sql.planner.bloat";
+  private static final int BLOAT = Integer.parseInt(

Review Comment:
   It should be passed via context parameter or through runtime property. A context param seems a better fit as it would allow you to run those complex queries without having any impact on the rest of the queries. 



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "nozjkoitop (via GitHub)" <gi...@apache.org>.
nozjkoitop commented on code in PR #16248:
URL: https://github.com/apache/druid/pull/16248#discussion_r1574791915


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java:
##########
@@ -95,12 +100,16 @@ public class CalciteRulesManager
    * and {@link CoreRules#FILTER_INTO_JOIN}, which are part of {@link #FANCY_JOIN_RULES}.
    * 4) {@link CoreRules#PROJECT_FILTER_TRANSPOSE} because PartialDruidQuery would like to have the Project on top of the Filter -
    * this rule could create a lot of non-useful plans.
+   *
+   * {@link CoreRules#PROJECT_MERGE} includes configurable bloat parameter, as a workaround for Calcite exception
+   * (there are not enough rules to produce a node with desired properties) thrown while running complex sql-queries with
+   * big amount of subqueries. `druid.sql.planner.bloat` should be set in broker's `jvm.config` file.
    */
   private static final List<RelOptRule> BASE_RULES =
       ImmutableList.of(
           CoreRules.AGGREGATE_STAR_TABLE,
           CoreRules.AGGREGATE_PROJECT_STAR_TABLE,
-          CoreRules.PROJECT_MERGE,
+          ProjectMergeRule.Config.DEFAULT.withBloat(BLOAT).toRule(),

Review Comment:
   Honestly, these are calcite internals, and all we get on the druid side is [RelOptPlanner.CannotPlanException](https://github.com/apache/calcite/blob/fe0da064dc3081b140e3c84e109aed63f202a4af/core/src/main/java/org/apache/calcite/plan/volcano/RelSubset.java#L718), which I believe could be thrown by tons of reasons. We can add the suggestion to try to set this context parameter at [QueryHandler](https://github.com/apache/druid/blob/173a206829e5fff7580fae068928a79f10726124/sql/src/main/java/org/apache/druid/sql/calcite/planner/QueryHandler.java#L676), but IMO it's a real shot in the dark there.



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Feature configurable calcite bloat (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe merged PR #16248:
URL: https://github.com/apache/druid/pull/16248


-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org