-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into redshift-string-precision
- Loading branch information
Showing
5 changed files
with
266 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package dialect | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/lib/sql" | ||
"github.com/artie-labs/transfer/lib/typing" | ||
"github.com/artie-labs/transfer/lib/typing/ext" | ||
) | ||
|
||
func (RedshiftDialect) DataTypeForKind(kd typing.KindDetails, _ bool) string { | ||
switch kd.Kind { | ||
case typing.Integer.Kind: | ||
if kd.OptionalIntegerKind != nil { | ||
switch *kd.OptionalIntegerKind { | ||
case typing.SmallIntegerKind: | ||
return "INT2" | ||
case typing.IntegerKind: | ||
return "INT4" | ||
case typing.NotSpecifiedKind, typing.BigIntegerKind: | ||
fallthrough | ||
default: | ||
// By default, we are using a larger data type to avoid the possibility of an integer overflow. | ||
return "INT8" | ||
} | ||
} | ||
|
||
return "INT8" | ||
case typing.Struct.Kind: | ||
return "SUPER" | ||
case typing.Array.Kind: | ||
// Redshift does not have a built-in JSON type (which means we'll cast STRUCT and ARRAY kinds as TEXT). | ||
// As a result, Artie will store this in JSON string and customers will need to extract this data out via SQL. | ||
// Columns that are automatically created by Artie are created as VARCHAR(MAX). | ||
// Rationale: https://github.com/artie-labs/transfer/pull/173 | ||
return "VARCHAR(MAX)" | ||
case typing.String.Kind: | ||
if kd.OptionalStringPrecision != nil { | ||
return fmt.Sprintf("VARCHAR(%d)", *kd.OptionalStringPrecision) | ||
} | ||
|
||
return "VARCHAR(MAX)" | ||
case typing.Boolean.Kind: | ||
// We need to append `NULL` to let Redshift know that NULL is an acceptable data type. | ||
return "BOOLEAN NULL" | ||
case typing.ETime.Kind: | ||
switch kd.ExtendedTimeDetails.Type { | ||
case ext.TimestampTzKindType: | ||
return "timestamp with time zone" | ||
case ext.DateKindType: | ||
return "date" | ||
case ext.TimeKindType: | ||
return "time" | ||
} | ||
case typing.EDecimal.Kind: | ||
return kd.ExtendedDecimalDetails.RedshiftKind() | ||
} | ||
|
||
return kd.Kind | ||
} | ||
|
||
func (RedshiftDialect) KindForDataType(rawType string, stringPrecision string) (typing.KindDetails, error) { | ||
rawType = strings.ToLower(rawType) | ||
if strings.HasPrefix(rawType, "numeric") { | ||
_, parameters, err := sql.ParseDataTypeDefinition(rawType) | ||
if err != nil { | ||
return typing.Invalid, err | ||
} | ||
return typing.ParseNumeric(parameters) | ||
} | ||
|
||
if strings.Contains(rawType, "character varying") { | ||
precision, err := strconv.ParseInt(stringPrecision, 10, 32) | ||
if err != nil { | ||
return typing.Invalid, fmt.Errorf("failed to parse string precision: %q, err: %w", stringPrecision, err) | ||
} | ||
|
||
return typing.KindDetails{ | ||
Kind: typing.String.Kind, | ||
OptionalStringPrecision: typing.ToPtr(int32(precision)), | ||
}, nil | ||
} | ||
|
||
switch rawType { | ||
case "super": | ||
return typing.Struct, nil | ||
case "smallint": | ||
return typing.KindDetails{ | ||
Kind: typing.Integer.Kind, | ||
OptionalIntegerKind: typing.ToPtr(typing.SmallIntegerKind), | ||
}, nil | ||
case "integer": | ||
return typing.KindDetails{ | ||
Kind: typing.Integer.Kind, | ||
OptionalIntegerKind: typing.ToPtr(typing.IntegerKind), | ||
}, nil | ||
case "bigint": | ||
return typing.KindDetails{ | ||
Kind: typing.Integer.Kind, | ||
OptionalIntegerKind: typing.ToPtr(typing.BigIntegerKind), | ||
}, nil | ||
case "double precision": | ||
return typing.Float, nil | ||
case "timestamp with time zone", "timestamp without time zone": | ||
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.TimestampTzKindType), nil | ||
case "time without time zone": | ||
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.TimeKindType), nil | ||
case "date": | ||
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateKindType), nil | ||
case "boolean": | ||
return typing.Boolean, nil | ||
} | ||
|
||
return typing.Invalid, fmt.Errorf("unsupported data type: %q", rawType) | ||
} |
Oops, something went wrong.