Initial commit

This commit is contained in:
sanyi
2025-12-30 11:02:17 +08:00
commit 6524fabd80
841 changed files with 293919 additions and 0 deletions
+89
View File
@@ -0,0 +1,89 @@
// Copyright 2025-present the zvec project
//
// 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.
#pragma once
#include <memory>
namespace zvec::sqlengine {
template <typename NodeOp, typename Node>
class Generic_Node {
public:
using Ptr = std::shared_ptr<Node>;
Generic_Node(NodeOp m_op);
virtual ~Generic_Node() = default;
void set_left(Ptr m_left);
void set_right(Ptr m_right);
const Ptr &left() const {
return left_;
}
const Ptr &right() const {
return right_;
}
Node *left_node() const {
return left_.get();
}
Node *right_node() const {
return right_.get();
}
void set_parent(Generic_Node *m_parent);
Generic_Node *parent();
virtual NodeOp op() const {
return op_;
}
virtual void set_op(NodeOp value) {
op_ = value;
}
virtual std::string text() const = 0;
protected:
NodeOp op_;
Ptr left_{nullptr};
Ptr right_{nullptr};
Generic_Node *parent_{nullptr};
};
template <typename NodeOp, typename Node>
Generic_Node<NodeOp, Node>::Generic_Node(NodeOp m_op) {
op_ = m_op;
}
template <typename NodeOp, typename Node>
void Generic_Node<NodeOp, Node>::set_left(Ptr m_left) {
left_ = std::move(m_left);
if (left_ != nullptr) {
left_->set_parent(this);
}
}
template <typename NodeOp, typename Node>
void Generic_Node<NodeOp, Node>::set_right(Ptr m_right) {
right_ = std::move(m_right);
if (right_ != nullptr) {
right_->set_parent(this);
}
}
template <typename NodeOp, typename Node>
void Generic_Node<NodeOp, Node>::set_parent(Generic_Node<NodeOp, Node> *value) {
this->parent_ = value;
}
template <typename NodeOp, typename Node>
Generic_Node<NodeOp, Node> *Generic_Node<NodeOp, Node>::parent() {
return parent_;
}
} // namespace zvec::sqlengine
+46
View File
@@ -0,0 +1,46 @@
// Copyright 2025-present the zvec project
//
// 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.
#pragma once
#include <memory>
#include <string>
#include <ailego/utility/string_helper.h>
#include "db/index/common/doc.h"
namespace zvec::sqlengine {
struct GroupBy {
using Ptr = std::shared_ptr<GroupBy>;
GroupBy() = default;
GroupBy(std::string group_by_field, uint32_t group_topk, uint32_t group_count)
: group_by_field(std::move(group_by_field)),
group_topk(group_topk),
group_count(group_count) {}
std::string to_string() {
return ailego::StringHelper::Concat("field[", group_by_field, "] topk[",
group_topk, "] count[", group_count,
"]");
}
std::string group_by_field;
uint32_t group_topk{0};
uint32_t group_count{0};
};
} // namespace zvec::sqlengine
+91
View File
@@ -0,0 +1,91 @@
// Copyright 2025-present the zvec project
//
// 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.
#include "util.h"
#include <sys/time.h>
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <ailego/encoding/json.h>
#include <ailego/utility/string_helper.h>
#include <arrow/type.h>
#include <arrow/type_fwd.h>
namespace zvec::sqlengine {
// std::string
// Util::trim(const std::string str, char c) {
// std::string tmp = str.substr(str.find_first_not_of(c));
// return tmp.substr(0, tmp.find_last_not_of(c) + 1);
//}
// above implementation will trim more than one c at front or rear. This is not
// expected.
// below implementation remove both side only one matched char, strictly.
// str is supposed to match on both side at same time. remove both side one byte
// each as c = 0
std::string Util::trim_one_both_side(const std::string &str, unsigned char c) {
int len = str.length();
if (len < 2) {
return str;
}
if (str.at(0) == c && str.at(len - 1) == c) {
return str.substr(1, len - 2);
}
return str;
}
void Util::string_replace(const std::string &src, const std::string &dst,
std::string *str) {
std::string::size_type pos = 0;
std::string::size_type srclen = src.size();
std::string::size_type dstlen = dst.size();
while ((pos = str->find(src, pos)) != std::string::npos) {
str->replace(pos, srclen, dst);
pos += dstlen;
}
return;
}
// normalize sql for parse result after parse
std::string Util::normalize(const std::string &sql) {
std::string new_sql = sql;
// rule 1. replace \" with "
Util::string_replace("\\\"", "\"", &new_sql);
// rule 2. replace \' with ''
Util::string_replace("\\\'", "\'", &new_sql);
return new_sql;
}
std::shared_ptr<arrow::Schema> Util::append_field(
const arrow::Schema &schema, const std::string &name,
std::shared_ptr<arrow::DataType> type) {
auto res = schema.AddField(schema.num_fields(), arrow::field(name, type));
return res.MoveValueUnsafe();
}
std::shared_ptr<arrow::DataType> Util::sparse_type() {
return arrow::struct_(arrow::FieldVector{
arrow::field("index", arrow::binary()),
arrow::field("value", arrow::binary()),
});
}
} // namespace zvec::sqlengine
+54
View File
@@ -0,0 +1,54 @@
// Copyright 2025-present the zvec project
//
// 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.
#include <memory>
#include <string>
#include <arrow/api.h>
#include <arrow/record_batch.h>
namespace zvec::sqlengine {
static const constexpr char *kFieldScore = "_zvec_score";
static const constexpr char *kFieldVector = "_zvec_vector";
static const constexpr char *kFieldSparseIndices = "_zvec_sindices";
static const constexpr char *kFieldSparseValues = "_zvec_svalues";
static const constexpr char *kFieldIsValid = "_zvec_is_valid";
static const constexpr char *kFieldGroupId = "_zvec_group_id";
static const inline std::string kCheckNotFiltered = "check_not_filtered";
static const inline std::string kFetchVector = "fetch_vector";
static const inline std::string kFetchSparseVector = "fetch_sparse_vector";
static const inline std::string kContainAll = "contain_all";
static const inline std::string kContainAny = "contain_any";
static const inline std::string kFuncArrayLength = "array_length";
#define enum_to_string(x) #x
class Util {
public:
static std::string trim_one_both_side(const std::string &str,
unsigned char c);
static void string_replace(const std::string &strsrc,
const std::string &strdst, std::string *str);
static std::string normalize(const std::string &sql);
static std::shared_ptr<arrow::Schema> append_field(
const arrow::Schema &schema, const std::string &name,
std::shared_ptr<arrow::DataType> type);
static std::shared_ptr<arrow::DataType> sparse_type();
};
} // namespace zvec::sqlengine