Data Types

SQL data types define the type of data that a column can store. When you run the DESC TABLE command, you can see the data type of each column.

String and Binary Data Types

Type NameDescriptionSize
StringUTF-8 encoded strings. Holds up to 2,147,483,647 bytes of dataThe length of the strings
BinaryVariable-length binary values. Holds up to 2,147,483,647 bytes of dataThe length of the data + 2 bytes

The maximum capacities of String and Binary are determined by their encodings and how the storage engine handles them. For example, String values are encoded into UTF-8. If all characters are 3 bytes in length, this field can store up to 715,827,882 characters. As for Binary types, they can store a maximum of 2,147,483,647 bytes.

Numeric Data Types

Type NameDescriptionSize
Int8-128 ~ 1271 Byte
Int16-32768 ~ 327672 Bytes
Int32-2147483648 ~ 21474836474 Bytes
Int64-9223372036854775808 ~ 92233720368547758078 Bytes
UInt80 ~ 2551 Byte
UInt160 ~ 655352 Bytes
UInt320 ~ 42949672954 Bytes
UInt640 ~ 184467440737095516158 Bytes
Float3232-bit IEEE754 floating point values4 Bytes
Float64Double precision IEEE 754 floating point values8 Bytes

Decimal Type

GreptimeDB supports the decimal type, a fixed-point type represented as decimal(precision, scale), where precision is the total number of digits, and scale is the number of digits in the fractional part. For example, 123.45 has a precision of 5 and a scale of 2.

  • precision can range from [1, 38].
  • scale can range from [0, precision].

The default decimal is decimal(38, 10) if the precision and scale are not specified.

  1. CREATE TABLE decimals(
  2. d DECIMAL(3, 2),
  3. ts TIMESTAMP TIME INDEX,
  4. );
  5. INSERT INTO decimals VALUES ('0.1',1000), ('0.2',2000);
  6. SELECT * FROM decimals;

Output:

  1. +------+---------------------+
  2. | d | ts |
  3. +------+---------------------+
  4. | 0.10 | 1970-01-01T00:00:01 |
  5. | 0.20 | 1970-01-01T00:00:02 |
  6. +------+---------------------+

Date and Time Types

Type NameDescriptionSize
TimestampSecond64-bit timestamp values with seconds precision8 Bytes
TimestampMillisecond64-bit timestamp values with milliseconds precision8 Bytes
TimestampMicroSecond64-bit timestamp values with microseconds precision8 Bytes
TimestampNanosecond64-bit timestamp values with nanoseconds precision8 Bytes

Boolean Type

Type NameDescriptionSize
BooleanBool values1 Byte

Use TRUE or FALSE to represent boolean values in SQL statements. For example:

  1. CREATE TABLE bools(
  2. b BOOLEAN,
  3. ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP() TIME INDEX,
  4. );
  1. INSERT INTO bools(b) VALUES (TRUE), (FALSE);

Data types compatible with MySQL and PostgreSQL

Type aliases

For users migrating from MySQL or PostgreSQL to GreptimeDB, GreptimeDB supports the following alias types.

Data TypeAlias Types
StringText, TinyText, MediumText, LongText, Varchar, Char
BinaryVarbinary
Int8TinyInt
Int16SmallInt
Int32Int
Int64BigInt
UInt8UnsignedInt
UInt16UnsignedTinyInt
UInt32UnsignedSmallInt
UInt64UnsignedBigInt
Float32Float
Float64Double
TimestampSecondTimestamp_s, Timestamp_sec, Timestamp(0)
TimestampMillisecondTimestamp, Timestamp_ms, Timestamp(3)
TimestampMicroSecondTimestamp_us, Timestamp(6)
TimestampNanosecondTimestamp_ns, Timestamp(9)

You can use these alias types when creating tables. For example, use Varchar instead of String, Double instead of Float64, and Timestamp(0) instead of TimestampSecond.

  1. CREATE TABLE alias_types (
  2. s TEXT,
  3. i DOUBLE,
  4. ts0 TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() TIME INDEX,
  5. PRIMARY KEY(s)
  6. );

Date and time types

In addition to the Timestamp types used as the default time type in GreptimeDB, GreptimeDB also supports Date and DateTime types for compatibility with MySQL and PostgreSQL.

Type nameDescriptionSize
Date32-bit date values represent the days since UNIX Epoch4 Bytes
DateTime64-bit datetime values represent the milliseconds since UNIX Epoch8 Bytes

Examples

Create Table

  1. CREATE TABLE data_types (
  2. s STRING,
  3. vbi BINARY,
  4. b BOOLEAN,
  5. tint INT8,
  6. sint INT16,
  7. i INT32,
  8. bint INT64,
  9. utint UINT8,
  10. usint UINT16,
  11. ui UINT32,
  12. ubint UINT64,
  13. f FLOAT32,
  14. d FLOAT64,
  15. dm DECIMAL(3, 2),
  16. dt DATE,
  17. dtt DATETIME,
  18. ts0 TIMESTAMPSECOND,
  19. ts3 TIMESTAMPMILLISECOND,
  20. ts6 TIMESTAMPMICROSECOND,
  21. ts9 TIMESTAMPNANOSECOND DEFAULT CURRENT_TIMESTAMP() TIME INDEX,
  22. PRIMARY KEY(s));

Describe Table

  1. DESC TABLE data_types;
  1. +--------+----------------------+------+------+---------------------+---------------+
  2. | Column | Type | Key | Null | Default | Semantic Type |
  3. +--------+----------------------+------+------+---------------------+---------------+
  4. | s | String | PRI | YES | | TAG |
  5. | vbi | Binary | | YES | | FIELD |
  6. | b | Boolean | | YES | | FIELD |
  7. | tint | Int8 | | YES | | FIELD |
  8. | sint | Int16 | | YES | | FIELD |
  9. | i | Int32 | | YES | | FIELD |
  10. | bint | Int64 | | YES | | FIELD |
  11. | utint | UInt8 | | YES | | FIELD |
  12. | usint | UInt16 | | YES | | FIELD |
  13. | ui | UInt32 | | YES | | FIELD |
  14. | ubint | UInt64 | | YES | | FIELD |
  15. | f | Float32 | | YES | | FIELD |
  16. | d | Float64 | | YES | | FIELD |
  17. | dm | Decimal(3, 2) | | YES | | FIELD |
  18. | dt | Date | | YES | | FIELD |
  19. | dtt | DateTime | | YES | | FIELD |
  20. | ts0 | TimestampSecond | | YES | | FIELD |
  21. | ts3 | TimestampMillisecond | | YES | | FIELD |
  22. | ts6 | TimestampMicrosecond | | YES | | FIELD |
  23. | ts9 | TimestampNanosecond | PRI | NO | current_timestamp() | TIMESTAMP |
  24. +--------+----------------------+------+------+---------------------+---------------+