|
8 | 8 | #include <Common/JSONBuilder.h> |
9 | 9 | #include <IO/ReadBufferFromMemory.h> |
10 | 10 | #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> |
13 | 14 |
|
14 | 15 | #include <mongocxx/client.hpp> |
15 | 16 |
|
@@ -97,15 +98,32 @@ static bsoncxx::types::bson_value::value fieldAsBSONValue(const Field & field, c |
97 | 98 | case TypeIndex::Tuple: |
98 | 99 | { |
99 | 100 | 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)); |
102 | 114 | return arr.view(); |
103 | 115 | } |
104 | 116 | case TypeIndex::Array: |
105 | 117 | { |
106 | 118 | 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(); |
107 | 125 | 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)); |
109 | 127 | return arr.view(); |
110 | 128 | } |
111 | 129 | default: |
|
0 commit comments