|
6 | 6 | } |
7 | 7 | --- |
8 | 8 |
|
9 | | -## BOOLEAN |
10 | | -### Description |
11 | | -BOOL, BOOLEAN |
12 | | -Like TINYINT, 0 stands for false and 1 for true. |
| 9 | +## Description |
| 10 | + |
| 11 | +BOOLEAN (alias: BOOL) is a data type in Doris that represents boolean values: true and false. |
| 12 | + |
| 13 | +Internally, BOOLEAN is stored as a uint8 value, where 0 represents false and 1 represents true. |
| 14 | + |
| 15 | +Unlike MySQL where BOOLEAN is an alias for TINYINT(1), Doris treats BOOLEAN as a separate data type, similar to PostgreSQL, Oracle, and other database systems. |
| 16 | + |
| 17 | +## Value Range |
| 18 | + |
| 19 | +BOOLEAN values can only be: |
| 20 | +- `true` (represented as 1 when displayed) |
| 21 | +- `false` (represented as 0 when displayed) |
| 22 | + |
| 23 | +In memory, BOOLEAN type only exists as 0 or 1, with no other possible values. |
| 24 | + |
| 25 | +## Literal Values |
| 26 | + |
| 27 | +In Doris, you can use the keywords `true` and `false` (case-insensitive) to represent boolean literal values: |
| 28 | + |
| 29 | +```sql |
| 30 | +mysql> select TrUe, False, true; |
| 31 | ++------+-------+------+ |
| 32 | +| TrUe | False | true | |
| 33 | ++------+-------+------+ |
| 34 | +| 1 | 0 | 1 | |
| 35 | ++------+-------+------+ |
| 36 | +``` |
| 37 | + |
| 38 | +## Supported Operations |
| 39 | + |
| 40 | +### Logical Operations |
| 41 | + |
| 42 | +BOOLEAN type supports logical operations such as AND, OR, NOT, and XOR: |
13 | 43 |
|
14 | | -### keywords |
15 | | -BOOLEAN |
| 44 | +```sql |
| 45 | +mysql> select true AND false, true OR false, NOT true, true XOR false; |
| 46 | ++----------------+---------------+----------+----------------+ |
| 47 | +| true AND false | true OR false | NOT true | true XOR false | |
| 48 | ++----------------+---------------+----------+----------------+ |
| 49 | +| 0 | 1 | 0 | 1 | |
| 50 | ++----------------+---------------+----------+----------------+ |
| 51 | +``` |
| 52 | + |
| 53 | +### Arithmetic Operations |
| 54 | + |
| 55 | +While BOOLEAN doesn't directly support arithmetic operations, expressions like `true + true` will work due to implicit type conversion: |
| 56 | + |
| 57 | +```sql |
| 58 | +mysql> select true + true; |
| 59 | ++-------------+ |
| 60 | +| true + true | |
| 61 | ++-------------+ |
| 62 | +| 2 | |
| 63 | ++-------------+ |
| 64 | +``` |
| 65 | + |
| 66 | +This works because the boolean values are implicitly cast to SMALLINT: `CAST(TRUE AS smallint) + CAST(TRUE AS smallint)`. |
| 67 | + |
| 68 | +## Type Conversion |
| 69 | + |
| 70 | +It's important to note that BOOLEAN is not equivalent to TINYINT in Doris, even though they may appear similar due to MySQL conventions. |
| 71 | + |
| 72 | +When inserting a boolean literal into a TINYINT column, implicit type conversion occurs: |
| 73 | + |
| 74 | +```sql |
| 75 | +CREATE TABLE test_boolean( |
| 76 | + u8 TINYINT |
| 77 | +) |
| 78 | +properties("replication_num" = "1"); |
| 79 | + |
| 80 | +mysql> insert into test_boolean values(true); |
| 81 | +``` |
| 82 | + |
| 83 | +In this example, the boolean literal `true` is converted to a TINYINT value. |
| 84 | + |
| 85 | +## Keywords |
| 86 | + |
| 87 | +BOOL, BOOLEAN |
0 commit comments