Skip to content

Commit 669ae39

Browse files
artpaulfilimonov
authored andcommitted
check that brackets are properly balanced in a type definition
fix GetName for empty enums Timezone support for DateTime (artpaul#120) * parse assign sign as separate token * support timezone in datetime type * add info about supported types * test datetime with timezone
1 parent cb10171 commit 669ae39

File tree

14 files changed

+382
-14
lines changed

14 files changed

+382
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ C++ client for [Yandex ClickHouse](https://clickhouse.yandex/)
77

88
* Array(T)
99
* Date
10+
<<<<<<< HEAD
1011
* DateTime, DateTime64
12+
=======
13+
* DateTime([timezone]), DateTime64(N, [timezone])
14+
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
1115
* Decimal32, Decimal64, Decimal128
1216
* Enum8, Enum16
1317
* FixedString(N)

clickhouse/client.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@
2121

2222
#define DBMS_NAME "ClickHouse"
2323
#define DBMS_VERSION_MAJOR 1
24+
<<<<<<< HEAD
2425
#define DBMS_VERSION_MINOR 2
2526

2627
#define REVISION 54405
28+
=======
29+
#define DBMS_VERSION_MINOR 1
30+
#define REVISION 54337
31+
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
2732

2833
#define DBMS_MIN_REVISION_WITH_TEMPORARY_TABLES 50264
2934
#define DBMS_MIN_REVISION_WITH_TOTAL_ROWS_IN_PROGRESS 51554
3035
#define DBMS_MIN_REVISION_WITH_BLOCK_INFO 51903
3136
#define DBMS_MIN_REVISION_WITH_CLIENT_INFO 54032
3237
#define DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE 54058
3338
#define DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO 54060
39+
<<<<<<< HEAD
3440
//#define DBMS_MIN_REVISION_WITH_TABLES_STATUS 54226
3541
//#define DBMS_MIN_REVISION_WITH_TIME_ZONE_PARAMETER_IN_DATETIME_DATA_TYPE 54337
3642
#define DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME 54372
3743
#define DBMS_MIN_REVISION_WITH_VERSION_PATCH 54401
3844
#define DBMS_MIN_REVISION_WITH_LOW_CARDINALITY_TYPE 54405
45+
=======
46+
#define DBMS_MIN_REVISION_WITH_TIME_ZONE_PARAMETER_IN_DATETIME_DATA_TYPE 54337
47+
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
3948

4049
namespace clickhouse {
4150

clickhouse/columns/date.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ ColumnDateTime::ColumnDateTime()
6565
{
6666
}
6767

68+
ColumnDateTime::ColumnDateTime(std::string timezone)
69+
: Column(Type::CreateDateTime(std::move(timezone)))
70+
, data_(std::make_shared<ColumnUInt32>())
71+
{
72+
}
73+
6874
void ColumnDateTime::Append(const std::time_t& value) {
6975
data_->Append(static_cast<uint32_t>(value));
7076
}
@@ -73,6 +79,10 @@ std::time_t ColumnDateTime::At(size_t n) const {
7379
return data_->At(n);
7480
}
7581

82+
std::string ColumnDateTime::Timezone() const {
83+
return DateTimeType(type_).Timezone();
84+
}
85+
7686
void ColumnDateTime::Append(ColumnRef column) {
7787
if (auto col = column->As<ColumnDateTime>()) {
7888
data_->Append(col->data_);
@@ -104,13 +114,33 @@ ColumnRef ColumnDateTime::Slice(size_t begin, size_t len) {
104114
return result;
105115
}
106116

117+
<<<<<<< HEAD
107118
void ColumnDateTime::Swap(Column& other) {
108119
auto & col = dynamic_cast<ColumnDateTime &>(other);
109120
data_.swap(col.data_);
110121
}
111122

112123
ItemView ColumnDateTime::GetItem(size_t index) const {
113124
return data_->GetItem(index);
125+
=======
126+
127+
ColumnDateTime64::ColumnDateTime64(size_t precision)
128+
: Column(Type::CreateDateTime64(precision))
129+
, data_(std::make_shared<ColumnUInt64>())
130+
{
131+
}
132+
133+
ColumnDateTime64::ColumnDateTime64(size_t precision, std::string timezone)
134+
: Column(Type::CreateDateTime64(precision, std::move(timezone)))
135+
, data_(std::make_shared<ColumnUInt64>())
136+
{
137+
}
138+
139+
ColumnDateTime64::ColumnDateTime64(TypeRef type, std::shared_ptr<ColumnUInt64> data)
140+
: Column(type)
141+
, data_(std::move(data))
142+
{
143+
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
114144
}
115145

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

170+
std::string ColumnDateTime64::Timezone() const {
171+
return DateTimeType(type_).Timezone();
172+
}
173+
140174
void ColumnDateTime64::Append(ColumnRef column) {
141175
if (auto col = column->As<ColumnDateTime64>()) {
142176
data_->Append(col->data_);

clickhouse/columns/date.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ColumnDate : public Column {
3131

3232
/// Clear column data .
3333
void Clear() override;
34-
34+
3535
/// Returns count of rows in the column.
3636
size_t Size() const override;
3737

@@ -50,13 +50,21 @@ class ColumnDate : public Column {
5050
class ColumnDateTime : public Column {
5151
public:
5252
ColumnDateTime();
53+
explicit ColumnDateTime(std::string timezone);
5354

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

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

61+
<<<<<<< HEAD
62+
=======
63+
/// Timezone associated with a data column.
64+
std::string Timezone() const;
65+
66+
public:
67+
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
6068
/// Appends content of given column to the end of current one.
6169
void Append(ColumnRef column) override;
6270

@@ -87,7 +95,12 @@ class ColumnDateTime : public Column {
8795
/** */
8896
class ColumnDateTime64 : public Column {
8997
public:
98+
<<<<<<< HEAD
9099
explicit ColumnDateTime64(size_t);
100+
=======
101+
explicit ColumnDateTime64(size_t precision);
102+
ColumnDateTime64(size_t precision, std::string timezone);
103+
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
91104

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

114+
/// Timezone associated with a data column.
115+
std::string Timezone() const;
116+
101117
public:
102118
/// Appends content of given column to the end of current one.
103119
void Append(ColumnRef column) override;

clickhouse/columns/factory.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,20 @@ static ColumnRef CreateTerminalColumn(const TypeAst& ast) {
6464
return std::make_shared<ColumnFixedString>(ast.elements.front().value);
6565

6666
case Type::DateTime:
67-
return std::make_shared<ColumnDateTime>();
67+
if (ast.elements.empty()) {
68+
return std::make_shared<ColumnDateTime>();
69+
} else {
70+
return std::make_shared<ColumnDateTime>(ast.elements[0].value_string);
71+
}
6872
case Type::DateTime64:
69-
return std::make_shared<ColumnDateTime64>(ast.elements.front().value);
73+
if (ast.elements.empty()) {
74+
return nullptr;
75+
}
76+
if (ast.elements.size() == 1) {
77+
return std::make_shared<ColumnDateTime64>(ast.elements[0].value);
78+
} else {
79+
return std::make_shared<ColumnDateTime64>(ast.elements[0].value, ast.elements[1].value_string);
80+
}
7081
case Type::Date:
7182
return std::make_shared<ColumnDate>();
7283

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

123-
enum_items.reserve(ast.elements.size());
124-
for (const auto& elem : ast.elements) {
134+
enum_items.reserve(ast.elements.size() / 2);
135+
for (size_t i = 0; i < ast.elements.size(); i += 2) {
125136
enum_items.push_back(
126-
Type::EnumItem{elem.name, (int16_t)elem.value});
137+
Type::EnumItem{ast.elements[i].value_string,
138+
(int16_t)ast.elements[i + 1].value});
127139
}
128140

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

168+
case TypeAst::Assign:
156169
case TypeAst::Null:
157170
case TypeAst::Number:
171+
case TypeAst::String:
158172
break;
159173
}
160174

clickhouse/types/type_parser.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ bool TypeParser::Parse(TypeAst* type) {
123123
type_->meta = TypeAst::Number;
124124
type_->value = std::stol(token.value.to_string());
125125
break;
126+
case Token::String:
127+
type_->meta = TypeAst::String;
128+
type_->value_string = std::string(token.value);
129+
break;
126130
case Token::LPar:
127131
type_->elements.emplace_back(TypeAst());
128132
open_elements_.push(type_);
@@ -132,6 +136,7 @@ bool TypeParser::Parse(TypeAst* type) {
132136
type_ = open_elements_.top();
133137
open_elements_.pop();
134138
break;
139+
case Token::Assign:
135140
case Token::Comma:
136141
type_ = open_elements_.top();
137142
open_elements_.pop();
@@ -140,10 +145,17 @@ bool TypeParser::Parse(TypeAst* type) {
140145
type_ = &type_->elements.back();
141146
break;
142147
case Token::EOS:
148+
<<<<<<< HEAD
143149
{
144150
// Ubalanced braces, brackets, etc is an error.
145151
if (open_elements_.size() != 1)
146152
return false;
153+
=======
154+
// Ubalanced braces, brackets, etc is an error.
155+
if (open_elements_.size() != 1) {
156+
return false;
157+
}
158+
>>>>>>> 7d44d98... check that brackets are properly balanced in a type definition
147159
return true;
148160
}
149161
case Token::Invalid:
@@ -162,8 +174,12 @@ TypeParser::Token TypeParser::NextToken() {
162174
continue;
163175

164176
case '=':
177+
<<<<<<< HEAD
165178
continue;
166179

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

209+
if (*cur_ == '\'') {
210+
for (st = ++cur_; cur_ < end_; ++cur_) {
211+
if (*cur_ == '\'') {
212+
return Token{Token::String, std::string_view(st, cur_++ - st)};
213+
}
214+
}
215+
216+
return Token{Token::Invalid, std::string_view()};
217+
}
218+
193219
if (isalpha(*cur_) || *cur_ == '_') {
194220
for (; cur_ < end_; ++cur_) {
195221
if (!isalpha(*cur_) && !isdigit(*cur_) && *cur_ != '_') {

clickhouse/types/type_parser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ namespace clickhouse {
1212
struct TypeAst {
1313
enum Meta {
1414
Array,
15+
Assign,
1516
Null,
1617
Nullable,
1718
Number,
19+
String,
1820
Terminal,
1921
Tuple,
2022
Enum,
@@ -31,6 +33,7 @@ struct TypeAst {
3133
/// Value associated with the node,
3234
/// used for fixed-width types and enum values.
3335
int64_t value = 0;
36+
std::string value_string;
3437
/// Subelements of the type.
3538
/// Used to store enum's names and values as well.
3639
std::vector<TypeAst> elements;
@@ -47,8 +50,10 @@ class TypeParser {
4750
struct Token {
4851
enum Type {
4952
Invalid = 0,
53+
Assign,
5054
Name,
5155
Number,
56+
String,
5257
LPar,
5358
RPar,
5459
Comma,

0 commit comments

Comments
 (0)