Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,7 @@ BUCKAROO_DEPS

# Visual Studio Code
/.vscode/

# Vim
*.swp
*.swo
65 changes: 65 additions & 0 deletions clickhouse/columns/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,69 @@ ItemView ColumnDateTime::GetItem(size_t index) const {
return data_->GetItem(index);
}


ColumnDateTime64::ColumnDateTime64()
: Column(Type::CreateDateTime64(3ul))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we shouldn't provide a default constructor at all or come up with a different default precision. I will be happy to get feedback regarding this line.

, data_(std::make_shared<ColumnDecimal>(18ul, 3ul))
{}

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


void ColumnDateTime64::Append(const Int128& value) {
data_->Append(value);
}

void ColumnDateTime64::Append(const std::string& value) {
data_->Append(value);
}


Int128 ColumnDateTime64::At(size_t n) const {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Decided to use Int128 because ColumnDecimal deals with this type. Poco supports https://pocoproject.org/docs/Poco.DateTime.html, but DateTime64 in Clickhouse has a higher resolution than in Poco.

return data_->At(n);
}

void ColumnDateTime64::Append(ColumnRef column) {
if (auto col = column->As<ColumnDateTime64>()) {
data_->Append(col->data_);
}
}

bool ColumnDateTime64::Load(CodedInputStream* input, size_t rows) {
return data_->Load(input, rows);
}

void ColumnDateTime64::Save(CodedOutputStream* output) {
data_->Save(output);
}

void ColumnDateTime64::Clear() {
data_->Clear();
}
size_t ColumnDateTime64::Size() const {
return data_->Size();
}

ItemView ColumnDateTime64::GetItem(size_t index) const {
return data_->GetItem(index);
}

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

ColumnRef ColumnDateTime64::Slice(size_t begin, size_t len) {
auto col = data_->Slice(begin, len)->As<ColumnDecimal>();
size_t precision = col->Type()->As<DateTime64Type>()->GetPrecision();
Copy link
Contributor

@Enmk Enmk Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would blow up terribly, do you mean col->Type()->As<DecimalType>()->GetPrecision()? Also please add following to the DecimalType:

inline size_t GetPrecision() const { return precision_; }

auto result = std::make_shared<ColumnDateTime64>(precision); // TODO FIXME

result->data_->Append(col);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why appending when you can swap?


return result;
}

}
41 changes: 41 additions & 0 deletions clickhouse/columns/date.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "decimal.h"
#include "numeric.h"

#include <ctime>
Expand Down Expand Up @@ -82,4 +83,44 @@ class ColumnDateTime : public Column {
std::shared_ptr<ColumnUInt32> data_;
};


/** */
class ColumnDateTime64 : public Column {
public:
ColumnDateTime64();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that having a default precision here is a good thing, can you remove it?

ColumnDateTime64(size_t);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should be explicit


/// Appends one element to the end of column.
void Append(const Int128& value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather use Int64 here, since it is a bit misleading to put 128-bit int into DateTime64 column

void Append(const std::string& value);

/// Returns element at given row number.
Int128 At(size_t n) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it makes sense to use Int64 here too.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add method GetPrecision() const

public:
/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override;

/// Loads column data from input stream.
bool Load(CodedInputStream* input, size_t rows) override;

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

/// Saves column data to output stream.
void Save(CodedOutputStream* output) 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) override;

void Swap(Column& other) override;

ItemView GetItem(size_t index) const override;
private:
std::shared_ptr<ColumnDecimal> data_;
};

}
2 changes: 2 additions & 0 deletions clickhouse/columns/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static ColumnRef CreateTerminalColumn(const TypeAst& ast) {

case Type::DateTime:
return std::make_shared<ColumnDateTime>();
case Type::DateTime64:
return std::make_shared<ColumnDateTime64>(ast.elements.front().value);
case Type::Date:
return std::make_shared<ColumnDate>();

Expand Down
1 change: 1 addition & 0 deletions clickhouse/columns/itemview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
ANY, /*String*/
ANY, /*FixedString*/
4, /*DateTime*/
8, /*DateTime64*/
2, /*Date*/
ERR, /*Array*/
ERR, /*Nullable*/
Expand Down
1 change: 1 addition & 0 deletions clickhouse/types/type_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static const std::unordered_map<std::string, Type::Code> kTypeCode = {
{ "String", Type::String },
{ "FixedString", Type::FixedString },
{ "DateTime", Type::DateTime },
{ "DateTime64", Type::DateTime64 },
{ "Date", Type::Date },
{ "Array", Type::Array },
{ "Nullable", Type::Nullable },
Expand Down
19 changes: 19 additions & 0 deletions clickhouse/types/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ std::string Type::GetName() const {
return "IPv6";
case DateTime:
return "DateTime";
case DateTime64:
return As<DateTime64Type>()->GetName();
case Date:
return "Date";
case Array:
Expand Down Expand Up @@ -81,6 +83,10 @@ TypeRef Type::CreateDateTime() {
return TypeRef(new Type(Type::DateTime));
}

TypeRef Type::CreateDateTime64(size_t precision) {
return TypeRef(new DateTime64Type(precision));
}

TypeRef Type::CreateDecimal(size_t precision, size_t scale) {
return TypeRef(new DecimalType(precision, scale));
}
Expand Down Expand Up @@ -219,6 +225,19 @@ EnumType::ValueToNameIterator EnumType::EndValueToName() const {
return value_to_name_.end();
}

/// class DateTime64Type

DateTime64Type::DateTime64Type(size_t precision) : Type(DateTime64), precision_(precision) {}

std::string DateTime64Type::GetName() const {
std::string datetime64_representation;
datetime64_representation.reserve(14);
datetime64_representation += "DateTime64(";
datetime64_representation += std::to_string(precision_);
datetime64_representation += ")";
return datetime64_representation;
}

/// class FixedStringType

FixedStringType::FixedStringType(size_t n) : Type(FixedString), size_(n) {
Expand Down
15 changes: 15 additions & 0 deletions clickhouse/types/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Type {
String,
FixedString,
DateTime,
DateTime64,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO that could break some implicit user assumptions, it is better to add new enum value to the end of the list.

Date,
Array,
Nullable,
Expand Down Expand Up @@ -76,6 +77,8 @@ class Type {

static TypeRef CreateDateTime();

static TypeRef CreateDateTime64(size_t precision);

static TypeRef CreateDecimal(size_t precision, size_t scale);

static TypeRef CreateIPv4();
Expand Down Expand Up @@ -144,6 +147,18 @@ class DecimalType : public Type {
const size_t precision_, scale_;
};

class DateTime64Type: public Type {
public:
DateTime64Type(size_t precision);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make it explicit


std::string GetName() const;

inline size_t GetPrecision() const { return precision_; }

private:
size_t precision_;
};

class EnumType : public Type {
public:
EnumType(Type::Code type, const std::vector<EnumItem>& items);
Expand Down