Skip to content

Commit a136bba

Browse files
Merge branch 'master' into async-metrics-sockets
2 parents 9bc8a28 + d0e7a3a commit a136bba

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/Processors/Sources/MongoDBSource.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ MongoDBSource::MongoDBSource(
166166
, cursor{collection.find(query, options)}
167167
, sample_block{sample_block_}
168168
, max_block_size{max_block_size_}
169+
, db_json_format_settings{.json= {.max_depth = 0, .quote_64bit_integers = false}}
170+
, json_format_settings{db_json_format_settings, 0, true, true}
169171
{
170172
for (const auto & idx : collections::range(0, sample_block.columns()))
171173
{

src/Processors/Sources/MongoDBSource.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class MongoDBSource final : public ISource
4848
Block sample_block;
4949
std::unordered_map<size_t, std::pair<size_t, std::pair<DataTypePtr, Field>>> arrays_info;
5050
const UInt64 max_block_size;
51-
52-
JSONBuilder::FormatSettings json_format_settings = {{}, 0, true, true};
5351
bool all_read = false;
52+
53+
const DB::FormatSettings db_json_format_settings;
54+
const JSONBuilder::FormatSettings json_format_settings;
5455
};
5556

5657
}

tests/integration/test_storage_mongodb/test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import datetime
23
import json
34
import uuid
@@ -1227,3 +1228,47 @@ def test_password_masking(started_cluster):
12271228
== "CREATE DICTIONARY default.mongodb_dictionary_password_masking (`_id` String) PRIMARY KEY _id SOURCE(MONGODB(HOST \\'127.0.0.1\\' PORT 27017 USER \\'testuser\\' PASSWORD \\'[HIDDEN]\\' DB \\'example\\' COLLECTION \\'test_clickhouse\\' OPTIONS \\'ssl=true\\')) LIFETIME(MIN 0 MAX 0) LAYOUT(FLAT())\n"
12281229
)
12291230
node.query("DROP DICTIONARY IF EXISTS mongodb_dictionary_password_masking;")
1231+
1232+
1233+
def test_json_serialization(started_cluster):
1234+
mongo_connection = get_mongo_connection(started_cluster)
1235+
db = mongo_connection["test"]
1236+
db.command("dropAllUsersFromDatabase")
1237+
db.command("createUser", "root", pwd=mongo_pass, roles=["readWrite"])
1238+
json_serialization_table = db["json_serialization_table"]
1239+
1240+
date = datetime.datetime.strptime("2025-05-17 13:14:15", "%Y-%m-%d %H:%M:%S")
1241+
1242+
def create_dataset(mongo, level) -> dict:
1243+
return {
1244+
"type_string": "Type string",
1245+
"type_oid": bson.ObjectId("60f7e65e16b1c1d1c8a2b6b3") if mongo else "60f7e65e16b1c1d1c8a2b6b3",
1246+
"type_binary": bson.Binary(b"binarydata", subtype=0) if mongo else base64.b64encode(b"binarydata").decode(),
1247+
"type_bool": True,
1248+
"type_int32": 123,
1249+
"type_int64": bson.int64.Int64(2**63 - 1) if mongo else int(2**63 - 1),
1250+
"type_double": float(3.141592653589793238),
1251+
"type_date": date if mongo else date.strftime("%Y-%m-%d %H:%M:%S"),
1252+
"type_timestamp": bson.timestamp.Timestamp(date, 1) if mongo else date.strftime("%Y-%m-%d %H:%M:%S"),
1253+
"type_document": {"nested_doc": create_dataset(mongo, level - 1)} if level > 0 else {},
1254+
"type_array": [create_dataset(mongo, level - 1)] if level > 0 else [],
1255+
"type_regex": bson.regex.Regex(r"^pattern.*$", "i") if mongo else {"^pattern.*$": "i"},
1256+
"type_null": None,
1257+
}
1258+
1259+
json_serialization_table.insert_one({"dataset": create_dataset(True, 10)})
1260+
node = started_cluster.instances["node"]
1261+
node.query(
1262+
f"""
1263+
CREATE OR REPLACE TABLE json_serialization_table(
1264+
dataset String
1265+
) ENGINE = MongoDB('mongo1:27017', 'test', 'json_serialization_table', 'root', '{mongo_pass}')
1266+
"""
1267+
)
1268+
1269+
assert node.query(f"SELECT COUNT() FROM json_serialization_table") == "1\n"
1270+
assert (node.query(f"SELECT dataset FROM json_serialization_table")[:-1]
1271+
== json.dumps(create_dataset(False, 10), separators=(',', ':')))
1272+
1273+
node.query("DROP TABLE json_serialization_table")
1274+
json_serialization_table.drop()

0 commit comments

Comments
 (0)