mirror of
https://github.com/hanzo-ds/cpp.git
synced 2026-10-30 20:31:26 +00:00
Pure rename applied on top of upstream tag v2.6.2 (8d2415d6), not HEAD.
The fork was cut at 737145d = v2.6.2-57, which still declared version 2.6.2
in version.h; renaming there would have shipped 57 unreleased commits wearing
a 2.6.2 label. Reset to the tag first so this commit changes names only.
clickhouse/ -> datastore/ (public include root)
namespace clickhouse -> namespace datastore
CLICKHOUSE_CPP_VERSION -> DATASTORE_CPP_VERSION
clickhouse-cpp-lib/-ut -> datastore-cpp-lib/-ut (CMake, Bazel)
CH_MAP_BOOL_TO_UINT8 -> DS_MAP_BOOL_TO_UINT8
CLICKHOUSE_* -> DATASTORE_* (test env vars)
doc links -> docs.hanzo.ai/datastore
This client speaks the NATIVE binary protocol only - there is no HTTP path
and no X-ClickHouse-* header anywhere in it, so the header rename that the
HTTP clients required does not apply and nothing here was broken.
The one string that reaches the wire is CLIENT_NAME, sent by both SendHello
and SendQuery's ClientInfo from a single macro, and it lands in
system.query_log.client_name. Both sites still read the one macro, so they
stay consistent and the server's Hello/Query consistency check is satisfied.
Old rows keep the old name; that is a reporting discontinuity, not a
migration, since nothing keys on it.
CI is repointed at our own server: ghcr.io/hanzoai/datastore:26.2.3.2, the
image production runs. Its entrypoint honours DATASTORE_SKIP_USER_SETUP, not
the CLICKHOUSE_ spelling the compose file used, and its config lives in
/etc/hanzo-datastore-server. ci/docker-compose/config.xml is now layered in
as a config.d drop-in so the image keeps its own white-labelling. That file
and users.xml deliberately keep a <clickhouse> root: it must match the roots
of the drop-ins the image ships, and a mismatch fails silently - the server
logs "Merging bypassed" and starts with every drop-in discarded.
The remote-TLS suites used to default to the third-party host
play.clickhouse.com. They now read DATASTORE_SECURE*_HOST and skip when it is
unset rather than point at a host we do not run.
Dropped as upstream infrastructure with no meaning here: AI_POLICY.md
(upstream's policy about their codebase), .travis.yml (dead CI referencing a
third-party apt repo), cross-repo-bug-relay.yml (relays our issues into an
upstream triage repo). LICENSE keeps its copyright notice, as Apache-2.0
requires.
Verified: cmake configure + 115-target build clean; the full suite is green
against ghcr.io/hanzoai/datastore:26.2.3.2 (894 passed, 0 failed); the
pristine v2.6.2 build run against that same server passes the identical set,
and both logged the same 3914 queries under their respective client names.
258 lines
8.8 KiB
C++
258 lines
8.8 KiB
C++
#pragma once
|
|
|
|
#include "../base/projected_iterator.h"
|
|
#include "array.h"
|
|
#include "column.h"
|
|
#include "tuple.h"
|
|
|
|
#include <functional>
|
|
#include <map>
|
|
|
|
namespace datastore {
|
|
|
|
template <typename KeyColumnType, typename ValueColumnType>
|
|
class ColumnMapT;
|
|
|
|
/**
|
|
* Represents column of Map(K, V).
|
|
*/
|
|
class ColumnMap : public Column {
|
|
public:
|
|
/** Create a map of given type, with actual values and offsets.
|
|
*
|
|
* Both `data` and `offsets` are used (and modified) internally bye ColumnArray.
|
|
* Users are strongly advised against modifying contents of `data` or `offsets` afterwards.
|
|
*/
|
|
explicit ColumnMap(ColumnRef data);
|
|
|
|
/// Increase the capacity of the column for large block insertion.
|
|
void Reserve(size_t new_cap) override;
|
|
|
|
/// Appends content of given column to the end of current one.
|
|
void Append(ColumnRef column) override;
|
|
|
|
/// Loads column prefix from input stream.
|
|
bool LoadPrefix(InputStream* input, size_t rows) override;
|
|
|
|
/// Loads column data from input stream.
|
|
bool LoadBody(InputStream* input, size_t rows) override;
|
|
|
|
/// Saves column prefix to output stream.
|
|
void SavePrefix(OutputStream* output) override;
|
|
|
|
/// Saves column data to output stream.
|
|
void SaveBody(OutputStream* output) override;
|
|
|
|
/// Clear column data .
|
|
void Clear() override;
|
|
|
|
/// Returns count of rows in the column.
|
|
size_t Size() const override;
|
|
|
|
/// Makes slice of the current column.
|
|
ColumnRef Slice(size_t, size_t) const override;
|
|
ColumnRef CloneEmpty() const override;
|
|
void Swap(Column&) override;
|
|
|
|
/// Converts map at pos n to column.
|
|
/// Type of row is tuple {key, value}.
|
|
ColumnRef GetAsColumn(size_t n) const;
|
|
|
|
protected:
|
|
template <typename K, typename V>
|
|
friend class ColumnMapT;
|
|
|
|
ColumnMap(ColumnMap&& map);
|
|
|
|
private:
|
|
std::shared_ptr<ColumnArray> data_;
|
|
};
|
|
|
|
template <typename K, typename V>
|
|
class ColumnMapT : public ColumnMap {
|
|
public:
|
|
using KeyColumnType = K;
|
|
using ValueColumnType = V;
|
|
using Key = std::decay_t<decltype(std::declval<KeyColumnType>().At(0))>;
|
|
using Value = std::decay_t<decltype(std::declval<ValueColumnType>().At(0))>;
|
|
using TupleColumnType = ColumnTupleT<KeyColumnType, ValueColumnType>;
|
|
using ArrayColumnType = ColumnArrayT<TupleColumnType>;
|
|
|
|
ColumnMapT(ColumnRef data)
|
|
: ColumnMap(data), typed_data_(data->AsStrict<ColumnArrayT<TupleColumnType>>()) {}
|
|
|
|
ColumnMapT(std::shared_ptr<KeyColumnType> keys, std::shared_ptr<ValueColumnType> values)
|
|
: ColumnMap(std::make_shared<ArrayColumnType>(std::make_shared<TupleColumnType>(
|
|
std::make_tuple(std::move(keys), std::move(values))))),
|
|
typed_data_(data_->template As<ArrayColumnType>()) {}
|
|
|
|
ColumnRef Slice(size_t begin, size_t len) const override {
|
|
return std::make_shared<ColumnMapT<K, V>>(typed_data_->Slice(begin, len));
|
|
}
|
|
|
|
ColumnRef CloneEmpty() const override {
|
|
return std::make_shared<ColumnMapT<K, V>>(typed_data_->CloneEmpty());
|
|
}
|
|
|
|
void Swap(Column& other) override {
|
|
auto& col = dynamic_cast<ColumnMapT<K, V>&>(other);
|
|
col.typed_data_.swap(typed_data_);
|
|
ColumnMap::Swap(other);
|
|
}
|
|
|
|
/// A single (row) value of the Map-column i.e. read-only map.
|
|
/// It has a linear time complexity to access items
|
|
/// Because data base type has same structure
|
|
/// "This lookup works now with a linear complexity."
|
|
/// https://docs.hanzo.ai/datastore/sql-reference/data-types/map
|
|
/// Convert it to a suitable container required to access more than one element
|
|
|
|
class MapValueView {
|
|
const typename ArrayColumnType::ArrayValueView data_;
|
|
|
|
public:
|
|
using ValueType = std::pair<Key, Value>;
|
|
|
|
MapValueView(typename ArrayColumnType::ArrayValueView data) : data_(std::move(data)) {}
|
|
|
|
inline auto operator[](const Key& key) const { return (*Find(key)).second; }
|
|
|
|
inline auto At(const Key& key) const {
|
|
auto it = Find(key);
|
|
if (it == end()) throw ValidationError("ColumnMap value key not found");
|
|
return (*it).second;
|
|
}
|
|
|
|
class Iterator {
|
|
typename ArrayColumnType::ArrayValueView::Iterator data_iterator_;
|
|
|
|
public:
|
|
Iterator() = default;
|
|
|
|
Iterator(typename ArrayColumnType::ArrayValueView::Iterator data_iterator)
|
|
: data_iterator_(data_iterator) {}
|
|
|
|
using ValueType = std::pair<Key, Value>;
|
|
using difference_type = size_t;
|
|
using value_type = ValueType;
|
|
using pointer = void;
|
|
using reference = ValueType&;
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
inline auto operator*() const {
|
|
auto tuple = *data_iterator_;
|
|
return ValueType{std::get<0>(tuple), std::get<1>(tuple)};
|
|
}
|
|
|
|
inline Iterator& operator++() {
|
|
++data_iterator_;
|
|
return *this;
|
|
}
|
|
|
|
inline bool operator==(const Iterator& other) const {
|
|
return this->data_iterator_ == other.data_iterator_;
|
|
}
|
|
|
|
inline bool operator!=(const Iterator& other) const { return !(*this == other); }
|
|
};
|
|
|
|
// minimalistic stl-like container interface, hence the lowercase
|
|
inline Iterator begin() const { return Iterator{data_.begin()}; }
|
|
|
|
inline Iterator cbegin() const { return Iterator{data_.cbegin()}; }
|
|
|
|
inline Iterator end() const { return Iterator{data_.end()}; }
|
|
|
|
inline Iterator cend() const { return Iterator{data_.cend()}; }
|
|
|
|
inline size_t size() const { return data_.size(); }
|
|
|
|
// It is ugly to have both size() and Size(), but it is for compatitability with both STL
|
|
// and rest of the datastore-cpp.
|
|
inline size_t Size() const { return data_.Size(); }
|
|
|
|
inline size_t Count(const Key& key) const {
|
|
size_t result = 0;
|
|
for (auto item : data_) {
|
|
if (std::get<0>(item) == key) {
|
|
++result;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
inline Iterator Find(const Key& key) const {
|
|
for (auto it = data_.begin(); it != data_.end(); ++it) {
|
|
if (std::get<0>(*it) == key) {
|
|
return Iterator{it};
|
|
}
|
|
}
|
|
return end();
|
|
}
|
|
|
|
inline bool operator==(const MapValueView& other) const {
|
|
if (size() != other.size()) {
|
|
return false;
|
|
}
|
|
const auto make_index = [](const auto& data) {
|
|
std::vector<size_t> result{data.Size()};
|
|
std::generate(result.begin(), result.end(), [i = 0] () mutable {return i++;});
|
|
std::sort(result.begin(), result.end(), [&data](size_t l, size_t r) {return data[l] < data[r];});
|
|
return result;
|
|
};
|
|
const auto index = make_index(data_);
|
|
for (const auto& val : other.data_) {
|
|
if (!std::binary_search(index.begin(), index.end(), val,
|
|
[&data = data_](const auto& l, size_t r) {return l < data[r];})) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
inline bool operator!=(const MapValueView& other) const { return !(*this == other); }
|
|
};
|
|
|
|
inline auto At(size_t index) const { return MapValueView{typed_data_->At(index)}; }
|
|
|
|
inline auto operator[](size_t index) const { return At(index); }
|
|
|
|
using ColumnMap::Append;
|
|
|
|
inline void Append(const MapValueView& value) { typed_data_->Append(value.data_); }
|
|
|
|
inline void Append(const std::vector<std::tuple<Key, Value>>& tuples) {
|
|
typed_data_->Append(tuples.begin(), tuples.end());
|
|
}
|
|
|
|
template <typename T>
|
|
inline void Append(const T& value) {
|
|
using BaseIter = decltype(value.begin());
|
|
using KeyOfT = decltype(std::declval<BaseIter>()->first);
|
|
using ValOfT = decltype(std::declval<BaseIter>()->second);
|
|
using Functor = std::function<std::tuple<KeyOfT, ValOfT>(const BaseIter&)>;
|
|
using Iterator = ProjectedIterator<Functor, BaseIter>;
|
|
|
|
Functor functor = [](const BaseIter& i) {
|
|
return std::make_tuple(std::cref(i->first), std::cref(i->second));
|
|
};
|
|
|
|
typed_data_->Append(Iterator{value.begin(), functor}, Iterator{value.end(), functor});
|
|
}
|
|
|
|
static auto Wrap(ColumnMap&& col) {
|
|
auto data = ArrayColumnType::Wrap(std::move(col.data_));
|
|
return std::make_shared<ColumnMapT<K, V>>(std::move(data));
|
|
}
|
|
|
|
static auto Wrap(Column&& col) { return Wrap(std::move(dynamic_cast<ColumnMap&&>(col))); }
|
|
|
|
// Helper to simplify integration with other APIs
|
|
static auto Wrap(ColumnRef&& col) { return Wrap(std::move(*col->AsStrict<ColumnMap>())); }
|
|
|
|
private:
|
|
std::shared_ptr<ArrayColumnType> typed_data_;
|
|
};
|
|
|
|
} // namespace datastore
|