跳到主要内容

Types API

GoosePyType 类表示我们 data types 的类型实例。

从其他类型转换

为了让 API 更易用,我们添加了从现有类型对象到 GoosePyType 实例的隐式转换。 这意味着在任何需要 GoosePyType 对象的地方,也可以传入下方列出的任一选项。

Python 内置类型

下表展示了 Python 内置类型到 Goose 类型的映射。

内置类型Goose 类型
boolBOOLEAN
bytearrayBLOB
bytesBLOB
floatDOUBLE
intBIGINT
strVARCHAR

Numpy DType

下表展示了 Numpy DType 到 Goose 类型的映射。

类型Goose 类型
boolBOOLEAN
float32FLOAT
float64DOUBLE
int16SMALLINT
int32INTEGER
int64BIGINT
int8TINYINT
uint16USMALLINT
uint32UINTEGER
uint64UBIGINT
uint8UTINYINT

嵌套类型

list[child_type]

list 类型对象会映射为其子类型的 LIST 类型。 并且可以进行任意层级的嵌套。

import goose.sqltypes
from typing import Union

goose.sqltypes.GoosePyType(list[dict[Union[str, int], str]])
MAP(UNION(u1 VARCHAR, u2 BIGINT), VARCHAR)[]

dict[key_type, value_type]

dict 类型对象会映射为由键类型和值类型组成的 MAP 类型。

import goose.sqltypes

print(goose.sqltypes.GoosePyType(dict[str, int]))
MAP(VARCHAR, BIGINT)

{'a': field_one, 'b': field_two, ..., 'n': field_n}

dict 对象会映射为由字典键和值组成的 STRUCT

import goose.sqltypes

print(goose.sqltypes.GoosePyType({'a': str, 'b': int}))
STRUCT(a VARCHAR, b BIGINT)

Union[type_1, ... type_n]

typing.Union 对象会映射为由给定类型组成的 UNION 类型。

import goose.sqltypes
from typing import Union

print(goose.sqltypes.GoosePyType(Union[int, str, bool, bytearray]))
UNION(u1 BIGINT, u2 VARCHAR, u3 BOOLEAN, u4 BLOB)

创建函数

对于内置类型,你可以使用 goose.sqltypes 中定义的常量:

Goose 类型
BIGINT
BIT
BLOB
BOOLEAN
DATE
DOUBLE
FLOAT
HUGEINT
INTEGER
INTERVAL
SMALLINT
SQLNULL
TIME_TZ
TIME
TIMESTAMP_MS
TIMESTAMP_NS
TIMESTAMP_S
TIMESTAMP_TZ
TIMESTAMP
TINYINT
UBIGINT
UHUGEINT
UINTEGER
USMALLINT
UTINYINT
UUID
VARCHAR

对于复杂类型,可以使用 GoosePyConnection 对象或 goose 模块上的方法。 凡是接受 GoosePyType 的位置,也都接受可隐式转换为 GoosePyType 的类型对象。

list_type | array_type

参数:

  • child_type: GoosePyType

struct_type | row_type

参数:

  • fields: Union[list[GoosePyType], dict[str, GoosePyType]]

map_type

参数:

  • key_type: GoosePyType
  • value_type: GoosePyType

decimal_type

参数:

  • width: int
  • scale: int

union_type

参数:

  • members: Union[list[GoosePyType], dict[str, GoosePyType]]

string_type

参数:

  • collation: Optional[str]