diff --git a/src/include/postgres_binary_reader.hpp b/src/include/postgres_binary_reader.hpp index 79853a32..c82a2aeb 100644 --- a/src/include/postgres_binary_reader.hpp +++ b/src/include/postgres_binary_reader.hpp @@ -231,7 +231,7 @@ struct PostgresBinaryReader { auto fractional_power_correction = fractional_power - config.scale; D_ASSERT(fractional_power_correction < 20); fractional_part = 0; - for (auto i = config.weight + 1; i < config.ndigits; i++) { + for (int32_t i = MaxValue(0, config.weight + 1); i < config.ndigits; i++) { if (i + 1 < config.ndigits) { // more digits remain - no need to compensate yet fractional_part *= NBASE; diff --git a/test/all_pg_types.sql b/test/all_pg_types.sql index d2312bc6..0e131ad2 100644 --- a/test/all_pg_types.sql +++ b/test/all_pg_types.sql @@ -185,3 +185,30 @@ INSERT INTO mixed_arrays VALUES ( INSERT INTO mixed_arrays VALUES ( ARRAY[1, 2, 3] ); + +-- Issue #136 - Inconsistent results from querying postgres numeric columns +create TABLE public_amounts ( + id bigint NOT NULL, + rate numeric NOT NULL +); +insert into public_amounts values + (1, 0.67), + (2, 0.067), + (3, 0.0067), + (4, 0.00067), + (5, 0.000067), + (6, 0.0000067), + (7, 0.00000067), + (8, 0.000000067), + (9, 0.0000000067), + (10, 0.00000000067), + (11, 0.000000000067), + (12, 0.0000000000067), + (13, 0.00000000000067), + (14, 0.000000000000067), + (15, 0.0000000000000067), + (16, 0.00000000000000067), + (17, 0.000000000000000067), + (18, 0.0000000000000000067), + (19, 0.00000000000000000067), + (20, 0.000000000000000000067); \ No newline at end of file diff --git a/test/sql/storage/bug136.test b/test/sql/storage/bug136.test new file mode 100644 index 00000000..b48b8ffe --- /dev/null +++ b/test/sql/storage/bug136.test @@ -0,0 +1,37 @@ +# name: test/sql/storage/bug136.test +# description: Issue #136 - Inconsistent results from querying postgres numeric columns +# group: [storage] + +require postgres_scanner + +require-env POSTGRES_TEST_DATABASE_AVAILABLE + +statement ok +ATTACH 'dbname=postgresscanner' AS s (TYPE POSTGRES) + +statement ok +USE s; + +query II +SELECT * FROM public_amounts ORDER BY id +---- +1 0.67 +2 0.067 +3 0.0067 +4 0.00067 +5 0.000067 +6 0.0000067 +7 0.00000067 +8 0.000000067 +9 0.0000000067 +10 0.00000000067 +11 0.000000000067 +12 0.0000000000067 +13 0.00000000000067 +14 0.000000000000067 +15 0.0000000000000067 +16 0.00000000000000067 +17 0.000000000000000067 +18 0.0000000000000000067 +19 0.00000000000000000067 +20 0.000000000000000000067