VGalaxies commented on code in PR #19346:
URL: https://github.com/apache/iotdb/pull/19346#discussion_r4641594158


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/columnfilter/ColumnFilterParser.java:
##########
@@ -208,11 +208,26 @@ public Expression visitLogicalNot(
     @Override
     public Expression visitLogicalBinary(
         final SubscriptionColumnFilterParser.LogicalBinaryContext context) {
-      final Expression left = visit(context.booleanExpression(0));
-      final Expression right = visit(context.booleanExpression(1));
-      return Objects.nonNull(context.AND())
-          ? LogicalExpression.and(left, right)
-          : LogicalExpression.or(left, right);
+      final LogicalExpression.Operator operator =
+          Objects.nonNull(context.AND())
+              ? LogicalExpression.Operator.AND
+              : LogicalExpression.Operator.OR;
+      final List<Expression> terms = new ArrayList<>();
+      appendLogicalTerms(terms, operator, visit(context.booleanExpression(0)));
+      appendLogicalTerms(terms, operator, visit(context.booleanExpression(1)));
+      return new LogicalExpression(operator, terms);
+    }
+
+    private static void appendLogicalTerms(
+        final List<Expression> terms,
+        final LogicalExpression.Operator operator,
+        final Expression expression) {
+      if (expression instanceof LogicalExpression
+          && ((LogicalExpression) expression).getOperator() == operator) {
+        terms.addAll(((LogicalExpression) expression).getTerms());

Review Comment:
   Thanks for catching this! Fixed in `7a9280d62e`:
   
   - Replaced the bottom-up recursive `appendLogicalTerms` / `addAll` 
flattening in `ColumnFilterParser.AstBuilder#visitLogicalBinary` with an 
iterative single-pass walk using an explicit 
`ArrayDeque<BooleanExpressionContext>` stack (also peeling parenthesized 
same-operator sub-expressions via `unwrapParenthesized`).
   - Same-operator chains are now flattened into a single `LogicalExpression` 
in strict `O(N)` time and `O(N)` allocations without intermediate 
`LogicalExpression` copies or Java recursion depth growth.
   - Strengthened 
`ColumnFilterParserTest#testFlattenedOrChainParsesIntoSingleLogicalExpression` 
(500-term `OR` chain checking exact left-to-right order) and added 
`testParenthesizedSameOperatorFlattensWhilePreservingMixedPrecedence`.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/columnfilter/ColumnFilterParser.java:
##########
@@ -208,11 +208,26 @@ public Expression visitLogicalNot(
     @Override
     public Expression visitLogicalBinary(
         final SubscriptionColumnFilterParser.LogicalBinaryContext context) {
-      final Expression left = visit(context.booleanExpression(0));
-      final Expression right = visit(context.booleanExpression(1));
-      return Objects.nonNull(context.AND())
-          ? LogicalExpression.and(left, right)
-          : LogicalExpression.or(left, right);
+      final LogicalExpression.Operator operator =
+          Objects.nonNull(context.AND())
+              ? LogicalExpression.Operator.AND
+              : LogicalExpression.Operator.OR;
+      final List<Expression> terms = new ArrayList<>();
+      appendLogicalTerms(terms, operator, visit(context.booleanExpression(0)));
+      appendLogicalTerms(terms, operator, visit(context.booleanExpression(1)));
+      return new LogicalExpression(operator, terms);

Review Comment:
   Thanks for the note. For the tag-filter path, `TagFilterValidator` already 
enforces `MAX_AST_NODES = 256` across all flattened terms 
(`visitLogicalExpression` invokes `process()` -> `accountNode()` on every 
element in `node.getTerms()`), so wide `AND`/`OR` chains remain strictly 
bounded by `MAX_AST_NODES`. `ColumnFilterValidator` did not enforce 
`MAX_AST_DEPTH`/`MAX_AST_NODES` previously, and the quadratic flattening cost 
in `ColumnFilterParser` has now been eliminated in `7a9280d62e`.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/columnfilter/ColumnFilterParser.java:
##########
@@ -208,11 +208,26 @@ public Expression visitLogicalNot(
     @Override
     public Expression visitLogicalBinary(
         final SubscriptionColumnFilterParser.LogicalBinaryContext context) {
-      final Expression left = visit(context.booleanExpression(0));
-      final Expression right = visit(context.booleanExpression(1));
-      return Objects.nonNull(context.AND())
-          ? LogicalExpression.and(left, right)
-          : LogicalExpression.or(left, right);
+      final LogicalExpression.Operator operator =
+          Objects.nonNull(context.AND())
+              ? LogicalExpression.Operator.AND
+              : LogicalExpression.Operator.OR;
+      final List<Expression> terms = new ArrayList<>();
+      appendLogicalTerms(terms, operator, visit(context.booleanExpression(0)));
+      appendLogicalTerms(terms, operator, visit(context.booleanExpression(1)));
+      return new LogicalExpression(operator, terms);
+    }
+
+    private static void appendLogicalTerms(
+        final List<Expression> terms,
+        final LogicalExpression.Operator operator,
+        final Expression expression) {
+      if (expression instanceof LogicalExpression
+          && ((LogicalExpression) expression).getOperator() == operator) {
+        terms.addAll(((LogicalExpression) expression).getTerms());
+      } else {
+        terms.add(expression);
+      }

Review Comment:
   Fixed in `7a9280d62e`: `ColumnFilterParser.AstBuilder#visitLogicalBinary` 
now walks same-operator `LogicalBinaryContext` subtrees iteratively with an 
explicit `ArrayDeque` stack and collects all terms into a single 
`LogicalExpression` in `O(N)` time without quadratic `addAll` copying.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to