-
Notifications
You must be signed in to change notification settings - Fork 199
Experimental support for the JSON data type #490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
slabko
wants to merge
1
commit into
master
Choose a base branch
from
json-string
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #include "json.h" | ||
| #include "../base/wire_format.h" | ||
|
|
||
| namespace clickhouse { | ||
|
|
||
| enum class JSONSerializationVersion : uint64_t { | ||
| // String is the only currently supported serialization of JSON. | ||
| // it should be enabled with output_format_native_write_json_as_string=1 | ||
| String = 1, | ||
| }; | ||
|
|
||
| ColumnJSON::ColumnJSON() | ||
| : Column(Type::CreateJSON()) | ||
| , data_(std::make_shared<ColumnString>()) | ||
| {} | ||
|
|
||
| ColumnJSON::ColumnJSON(std::vector<std::string> data) | ||
| : Column(Type::CreateJSON()) | ||
| , data_(std::make_shared<ColumnString>(std::move(data))) | ||
| {} | ||
|
|
||
| void ColumnJSON::Append(std::string_view str) { | ||
| data_->Append(str); | ||
| } | ||
|
|
||
| void ColumnJSON::Append(const char* str) { | ||
| data_->Append(str); | ||
| } | ||
| void ColumnJSON::Append(std::string&& str) { | ||
| data_->Append(std::move(str)); | ||
| } | ||
|
|
||
| std::string_view ColumnJSON::At(size_t n) const { | ||
| return data_->At(n); | ||
| } | ||
|
|
||
| void ColumnJSON::Append(ColumnRef column) { | ||
| if (auto col = column->As<ColumnJSON>()) { | ||
| data_->Append(col->data_); | ||
| } | ||
| } | ||
|
|
||
| void ColumnJSON::Reserve(size_t new_cap) { | ||
| data_->Reserve(new_cap); | ||
| } | ||
|
|
||
| bool ColumnJSON::LoadPrefix(InputStream* input, size_t) { | ||
| uint64_t v; | ||
| if (!WireFormat::ReadFixed(*input, &v)) { | ||
| return false; | ||
| } | ||
| if (v != static_cast<uint64_t>(JSONSerializationVersion::String)) { | ||
| // Hard stop: the library can only parse JSON when `output_format_native_write_json_as_string` is enabled. | ||
| // Further processing is meaningless after this error and the user must be notified immediately. | ||
| throw ProtocolError("Unsupported JSON serialization version. " | ||
| "Make sure output_format_native_write_json_as_string=1 is set."); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool ColumnJSON::LoadBody(InputStream* input, size_t rows) { | ||
| return data_->LoadBody(input, rows); | ||
| } | ||
|
|
||
| void ColumnJSON::SavePrefix(OutputStream* output) { | ||
| WireFormat::WriteFixed(*output, static_cast<uint64_t>(JSONSerializationVersion::String)); | ||
| } | ||
|
|
||
| void ColumnJSON::SaveBody(OutputStream* output) { | ||
| data_->SaveBody(output); | ||
| } | ||
|
|
||
| void ColumnJSON::Clear() { | ||
| data_->Clear(); | ||
| } | ||
|
|
||
| size_t ColumnJSON::Size() const { | ||
| return data_->Size(); | ||
| } | ||
|
|
||
| ColumnRef ColumnJSON::Slice(size_t begin, size_t len) const { | ||
| auto ret = std::make_shared<ColumnJSON>(); | ||
| auto sliced_data = data_->Slice(begin, len)->As<ColumnString>(); | ||
| ret->data_->Swap(*sliced_data); | ||
| return ret; | ||
| } | ||
|
|
||
| ColumnRef ColumnJSON::CloneEmpty() const | ||
| { | ||
| return std::make_shared<ColumnJSON>(); | ||
| } | ||
|
|
||
| void ColumnJSON::Swap(Column& other) { | ||
| auto & col = dynamic_cast<ColumnJSON &>(other); | ||
| data_.swap(col.data_); | ||
| } | ||
|
|
||
| ItemView ColumnJSON::GetItem(size_t index) const { | ||
| return ItemView{Type::JSON, data_->GetItem(index)}; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #pragma once | ||
|
|
||
| #include "column.h" | ||
| #include "string.h" | ||
| #include "nullable.h" | ||
|
|
||
| namespace clickhouse { | ||
|
|
||
| /** | ||
| * JSON Column: Represents JSON values as strings. | ||
| * Works only when ClickHouse outputs JSON as strings and requires the setting | ||
| * output_format_native_write_json_as_string to be set to 1 for selecting data. | ||
| * Inserting JSON data does not require setting this setting. | ||
| * | ||
| * WARNING: THIS IS AN EXPERIMENTAL IMPLEMENTATION. | ||
| * The API may change in the future as we continue working on full support for JSON columns. | ||
| * | ||
| * ClickHouse does not accept empty strings as JSON; it requires an empty object ({}). | ||
| * For nullable columns, each row marked a NULL must contain {}. | ||
| * For convenience `clickhouse::ColumnNullableT<ColumnJSON>` automatically inserts {} for NULL rows. | ||
| */ | ||
| class ColumnJSON : public Column { | ||
| public: | ||
|
|
||
| ColumnJSON(); | ||
| explicit ColumnJSON(std::vector<std::string> data); | ||
|
|
||
| /// Appends one element to the column. | ||
| void Append(std::string_view str); | ||
|
|
||
| void Append(const char* str); | ||
| void Append(std::string&& str); | ||
|
|
||
| std::string_view At(size_t n) const; | ||
| inline std::string_view operator [] (size_t n) const { return At(n); } | ||
|
|
||
| /// Appends content of given column to the end of current one. | ||
| void Append(ColumnRef column) override; | ||
|
|
||
| /// Increase the capacity of the column for large block insertion. | ||
| void Reserve(size_t new_cap) 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. Column types with prefixes must implement it. | ||
| 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 begin, size_t len) const override; | ||
| ColumnRef CloneEmpty() const override; | ||
| void Swap(Column& other) override; | ||
|
|
||
| ItemView GetItem(size_t index) const override; | ||
|
|
||
| private: | ||
| std::shared_ptr<ColumnString> data_; | ||
| }; | ||
|
|
||
| template <> | ||
| inline void ColumnNullableT<ColumnJSON>::Append(std::optional<std::string_view> value) { | ||
| ColumnNullable::Append(!value.has_value()); | ||
| if (value.has_value()) { | ||
| typed_nested_data_->Append(*value); | ||
| } else { | ||
| typed_nested_data_->Append(std::string_view("{}")); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ColumnJSONis added to the generic server roundtrip test matrix, butCheckIfShouldSkipTest()doesn't currently have a capability/version gate for JSON. This will cause failures against ClickHouse versions without the JSON type (again,.travis.ymluses 21.3.x). Consider adding a skip similar to Date32/Int128, either via a known minimum server version for JSON or by probing server support (e.g. attempt a trivialSELECT CAST('{}' AS JSON)and skip on error).