Skip to content

Commit 8911b2e

Browse files
authored
feat: Add a Bool column type (#6861)
1 parent 9d07c38 commit 8911b2e

File tree

8 files changed

+41
-3
lines changed

8 files changed

+41
-3
lines changed

snuba/clickhouse/columns.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Sequence, Union
44

5-
from snuba.utils.schemas import UUID, AggregateFunction, Any, Array, Column
5+
from snuba.utils.schemas import UUID, AggregateFunction, Any, Array, Bool, Column
66
from snuba.utils.schemas import ColumnSet as BaseColumnSet
77
from snuba.utils.schemas import (
88
ColumnType,
@@ -35,6 +35,7 @@
3535
"Any",
3636
"AggregateFunction",
3737
"Array",
38+
"Bool",
3839
"Column",
3940
"ColumnSet",
4041
"ColumnType",

snuba/datasets/configuration/json_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def del_name_field(column_schema: dict[str, Any]) -> dict[str, Any]:
141141

142142

143143
NO_ARG_SCHEMA = make_column_schema(
144-
column_type={"enum": ["String", "DateTime", "UUID", "IPv4", "IPv6"]},
144+
column_type={"enum": ["String", "DateTime", "UUID", "IPv4", "IPv6", "Bool"]},
145145
args={
146146
"type": "object",
147147
"properties": {},

snuba/datasets/configuration/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from snuba.utils.schemas import (
2121
UUID,
2222
AggregateFunction,
23+
Bool,
2324
ColumnType,
2425
FixedString,
2526
Int,
@@ -75,6 +76,7 @@ def get_mandatory_condition_checkers(
7576
"UUID": UUID,
7677
"IPv4": IPv4,
7778
"IPv6": IPv6,
79+
"Bool": Bool,
7880
}
7981

8082

snuba/migrations/parse_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
UUID,
1212
AggregateFunction,
1313
Array,
14+
Bool,
1415
ColumnType,
1516
Date,
1617
DateTime,
@@ -35,7 +36,7 @@
3536
# datetime64 needs to be before basic_type to not be parsed as DateTime
3637
primitive = datetime64 / basic_type / uint / int / float / fixedstring / enum
3738
# DateTime must come before Date
38-
basic_type = "DateTime" / "Date" / "IPv4" / "IPv6" / "String" / "UUID"
39+
basic_type = "DateTime" / "Date" / "IPv4" / "IPv6" / "String" / "UUID" / "Bool"
3940
uint = "UInt" uint_size
4041
int = "Int" uint_size
4142
uint_size = "8" / "16" / "32" / "64" / "128"
@@ -82,6 +83,7 @@ def merge_modifiers(
8283

8384

8485
_TYPES: dict[str, type[ColumnType[MigrationModifiers]]] = {
86+
"Bool": Bool,
8587
"Date": Date,
8688
"DateTime": DateTime,
8789
"IPv4": IPv4,

snuba/utils/schemas.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,23 @@ def _valid_tuple(self, tuple_column: Tuple[AnyType], value: tuple[Any]) -> bool:
852852
f"Invalid value {el} for column type {tuple_column.types[i]}"
853853
)
854854
return True
855+
856+
857+
class Bool(ColumnType[TModifiers]):
858+
def __init__(self, modifiers: Optional[TModifiers] = None) -> None:
859+
super().__init__(modifiers)
860+
861+
def __eq__(self, other: object) -> bool:
862+
return (
863+
self.__class__ == other.__class__
864+
and self.get_modifiers() == cast(Int[TModifiers], other).get_modifiers()
865+
)
866+
867+
def _for_schema_impl(self) -> str:
868+
return "Bool"
869+
870+
def set_modifiers(self, modifiers: Optional[TModifiers]) -> Bool[TModifiers]:
871+
return Bool(modifiers=modifiers)
872+
873+
def get_raw(self) -> Bool[TModifiers]:
874+
return Bool()

tests/clickhouse/test_columns.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
UUID,
88
AggregateFunction,
99
Array,
10+
Bool,
1011
Column,
1112
ColumnType,
1213
Date,
@@ -173,6 +174,13 @@
173174
"Nullable(Enum('a' = 1, 'b' = 2))",
174175
id="enums",
175176
),
177+
pytest.param(
178+
Bool(Modifier(nullable=True)),
179+
Bool(),
180+
Bool(),
181+
"Nullable(Bool)",
182+
id="bools",
183+
),
176184
]
177185

178186

tests/datasets/configuration/test_storage_loader.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from snuba.clickhouse.columns import (
88
Array,
9+
Bool,
910
Column,
1011
DateTime,
1112
DateTime64,
@@ -153,6 +154,7 @@ def test_column_parser(self) -> None:
153154
{"name": "int_col", "type": "UInt", "args": {"size": 64}},
154155
{"name": "float_col", "type": "Float", "args": {"size": 32}},
155156
{"name": "string_col", "type": "String"},
157+
{"name": "bool_col", "type": "Bool"},
156158
{"name": "time_col", "type": "DateTime"},
157159
{
158160
"name": "time64_col",
@@ -209,6 +211,7 @@ def test_column_parser(self) -> None:
209211
Column("int_col", UInt(64)),
210212
Column("float_col", Float(32)),
211213
Column("string_col", String()),
214+
Column("bool_col", Bool()),
212215
Column("time_col", DateTime()),
213216
Column("time64_col", DateTime64(3, "America/New_York")),
214217
Column("nested_col", Nested([Column("sub_col", UInt(64))])),

tests/migrations/test_parse_schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
UUID,
77
AggregateFunction,
88
Array,
9+
Bool,
910
ColumnType,
1011
Date,
1112
DateTime,
@@ -38,6 +39,7 @@
3839
(("UInt32", "", "", ""), UInt(32)),
3940
(("UInt128", "", "", ""), UInt(128)),
4041
(("UUID", "", "", ""), UUID()),
42+
(("Bool", "", "", ""), Bool()),
4143
# Aggregate functions
4244
(
4345
("AggregateFunction(uniq, UInt8)", "", "", ""),

0 commit comments

Comments
 (0)