ExecExprVisitor.cpp 12.4 KB
Newer Older
F
FluorineDog 已提交
1 2 3 4 5 6 7 8 9 10 11
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License

N
neza2017 已提交
12
#include <optional>
F
FluorineDog 已提交
13 14 15
#include <boost/dynamic_bitset.hpp>
#include <utility>
#include <deque>
16
#include "segcore/SegmentGrowingImpl.h"
G
GuoRentong 已提交
17
#include "query/ExprImpl.h"
N
neza2017 已提交
18 19 20 21 22 23 24 25 26
#include "query/generated/ExecExprVisitor.h"

namespace milvus::query {
#if 1
// THIS CONTAINS EXTRA BODY FOR VISITOR
// WILL BE USED BY GENERATOR
namespace impl {
class ExecExprVisitor : ExprVisitor {
 public:
27
    using RetType = std::deque<boost::dynamic_bitset<>>;
F
FluorineDog 已提交
28
    ExecExprVisitor(const segcore::SegmentInternalInterface& segment, int64_t row_count)
29
        : segment_(segment), row_count_(row_count) {
N
neza2017 已提交
30 31 32 33 34 35 36 37 38 39 40
    }
    RetType
    call_child(Expr& expr) {
        Assert(!ret_.has_value());
        expr.accept(*this);
        Assert(ret_.has_value());
        auto ret = std::move(ret_);
        ret_ = std::nullopt;
        return std::move(ret.value());
    }

G
GuoRentong 已提交
41
 public:
F
FluorineDog 已提交
42
    template <typename T, typename IndexFunc, typename ElementFunc>
G
GuoRentong 已提交
43
    auto
F
FluorineDog 已提交
44
    ExecRangeVisitorImpl(RangeExprImpl<T>& expr, IndexFunc func, ElementFunc element_func) -> RetType;
G
GuoRentong 已提交
45 46 47 48 49

    template <typename T>
    auto
    ExecRangeVisitorDispatcher(RangeExpr& expr_raw) -> RetType;

S
sunby 已提交
50 51 52 53
    template <typename T>
    auto
    ExecTermVisitorImpl(TermExpr& expr_raw) -> RetType;

N
neza2017 已提交
54
 private:
55 56
    const segcore::SegmentInternalInterface& segment_;
    int64_t row_count_;
N
neza2017 已提交
57 58 59 60 61 62 63
    std::optional<RetType> ret_;
};
}  // namespace impl
#endif

void
ExecExprVisitor::visit(BoolUnaryExpr& expr) {
F
FluorineDog 已提交
64 65 66 67 68 69 70 71
    using OpType = BoolUnaryExpr::OpType;
    auto vec = call_child(*expr.child_);
    RetType ret;
    for (int chunk_id = 0; chunk_id < vec.size(); ++chunk_id) {
        auto chunk = vec[chunk_id];
        switch (expr.op_type_) {
            case OpType::LogicalNot: {
                chunk.flip();
F
FluorineDog 已提交
72
                break;
F
FluorineDog 已提交
73 74 75 76 77 78 79 80
            }
            default: {
                PanicInfo("Invalid OpType");
            }
        }
        ret.emplace_back(std::move(chunk));
    }
    ret_ = std::move(ret);
N
neza2017 已提交
81 82 83 84
}

void
ExecExprVisitor::visit(BoolBinaryExpr& expr) {
F
FluorineDog 已提交
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
    using OpType = BoolBinaryExpr::OpType;
    RetType ret;
    auto left = call_child(*expr.left_);
    auto right = call_child(*expr.right_);
    Assert(left.size() == right.size());

    for (int chunk_id = 0; chunk_id < left.size(); ++chunk_id) {
        boost::dynamic_bitset<> chunk_res;
        auto left_chunk = std::move(left[chunk_id]);
        auto right_chunk = std::move(right[chunk_id]);
        chunk_res = std::move(left_chunk);
        switch (expr.op_type_) {
            case OpType::LogicalAnd: {
                chunk_res &= right_chunk;
                break;
            }
            case OpType::LogicalOr: {
                chunk_res |= right_chunk;
                break;
            }
            case OpType::LogicalXor: {
                chunk_res ^= right_chunk;
                break;
            }
            case OpType::LogicalMinus: {
                chunk_res -= right_chunk;
                break;
            }
        }
        ret.emplace_back(std::move(chunk_res));
    }
    ret_ = std::move(ret);
N
neza2017 已提交
117 118
}

F
FluorineDog 已提交
119
template <typename T, typename IndexFunc, typename ElementFunc>
G
GuoRentong 已提交
120
auto
F
FluorineDog 已提交
121 122
ExecExprVisitor::ExecRangeVisitorImpl(RangeExprImpl<T>& expr, IndexFunc index_func, ElementFunc element_func)
    -> RetType {
G
GuoRentong 已提交
123
    auto& schema = segment_.get_schema();
G
GuoRentong 已提交
124
    auto field_offset = expr.field_offset_;
G
GuoRentong 已提交
125
    auto& field_meta = schema[field_offset];
B
BossZou 已提交
126 127 128
    auto indexing_barrier = segment_.num_chunk_index(field_offset);
    auto size_per_chunk = segment_.size_per_chunk();
    auto num_chunk = upper_div(row_count_, size_per_chunk);
129 130 131
    RetType results;

    using Index = knowhere::scalar::StructuredIndex<T>;
F
FluorineDog 已提交
132
    for (auto chunk_id = 0; chunk_id < indexing_barrier; ++chunk_id) {
133 134 135 136
        const Index& indexing = segment_.chunk_scalar_index<T>(field_offset, chunk_id);
        // NOTE: knowhere is not const-ready
        // This is a dirty workaround
        auto data = index_func(const_cast<Index*>(&indexing));
B
BossZou 已提交
137
        Assert(data->size() == size_per_chunk);
138
        results.emplace_back(std::move(*data));
F
FluorineDog 已提交
139 140
    }

141
    for (auto chunk_id = indexing_barrier; chunk_id < num_chunk; ++chunk_id) {
B
BossZou 已提交
142 143
        boost::dynamic_bitset<> result(size_per_chunk);
        result.resize(size_per_chunk);
144
        auto chunk = segment_.chunk_data<T>(field_offset, chunk_id);
G
GuoRentong 已提交
145
        const T* data = chunk.data();
B
BossZou 已提交
146
        for (int index = 0; index < size_per_chunk; ++index) {
F
FluorineDog 已提交
147
            result[index] = element_func(data[index]);
G
GuoRentong 已提交
148
        }
B
BossZou 已提交
149
        Assert(result.size() == size_per_chunk);
150
        results.emplace_back(std::move(result));
G
GuoRentong 已提交
151 152 153 154 155 156 157 158 159 160 161 162
    }
    return results;
}
#pragma clang diagnostic push
#pragma ide diagnostic ignored "Simplify"
template <typename T>
auto
ExecExprVisitor::ExecRangeVisitorDispatcher(RangeExpr& expr_raw) -> RetType {
    auto& expr = static_cast<RangeExprImpl<T>&>(expr_raw);
    auto conditions = expr.conditions_;
    std::sort(conditions.begin(), conditions.end());
    using OpType = RangeExpr::OpType;
F
FluorineDog 已提交
163 164
    using Index = knowhere::scalar::StructuredIndex<T>;
    using Operator = knowhere::scalar::OperatorType;
G
GuoRentong 已提交
165 166 167 168 169 170 171
    if (conditions.size() == 1) {
        auto cond = conditions[0];
        // auto [op, val] = cond; // strange bug on capture
        auto op = std::get<0>(cond);
        auto val = std::get<1>(cond);
        switch (op) {
            case OpType::Equal: {
F
FluorineDog 已提交
172
                auto index_func = [val](Index* index) { return index->In(1, &val); };
S
sunby 已提交
173
                return ExecRangeVisitorImpl(expr, index_func, [val](T x) { return (x == val); });
G
GuoRentong 已提交
174 175 176
            }

            case OpType::NotEqual: {
N
neza2017 已提交
177
                auto index_func = [val](Index* index) { return index->NotIn(1, &val); };
S
sunby 已提交
178
                return ExecRangeVisitorImpl(expr, index_func, [val](T x) { return (x != val); });
G
GuoRentong 已提交
179 180 181
            }

            case OpType::GreaterEqual: {
F
FluorineDog 已提交
182
                auto index_func = [val](Index* index) { return index->Range(val, Operator::GE); };
S
sunby 已提交
183
                return ExecRangeVisitorImpl(expr, index_func, [val](T x) { return (x >= val); });
G
GuoRentong 已提交
184 185 186
            }

            case OpType::GreaterThan: {
F
FluorineDog 已提交
187
                auto index_func = [val](Index* index) { return index->Range(val, Operator::GT); };
S
sunby 已提交
188
                return ExecRangeVisitorImpl(expr, index_func, [val](T x) { return (x > val); });
G
GuoRentong 已提交
189 190 191
            }

            case OpType::LessEqual: {
F
FluorineDog 已提交
192
                auto index_func = [val](Index* index) { return index->Range(val, Operator::LE); };
S
sunby 已提交
193
                return ExecRangeVisitorImpl(expr, index_func, [val](T x) { return (x <= val); });
G
GuoRentong 已提交
194 195 196
            }

            case OpType::LessThan: {
F
FluorineDog 已提交
197
                auto index_func = [val](Index* index) { return index->Range(val, Operator::LT); };
S
sunby 已提交
198
                return ExecRangeVisitorImpl(expr, index_func, [val](T x) { return (x < val); });
G
GuoRentong 已提交
199 200 201 202 203 204 205 206 207 208
            }
            default: {
                PanicInfo("unsupported range node");
            }
        }
    } else if (conditions.size() == 2) {
        OpType op1, op2;
        T val1, val2;
        std::tie(op1, val1) = conditions[0];
        std::tie(op2, val2) = conditions[1];
Z
zhenshan.cao 已提交
209 210 211 212 213 214 215 216
        // TODO: disable check?
        if (val1 > val2) {
            // Empty
            auto size_per_chunk = segment_.size_per_chunk();
            auto num_chunk = upper_div(row_count_, size_per_chunk);
            RetType ret(num_chunk, boost::dynamic_bitset<>(size_per_chunk));
            return ret;
        }
G
GuoRentong 已提交
217 218 219
        auto ops = std::make_tuple(op1, op2);
        if (false) {
        } else if (ops == std::make_tuple(OpType::GreaterThan, OpType::LessThan)) {
F
FluorineDog 已提交
220
            auto index_func = [val1, val2](Index* index) { return index->Range(val1, false, val2, false); };
S
sunby 已提交
221
            return ExecRangeVisitorImpl(expr, index_func, [val1, val2](T x) { return (val1 < x && x < val2); });
G
GuoRentong 已提交
222
        } else if (ops == std::make_tuple(OpType::GreaterThan, OpType::LessEqual)) {
F
FluorineDog 已提交
223
            auto index_func = [val1, val2](Index* index) { return index->Range(val1, false, val2, true); };
S
sunby 已提交
224
            return ExecRangeVisitorImpl(expr, index_func, [val1, val2](T x) { return (val1 < x && x <= val2); });
G
GuoRentong 已提交
225
        } else if (ops == std::make_tuple(OpType::GreaterEqual, OpType::LessThan)) {
F
FluorineDog 已提交
226
            auto index_func = [val1, val2](Index* index) { return index->Range(val1, true, val2, false); };
S
sunby 已提交
227
            return ExecRangeVisitorImpl(expr, index_func, [val1, val2](T x) { return (val1 <= x && x < val2); });
G
GuoRentong 已提交
228
        } else if (ops == std::make_tuple(OpType::GreaterEqual, OpType::LessEqual)) {
F
FluorineDog 已提交
229
            auto index_func = [val1, val2](Index* index) { return index->Range(val1, true, val2, true); };
S
sunby 已提交
230
            return ExecRangeVisitorImpl(expr, index_func, [val1, val2](T x) { return (val1 <= x && x <= val2); });
G
GuoRentong 已提交
231 232 233 234 235 236 237 238 239
        } else {
            PanicInfo("unsupported range node");
        }
    } else {
        PanicInfo("unsupported range node");
    }
}
#pragma clang diagnostic pop

N
neza2017 已提交
240 241
void
ExecExprVisitor::visit(RangeExpr& expr) {
G
GuoRentong 已提交
242
    auto& field_meta = segment_.get_schema()[expr.field_offset_];
G
GuoRentong 已提交
243 244 245
    Assert(expr.data_type_ == field_meta.get_data_type());
    RetType ret;
    switch (expr.data_type_) {
N
neza2017 已提交
246 247 248 249
        case DataType::BOOL: {
            ret = ExecRangeVisitorDispatcher<bool>(expr);
            break;
        }
G
GuoRentong 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        case DataType::INT8: {
            ret = ExecRangeVisitorDispatcher<int8_t>(expr);
            break;
        }
        case DataType::INT16: {
            ret = ExecRangeVisitorDispatcher<int16_t>(expr);
            break;
        }
        case DataType::INT32: {
            ret = ExecRangeVisitorDispatcher<int32_t>(expr);
            break;
        }
        case DataType::INT64: {
            ret = ExecRangeVisitorDispatcher<int64_t>(expr);
            break;
        }
        case DataType::FLOAT: {
            ret = ExecRangeVisitorDispatcher<float>(expr);
            break;
        }
        case DataType::DOUBLE: {
            ret = ExecRangeVisitorDispatcher<double>(expr);
            break;
        }
        default:
            PanicInfo("unsupported");
    }
    ret_ = std::move(ret);
N
neza2017 已提交
278 279
}

S
sunby 已提交
280 281 282 283 284
template <typename T>
auto
ExecExprVisitor::ExecTermVisitorImpl(TermExpr& expr_raw) -> RetType {
    auto& expr = static_cast<TermExprImpl<T>&>(expr_raw);
    auto& schema = segment_.get_schema();
G
GuoRentong 已提交
285 286

    auto field_offset = expr_raw.field_offset_;
S
sunby 已提交
287
    auto& field_meta = schema[field_offset];
B
BossZou 已提交
288 289
    auto size_per_chunk = segment_.size_per_chunk();
    auto num_chunk = upper_div(row_count_, size_per_chunk);
S
sunby 已提交
290 291
    RetType bitsets;
    for (int64_t chunk_id = 0; chunk_id < num_chunk; ++chunk_id) {
292
        Span<T> chunk = segment_.chunk_data<T>(field_offset, chunk_id);
S
sunby 已提交
293

B
BossZou 已提交
294
        auto size = chunk_id == num_chunk - 1 ? row_count_ - chunk_id * size_per_chunk : size_per_chunk;
S
sunby 已提交
295

B
BossZou 已提交
296
        boost::dynamic_bitset<> bitset(size_per_chunk);
S
sunby 已提交
297
        for (int i = 0; i < size; ++i) {
298
            auto value = chunk.data()[i];
S
sunby 已提交
299 300 301 302 303 304 305 306 307 308
            bool is_in = std::binary_search(expr.terms_.begin(), expr.terms_.end(), value);
            bitset[i] = is_in;
        }
        bitsets.emplace_back(std::move(bitset));
    }
    return bitsets;
}

void
ExecExprVisitor::visit(TermExpr& expr) {
G
GuoRentong 已提交
309
    auto& field_meta = segment_.get_schema()[expr.field_offset_];
S
sunby 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    Assert(expr.data_type_ == field_meta.get_data_type());
    RetType ret;
    switch (expr.data_type_) {
        case DataType::BOOL: {
            ret = ExecTermVisitorImpl<bool>(expr);
            break;
        }
        case DataType::INT8: {
            ret = ExecTermVisitorImpl<int8_t>(expr);
            break;
        }
        case DataType::INT16: {
            ret = ExecTermVisitorImpl<int16_t>(expr);
            break;
        }
        case DataType::INT32: {
            ret = ExecTermVisitorImpl<int32_t>(expr);
            break;
        }
        case DataType::INT64: {
            ret = ExecTermVisitorImpl<int64_t>(expr);
            break;
        }
        case DataType::FLOAT: {
            ret = ExecTermVisitorImpl<float>(expr);
            break;
        }
        case DataType::DOUBLE: {
            ret = ExecTermVisitorImpl<double>(expr);
            break;
        }
        default:
            PanicInfo("unsupported");
    }
    ret_ = std::move(ret);
}
N
neza2017 已提交
346
}  // namespace milvus::query