Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ C++ client for [Yandex ClickHouse](https://clickhouse.yandex/)

* Array(T)
* Date
<<<<<<< HEAD
* DateTime, DateTime64
=======
* DateTime([timezone]), DateTime64(N, [timezone])
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
* Decimal32, Decimal64, Decimal128
* Enum8, Enum16
* FixedString(N)
Expand Down
9 changes: 9 additions & 0 deletions clickhouse/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@

#define DBMS_NAME "ClickHouse"
#define DBMS_VERSION_MAJOR 1
<<<<<<< HEAD
#define DBMS_VERSION_MINOR 2

#define REVISION 54405
=======
#define DBMS_VERSION_MINOR 1
#define REVISION 54337
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition

#define DBMS_MIN_REVISION_WITH_TEMPORARY_TABLES 50264
#define DBMS_MIN_REVISION_WITH_TOTAL_ROWS_IN_PROGRESS 51554
#define DBMS_MIN_REVISION_WITH_BLOCK_INFO 51903
#define DBMS_MIN_REVISION_WITH_CLIENT_INFO 54032
#define DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE 54058
#define DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO 54060
<<<<<<< HEAD
//#define DBMS_MIN_REVISION_WITH_TABLES_STATUS 54226
//#define DBMS_MIN_REVISION_WITH_TIME_ZONE_PARAMETER_IN_DATETIME_DATA_TYPE 54337
#define DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME 54372
#define DBMS_MIN_REVISION_WITH_VERSION_PATCH 54401
#define DBMS_MIN_REVISION_WITH_LOW_CARDINALITY_TYPE 54405
=======
#define DBMS_MIN_REVISION_WITH_TIME_ZONE_PARAMETER_IN_DATETIME_DATA_TYPE 54337
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition

namespace clickhouse {

Expand Down
34 changes: 34 additions & 0 deletions clickhouse/columns/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ ColumnDateTime::ColumnDateTime()
{
}

ColumnDateTime::ColumnDateTime(std::string timezone)
: Column(Type::CreateDateTime(std::move(timezone)))
, data_(std::make_shared<ColumnUInt32>())
{
}

void ColumnDateTime::Append(const std::time_t& value) {
data_->Append(static_cast<uint32_t>(value));
}
Expand All @@ -73,6 +79,10 @@ std::time_t ColumnDateTime::At(size_t n) const {
return data_->At(n);
}

std::string ColumnDateTime::Timezone() const {
return DateTimeType(type_).Timezone();
}

void ColumnDateTime::Append(ColumnRef column) {
if (auto col = column->As<ColumnDateTime>()) {
data_->Append(col->data_);
Expand Down Expand Up @@ -104,13 +114,33 @@ ColumnRef ColumnDateTime::Slice(size_t begin, size_t len) {
return result;
}

<<<<<<< HEAD
void ColumnDateTime::Swap(Column& other) {
auto & col = dynamic_cast<ColumnDateTime &>(other);
data_.swap(col.data_);
}

ItemView ColumnDateTime::GetItem(size_t index) const {
return data_->GetItem(index);
=======

ColumnDateTime64::ColumnDateTime64(size_t precision)
: Column(Type::CreateDateTime64(precision))
, data_(std::make_shared<ColumnUInt64>())
{
}

ColumnDateTime64::ColumnDateTime64(size_t precision, std::string timezone)
: Column(Type::CreateDateTime64(precision, std::move(timezone)))
, data_(std::make_shared<ColumnUInt64>())
{
}

ColumnDateTime64::ColumnDateTime64(TypeRef type, std::shared_ptr<ColumnUInt64> data)
: Column(type)
, data_(std::move(data))
{
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
}

ColumnDateTime64::ColumnDateTime64(size_t precision)
Expand All @@ -137,6 +167,10 @@ Int64 ColumnDateTime64::At(size_t n) const {
return data_->At(n);
}

std::string ColumnDateTime64::Timezone() const {
return DateTimeType(type_).Timezone();
}

void ColumnDateTime64::Append(ColumnRef column) {
if (auto col = column->As<ColumnDateTime64>()) {
data_->Append(col->data_);
Expand Down
18 changes: 17 additions & 1 deletion clickhouse/columns/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ColumnDate : public Column {

/// Clear column data .
void Clear() override;

/// Returns count of rows in the column.
size_t Size() const override;

Expand All @@ -50,13 +50,21 @@ class ColumnDate : public Column {
class ColumnDateTime : public Column {
public:
ColumnDateTime();
explicit ColumnDateTime(std::string timezone);

/// Appends one element to the end of column.
void Append(const std::time_t& value);

/// Returns element at given row number.
std::time_t At(size_t n) const;

<<<<<<< HEAD
=======
/// Timezone associated with a data column.
std::string Timezone() const;

public:
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;

Expand Down Expand Up @@ -87,7 +95,12 @@ class ColumnDateTime : public Column {
/** */
class ColumnDateTime64 : public Column {
public:
<<<<<<< HEAD
explicit ColumnDateTime64(size_t);
=======
explicit ColumnDateTime64(size_t precision);
ColumnDateTime64(size_t precision, std::string timezone);
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition

/// Appends one element to the end of column.
void Append(const Int64& value);
Expand All @@ -98,6 +111,9 @@ class ColumnDateTime64 : public Column {
/// Returns element at given row number.
Int64 At(size_t n) const;

/// Timezone associated with a data column.
std::string Timezone() const;

public:
/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;
Expand Down
24 changes: 19 additions & 5 deletions clickhouse/columns/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,20 @@ static ColumnRef CreateTerminalColumn(const TypeAst& ast) {
return std::make_shared<ColumnFixedString>(ast.elements.front().value);

case Type::DateTime:
return std::make_shared<ColumnDateTime>();
if (ast.elements.empty()) {
return std::make_shared<ColumnDateTime>();
} else {
return std::make_shared<ColumnDateTime>(ast.elements[0].value_string);
}
case Type::DateTime64:
return std::make_shared<ColumnDateTime64>(ast.elements.front().value);
if (ast.elements.empty()) {
return nullptr;
}
if (ast.elements.size() == 1) {
return std::make_shared<ColumnDateTime64>(ast.elements[0].value);
} else {
return std::make_shared<ColumnDateTime64>(ast.elements[0].value, ast.elements[1].value_string);
}
case Type::Date:
return std::make_shared<ColumnDate>();

Expand Down Expand Up @@ -120,10 +131,11 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast) {
case TypeAst::Enum: {
std::vector<Type::EnumItem> enum_items;

enum_items.reserve(ast.elements.size());
for (const auto& elem : ast.elements) {
enum_items.reserve(ast.elements.size() / 2);
for (size_t i = 0; i < ast.elements.size(); i += 2) {
enum_items.push_back(
Type::EnumItem{elem.name, (int16_t)elem.value});
Type::EnumItem{ast.elements[i].value_string,
(int16_t)ast.elements[i + 1].value});
}

if (ast.code == Type::Enum8) {
Expand Down Expand Up @@ -153,8 +165,10 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast) {
return CreateTerminalColumn(ast.elements.back());
}

case TypeAst::Assign:
case TypeAst::Null:
case TypeAst::Number:
case TypeAst::String:
break;
}

Expand Down
26 changes: 26 additions & 0 deletions clickhouse/types/type_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ bool TypeParser::Parse(TypeAst* type) {
type_->meta = TypeAst::Number;
type_->value = std::stol(token.value.to_string());
break;
case Token::String:
type_->meta = TypeAst::String;
type_->value_string = std::string(token.value);
break;
case Token::LPar:
type_->elements.emplace_back(TypeAst());
open_elements_.push(type_);
Expand All @@ -132,6 +136,7 @@ bool TypeParser::Parse(TypeAst* type) {
type_ = open_elements_.top();
open_elements_.pop();
break;
case Token::Assign:
case Token::Comma:
type_ = open_elements_.top();
open_elements_.pop();
Expand All @@ -140,10 +145,17 @@ bool TypeParser::Parse(TypeAst* type) {
type_ = &type_->elements.back();
break;
case Token::EOS:
<<<<<<< HEAD
{
// Ubalanced braces, brackets, etc is an error.
if (open_elements_.size() != 1)
return false;
=======
// Ubalanced braces, brackets, etc is an error.
if (open_elements_.size() != 1) {
return false;
}
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
return true;
}
case Token::Invalid:
Expand All @@ -162,8 +174,12 @@ TypeParser::Token TypeParser::NextToken() {
continue;

case '=':
<<<<<<< HEAD
continue;

=======
return Token{Token::Assign, std::string_view(cur_++, 1)};
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
case '(':
return Token{Token::LPar, StringView(cur_++, 1)};
case ')':
Expand All @@ -190,6 +206,16 @@ TypeParser::Token TypeParser::NextToken() {
default: {
const char* st = cur_;

if (*cur_ == '\'') {
for (st = ++cur_; cur_ < end_; ++cur_) {
if (*cur_ == '\'') {
return Token{Token::String, std::string_view(st, cur_++ - st)};
}
}

return Token{Token::Invalid, std::string_view()};
}

if (isalpha(*cur_) || *cur_ == '_') {
for (; cur_ < end_; ++cur_) {
if (!isalpha(*cur_) && !isdigit(*cur_) && *cur_ != '_') {
Expand Down
5 changes: 5 additions & 0 deletions clickhouse/types/type_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace clickhouse {
struct TypeAst {
enum Meta {
Array,
Assign,
Null,
Nullable,
Number,
String,
Terminal,
Tuple,
Enum,
Expand All @@ -31,6 +33,7 @@ struct TypeAst {
/// Value associated with the node,
/// used for fixed-width types and enum values.
int64_t value = 0;
std::string value_string;
/// Subelements of the type.
/// Used to store enum's names and values as well.
std::vector<TypeAst> elements;
Expand All @@ -47,8 +50,10 @@ class TypeParser {
struct Token {
enum Type {
Invalid = 0,
Assign,
Name,
Number,
String,
LPar,
RPar,
Comma,
Expand Down
Loading