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

SNOW-889572 Refactored snowflake type implementation to map #890

Merged
merged 6 commits into from
Aug 24, 2023
Merged
47 changes: 34 additions & 13 deletions datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,48 @@ const (
unSupportedType
)

var snowflakeTypes = [...]string{"FIXED", "REAL", "TEXT", "DATE", "VARIANT",
"TIMESTAMP_LTZ", "TIMESTAMP_NTZ", "TIMESTAMP_TZ", "OBJECT", "ARRAY",
"BINARY", "TIME", "BOOLEAN", "NULL", "SLICE", "CHANGE_TYPE", "NOT_SUPPORTED"}
var snowflakeToDriverType = map[string]snowflakeType{
"FIXED": fixedType,
"REAL": realType,
"TEXT": textType,
"DATE": dateType,
"VARIANT": variantType,
"TIMESTAMP_LTZ": timestampLtzType,
"TIMESTAMP_NTZ": timestampNtzType,
"TIMESTAMP_TZ": timestampTzType,
"OBJECT": objectType,
"ARRAY": arrayType,
"BINARY": binaryType,
"TIME": timeType,
"BOOLEAN": booleanType,
"NULL": nullType,
"SLICE": sliceType,
"CHANGE_TYPE": changeType,
"NOT_SUPPORTED": unSupportedType}

func (st snowflakeType) String() string {
return snowflakeTypes[st]
var driverTypeToSnowflake = invertMap(snowflakeToDriverType)

func invertMap(m map[string]snowflakeType) map[snowflakeType]string {
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
inv := make(map[snowflakeType]string)
for k, v := range m {
if _, ok := inv[v]; ok {
panic("failed to create driverTypeToSnowflake map due to duplicated values")
}
inv[v] = k
}
return inv
}

func (st snowflakeType) Byte() byte {
return byte(st)
}

func (st snowflakeType) String() string {
return driverTypeToSnowflake[st]
}

func getSnowflakeType(typ string) snowflakeType {
for i, sft := range snowflakeTypes {
if sft == typ {
return snowflakeType(i)
} else if snowflakeType(i) == nullType {
break
}
}
return nullType
return snowflakeToDriverType[typ]
}

var (
Expand Down