Skip to content

Commit 0f4b76d

Browse files
authored
fix negative integer parsing in Enum8 and Enum16 types (#295)
* fix negative integer parsing in Enum8 and Enum16 types * add pr to changelog
1 parent da9e86a commit 0f4b76d

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- fix negative integer parsing in Enum8 and Enum16 types https://github.com/plausible/ch/pull/295
6+
37
## 0.7.0 (2026-01-13)
48

59
- use `disconnect_and_retry` (added in DBConnection v2.9.0) instead of `disconnect` for connection errors https://github.com/plausible/ch/pull/292

lib/ch/types.ex

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,19 +546,20 @@ defmodule Ch.Types do
546546
end
547547

548548
defp decode_int(<<?-, i, rest::bytes>>, stack, outer_acc) when is_numeric(i) do
549-
decode_int_cont(rest, -(i - ?0), stack, outer_acc)
549+
decode_int_cont(rest, i - ?0, -1, stack, outer_acc)
550550
end
551551

552552
defp decode_int(<<i, rest::bytes>>, stack, outer_acc) when is_numeric(i) do
553-
decode_int_cont(rest, i - ?0, stack, outer_acc)
553+
decode_int_cont(rest, i - ?0, 1, stack, outer_acc)
554554
end
555555

556-
defp decode_int_cont(<<i, rest::bytes>>, acc, stack, outer_acc) when is_numeric(i) do
557-
decode_int_cont(rest, acc * 10 + i - ?0, stack, outer_acc)
556+
defp decode_int_cont(<<i, rest::bytes>>, acc, multiplier, stack, outer_acc)
557+
when is_numeric(i) do
558+
decode_int_cont(rest, acc * 10 + i - ?0, multiplier, stack, outer_acc)
558559
end
559560

560-
defp decode_int_cont(<<rest::bytes>>, int, stack, acc) do
561-
decode(stack, rest, [int | acc])
561+
defp decode_int_cont(<<rest::bytes>>, int, multiplier, stack, acc) do
562+
decode(stack, rest, [int * multiplier | acc])
562563
end
563564

564565
@doc """

test/ch/types_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ defmodule Ch.TypesTest do
158158

159159
assert decode(" Enum8 ( 'hello' = 1 , 'world' = 2 ) ") ==
160160
{:enum8, [{"hello", 1}, {"world", 2}]}
161+
162+
assert decode("Enum8('enum8_min' = -128, 'enum8_zero' = 0, 'enum8_max' = 127)") ==
163+
{:enum8, [{"enum8_min", -128}, {"enum8_zero", 0}, {"enum8_max", 127}]}
164+
165+
assert decode("Enum16('enum16_min' = -32768, 'enum16_zero' = 0, 'enum16_max' = 32767)") ==
166+
{:enum16, [{"enum16_min", -32768}, {"enum16_zero", 0}, {"enum16_max", 32767}]}
161167
end
162168

163169
test "simple aggregate function" do

0 commit comments

Comments
 (0)