aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/MeiliSearch/SourceMeiliSearch.cpp
blob: f567af23a12ca10b8e8a1054b0dbc924b74f87ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <Columns/ColumnString.h>
#include <Columns/ColumnVector.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/IColumn.h>
#include <Core/ExternalResultDescription.h>
#include <Core/Field.h>
#include <Core/Types.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/Serializations/ISerialization.h>
#include <IO/Operators.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
#include <Storages/MeiliSearch/SourceMeiliSearch.h>
#include <base/JSON.h>
#include <base/range.h>
#include <base/types.h>
#include <magic_enum.hpp>
#include <Common/Exception.h>
#include <Common/quoteString.h>
#include "Interpreters/ProcessList.h"

namespace DB
{
namespace ErrorCodes
{
    extern const int MEILISEARCH_EXCEPTION;
    extern const int UNSUPPORTED_MEILISEARCH_TYPE;
    extern const int MEILISEARCH_MISSING_SOME_COLUMNS;
}

String MeiliSearchSource::doubleQuoteIfNeed(const String & param) const
{
    if (route == QueryRoute::search)
        return doubleQuoteString(param);
    return param;
}

String MeiliSearchSource::constructAttributesToRetrieve() const
{
    WriteBufferFromOwnString columns_to_get;

    if (route == QueryRoute::search)
        columns_to_get << "[";

    auto it = description.sample_block.begin();
    while (it != description.sample_block.end())
    {
        columns_to_get << doubleQuoteIfNeed(it->name);
        ++it;
        if (it != description.sample_block.end())
            columns_to_get << ",";
    }

    if (route == QueryRoute::search)
        columns_to_get << "]";

    return columns_to_get.str();
}

MeiliSearchSource::MeiliSearchSource(
    const MeiliSearchConfiguration & config,
    const Block & sample_block,
    UInt64 max_block_size_,
    QueryRoute route_,
    std::unordered_map<String, String> query_params_)
    : ISource(sample_block.cloneEmpty())
    , connection(config)
    , max_block_size{max_block_size_}
    , route{route_}
    , query_params{query_params_}
    , offset{0}
{
    description.init(sample_block);

    auto attributes_to_retrieve = constructAttributesToRetrieve();

    query_params[doubleQuoteIfNeed("attributesToRetrieve")] = attributes_to_retrieve;
    query_params[doubleQuoteIfNeed("limit")] = std::to_string(max_block_size);
}


MeiliSearchSource::~MeiliSearchSource() = default;

Field getField(JSON value, DataTypePtr type_ptr)
{
    TypeIndex type_id = type_ptr->getTypeId();

    if (type_id == TypeIndex::UInt64 || type_id == TypeIndex::UInt32 || type_id == TypeIndex::UInt16 || type_id == TypeIndex::UInt8)
    {
        if (value.isBool())
            return value.getBool();
        else
            return value.get<UInt64>();
    }
    else if (type_id == TypeIndex::Int64 || type_id == TypeIndex::Int32 || type_id == TypeIndex::Int16 || type_id == TypeIndex::Int8)
    {
        return value.get<Int64>();
    }
    else if (type_id == TypeIndex::String)
    {
        if (value.isObject())
            return value.toString();
        else
            return value.get<String>();
    }
    else if (type_id == TypeIndex::Float64 || type_id == TypeIndex::Float32)
    {
        return value.get<Float64>();
    }
    else if (type_id == TypeIndex::Date)
    {
        return UInt16{LocalDate{String(value.toString())}.getDayNum()};
    }
    else if (type_id == TypeIndex::Date32)
    {
        return Int32{LocalDate{String(value.toString())}.getExtenedDayNum()};
    }
    else if (type_id == TypeIndex::DateTime)
    {
        ReadBufferFromString in(value.toString());
        time_t time = 0;
        readDateTimeText(time, in, assert_cast<const DataTypeDateTime *>(type_ptr.get())->getTimeZone());
        if (time < 0)
            time = 0;
        return time;
    }
    else if (type_id == TypeIndex::Nullable)
    {
        if (value.isNull())
            return Null();

        const auto * null_type = typeid_cast<const DataTypeNullable *>(type_ptr.get());
        DataTypePtr nested = null_type->getNestedType();

        return getField(value, nested);
    }
    else if (type_id == TypeIndex::Array)
    {
        const auto * array_type = typeid_cast<const DataTypeArray *>(type_ptr.get());
        DataTypePtr nested = array_type->getNestedType();

        Array array;
        for (const auto el : value)
            array.push_back(getField(el, nested));

        return array;
    }
    else
    {
        const std::string_view type_name = magic_enum::enum_name(type_id);
        throw Exception(ErrorCodes::UNSUPPORTED_MEILISEARCH_TYPE, "MeiliSearch storage doesn't support type: {}", type_name);
    }
}

void insertWithTypeId(MutableColumnPtr & column, JSON value, DataTypePtr type_ptr)
{
    column->insert(getField(value, type_ptr));
}

size_t MeiliSearchSource::parseJSON(MutableColumns & columns, const JSON & jres) const
{
    size_t cnt_match = 0;

    for (const auto json : jres)
    {
        ++cnt_match;
        size_t cnt_fields = 0;
        for (const auto kv_pair : json)
        {
            ++cnt_fields;
            const auto & name = kv_pair.getName();
            size_t pos = description.sample_block.getPositionByName(name);
            MutableColumnPtr & col = columns[pos];
            DataTypePtr type_ptr = description.sample_block.getByPosition(pos).type;
            insertWithTypeId(col, kv_pair.getValue(), type_ptr);
        }
        if (cnt_fields != columns.size())
            throw Exception(
                ErrorCodes::MEILISEARCH_MISSING_SOME_COLUMNS, "Some columns were not found in the table, json = {}", json.toString());
    }
    return cnt_match;
}

Chunk MeiliSearchSource::generate()
{
    if (all_read)
        return {};

    MutableColumns columns = description.sample_block.cloneEmptyColumns();
    query_params[doubleQuoteIfNeed("offset")] = std::to_string(offset);

    size_t cnt_match = 0;

    if (route == QueryRoute::search)
    {
        auto response = connection.searchQuery(query_params);
        JSON jres = JSON(response).begin();
        if (jres.getName() == "message")
            throw Exception::createRuntime(ErrorCodes::MEILISEARCH_EXCEPTION, jres.toString());

        cnt_match = parseJSON(columns, jres.getValue());
    }
    else
    {
        auto response = connection.getDocumentsQuery(query_params);
        JSON jres(response);
        if (!jres.isArray())
        {
            auto error = jres.getWithDefault<String>("message");
            throw Exception::createRuntime(ErrorCodes::MEILISEARCH_EXCEPTION, error);
        }
        cnt_match = parseJSON(columns, jres);
    }

    offset += cnt_match;

    if (cnt_match == 0)
    {
        all_read = true;
        return {};
    }

    return Chunk(std::move(columns), cnt_match);
}


}