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


##########
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:
   Flattening removes the effective protection that `MAX_AST_DEPTH` previously 
provided against very large flat `AND`/`OR` chains. A user can now submit an 
expression with thousands of `OR` terms and still pass depth validation, 
potentially causing high CPU/memory usage during parsing/validation/evaluation. 
Consider adding an explicit limit on total boolean terms / total AST nodes 
(e.g., cap `LogicalExpression.getTerms().size()` during build or enforce a 
`MAX_AST_NODES` in the validator) so wide expressions can still be rejected.



##########
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:
   With left-recursive parse trees (e.g., long `a OR b OR c ...` chains), this 
bottom-up flattening can become quadratic because each parent node copies all 
previously accumulated terms via `addAll(...)`. If large expressions are 
possible, consider flattening in a single pass (e.g., iterative walk over the 
parse tree for the same operator) or using an approach that avoids repeatedly 
copying growing term lists.



-- 
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