Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INET data type #2439

Merged
merged 5 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions _includes/sidebar-data-v2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -735,57 +735,57 @@
]
},
{
"title": "<code>INT</code>",
"title": "<code>ARRAY</code>",
"urls": [
"/${VERSION}/int.html"
"/${VERSION}/array.html"
]
},
{
"title": "<code>UUID</code>",
"title": "<code>BOOL</code>",
"urls": [
"/${VERSION}/uuid.html"
"/${VERSION}/bool.html"
]
},
{
"title": "<code>SERIAL</code>",
"title": "<code>BYTES</code>",
"urls": [
"/${VERSION}/serial.html"
"/${VERSION}/bytes.html"
]
},
{
"title": "<code>DECIMAL</code>",
"title": "<code>COLLATE</code>",
"urls": [
"/${VERSION}/decimal.html"
"/${VERSION}/collate.html"
]
},
{
"title": "<code>FLOAT</code>",
"title": "<code>DATE</code>",
"urls": [
"/${VERSION}/float.html"
"/${VERSION}/date.html"
]
},
{
"title": "<code>BOOL</code>",
"title": "<code>DECIMAL</code>",
"urls": [
"/${VERSION}/bool.html"
"/${VERSION}/decimal.html"
]
},
{
"title": "<code>DATE</code>",
"title": "<code>FLOAT</code>",
"urls": [
"/${VERSION}/date.html"
"/${VERSION}/float.html"
]
},
{
"title": "<code>TIME</code>",
"title": "<code>INET</code>",
"urls": [
"/${VERSION}/time.html"
"/${VERSION}/inet.html"
]
},
{
"title": "<code>TIMESTAMP</code>",
"title": "<code>INT</code>",
"urls": [
"/${VERSION}/timestamp.html"
"/${VERSION}/int.html"
]
},
{
Expand All @@ -794,28 +794,34 @@
"/${VERSION}/interval.html"
]
},
{
"title": "<code>SERIAL</code>",
"urls": [
"/${VERSION}/serial.html"
]
},
{
"title": "<code>STRING</code>",
"urls": [
"/${VERSION}/string.html"
]
},
{
"title": "<code>COLLATE</code>",
"title": "<code>TIME</code>",
"urls": [
"/${VERSION}/collate.html"
"/${VERSION}/time.html"
]
},
{
"title": "<code>BYTES</code>",
"title": "<code>TIMESTAMP</code>",
"urls": [
"/${VERSION}/bytes.html"
"/${VERSION}/timestamp.html"
]
},
{
"title": "<code>ARRAY</code>",
"title": "<code>UUID</code>",
"urls": [
"/${VERSION}/array.html"
"/${VERSION}/uuid.html"
]
}
]
Expand Down
23 changes: 12 additions & 11 deletions v2.0/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ CockroachDB supports the following data types. Click a type for more details.

Type | Description | Example
-----|-------------|--------
[`INT`](int.html) | A signed integer, up to 64 bits. | `12345`
[`UUID`](uuid.html) | <span class="version-tag">New in v1.1:</span> A 128-bit hexadecimal value. | `7f9c24e8-3b12-4fef-91e0-56a2d5a246ec`
[`SERIAL`](serial.html) | A unique 64-bit signed integer. | `148591304110702593 `
[`DECIMAL`](decimal.html) | An exact, fixed-point number. | `1.2345`
[`FLOAT`](float.html) | A 64-bit, inexact, floating-point number. | `1.2345`
[`ARRAY`](array.html) | <span class="version-tag">New in v1.1:</span> A 1-dimensional, 1-indexed, homogenous array of any non-array data type. | `{"sky","road","car"}`
[`BOOL`](bool.html) | A Boolean value. | `true`
[`BYTES`](bytes.html) | A string of binary characters. | `b'\141\061\142\062\143\063'`
[`COLLATE`](collate.html) | The `COLLATE` feature lets you sort [`STRING`](string.html) values according to language- and country-specific rules, known as collations. | `'a1b2c3' COLLATE en`
[`DATE`](date.html) | A date. | `DATE '2016-01-25'`
[`TIME`](time.html) | A time of day with no time zone. | `TIME '01:23:45.123456'`
[`TIMESTAMP`](timestamp.html) | A date and time pairing. | `TIMESTAMP '2016-01-25 10:10:10'`
[`DECIMAL`](decimal.html) | An exact, fixed-point number. | `1.2345`
[`FLOAT`](float.html) | A 64-bit, inexact, floating-point number. | `1.2345`
[`INET`](inet.html) | <span class="version-tag">New in v2.0:</span> A IPv4 or IPv6 address. | `192.168.0.1`
[`INT`](int.html) | A signed integer, up to 64 bits. | `12345`
[`INTERVAL`](interval.html) | A span of time. | `INTERVAL '2h30m30s'`
[`SERIAL`](serial.html) | A unique 64-bit signed integer. | `148591304110702593 `
[`STRING`](string.html) | A string of Unicode characters. | `'a1b2c3'`
[`COLLATE`](collate.html) | The `COLLATE` feature lets you sort [`STRING`](string.html) values according to language- and country-specific rules, known as collations. | `'a1b2c3' COLLATE en`
[`BYTES`](bytes.html) | A string of binary characters. | `b'\141\061\142\062\143\063'`
[`ARRAY`](array.html) | <span class="version-tag">New in v1.1:</span> A 1-dimensional, 1-indexed, homogenous array of any non-array data type. | `{"sky","road","car"}`
[`TIME`](time.html) | <span class="version-tag">New in v2.0:</span> A time of day with no time zone. | `TIME '01:23:45.123456'`
[`TIMESTAMP`](timestamp.html) | A date and time pairing. | `TIMESTAMP '2016-01-25 10:10:10'`
[`UUID`](uuid.html) | <span class="version-tag">New in v1.1:</span> A 128-bit hexadecimal value. | `7f9c24e8-3b12-4fef-91e0-56a2d5a246ec`

## Data Type Conversions & Casts

Expand All @@ -44,5 +45,5 @@ CockroachDB supports explicit type conversions using the following methods:
- 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.


You can find each data type's supported converstion and casting on its
You can find each data type's supported conversion and casting on its
respective page in its section **Supported Casting & Conversion**.
88 changes: 86 additions & 2 deletions v2.0/inet.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,90 @@
---
title: INET
summary:
summary: The INET data type stores an IPv4 or IPv6 address.
toc: false
---
<span class="version-tag">New in v2.0:</span> Documentation for the `INET` datatype coming soon!
<span class="version-tag">New in v2.0:</span> The `INET` [data type](data-types.html) stores an IPv4 or IPv6 address.

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

## Syntax

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nvanbenschoten - how do you determine if a data type can be an interpreted literal, an annotated string literal, or coerced to type INET? Do any of these apply to INET?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three of them apply to INET! I think the easiest way to determine this is to try it out:

CREATE TABLE t (a INET);
INSERT INTO t VALUES (INET '190.0.0.0'), ('190.0.0.0':::INET), ('190.0.0.0');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: the first one doesn't actually look like it works.. I'll have to fix that during my QA process (cockroachdb/cockroach#22390). I think it's safe to assume that all three will work by the time this is published.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

A constant value of type `INET` can be expressed using an
[interpreted literal](sql-constants.html#interpreted-literals), or a
string literal
[annotated with](sql-expressions.html#explicitly-typed-expressions)
type `INET` or
[coerced to](sql-expressions.html#explicit-type-coercions) type
`INET`.

`INET` constants can be expressed using the following formats:

Format | Description
-------|-------------
IPv4 | Standard [RFC791](https://tools.ietf.org/html/rfc791)-specified format of 4 octets expressed individually in decimal numbers and separated by periods. Optionally, the address can be followed by a subnet mask.<br><br>Examples: `'190.0.0.0'`, `'190.0.0.0/24'`
IPv6 | Standard [RFC8200](https://tools.ietf.org/html/rfc8200)-specified format of 8 colon-separated groups of 4 hexadecimal digits. An IPv6 address can be mapped to an IPv4 address. Optionally, the address can be followed by a subnet mask.<br><br>Examples: `'2001:4f8:3:ba:2e0:81ff:fe22:d1f1'`, `'2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120'`, `'::ffff:192.168.0.1/24'`

{{site.data.alerts.callout_info}}IPv4 addresses will sort before IPv6 addresses, including IPv4-mapped IPv6 addresses.{{site.data.alerts.end}}

## Size

An `INET` value is 32 bits for IPv4 or 128 bits for IPv6.

## Example

{% include copy-clipboard.html %}
~~~ sql
> CREATE TABLE computers (
ip INET PRIMARY KEY,
user_email STRING,
registration_date DATE
);
~~~

{% include copy-clipboard.html %}
~~~ sql
> SHOW COLUMNS FROM computers;
~~~
~~~
+-------------------+--------+-------+---------+-------------+
| Field | Type | Null | Default | Indices |
+-------------------+--------+-------+---------+-------------+
| ip | INET | false | NULL | {"primary"} |
| user_email | STRING | true | NULL | {} |
| registration_date | DATE | true | NULL | {} |
+-------------------+--------+-------+---------+-------------+
~~~

{% include copy-clipboard.html %}
~~~ sql
> INSERT INTO computers
VALUES
('192.168.0.1', 'info@cockroachlabs.com', '2018-01-31'),
('192.168.0.2/10', 'lauren@cockroachlabs.com', '2018-01-31'),
('2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120', 'test@cockroachlabs.com', '2018-01-31');
~~~

{% include copy-clipboard.html %}
~~~ sql
> SELECT * FROM computers;
~~~
~~~
+--------------------------------------+--------------------------+---------------------------+
| ip | user_email | registration_date |
+--------------------------------------+--------------------------+---------------------------+
| 192.168.0.1 | info@cockroachlabs.com | 2018-01-31 00:00:00+00:00 |
| 192.168.0.2/10 | lauren@cockroachlabs.com | 2018-01-31 00:00:00+00:00 |
| 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120 | test@cockroachlabs.com | 2018-01-31 00:00:00+00:00 |
+--------------------------------------+--------------------------+---------------------------+
~~~

## Supported Casting & Conversion
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nvanbenschoten - similar to above, how do you determine what INET can be cast to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the easiest way is to look at the code: https://github.com/cockroachdb/cockroach/blob/94a2182a5e00fc0963c0f20c1dc278b12a9df7d2/pkg/sql/sem/tree/expr.go#L1254.

inetCastTypes is a list of all the types that INET can be cast to. It looks like only STRINGs can be cast to INET. I'll be auditing all of this as part of my QA process.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


`INET` values can be [cast](data-types.html#data-type-conversions-casts) to the following data type:

- `STRING` - Converts to format `'Address/subnet'`.

## See Also

[Data Types](data-types.html)
[Functions and Operators](functions-and-operators.html)
6 changes: 3 additions & 3 deletions v2.0/sql-constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ with character escapes are as follows:

This is a CockroachDB-specific extension to express byte array
literals: the delimiter `x'` or `e'\\x` followed by an arbitrary sequence of
hexadecimal digits, followed by a closing `'`.
hexadecimal digits, followed by a closing `'`.

For example, all the following formats are equivalent to `b'cat'`:

- `x'636174'`
- `x'636174'`
- `X'636174'`
- `e'\\x636174'::BYTES`

Expand Down Expand Up @@ -219,7 +219,7 @@ interpreted literal. These are special cases of

For more information about the allowable format of interpreted
literals, refer to the "Syntax" section of the respective data types:
[`DATE`](date.html#syntax), [`INTERVAL`](interval.html#syntax),
[`DATE`](date.html#syntax), [`INET`](inet.html#syntax), [`INTERVAL`](interval.html#syntax), [`TIME`](time.html#syntax),
[`TIMESTAMP`/`TIMESTAMPTZ`](timestamp.html#syntax).

## Named constants
Expand Down
11 changes: 6 additions & 5 deletions v2.0/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ The size of a `STRING` value is variable, but it's recommended to keep values un

Type | Details
-----|--------
`INT` | Requires supported [`INT`](int.html) string format, e.g., `'10'`.
`DECIMAL` | Requires supported [`DECIMAL`](decimal.html) string format, e.g., `'1.1'`.
`FLOAT` | Requires supported [`FLOAT`](float.html) string format, e.g., `'1.1'`.
`BOOL` | Requires supported [`BOOL`](bool.html) string format, e.g., `'true'`.
`BYTES` | Requires supported [`BYTES`](bytes.html) string format, e.g., `b'\141\061\142\062\143\063'`.
`DATE` | Requires supported [`DATE`](date.html) string format, e.g., `'2016-01-25'`.
`DECIMAL` | Requires supported [`DECIMAL`](decimal.html) string format, e.g., `'1.1'`.
`FLOAT` | Requires supported [`FLOAT`](float.html) string format, e.g., `'1.1'`.
`INET` | Requires supported [`INET`](inet.html) string format, e.g, `'192.168.0.1'`.
`INT` | Requires supported [`INT`](int.html) string format, e.g., `'10'`.
`INTERVAL` | Requires supported [`INTERVAL`](interval.html) string format, e.g., `'1h2m3s4ms5us6ns'`.
`TIME` | <span class="version-tag">New in v2.0:</span> Requires supported [`TIME`](time.html) string format, e.g., `'01:22:12'` (microsecond precision).
`TIMESTAMP` | Requires supported [`TIMESTAMP`](timestamp.html) string format, e.g., `'2016-01-25 10:10:10.555555'`.
`INTERVAL` | Requires supported [`INTERVAL`](interval.html) string format, e.g., `'1h2m3s4ms5us6ns'`.
`BYTES` | Requires supported [`BYTES`](bytes.html) string format, e.g., `b'\141\061\142\062\143\063'`.

## See Also

Expand Down
10 changes: 5 additions & 5 deletions v2.0/uuid.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ A `UUID` value can be expressed using the following formats:

Format | Description
-------|-------------
Standard [RCF4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format | Hyphen-seperated groups of 8, 4, 4, 4, 12 hexadecimal digits.<br><br> Example: `acde070d-8c4c-4f0d-9d8a-162843c10333`
With braces | The standard [RCF4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format with braces.<br><br>Example: `{acde070d-8c4c-4f0d-9d8a-162843c10333}`
Standard [RFC4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format | Hyphen-seperated groups of 8, 4, 4, 4, 12 hexadecimal digits.<br><br> Example: `acde070d-8c4c-4f0d-9d8a-162843c10333`
With braces | The standard [RFC4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format with braces.<br><br>Example: `{acde070d-8c4c-4f0d-9d8a-162843c10333}`
As `BYTES` | `UUID` value specified as bytes.<br><br>Example: `b'kafef00ddeadbeed'`
`UUID` used as a URN | `UUID` can be used as a Uniform Resource Name (URN). In that case, the format is [specified](https://www.ietf.org/rfc/rfc2141.txt) as "urn:uuid:" followed by standard [RCF4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format.<br><br>Example: `urn:uuid:63616665-6630-3064-6465-616462656564`
`UUID` used as a URN | `UUID` can be used as a Uniform Resource Name (URN). In that case, the format is [specified](https://www.ietf.org/rfc/rfc2141.txt) as "urn:uuid:" followed by standard [RFC4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format.<br><br>Example: `urn:uuid:63616665-6630-3064-6465-616462656564`

## Size
A `UUID` value is 128 bits in width, but the total storage size is likely to be larger due to CockroachDB metadata.
Expand All @@ -27,7 +27,7 @@ A `UUID` value is 128 bits in width, but the total storage size is likely to be

### Create a table with manually-entered `UUID` values

#### Create a table with `UUID` in standard [RCF4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format
#### Create a table with `UUID` in standard [RFC4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format

~~~ sql
> CREATE TABLE v (token uuid);
Expand All @@ -46,7 +46,7 @@ A `UUID` value is 128 bits in width, but the total storage size is likely to be
(1 row)
~~~

#### Create a table with `UUID` in standard [RCF4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format with braces
#### Create a table with `UUID` in standard [RFC4122](http://www.ietf.org/rfc/rfc4122.txt)-specified format with braces

~~~ sql
> INSERT INTO v VALUES ('{63616665-6630-3064-6465-616462656563}');
Expand Down