Skip to content

Commit 646e1b1

Browse files
authored
Merge pull request ClickHouse#78777 from CurtizJ/fix-several-mongo-queries
Fix several queries from tables with `MongoDB` engine
2 parents 5fd234a + 887d0b4 commit 646e1b1

File tree

4 files changed

+323
-93
lines changed

4 files changed

+323
-93
lines changed

src/Common/BSONCXXHelper.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#include <Common/JSONBuilder.h>
99
#include <IO/ReadBufferFromMemory.h>
1010
#include <IO/ReadHelpers.h>
11-
#include <DataTypes/FieldToDataType.h>
12-
#include "DataTypes/DataTypeNullable.h"
11+
#include <DataTypes/DataTypeNullable.h>
12+
#include <DataTypes/DataTypeArray.h>
13+
#include <DataTypes/DataTypeTuple.h>
1314

1415
#include <mongocxx/client.hpp>
1516

@@ -97,15 +98,32 @@ static bsoncxx::types::bson_value::value fieldAsBSONValue(const Field & field, c
9798
case TypeIndex::Tuple:
9899
{
99100
auto arr = bsoncxx::v_noabi::builder::basic::array();
100-
for (const auto & elem : field.safeGet<Tuple>())
101-
arr.append(fieldAsBSONValue(elem, applyVisitor(FieldToDataType(), elem), is_oid));
101+
const auto * tuple_type = typeid_cast<const DataTypeTuple *>(type.get());
102+
103+
if (!tuple_type)
104+
throw Exception(ErrorCodes::TYPE_MISMATCH, "Expected Tuple type, got {}.", type->getPrettyName());
105+
106+
const auto & elem_types = tuple_type->getElements();
107+
const auto & tuple_value = field.safeGet<Tuple>();
108+
109+
if (elem_types.size() != tuple_value.size())
110+
throw Exception(ErrorCodes::TYPE_MISMATCH, "Tuple size mismatch, expected {} elements, got {} in field.", elem_types.size(), tuple_value.size());
111+
112+
for (size_t i = 0; i < elem_types.size(); ++i)
113+
arr.append(fieldAsBSONValue(tuple_value[i], elem_types[i], is_oid));
102114
return arr.view();
103115
}
104116
case TypeIndex::Array:
105117
{
106118
auto arr = bsoncxx::v_noabi::builder::basic::array();
119+
const auto * array_type = typeid_cast<const DataTypeArray *>(type.get());
120+
121+
if (!array_type)
122+
throw Exception(ErrorCodes::TYPE_MISMATCH, "Expected Array type, got {}.", type->getPrettyName());
123+
124+
const auto & elem_type = array_type->getNestedType();
107125
for (const auto & elem : field.safeGet<Array>())
108-
arr.append(fieldAsBSONValue(elem, applyVisitor(FieldToDataType(), elem), is_oid));
126+
arr.append(fieldAsBSONValue(elem, elem_type, is_oid));
109127
return arr.view();
110128
}
111129
default:

0 commit comments

Comments
 (0)