Skip to content

Commit

Permalink
Added support for TINYINT
Browse files Browse the repository at this point in the history
  • Loading branch information
shtilman authored and andrewseidl committed May 15, 2018
1 parent 2999468 commit 656aad1
Show file tree
Hide file tree
Showing 41 changed files with 364 additions and 12 deletions.
99 changes: 99 additions & 0 deletions Analyzer/Analyzer.cpp
Expand Up @@ -362,8 +362,37 @@ SQLTypeInfo BinOper::common_numeric_type(const SQLTypeInfo& type1, const SQLType
}
CHECK(type1.is_number() && type2.is_number());
switch (type1.get_type()) {
case kTINYINT:
switch (type2.get_type()) {
case kSMALLINT:
common_type = SQLTypeInfo(kSMALLINT, notnull);
break;
case kINT:
common_type = SQLTypeInfo(kINT, notnull);
break;
case kBIGINT:
common_type = SQLTypeInfo(kBIGINT, notnull);
break;
case kFLOAT:
common_type = SQLTypeInfo(kFLOAT, notnull);
break;
case kDOUBLE:
common_type = SQLTypeInfo(kDOUBLE, notnull);
break;
case kNUMERIC:
case kDECIMAL:
common_type =
SQLTypeInfo(kDECIMAL, std::max(5 + type2.get_scale(), type2.get_dimension()), type2.get_scale(), notnull);
break;
default:
CHECK(false);
}
break;
case kSMALLINT:
switch (type2.get_type()) {
case kTINYINT:
common_type = SQLTypeInfo(kSMALLINT, notnull);
break;
case kINT:
common_type = SQLTypeInfo(kINT, notnull);
break;
Expand All @@ -387,6 +416,9 @@ SQLTypeInfo BinOper::common_numeric_type(const SQLTypeInfo& type1, const SQLType
break;
case kINT:
switch (type2.get_type()) {
case kTINYINT:
common_type = SQLTypeInfo(kINT, notnull);
break;
case kSMALLINT:
common_type = SQLTypeInfo(kINT, notnull);
break;
Expand All @@ -412,6 +444,9 @@ SQLTypeInfo BinOper::common_numeric_type(const SQLTypeInfo& type1, const SQLType
break;
case kBIGINT:
switch (type2.get_type()) {
case kTINYINT:
common_type = SQLTypeInfo(kBIGINT, notnull);
break;
case kSMALLINT:
common_type = SQLTypeInfo(kBIGINT, notnull);
break;
Expand All @@ -434,6 +469,9 @@ SQLTypeInfo BinOper::common_numeric_type(const SQLTypeInfo& type1, const SQLType
break;
case kFLOAT:
switch (type2.get_type()) {
case kTINYINT:
common_type = SQLTypeInfo(kFLOAT, notnull);
break;
case kSMALLINT:
common_type = SQLTypeInfo(kFLOAT, notnull);
break;
Expand All @@ -456,6 +494,7 @@ SQLTypeInfo BinOper::common_numeric_type(const SQLTypeInfo& type1, const SQLType
break;
case kDOUBLE:
switch (type2.get_type()) {
case kTINYINT:
case kSMALLINT:
case kINT:
case kBIGINT:
Expand Down Expand Up @@ -579,8 +618,40 @@ IntFracRepr decimal_to_int_frac(const int64_t dec, const SQLTypeInfo& ti) {

void Constant::cast_number(const SQLTypeInfo& new_type_info) {
switch (type_info.get_type()) {
case kTINYINT:
switch (new_type_info.get_type()) {
case kTINYINT:
break;
case kINT:
constval.intval = (int32_t)constval.tinyintval;
break;
case kSMALLINT:
constval.smallintval = (int16_t)constval.tinyintval;
break;
case kBIGINT:
constval.bigintval = (int64_t)constval.tinyintval;
break;
case kDOUBLE:
constval.doubleval = (double)constval.tinyintval;
break;
case kFLOAT:
constval.floatval = (float)constval.tinyintval;
break;
case kNUMERIC:
case kDECIMAL:
constval.bigintval = (int64_t)constval.tinyintval;
for (int i = 0; i < new_type_info.get_scale(); i++)
constval.bigintval *= 10;
break;
default:
CHECK(false);
}
break;
case kINT:
switch (new_type_info.get_type()) {
case kTINYINT:
constval.tinyintval = (int8_t)constval.intval;
break;
case kINT:
break;
case kSMALLINT:
Expand All @@ -607,6 +678,9 @@ void Constant::cast_number(const SQLTypeInfo& new_type_info) {
break;
case kSMALLINT:
switch (new_type_info.get_type()) {
case kTINYINT:
constval.tinyintval = (int8_t)constval.smallintval;
break;
case kINT:
constval.intval = (int32_t)constval.smallintval;
break;
Expand All @@ -633,6 +707,9 @@ void Constant::cast_number(const SQLTypeInfo& new_type_info) {
break;
case kBIGINT:
switch (new_type_info.get_type()) {
case kTINYINT:
constval.tinyintval = (int8_t)constval.bigintval;
break;
case kINT:
constval.intval = (int32_t)constval.bigintval;
break;
Expand All @@ -658,6 +735,9 @@ void Constant::cast_number(const SQLTypeInfo& new_type_info) {
break;
case kDOUBLE:
switch (new_type_info.get_type()) {
case kTINYINT:
constval.tinyintval = (int8_t)constval.doubleval;
break;
case kINT:
constval.intval = (int32_t)constval.doubleval;
break;
Expand All @@ -684,6 +764,9 @@ void Constant::cast_number(const SQLTypeInfo& new_type_info) {
break;
case kFLOAT:
switch (new_type_info.get_type()) {
case kTINYINT:
constval.tinyintval = (int8_t)constval.floatval;
break;
case kINT:
constval.intval = (int32_t)constval.floatval;
break;
Expand Down Expand Up @@ -711,6 +794,11 @@ void Constant::cast_number(const SQLTypeInfo& new_type_info) {
case kNUMERIC:
case kDECIMAL:
switch (new_type_info.get_type()) {
case kTINYINT:
for (int i = 0; i < type_info.get_scale(); i++)
constval.bigintval /= 10;
constval.tinyintval = (int8_t)constval.bigintval;
break;
case kINT:
for (int i = 0; i < type_info.get_scale(); i++)
constval.bigintval /= 10;
Expand Down Expand Up @@ -745,6 +833,9 @@ void Constant::cast_number(const SQLTypeInfo& new_type_info) {
break;
case kTIMESTAMP:
switch (new_type_info.get_type()) {
case kTINYINT:
constval.tinyintval = (int8_t)constval.timeval;
break;
case kINT:
constval.intval = (int32_t)constval.timeval;
break;
Expand Down Expand Up @@ -772,6 +863,9 @@ void Constant::cast_number(const SQLTypeInfo& new_type_info) {
break;
case kBOOLEAN:
switch (new_type_info.get_type()) {
case kTINYINT:
constval.tinyintval = constval.boolval ? 1 : 0;
break;
case kINT:
constval.intval = constval.boolval ? 1 : 0;
break;
Expand Down Expand Up @@ -874,6 +968,9 @@ void Constant::set_null_value() {
case kBOOLEAN:
constval.boolval = NULL_BOOLEAN;
break;
case kTINYINT:
constval.tinyintval = NULL_TINYINT;
break;
case kINT:
constval.intval = NULL_INT;
break;
Expand Down Expand Up @@ -1617,6 +1714,8 @@ bool Datum_equal(const SQLTypeInfo& ti, Datum val1, Datum val2) {
return val1.intval == val2.intval;
case kSMALLINT:
return val1.smallintval == val2.smallintval;
case kTINYINT:
return val1.tinyintval == val2.tinyintval;
case kFLOAT:
return val1.floatval == val2.floatval;
case kDOUBLE:
Expand Down
15 changes: 15 additions & 0 deletions DataMgr/ArrayNoneEncoder.h
Expand Up @@ -240,6 +240,21 @@ class ArrayNoneEncoder : public Encoder {
}
}
} break;
case kTINYINT: {
const int8_t* int_array = (int8_t*)array.pointer;
for (size_t i = 0; i < array.length / sizeof(int8_t); i++) {
if (int_array[i] == NULL_TINYINT)
has_nulls = true;
else if (initialized) {
elem_min.tinyintval = std::min(elem_min.tinyintval, int_array[i]);
elem_max.tinyintval = std::max(elem_max.tinyintval, int_array[i]);
} else {
elem_min.tinyintval = int_array[i];
elem_max.tinyintval = int_array[i];
initialized = true;
}
}
} break;
case kBIGINT:
case kNUMERIC:
case kDECIMAL: {
Expand Down
5 changes: 5 additions & 0 deletions DataMgr/ChunkMetadata.h
Expand Up @@ -41,6 +41,11 @@ struct ChunkMetadata {
chunkStats.max.tinyintval = max;
break;
}
case kTINYINT: {
chunkStats.min.tinyintval = min;
chunkStats.max.tinyintval = max;
break;
}
case kSMALLINT: {
chunkStats.min.smallintval = min;
chunkStats.max.smallintval = max;
Expand Down
4 changes: 4 additions & 0 deletions DataMgr/Encoder.cpp
Expand Up @@ -29,6 +29,10 @@ Encoder* Encoder::Create(Data_Namespace::AbstractBuffer* buffer, const SQLTypeIn
return new NoneEncoder<int8_t>(buffer);
break;
}
case kTINYINT: {
return new NoneEncoder<int8_t>(buffer);
break;
}
case kSMALLINT: {
return new NoneEncoder<int16_t>(buffer);
break;
Expand Down
30 changes: 30 additions & 0 deletions Import/Importer.cpp
Expand Up @@ -252,6 +252,9 @@ int8_t* appendDatum(int8_t* buf, Datum d, const SQLTypeInfo& ti) {
case kSMALLINT:
*(int16_t*)buf = d.smallintval;
return buf + sizeof(int16_t);
case kTINYINT:
*(int8_t*)buf = d.tinyintval;
return buf + sizeof(int8_t);
case kFLOAT:
*(float*)buf = d.floatval;
return buf + sizeof(float);
Expand Down Expand Up @@ -323,6 +326,9 @@ Datum TDatumToDatum(const TDatum& datum, SQLTypeInfo& ti) {
case kSMALLINT:
d.smallintval = datum.is_null ? inline_fixed_encoding_null_val(ti) : datum.val.int_val;
break;
case kTINYINT:
d.tinyintval = datum.is_null ? inline_fixed_encoding_null_val(ti) : datum.val.int_val;
break;
case kFLOAT:
d.floatval = datum.is_null ? NULL_FLOAT : datum.val.real_val;
break;
Expand Down Expand Up @@ -387,6 +393,18 @@ void TypedImportBuffer::add_value(const ColumnDescriptor* cd,
}
break;
}
case kTINYINT: {
if (!is_null && (isdigit(val[0]) || val[0] == '-')) {
SQLTypeInfo ti = cd->columnType;
Datum d = StringToDatum(val, ti);
addTinyint(d.tinyintval);
} else {
if (cd->columnType.get_notnull())
throw std::runtime_error("NULL for column " + cd->columnName);
addTinyint(inline_fixed_encoding_null_val(cd->columnType));
}
break;
}
case kSMALLINT: {
if (!is_null && (isdigit(val[0]) || val[0] == '-')) {
SQLTypeInfo ti = cd->columnType;
Expand Down Expand Up @@ -1103,6 +1121,15 @@ void TypedImportBuffer::add_value(const ColumnDescriptor* cd, const TDatum& datu
}
break;
}
case kTINYINT:
if (!is_null) {
addTinyint((int8_t)datum.val.int_val);
} else {
if (cd->columnType.get_notnull())
throw std::runtime_error("NULL for column " + cd->columnName);
addTinyint(inline_fixed_encoding_null_val(cd->columnType));
}
break;
case kSMALLINT:
if (!is_null) {
addSmallint((int16_t)datum.val.int_val);
Expand Down Expand Up @@ -2047,6 +2074,9 @@ void Loader::distributeToShards(std::vector<OneShardBuffers>& all_shard_import_b
case kBOOLEAN:
shard_output_buffers[col_idx]->addBoolean(int_value_at(*input_buffer, i));
break;
case kTINYINT:
shard_output_buffers[col_idx]->addTinyint(int_value_at(*input_buffer, i));
break;
case kSMALLINT:
shard_output_buffers[col_idx]->addSmallint(int_value_at(*input_buffer, i));
break;
Expand Down
17 changes: 17 additions & 0 deletions Import/Importer.h
Expand Up @@ -132,6 +132,9 @@ class TypedImportBuffer : boost::noncopyable {
case kBOOLEAN:
bool_buffer_ = new std::vector<int8_t>();
break;
case kTINYINT:
tinyint_buffer_ = new std::vector<int8_t>();
break;
case kSMALLINT:
smallint_buffer_ = new std::vector<int16_t>();
break;
Expand Down Expand Up @@ -198,6 +201,9 @@ class TypedImportBuffer : boost::noncopyable {
case kBOOLEAN:
delete bool_buffer_;
break;
case kTINYINT:
delete tinyint_buffer_;
break;
case kSMALLINT:
delete smallint_buffer_;
break;
Expand Down Expand Up @@ -258,6 +264,8 @@ class TypedImportBuffer : boost::noncopyable {

void addBoolean(const int8_t v) { bool_buffer_->push_back(v); }

void addTinyint(const int8_t v) { tinyint_buffer_->push_back(v); }

void addSmallint(const int16_t v) { smallint_buffer_->push_back(v); }

void addInt(const int32_t v) { int_buffer_->push_back(v); }
Expand Down Expand Up @@ -333,6 +341,8 @@ class TypedImportBuffer : boost::noncopyable {
switch (column_desc_->columnType.get_type()) {
case kBOOLEAN:
return reinterpret_cast<int8_t*>(&((*bool_buffer_)[0]));
case kTINYINT:
return reinterpret_cast<int8_t*>(&((*tinyint_buffer_)[0]));
case kSMALLINT:
return reinterpret_cast<int8_t*>(&((*smallint_buffer_)[0]));
case kINT:
Expand All @@ -358,6 +368,8 @@ class TypedImportBuffer : boost::noncopyable {
switch (column_desc_->columnType.get_type()) {
case kBOOLEAN:
return sizeof((*bool_buffer_)[0]);
case kTINYINT:
return sizeof((*tinyint_buffer_)[0]);
case kSMALLINT:
return sizeof((*smallint_buffer_)[0]);
case kINT:
Expand Down Expand Up @@ -414,6 +426,10 @@ class TypedImportBuffer : boost::noncopyable {
bool_buffer_->clear();
break;
}
case kTINYINT: {
tinyint_buffer_->clear();
break;
}
case kSMALLINT: {
smallint_buffer_->clear();
break;
Expand Down Expand Up @@ -493,6 +509,7 @@ class TypedImportBuffer : boost::noncopyable {
private:
union {
std::vector<int8_t>* bool_buffer_;
std::vector<int8_t>* tinyint_buffer_;
std::vector<int16_t>* smallint_buffer_;
std::vector<int32_t>* int_buffer_;
std::vector<int64_t>* bigint_buffer_;
Expand Down

0 comments on commit 656aad1

Please sign in to comment.