Skip to content

decimal support for reading and writing #80

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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions R/ClickhouseResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ setMethod("dbFetch", signature = "ClickhouseResult", definition = function(res,
}
ret <- fetch(res@ptr, n)
ret <- convert_Int64(ret, res@Int64)
ret <- convert_Decimal(ret)

if(res@toUTF8 == TRUE) ret <- encode_UTF(ret)

Expand All @@ -50,6 +51,28 @@ convert_Int64 <- function(df, Int64) {
}
}

convert_Decimal <- function(df) {
# identify the columns by index that need changing
toConvert <- which(grepl("Decimal", attr(df, 'data.type')))

# get the scales
parse_scale <- function(str){
x <- strsplit(str, ",")[[1]][[2]]
x <- substr(x,1,nchar(x)-1)
x <- as.numeric(x)
return(as.numeric(x))
}

for(column_i in toConvert) {
raw_type <- attr(df, 'data.type')[[column_i]]
scale <- parse_scale(raw_type)
# forces String to decimal representation
df[column_i] <- lapply(df[column_i], as.numeric)
df[column_i] <- df[column_i] / (10^scale)
}
return(df)
}

encode_UTF <- function(df){
toConvert <- which(attr(df, "data.type") %in% c("String", "FixedString", "Nullable(String)", "Nullable(FixedString)"))

Expand Down
34 changes: 29 additions & 5 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using namespace Rcpp;
using namespace clickhouse;

ColumnRef convertDecimalColumn(SEXP v);

// [[Rcpp::export]]
DataFrame fetch(XPtr<Result> res, ssize_t n) {
return res->fetchFrame(n);
Expand Down Expand Up @@ -165,10 +167,10 @@ std::shared_ptr<CT> vecToScalar(SEXP v, std::shared_ptr<ColumnUInt8> nullCol = n
type_of_cor = Rf_inherits(v, "integer64") ? 99 : type_of;

switch(type_of_cor) {
case 99: {
toColumnN<CT, NumericVector>(v, col, nullCol);
break;
}
case 99: {
toColumnN<CT, NumericVector>(v, col, nullCol);
break;
}
case INTSXP: {
// the lambda could be a default argument of toColumn, but that
// appears to trigger a bug in GCC
Expand Down Expand Up @@ -337,7 +339,12 @@ std::shared_ptr<CT> vecToEnum(SEXP v, TypeRef type, std::shared_ptr<ColumnUInt8>

ColumnRef vecToColumn(TypeRef t, SEXP v, std::shared_ptr<ColumnUInt8> nullCol = nullptr) {
using TC = Type::Code;

printf("%i insert THIS !!!\n",t->GetCode());

switch(t->GetCode()) {
case TC::Decimal:
return convertDecimalColumn(v);
case TC::Int8:
return vecToScalar<ColumnInt8, int8_t>(v, nullCol);
case TC::Int16:
Expand Down Expand Up @@ -427,10 +434,27 @@ void insert(XPtr<Client> conn, String tableName, DataFrame df) {
ColumnRef ccol = vecToColumn(colTypes[i], df[i]);
block.AppendColumn(std::string(names[i]), ccol);
}

conn->Insert(tableName, block);
}

ColumnRef convertDecimalColumn(SEXP v) {
// R_CH-column: Rcpp IntegerVector
IntegerVector cv = Rcpp::as<IntegerVector>(v);


// CH_CPP-column: sharedPtr to ColumnDecimal with (18,1)
std::shared_ptr<ColumnDecimal> col = std::make_shared<ColumnDecimal>(18, 1);


for(typename IntegerVector::stored_type e : cv) {
col->Append(e);
}

return col;
}



// [[Rcpp::export]]
bool validPtr(SEXP ptr) {
return R_ExternalPtrAddr(ptr);
Expand Down
16 changes: 16 additions & 0 deletions src/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ void convertEntries(std::shared_ptr<const CT> in, NullCol nullCol, RT &out,
}
}

template<>
void convertEntries<ch::ColumnDecimal, Rcpp::StringVector>(std::shared_ptr<const ch::ColumnDecimal> in, NullCol nullCol, Rcpp::StringVector &out,
size_t offset, size_t start, size_t end) {
for(size_t j = start; j < end; j++) {
// can't use the ternary operator here, since that would require explicit
// conversion from the Clickhouse storage type (which is far messier)
if(nullCol && nullCol->IsNull(j)) {
out[offset+j-start] = Rcpp::StringVector::get_na();
} else {
out[offset+j-start] = (in->At(j).to_string());
}
}
}

template<>
void convertEntries<ch::ColumnInt64, Rcpp::StringVector>(std::shared_ptr<const ch::ColumnInt64> in, NullCol nullCol, Rcpp::StringVector &out,
Expand Down Expand Up @@ -278,6 +291,9 @@ std::unique_ptr<Converter> Result::buildConverter(std::string name, ch::TypeRef
case TC::UInt64: {
return std::unique_ptr<ScalarConverter<ch::ColumnUInt64, Rcpp::StringVector>>(new ScalarConverter<ch::ColumnUInt64, Rcpp::StringVector>);
}
case TC::Decimal: {
return std::unique_ptr<ScalarConverter<ch::ColumnDecimal, Rcpp::StringVector>>(new ScalarConverter<ch::ColumnDecimal, Rcpp::StringVector>);
}
case TC::UUID:
return std::unique_ptr<ScalarConverter<ch::ColumnUUID, Rcpp::StringVector>>(new ScalarConverter<ch::ColumnUUID, Rcpp::StringVector>);
case TC::Float32:
Expand Down