From e9d1231a5d5eaa2b6310c51dbfeec74c422c4cb0 Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Mon, 8 Dec 2025 17:56:47 -0500 Subject: [PATCH] Use `ColumnDecimal->GetString` Instead of converting the Int128 value from `clickhouse-cpp`, use the new `ColumnDecimal->GetString()` function to get the stringification of a decimal value fetched from ClickHouse. This greatly simplifies the code and restores the ability to use the `clickhouse-cpp` shared library, re-enabled in the `Makefile` for all OSes other than Darwin. --- .gitmodules | 2 +- Makefile | 4 ++-- src/binary.cpp | 45 ++----------------------------------------- vendor/clickhouse-cpp | 2 +- 4 files changed, 6 insertions(+), 47 deletions(-) diff --git a/.gitmodules b/.gitmodules index 55b8de3..147d9b5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "clickhouse-cpp"] path = vendor/clickhouse-cpp - url = https://github.com/ClickHouse/clickhouse-cpp.git + url = https://github.com/theory/clickhouse-cpp.git diff --git a/Makefile b/Makefile index d8a41c6..982fdea 100644 --- a/Makefile +++ b/Makefile @@ -33,9 +33,9 @@ CH_CPP_FLAGS = -D CMAKE_BUILD_TYPE=Release -D WITH_OPENSSL=ON # Build static on Darwin by default. ifndef ($(CH_BUILD)) -# ifeq ($(OS),Darwin) +ifeq ($(OS),Darwin) CH_BUILD = static -# endif +endif endif # Are we statically compiling clickhouse-cpp into the extension or no? diff --git a/src/binary.cpp b/src/binary.cpp index f392da6..84dc9ed 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -778,50 +778,9 @@ static Datum make_datum(clickhouse::ColumnRef col, size_t row, Oid * valtype, bo case Type::Code::Decimal32: case Type::Code::Decimal: { - auto decCol = col->As(); - auto val = decCol->At(row); - - /* Convert the Int128 to a string. */ - std::stringstream ss; - ss << val; - std::string str = ss.str(); - - /* Start a destination string. */ - std::stringstream res; - auto scale = decCol->GetScale(); - - /* Output a dash for negative values */ - if (val < 0) - { - res << '-'; - str.erase(0, 1); - } - - if (scale == 0) - { - /* No decimal point, just output the entire value. */ - res << str; - } - else if (str.length() <= scale) - { - /* Append the entire value prepended with zeros after the decimal. */ - res << "0." << std::string(scale-str.length(), '0') << str; - } - else - { - /* There are digits before the decimal. */ - auto decAt = str.length() - scale; - res << str.substr(0, decAt); - - /* Append any digits after the decimal. */ - if (decAt < str.length()) - { - res << '.' << str.substr(decAt); - } - } - + auto val = col->As()->GetString(row); ret = DirectFunctionCall3(numeric_in, - CStringGetDatum(res.str().c_str()), + CStringGetDatum(val.c_str()), ObjectIdGetDatum(0), Int32GetDatum(-1)); *valtype = NUMERICOID; diff --git a/vendor/clickhouse-cpp b/vendor/clickhouse-cpp index b8544bb..6ff8e4b 160000 --- a/vendor/clickhouse-cpp +++ b/vendor/clickhouse-cpp @@ -1 +1 @@ -Subproject commit b8544bbf43140a0490a8929e7a726de466aefff3 +Subproject commit 6ff8e4bc84a50e529878986c4dd0014f57da1f59