Skip to main content
Version: v4

3.4 支援數據型別

Boolean

Boolean

此型別允許 truefalse.

Integer

TINYINT

8 位元的 2 進位整數,其最小值為 -2^7,最大值為 2^7 - 1

SMALLINT

16 位元的 2 進位整數,其最小值為 -2^15,最大值為 2^15 - 1

INTEGER

32 位元的 2 進位整數,其最小值為 -2^31,最大值為 2^31 - 1

BIGINT

64 位元的 2 進位整數,其最小值為 -2^63,最大值為 2^63 - 1

Floating-Point

REAL

32 位元的可變精度二進位浮點數,實踐了 IEEE Standard 754。

用法: REAL '10.3', REAL '10.3e0', REAL '1.03e1'

DOUBLE

64 位元的可變精度二進位浮點數,其實踐了 IEEE Standard 754。

用法: DOUBLE '10.3', DOUBLE '1.03e1', 10.3e0, 1.03e1

Fixed-Precision

DECIMAL

固定精度的十進位數,最高精度為 38 位元。

Decimal提供兩個參數設定:

  • 精度 (precision) - 總位元數

  • 尺度 (scale) - 小數部分的位數,預設為 0。

型別定義: DECIMAL(10,3), DECIMAL(20) 用法: DECIMAL '10.3', DECIMAL '1234567890', 1.1

String

VARCHAR

可設定最大長度的字串。

型別定義: varchar, varchar(20)

在 SQL 中支援一般文字以及 Unicode:

  • 一般文字 : 'Hello winter !'
  • Unicode 以及跳脫字元: U&'Hello winter \2603 !'
  • Unicode 以及自定義的跳脫字元: U&'Hello winter #2603 !' UESCAPE '#'

Unicode 字串以 U& 作為前綴,4 位數的 Unicode 字串之前需加上跳脫字元 ( / ),上述的例子中, \2603#2603 就代表著一個雪人圖案。而在 6 位數的長 Unicode 之前則需要額外加上加號 ( + )。例如:\+01F600 是一個笑臉圖案。

CHAR

固定長度的字串,預設長度為 1。

一個 CHAR(x) 長度必定為 x。舉例來說,若將 dog 轉換成 CHAR(7) 則會使原本的 dog 之後增加 4 個空白字元。另外,在比較 CHAR 時一律包括空格,所以兩個長度不同的字串 CHAR(x)CHAR(y) 將永遠不相等。

型別定義: char, char(20)

VARBINARY

可變長度的二進位數據。

使用 VARBINARY 必須添加前綴字 x,且以 16 進位表示。例如,eh? 的二進位寫法是 X'65683F'

目前尚未支援指定長度: varbinary(n)

JSON

可以是 JSON Object、JSON Array、JSON Number、JSON String、truefalse 或是 null

Date and Time

DATE

年、月、日。

用法: DATE '2001-08-22'

TIME

時、分、秒、毫秒,且不包含時區。

用法: TIME '01:02:03.456'

TIME WITH TIME ZONE

時、分、秒、毫秒,並且包含時區。

用法: TIME '01:02:03.456 America/Los_Angeles'

TIMESTAMP

TIMESTAMPTIMESTAMP(3) (精確度到毫秒) 相同。

TIMESTAMP(P)

包括一天的日期以及時、分、秒,不包含時區,P 代表著秒的精度,最大 P 值為 12:

  • TIMESTAMP(0): 精確度到秒
  • TIMESTAMP(3): 精確度到毫秒
  • TIMESTAMP(6): 精確度到微秒

TIMESTAMP(P) WITHOUT TIME ZONETIMESTAMP(P) 相同.

用法:

SELECT TIMESTAMP '2020-06-10 15:55:23';
-- 2020-06-10 15:55:23

SELECT TIMESTAMP '2020-06-10 15:55:23.383345';
-- 2020-06-10 15:55:23.383345

SELECT typeof(TIMESTAMP '2020-06-10 15:55:23.383345');
-- timestamp(6)

SELECT cast(TIMESTAMP '2020-06-10 15:55:23.383345' as TIMESTAMP(1));
-- 2020-06-10 15:55:23.4

SELECT cast(TIMESTAMP '2020-06-10 15:55:23.383345' as TIMESTAMP(12));
-- 2020-06-10 15:55:23.383345000000

TIMESTAMP WITH TIME ZONE

TIMESTAMP WITH TIME ZONETIMESTAMP(3) WITH TIME ZONE 相同。

TIMESTAMP(P) WITH TIME ZONE

包括一天的日期以及時、分、秒,以及時區,P 代表著秒的精度,最大 P 值為 12:

用法: TIMESTAMP '2001-08-22 03:04:05.321 America/Los_Angeles'

INTERVAL YEAR TO MONTH

年、月的時間間隔。

用法: INTERVAL '3' MONTH

INTERVAL DAY TO SECOND

日、時、分、秒、毫秒的時間間隔。

用法: INTERVAL '2' DAY

Structural

ARRAY

用法: ARRAY[1, 2, 3]

MAP

用法: MAP(ARRAY['foo', 'bar'], ARRAY[1, 2])

ROW

  • 混合型別,欄位可不命名。

    用法: CAST(ROW(1, 2e0) AS ROW(x BIGINT, y DOUBLE))

  • 使用 (.) 取得 Row 型別中的欄位

    用法: CAST(ROW(1, 2.0) AS ROW(x BIGINT, y DOUBLE)).x

  • 使用 ([p]) 也可以取得 Row 型別中的欄位,p 值最小為 1

    用法: ROW(1, 2.0)[1]

Network Address

IPADDRESS

支援 IPv4 以及 IPv6,在系統內部,一律都使用 IPv6,如果原本是 IPv4 則會依照 RFC 4291 Section-2.5.5.2 轉換成 IPv6

用法: IPADDRESS '10.0.0.1', IPADDRESS '2001:db8::1'

UUID

UUID

支援 REC 4122 所定義的 UUID (Universally Unique IDentifier) 格式。

用法: UUID '12151fd2-7586-11e9-8f9e-2a86e4085a59'

HyperLogLog

Calculating the approximate distinct count can be done much more cheaply than an exact count using the HyperLogLog data sketch. See HyperLogLog Fucntion.

HyperLogLog

A HyperLogLog sketch allows efficient computation of approx_distinct. It starts as a sparse representation, switching to a dense representation when it becomes more efficient.

P4HyperLogLog

A P4HyperLogLog sketch is similar to hyperloglog_type, but it starts (and remains) in the dense representation.

Quantile Digest

QDigest

A quantile digest (qdigest) is a summary structure which captures the approximate distribution of data for a given input set, and can be queried to retrieve approximate quantile values from the distribution. The level of accuracy for a qdigest is tunable, allowing for more precise results at the expense of space.

A qdigest can be used to give approximate answer to queries asking for what value belongs at a certain quantile. A useful property of qdigests is that they are additive, meaning they can be merged together without losing precision.

A qdigest may be helpful whenever the partial results of approx_percentile can be reused. For example, one may be interested in a daily reading of the 99th percentile values that are read over the course of a week. Instead of calculating the past week of data with approx_percentile, qdigest\ s could be stored daily, and quickly merged to retrieve the 99th percentile value.

T-Digest

TDigest

A T-digest (tdigest) is a summary structure which, similarly to qdigest, captures the approximate distribution of data for a given input set. It can be queried to retrieve approximate quantile values from the distribution.

TDigest has the following advantages compared to QDigest:

  • higher performance
  • lower memory usage
  • higher accuracy at high and low percentiles

T-digests are additive, meaning they can be merged together.