Skip to content

Commit 00f550e

Browse files
authored
[Fix][MongoDB] The Long type cannot handle string values in scientific notation (#8783)
1 parent 3455316 commit 00f550e

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/source/split/SamplingSplitStrategy.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.seatunnel.connectors.seatunnel.mongodb.source.split;
1919

20+
import org.apache.seatunnel.shade.com.google.common.annotations.VisibleForTesting;
2021
import org.apache.seatunnel.shade.com.google.common.base.Preconditions;
2122
import org.apache.seatunnel.shade.com.google.common.collect.Lists;
2223

@@ -33,6 +34,7 @@
3334
import com.mongodb.client.model.Sorts;
3435

3536
import java.io.Serializable;
37+
import java.math.BigDecimal;
3638
import java.util.Collections;
3739
import java.util.List;
3840
import java.util.Optional;
@@ -103,7 +105,8 @@ public List<MongoSplit> split() {
103105
return createSplits(splitKey, rightBoundaries);
104106
}
105107

106-
private ImmutablePair<Long, Long> getDocumentNumAndAvgSize() {
108+
@VisibleForTesting
109+
protected ImmutablePair<Long, Long> getDocumentNumAndAvgSize() {
107110
String collectionName =
108111
clientProvider.getDefaultCollection().getNamespace().getCollectionName();
109112
BsonDocument statsCmd = new BsonDocument("collStats", new BsonString(collectionName));
@@ -112,7 +115,7 @@ private ImmutablePair<Long, Long> getDocumentNumAndAvgSize() {
112115
// fix issue https://github.com/apache/seatunnel/issues/7575
113116
long total =
114117
Optional.ofNullable(count)
115-
.map(v -> Long.parseLong(String.valueOf(count)))
118+
.map(v -> new BigDecimal(String.valueOf(count)).longValue())
116119
.orElse(0L);
117120
Object avgDocumentBytes = res.get("avgObjSize");
118121
long avgObjSize =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.seatunnel.connectors.seatunnel.mongodb.source.split;
19+
20+
import org.apache.seatunnel.connectors.seatunnel.mongodb.internal.MongodbClientProvider;
21+
22+
import org.apache.commons.lang3.tuple.ImmutablePair;
23+
24+
import org.bson.BsonDocument;
25+
import org.bson.BsonString;
26+
import org.bson.Document;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
import org.mockito.Mock;
30+
import org.mockito.MockitoAnnotations;
31+
32+
import com.mongodb.MongoNamespace;
33+
import com.mongodb.client.MongoCollection;
34+
import com.mongodb.client.MongoDatabase;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.mockito.Mockito.when;
38+
39+
public class SamplingSplitStrategyTest {
40+
41+
@Mock private MongodbClientProvider clientProvider;
42+
43+
@Mock private MongoCollection<BsonDocument> collection;
44+
45+
@Mock private MongoDatabase database;
46+
47+
private SamplingSplitStrategy strategy;
48+
49+
@BeforeEach
50+
public void setUp() {
51+
MockitoAnnotations.openMocks(this);
52+
strategy = new SamplingSplitStrategy(clientProvider, "splitKey", null, null, 100L, 1000L);
53+
when(clientProvider.getDefaultCollection()).thenReturn(collection);
54+
when(clientProvider.getDefaultDatabase()).thenReturn(database);
55+
56+
MongoNamespace namespace = new MongoNamespace("databaseName", "collectionName");
57+
when(collection.getNamespace()).thenReturn(namespace);
58+
}
59+
60+
@Test
61+
public void testGetDocumentNumAndAvgSize() {
62+
BsonDocument statsCmd = new BsonDocument("collStats", new BsonString("collectionName"));
63+
Document res = new Document();
64+
res.put("count", "1.3360484963E10");
65+
res.put("avgObjSize", 200.0);
66+
67+
when(database.runCommand(statsCmd)).thenReturn(res);
68+
69+
ImmutablePair<Long, Long> result = strategy.getDocumentNumAndAvgSize();
70+
71+
assertEquals(Long.valueOf(13360484963L), result.getLeft());
72+
assertEquals(Long.valueOf(200), result.getRight());
73+
}
74+
}

0 commit comments

Comments
 (0)