Gabriel39 commented on code in PR #70604:
URL: https://github.com/apache/doris/pull/70604#discussion_r4582047290


##########
fe/fe-connector/fe-connector-mongodb/src/main/java/org/apache/doris/connector/mongodb/MongoPredicateTranslator.java:
##########
@@ -0,0 +1,292 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.connector.mongodb;
+
+import org.apache.doris.connector.spi.ConnectorType;
+import org.apache.doris.connector.spi.pushdown.ConnectorAnd;
+import org.apache.doris.connector.spi.pushdown.ConnectorBetween;
+import org.apache.doris.connector.spi.pushdown.ConnectorColumnRef;
+import org.apache.doris.connector.spi.pushdown.ConnectorComparison;
+import org.apache.doris.connector.spi.pushdown.ConnectorExpression;
+import org.apache.doris.connector.spi.pushdown.ConnectorIn;
+import org.apache.doris.connector.spi.pushdown.ConnectorIsNull;
+import org.apache.doris.connector.spi.pushdown.ConnectorLike;
+import org.apache.doris.connector.spi.pushdown.ConnectorLiteral;
+import org.apache.doris.connector.spi.pushdown.ConnectorOr;
+
+import org.bson.Document;
+import org.bson.types.Decimal128;
+
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/** Candidate filters only: the scan provider never consumes the original 
Doris predicates. */
+final class MongoPredicateTranslator {
+    enum Kind { EXACT, SUPERSET, UNSUPPORTED }
+
+    record Result(Kind kind, Document filter) {
+        static Result unsupported() {
+            return new Result(Kind.UNSUPPORTED, new Document());
+        }
+    }
+
+    private static final int MAX_TERMS = 1024;
+    private static final int MAX_DEPTH = 64;
+    private final Map<String, MongoSchema.Binding> bindings;
+    private final boolean like;
+    private int terms;
+
+    MongoPredicateTranslator(MongoSchema schema, boolean like) {
+        bindings = 
schema.getBindings().stream().collect(Collectors.toMap(MongoSchema.Binding::getName,
 b -> b));
+        this.like = like;
+    }
+
+    Result translate(ConnectorExpression expression) {
+        terms = 0;
+        return translate(expression, 0);
+    }
+
+    private Result translate(ConnectorExpression expression, int depth) {
+        if (depth > MAX_DEPTH || ++terms > MAX_TERMS) {
+            return Result.unsupported();
+        }
+        if (expression instanceof ConnectorAnd || expression instanceof 
ConnectorOr) {
+            boolean and = expression instanceof ConnectorAnd;
+            List<Document> filters = new ArrayList<>();
+            boolean exact = true;
+            for (ConnectorExpression child : expression.getChildren()) {
+                Result result = translate(child, depth + 1);
+                if (result.kind() == Kind.UNSUPPORTED) {
+                    if (!and) {
+                        // Dropping even one OR arm loses rows; AND can keep 
independently safe candidates.
+                        return Result.unsupported();
+                    }
+                    exact = false;
+                } else {
+                    filters.add(result.filter());
+                    exact &= result.kind() == Kind.EXACT;
+                }
+            }
+            return filters.isEmpty() ? Result.unsupported()
+                    : new Result(exact ? Kind.EXACT : Kind.SUPERSET,
+                            new Document(and ? "$and" : "$or", filters));
+        }
+        if (expression instanceof ConnectorComparison comparison) {
+            ConnectorExpression column = comparison.getLeft();
+            ConnectorExpression constant = comparison.getRight();
+            String operator = switch (comparison.getOperator()) {
+                case EQ, EQ_FOR_NULL -> "$eq";
+                case GT -> "$gt";
+                case GE -> "$gte";
+                case LT -> "$lt";
+                case LE -> "$lte";
+                default -> null;
+            };
+            if (operator == null) {
+                return Result.unsupported();
+            }
+            if (column instanceof ConnectorLiteral && constant instanceof 
ConnectorColumnRef) {
+                ConnectorExpression tmp = column;
+                column = constant;
+                constant = tmp;
+                operator = switch (operator) {
+                    case "$gt" -> "$lt";
+                    case "$gte" -> "$lte";
+                    case "$lt" -> "$gt";
+                    case "$lte" -> "$gte";
+                    default -> operator;
+                };
+            }
+            MongoSchema.Binding binding = binding(column);
+            if (binding == null || !(constant instanceof ConnectorLiteral 
literal)) {
+                return Result.unsupported();
+            }
+            Object value = typedValue(binding, literal);
+            return value == null ? Result.unsupported() : candidate(binding, 
operator, value);
+        }
+        if (expression instanceof ConnectorBetween between) {
+            return translate(new ConnectorAnd(List.of(
+                    new ConnectorComparison(ConnectorComparison.Operator.GE, 
between.getValue(), between.getLower()),
+                    new ConnectorComparison(ConnectorComparison.Operator.LE, 
between.getValue(), between.getUpper()))),
+                    depth + 1);
+        }
+        if (expression instanceof ConnectorIn in) {
+            MongoSchema.Binding binding = binding(in.getValue());
+            if (in.isNegated() || binding == null || in.getInList().isEmpty()
+                    || in.getInList().size() > MAX_TERMS) {
+                return Result.unsupported();
+            }
+            List<Object> values = new ArrayList<>();
+            for (ConnectorExpression item : in.getInList()) {
+                if (!(item instanceof ConnectorLiteral literal)) {
+                    return Result.unsupported();
+                }
+                // NULL contributes no TRUE arm to a positive IN; NOT IN 
deliberately stays local.
+                if (literal.isNull()) {
+                    continue;
+                }
+                Object value = typedValue(binding, literal);
+                if (value == null) {
+                    return Result.unsupported();
+                }
+                values.add(value);
+            }
+            return candidate(binding, "$in", values);
+        }
+        if (expression instanceof ConnectorIsNull isNull && 
isNull.isNegated()) {
+            MongoSchema.Binding binding = binding(isNull.getOperand());
+            return binding == null ? Result.unsupported() : candidate(binding, 
"$ne", null);

Review Comment:
   Fixed in b6f21416163. IS NOT NULL candidates now require a scalar target; 
ARRAY/JSON and other complex targets remain local. Added a translator UT and 
enabled/disabled SQL regressions for ARRAY<STRING> and JSON arrays containing 
null. Both retain the previously discarded row, and all 40 sorted off/on result 
pairs match.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to