Skip to content

Commit ecbff69

Browse files
author
jseldess
authored
Merge pull request #1008 from knz/knz-updates
New docs on expressions and constants
2 parents f9f735a + 22b5eee commit ecbff69

16 files changed

+1373
-170
lines changed

bool.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ The `BOOL` [data type](data-types.html) stores a Boolean value of `false` or `tr
1212

1313
In CockroachDB, `BOOLEAN` is an alias for `BOOL`.
1414

15-
## Format
15+
## Syntax
1616

17-
When inserting into a `BOOL` column, format the value as `false` or `true` (case-insensitive).
17+
There are two predefined
18+
[named constants](sql-constants.html#named-constants) for `BOOL`:
19+
`TRUE` and `FALSE` (the names are case-insensitive).
1820

19-
Alternately, you can cast `0` or `1` as a `BOOL`:
21+
Alternately, a boolean value can be obtained by coercing a numeric
22+
value: zero is coerced to `FALSE`, and any non-zero value to `TRUE`.
2023

2124
- `CAST(0 AS BOOL)` (false)
22-
- `CAST(1 AS BOOL)` (true)
25+
- `CAST(123 AS BOOL)` (true)
2326

2427
## Size
2528

@@ -69,4 +72,4 @@ Type | Details
6972

7073
## See Also
7174

72-
[Data Types](data-types.html)
75+
[Data Types](data-types.html)

bytes.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ In CockroachDB, the following are aliases for `BYTES`:
1515
- `BYTEA`
1616
- `BLOB`
1717

18-
## Formats
18+
## Syntax
1919

20-
When inserting into a `BYTES` column, use any of the following formats:
20+
To express a byte array constant, see the section on
21+
[byte array literals](sql-constants.html#byte-array-literals) for more
22+
details. For example, the following three are equivalent literals for the same
23+
byte array: `b'abc'`, `b'\141\142\143'`, `b'\x61\x62\x63'`.
2124

22-
- 1 octet per byte: `b'\141\061\142\062\143\063'`
23-
- 2 hexadecimal digits per byte: `b'\x61\x31\x62\x32\x63\x33'`.
24-
- String literal: `'a1b2c3'`
25-
26-
You can also use these in combination, for example, `b'\141\061\x62\x32\c3'`.
25+
In addition to this syntax, CockroachDB also supports using
26+
[string literals](sql-constants.html#string-literals), including the
27+
syntax `'...'`, `e'...'` and `x'....'` in contexts where a byte array
28+
is otherwise expected.
2729

2830
## Size
2931

@@ -34,7 +36,12 @@ The size of a `BYTES` value is variable, but it's recommended to keep values und
3436
~~~ sql
3537
> CREATE TABLE bytes (a INT PRIMARY KEY, b BYTES);
3638

37-
> INSERT INTO bytes VALUES (1, 'abc'), (2, b'\141\142\143'), (3, b'\x61\x62\x63'), (4, b'\141\x62\c');
39+
> -- explicitly typed BYTES literals
40+
> INSERT INTO bytes VALUES (1, b'\141\142\143'), (2, b'\x61\x62\x63'), (3, b'\141\x62\c');
41+
42+
> -- string literal implicitly typed as BYTES
43+
> INSERT INTO bytes VALUES (4, 'abc');
44+
3845

3946
> SELECT * FROM bytes;
4047
~~~
@@ -50,14 +57,16 @@ The size of a `BYTES` value is variable, but it's recommended to keep values und
5057
(4 rows)
5158
~~~
5259

53-
## Supported Casting & Conversion
60+
## Supported Conversions
5461

55-
`BYTES` values can be [cast](data-types.html#data-type-conversions--casts) to any of the following data types:
62+
`BYTES` values can be
63+
[cast](data-types.html#data-type-conversions--casts) explicitly to
64+
`STRING`. The conversion verifies that the byte array contains only
65+
valid UTF-8 byte sequences; an error is reported otherwise.
5666

57-
Type | Details
58-
-----|--------
59-
`STRING` | Requires the byte array to contain only valid UTF-8 character encodings.
67+
`STRING` values can be cast explicitly to `BYTES`. This conversion
68+
always succeeds.
6069

6170
## See Also
6271

63-
[Data Types](data-types.html)
72+
[Data Types](data-types.html)

data-types.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ Type | Description | Example
2626

2727
CockroachDB supports explicit type conversions using the following methods:
2828

29-
- `<value>::<data type>`, or its equivalent longer form `CAST(<value> AS <data type>)`, which converts an arbitrary expression of one built-in type to another (this is also known as "casting"). For example:
30-
`NOW()::DECIMAL`, `VARIANCE(a+2)::INT`.
31-
3229
- `<type> 'string literal'`, to convert from the literal representation of a value to a value of that type. For example:
33-
`DATE '2008-12-21'`, `INT '123'`, or `BOOL 'true'`.
30+
`DATE '2008-12-21'`, `INT '123'`, or `BOOL 'true'`.
31+
32+
- `<value>::<data type>`, or its equivalent longer form `CAST(<value> AS <data type>)`, which converts an arbitrary expression of one built-in type to another (this is also known as type coercion or "casting"). For example:
33+
`NOW()::DECIMAL`, `VARIANCE(a+2)::INT`.
34+
35+
{{site.data.alerts.callout_success}}
36+
To create constant values, consider using a
37+
<a href="sql-expressions.html#explicitly-typed-expressions">type annotation</a>
38+
instead of a cast, as it provides more predictable results.
39+
{{site.data.alerts.end}}
3440

3541
- Other [built-in conversion functions](functions-and-operators.html) when the type is not a SQL type, for example `from_ip()`, `to_ip()` to convert IP addresses between `STRING` and `BYTES` values.
3642

37-
You can find each data type's supported converstion and casting on its respective page in the **Supported Casting & Conversion** section.
43+
44+
You can find each data type's supported converstion and casting on its
45+
respective page in its section **Supported Casting & Conversion**.

date.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ The `DATE` [data type](data-types.html) stores a year, month, and day.
88

99
<div id="toc"></div>
1010

11-
## Format
11+
## Syntax
1212

13-
When inserting into a `DATE` column, format the value as `DATE '2016-01-25'`.
13+
A constant value of type `DATE` can be expressed using an
14+
[interpreted literal](sql-constants.html#interpreted-literals), or a
15+
string literal
16+
[annotated with](sql-expressions.html#explicitly-typed-expressions)
17+
type `DATE` or
18+
[coerced to](sql-expressions.html#explicit-type-coercions) type
19+
`DATE`.
1420

15-
Alternatively, you can use a string literal, e.g., `'2016-01-25'`, which CockroachDB will resolve into the `DATE` type.
21+
The string format for dates is `YYYY-MM-DD`. For example: `DATE '2016-12-23'`.
1622

17-
Note that in some contexts, dates may be displayed with hours, minutes, seconds, and timezone set to 0.
23+
CockroachDB also supports using uninterpreted
24+
[string literals](sql-constants.html#string-literals) in contexts
25+
where a `DATE` value is otherwise expected.
1826

1927
## Size
2028

@@ -36,15 +44,20 @@ A `DATE` column supports values up to 8 bytes in width, but the total storage si
3644
+-------+------+-------+---------+
3745
~~~
3846
~~~ sql
47+
> -- explicitly typed DATE literal
3948
> INSERT INTO dates VALUES (DATE '2016-03-26', 12345);
4049

50+
> -- string literal implicitly typed as DATE
51+
> INSERT INTO dates VALUES ('2016-03-27', 12345);
52+
4153
> SELECT * FROM dates;
4254
~~~
4355
~~~
4456
+---------------------------+-------+
4557
| a | b |
4658
+---------------------------+-------+
4759
| 2016-03-26 00:00:00+00:00 | 12345 |
60+
| 2016-03-27 00:00:00+00:00 | 12345 |
4861
+---------------------------+-------+
4962
~~~
5063

@@ -64,4 +77,4 @@ Type | Details
6477

6578
## See Also
6679

67-
[Data Types](data-types.html)
80+
[Data Types](data-types.html)

decimal.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ When inserting a decimal value:
2626
- If digits to the left and right of the decimal point exceed the column's `precision`, CockroachDB gives an error.
2727
- If the column's `precision` and `scale` are identical, the inserted value must round to less than 1.
2828

29-
## Format
29+
## Syntax
3030

31-
When inserting into a `DECIMAL` column, format the value as a numeric literal, e.g., `1.2345` or `1`.
32-
33-
Alternately, you can cast a float as a decimal: `CAST(1.2345 AS DECIMAL)`. However, note that the precision will be limited to 17 digits in total (both to the left and right of the decimal point).
31+
A constant value of type `DECIMAL` can be entered as a [numeric literal](sql-constants.html#numeric-literals).
32+
For example: `1.414` or `-1234`.
3433

3534
## Size
3635

@@ -53,19 +52,19 @@ The size of a `DECIMAL` value is variable, starting at 9 bytes. It's recommended
5352
+-------+---------------+-------+---------+
5453
~~~
5554
~~~ sql
56-
> INSERT INTO decimals VALUES (1.01234567890123456789, 1.01234567890123456789, CAST(1.01234567890123456789 AS DECIMAL));
55+
> INSERT INTO decimals VALUES (1.01234567890123456789, 1.01234567890123456789, 1.01234567890123456789);
5756

5857
> SELECT * FROM decimals;
5958
~~~
6059
~~~
61-
+------------------------+---------+--------------------+
62-
| a | b | c |
63-
+------------------------+---------+--------------------+
64-
| 1.01234567890123456789 | 1.01235 | 1.0123456789012346 |
65-
+------------------------+---------+--------------------+
60+
+------------------------+---------+-----------------------+
61+
| a | b | c |
62+
+------------------------+---------+-----------------------+
63+
| 1.01234567890123456789 | 1.01235 | 1.0123456789012346789 |
64+
+------------------------+---------+-----------------------+
6665
# The value in "a" matches what was inserted exactly.
6766
# The value in "b" has been rounded to the column's scale.
68-
# The value in "c" has been limited to 17 digits.
67+
# The value in "c" is handled like "a" because NUMERIC is an alias.
6968
~~~
7069

7170
## Supported Casting & Conversion
@@ -79,8 +78,6 @@ Type | Details
7978
`BOOL` | **0** converts to `false`; all other values convert to `true`
8079
`STRING` | ––
8180

82-
{{site.data.alerts.callout_info}}Because the <a href="serial.html"><code>SERIAL</code> data type</a> represents values automatically generated CockroachDB to uniquely identify rows, you cannot meaningfully cast other data types as <code>SERIAL</code> values.{{site.data.alerts.end}}
83-
8481
## See Also
8582

86-
[Data Types](data-types.html)
83+
[Data Types](data-types.html)

float.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ summary: The FLOAT data type stores inexact, floating-point numbers with up to 1
44
toc: false
55
---
66

7-
The `FLOAT` [data type](data-types.html) stores inexact, floating-point numbers with up to 17 digits in total and at least one digit to the right of the decimal point.
7+
The `FLOAT` [data type](data-types.html) stores inexact, floating-point numbers with up to 17 digits of decimal precision.
8+
9+
They are handled internally using the [standard double-precision
10+
(64-bit binary-encoded) IEEE754 format](https://en.wikipedia.org/wiki/IEEE_floating_point).
811

912
<div id="toc"></div>
1013

@@ -15,14 +18,20 @@ In CockroachDB, the following are aliases for `FLOAT`:
1518
- `REAL`
1619
- `DOUBLE PRECISION`
1720

18-
## Format
21+
## Syntax
1922

20-
When inserting into a `FLOAT` column, format the value as a numeric literal, e.g., `1.2345` or `1`.
23+
A constant value of type `FLOAT` can be entered as a [numeric literal](sql-constants.html#numeric-literals).
24+
For example: `1.414` or `-1234`.
2125

22-
Alternately, you can cast `+Inf` (positive infinity), `-Inf` (negative infinity), or `NaN` (not a number) as a float:
26+
The special IEEE754 values for positive infinity, negative infinity
27+
and Not A Number (NaN) cannot be entered using numeric literals
28+
directly and must be converted using an
29+
[interpreted literal](sql-constants.html#interpreted-literals) or an
30+
[explicit conversion](sql-expressions.html#explicit-type-coercions) from
31+
a string literal instead. For example:
2332

24-
- `CAST('+Inf' AS FLOAT)`
25-
- `CAST('-Inf' AS FLOAT)`
33+
- `FLOAT '+Inf'`
34+
- `'-Inf'::FLOAT`
2635
- `CAST('NaN' AS FLOAT)`
2736

2837
## Size
@@ -61,16 +70,15 @@ A `FLOAT` column supports values up to 8 bytes in width, but the total storage s
6170

6271
## Supported Casting & Conversion
6372

64-
`DECIMAL` values can be [cast](data-types.html#data-type-conversions--casts) to any of the following data types:
73+
`FLOAT` values can be [cast](data-types.html#data-type-conversions--casts) to any of the following data types:
6574

6675
Type | Details
6776
-----|--------
6877
`INT` | Truncates decimal precision and requires values to be between -2^63 and 2^63-1
69-
`DECIMAL` | ––
78+
`DECIMAL` | Causes an error to be reported if the value is NaN or +/- Inf.
7079
`BOOL` | **0** converts to `false`; all other values convert to `true`
71-
72-
{{site.data.alerts.callout_info}}Because the <a href="serial.html"><code>SERIAL</code> data type</a> represents values automatically generated CockroachDB to uniquely identify rows, you cannot meaningfully cast other data types as <code>SERIAL</code> values.{{site.data.alerts.end}}
80+
`STRING` | --
7381

7482
## See Also
7583

76-
[Data Types](data-types.html)
84+
[Data Types](data-types.html)

int.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ In CockroachDB, the following are aliases for `INT`:
2020
- `INT64`
2121
- `BIGINT`
2222

23-
## Formats
23+
## Syntax
2424

25-
An `INT` column accepts numeric literals and hexadecimal-encoded numeric literals.
26-
27-
### Numeric Literal
28-
29-
When inserting a numeric literal into an `INT` column, format the value as `12345`.
30-
31-
### Hexadecimal-Encoded Numeric Literal
32-
33-
When inserting a hexadecimal-encoded numeric literal into a `INT` column, format the value as hexadecimal digits preceded by `0x`. For example, `0xcafe1111` corresponds to the numeric literal `3405648145`.
25+
A constant value of type `INT` can be entered as a [numeric literal](sql-constants.html#numeric-literals).
26+
For example: `42`, `-1234` or `0xCAFE`.
3427

3528
## Size
3629

@@ -74,15 +67,13 @@ CockroachDB does not offer multiple integer types for different widths; instead,
7467
Type | Details
7568
-----|--------
7669
`DECIMAL` | ––
77-
`FLOAT` | Requires `INT` value to be less than 2^53
70+
`FLOAT` | Loses precision if the `INT` value is larger than 2^53 in magnitude
7871
`BOOL` | **0** converts to `false`; all other values convert to `true`
7972
`DATE` | Converts to days since the Unix epoch (Jan. 1, 1970)
8073
`TIMESTAMP` | Converts to seconds since the Unix epoch (Jan. 1, 1970)
8174
`INTERVAL` | Converts to microseconds
8275
`STRING` | ––
8376

84-
{{site.data.alerts.callout_info}}Because the <a href="serial.html"><code>SERIAL</code> data type</a> represents values automatically generated CockroachDB to uniquely identify rows, you cannot meaningfully cast other data types as <code>SERIAL</code> values.{{site.data.alerts.end}}
85-
8677
## See Also
8778

88-
[Data Types](data-types.html)
79+
[Data Types](data-types.html)

interval.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,28 @@ The `INTERVAL` [data type](data-types.html) stores a value that represents a spa
88

99
<div id="toc"></div>
1010

11-
## Formats
11+
## Syntax
1212

13-
When inserting into an `INTERVAL` column, use one of the following formats:
13+
A constant value of type `INTERVAL` can be expressed using an
14+
[interpreted literal](sql-constants.html#interpreted-literals), or a
15+
string literal
16+
[annotated with](sql-expressions.html#explicitly-typed-expressions)
17+
type `INTERVAL` or
18+
[coerced to](sql-expressions.html#explicit-type-coercions) type
19+
`INTERVAL`.
20+
21+
`INTERVAL` constants can be expressed using the following formats:
1422

1523
Format | Description
1624
-------|--------
17-
Golang | `INTERVAL '1h2m3s4ms5us6ns'`<br><br>Note that `ms` is milliseconds, `us` is microseconds, and `ns` is nanoseconds. Also, all fields support both integers and floats.
18-
Traditional Postgres | `INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'`
19-
ISO 8601 | `INTERVAL 'P1Y2M3DT4H5M6S'`
2025
SQL Standard | `INTERVAL 'Y-M D H:M:S'`<br><br>`Y-M D`: Using a single value defines days only; using two values defines years and months. Values must be integers.<br><br>`H:M:S`: Using a single value defines seconds only; using two values defines hours and minutes. Values can be integers or floats.<br><br>Note that each side is optional.
26+
ISO 8601 | `INTERVAL 'P1Y2M3DT4H5M6S'`
27+
Traditional PostgreSQL | `INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'`
28+
Golang | `INTERVAL '1h2m3s4ms5us6ns'`<br><br>Note that `ms` is milliseconds, `us` is microseconds, and `ns` is nanoseconds. Also, all fields support both integers and floats.
2129

22-
Alternatively, you can use a string literal, e.g., `'1h2m3s4ms5us6ns'` or`'1 year 2 months 3 days 4 hours 5 minutes 6 seconds'`, which CockroachDB will resolve into the `INTERVAL` type.
30+
CockroachDB also supports using uninterpreted
31+
[string literals](sql-constants.html#string-literals) in contexts
32+
where a `INTERVAL` value is otherwise expected.
2333

2434
Intervals are stored internally as months, days, and nanoseconds.
2535

@@ -51,9 +61,9 @@ CREATE TABLE
5161
~~~
5262

5363
~~~ sql
54-
> INSERT INTO intervals VALUES
55-
(1, INTERVAL '1h2m3s4ms5us6ns'),
56-
(2, INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'),
64+
> INSERT INTO intervals VALUES
65+
(1, INTERVAL '1h2m3s4ms5us6ns'),
66+
(2, INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'),
5767
(3, INTERVAL '1-2 3 4:5:6');
5868
~~~
5969

@@ -89,4 +99,4 @@ Type | Details
8999

90100
## See Also
91101

92-
[Data Types](data-types.html)
102+
[Data Types](data-types.html)

0 commit comments

Comments
 (0)