Skip to content

Commit d68ea68

Browse files
committed
feat: add bool/boolean type alias
Add bool and boolean as type aliases mapping to MySQL tinyint. Update tests and documentation accordingly.
1 parent 028dbad commit d68ea68

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

docs/src/design/tables/attributes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ libraries, making table definitions more readable and explicit about data precis
8585

8686
| Alias | MySQL Type | Description |
8787
|-------|------------|-------------|
88+
| `bool` / `boolean` | `tinyint` | Boolean value (0 or 1) |
8889
| `int8` | `tinyint` | 8-bit signed integer (-128 to 127) |
8990
| `uint8` | `tinyint unsigned` | 8-bit unsigned integer (0 to 255) |
9091
| `int16` | `smallint` | 16-bit signed integer (-32,768 to 32,767) |
@@ -108,6 +109,7 @@ class Measurement(dj.Manual):
108109
precise_value : float64 # double-precision measurement
109110
sample_count : uint32 # unsigned 32-bit counter
110111
sensor_flags : uint8 # 8-bit status flags
112+
is_valid : bool # boolean flag
111113
"""
112114
```
113115

src/datajoint/declare.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"UINT16": "smallint unsigned",
2929
"INT8": "tinyint",
3030
"UINT8": "tinyint unsigned",
31+
"BOOL": "tinyint",
3132
}
3233
MAX_TABLE_NAME_LENGTH = 64
3334
CONSTANT_LITERALS = {
@@ -50,14 +51,14 @@
5051
UINT16=r"uint16$",
5152
INT8=r"int8$",
5253
UINT8=r"uint8$",
54+
BOOL=r"bool(ean)?$", # aliased to tinyint
5355
# Native MySQL types
5456
INTEGER=r"((tiny|small|medium|big|)int|integer)(\s*\(.+\))?(\s+unsigned)?(\s+auto_increment)?|serial$",
5557
DECIMAL=r"(decimal|numeric)(\s*\(.+\))?(\s+unsigned)?$",
5658
FLOAT=r"(double|float|real)(\s*\(.+\))?(\s+unsigned)?$",
5759
STRING=r"(var)?char\s*\(.+\)$",
5860
JSON=r"json$",
5961
ENUM=r"enum\s*\(.+\)$",
60-
BOOL=r"bool(ean)?$", # aliased to tinyint(1)
6162
TEMPORAL=r"(date|datetime|time|timestamp|year)(\s*\(.+\))?$",
6263
INTERNAL_BLOB=r"(tiny|small|medium|long|)blob$",
6364
EXTERNAL_BLOB=r"blob@(?P<store>[a-z][\-\w]*)$",

tests/schema_type_aliases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class TypeAliasTable(dj.Manual):
2222
val_uint16 : uint16 # 16-bit unsigned integer
2323
val_int8 : int8 # 8-bit signed integer
2424
val_uint8 : uint8 # 8-bit unsigned integer
25+
val_bool : bool # boolean value
2526
"""
2627

2728

tests/test_type_aliases.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class TestTypeAliasPatterns:
2525
("uint16", "UINT16"),
2626
("int8", "INT8"),
2727
("uint8", "UINT8"),
28+
("bool", "BOOL"),
29+
("boolean", "BOOL"),
2830
],
2931
)
3032
def test_type_alias_pattern_matching(self, alias, expected_category):
@@ -47,6 +49,8 @@ def test_type_alias_pattern_matching(self, alias, expected_category):
4749
("uint16", "smallint unsigned"),
4850
("int8", "tinyint"),
4951
("uint8", "tinyint unsigned"),
52+
("bool", "tinyint"),
53+
("boolean", "tinyint"),
5054
],
5155
)
5256
def test_type_alias_mysql_mapping(self, alias, expected_mysql_type):
@@ -107,6 +111,7 @@ def test_heading_preserves_type_aliases(self, schema_type_aliases):
107111
assert "uint16" in heading_str
108112
assert "int8" in heading_str
109113
assert "uint8" in heading_str
114+
assert "bool" in heading_str
110115

111116

112117
class TestTypeAliasInsertFetch:
@@ -129,6 +134,7 @@ def test_insert_and_fetch(self, schema_type_aliases):
129134
val_uint16=65535, # max uint16
130135
val_int8=127, # max int8
131136
val_uint8=255, # max uint8
137+
val_bool=1, # boolean true
132138
)
133139

134140
table.insert1(test_data)
@@ -145,6 +151,7 @@ def test_insert_and_fetch(self, schema_type_aliases):
145151
assert fetched["val_uint16"] == test_data["val_uint16"]
146152
assert fetched["val_int8"] == test_data["val_int8"]
147153
assert fetched["val_uint8"] == test_data["val_uint8"]
154+
assert fetched["val_bool"] == test_data["val_bool"]
148155

149156
def test_insert_primary_key_with_aliases(self, schema_type_aliases):
150157
"""Test using type aliases in primary key."""

0 commit comments

Comments
 (0)