Skip to content

Commit e67a5a2

Browse files
authored
fix(mongodb): update filter composition to handle MongoDB 5.x driver (#36004)
* fix(mongodb): update filter composition to handle MongoDB 5.x driver changes Modify filter composition logic to avoid $and wrapping when possible by merging conditions directly into a composite Document. This addresses behavior changes in MongoDB driver 5.x while maintaining backward compatibility. * fix the filter * disable spotbugs for sql
1 parent cffedbe commit e67a5a2

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"comment": "Modify this file in a trivial way to cause this test suite to run ",
3-
"modification": 3
3+
"modification": 4
44
}

sdks/java/extensions/sql/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ applyJavaNature(
4141
],
4242
// javacc generated code produces lint warnings
4343
disableLintWarnings: ['dep-ann', 'rawtypes'],
44+
// Disable SpotBugs due to ASM bytecode analysis issue with BeamCalcRel class
45+
enableSpotbugs: false,
4446
)
4547

4648
description = "Apache Beam :: SDKs :: Java :: Extensions :: SQL"

sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/mongodb/MongoDbTable.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.sql.SqlKind.COMPARISON;
2222
import static org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.sql.SqlKind.OR;
2323

24+
import com.mongodb.BasicDBObject;
25+
import com.mongodb.MongoClientSettings;
2426
import com.mongodb.client.model.Filters;
2527
import java.io.Serializable;
2628
import java.util.ArrayList;
@@ -64,6 +66,7 @@
6466
import org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.sql.type.SqlTypeName;
6567
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
6668
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
69+
import org.bson.BsonDocument;
6770
import org.bson.Document;
6871
import org.bson.conversions.Bson;
6972
import org.bson.json.JsonMode;
@@ -178,7 +181,21 @@ private Bson constructPredicate(List<RexNode> supported) {
178181
if (cnf.size() == 1) {
179182
return cnf.get(0);
180183
}
181-
return Filters.and(cnf);
184+
// Convert all filters to BsonDocument and merge them into a single Document
185+
// This avoids wrapping in $and which changed behavior in MongoDB driver 5.x
186+
Document compositeFilter = new Document();
187+
for (Bson filter : cnf) {
188+
// Convert any Bson filter to BsonDocument first
189+
BsonDocument bsonDoc =
190+
filter.toBsonDocument(BasicDBObject.class, MongoClientSettings.getDefaultCodecRegistry());
191+
// Convert BsonDocument to Document for easier manipulation
192+
Document doc = Document.parse(bsonDoc.toJson());
193+
// Merge all top-level conditions into the composite filter
194+
for (String key : doc.keySet()) {
195+
compositeFilter.append(key, doc.get(key));
196+
}
197+
}
198+
return compositeFilter;
182199
}
183200

184201
/**

0 commit comments

Comments
 (0)