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

Misc cleanup of qvalue code #1288

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions flow/connectors/postgres/qvalue_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,9 @@ func parseFieldFromQValueKind(qvalueKind qvalue.QValueKind, value interface{}) (
}
default:
textVal, ok := value.(string)
if !ok {
return qvalue.QValue{}, fmt.Errorf("failed to parse value %v into QValueKind %v", value, qvalueKind)
if ok {
val = qvalue.QValue{Kind: qvalue.QValueKindString, Value: textVal}
}
val = qvalue.QValue{Kind: qvalue.QValueKindString, Value: textVal}
}

// parsing into pgtype failed.
Expand Down
28 changes: 13 additions & 15 deletions flow/connectors/snowflake/merge_stmt_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *mergeStmtGenerator) generateMergeStmt() (string, error) {
}

targetColumnName := SnowflakeIdentifierNormalize(column.Name)
switch qvalue.QValueKind(genericColumnType) {
switch qvKind {
case qvalue.QValueKindBytes, qvalue.QValueKindBit:
flattenedCastsSQLArray = append(flattenedCastsSQLArray, fmt.Sprintf("BASE64_DECODE_BINARY(%s:\"%s\") "+
"AS %s", toVariantColumnName, column.Name, targetColumnName))
Expand All @@ -61,21 +61,19 @@ func (m *mergeStmtGenerator) generateMergeStmt() (string, error) {
// flattenedCastsSQLArray = append(flattenedCastsSQLArray, fmt.Sprintf("TIME_FROM_PARTS(0,0,0,%s:%s:"+
// "Microseconds*1000) "+
// "AS %s", toVariantColumnName, columnName, columnName))
default:
if qvKind == qvalue.QValueKindNumeric {
precision, scale := numeric.ParseNumericTypmod(column.TypeModifier)
if column.TypeModifier == -1 || precision > 38 || scale > 37 {
precision = numeric.PeerDBNumericPrecision
scale = numeric.PeerDBNumericScale
}
numericType := fmt.Sprintf("NUMERIC(%d,%d)", precision, scale)
flattenedCastsSQLArray = append(flattenedCastsSQLArray,
fmt.Sprintf("TRY_CAST((%s:\"%s\")::text AS %s) AS %s",
toVariantColumnName, column.Name, numericType, targetColumnName))
} else {
flattenedCastsSQLArray = append(flattenedCastsSQLArray, fmt.Sprintf("CAST(%s:\"%s\" AS %s) AS %s",
toVariantColumnName, column.Name, sfType, targetColumnName))
case qvalue.QValueKindNumeric:
precision, scale := numeric.ParseNumericTypmod(column.TypeModifier)
if column.TypeModifier == -1 || precision > 38 || scale > 37 {
precision = numeric.PeerDBNumericPrecision
scale = numeric.PeerDBNumericScale
}
numericType := fmt.Sprintf("NUMERIC(%d,%d)", precision, scale)
flattenedCastsSQLArray = append(flattenedCastsSQLArray,
fmt.Sprintf("TRY_CAST((%s:\"%s\")::text AS %s) AS %s",
toVariantColumnName, column.Name, numericType, targetColumnName))
default:
flattenedCastsSQLArray = append(flattenedCastsSQLArray, fmt.Sprintf("CAST(%s:\"%s\" AS %s) AS %s",
toVariantColumnName, column.Name, sfType, targetColumnName))
}
}
flattenedCastsSQL := strings.Join(flattenedCastsSQLArray, ",")
Expand Down
2 changes: 1 addition & 1 deletion flow/model/conversion_avro.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewQRecordAvroConverter(
}

func (qac *QRecordAvroConverter) Convert() (map[string]interface{}, error) {
m := map[string]interface{}{}
m := make(map[string]interface{}, len(qac.QRecord))

for idx, val := range qac.QRecord {
key := qac.ColNames[idx]
Expand Down
5 changes: 0 additions & 5 deletions flow/model/qrecord_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ func (src *QRecordBatchCopyFromSource) Values() ([]interface{}, error) {
values[i] = timestampTZ

case qvalue.QValueKindUUID:
if qValue.Value == nil {
values[i] = nil
break
}

v, ok := qValue.Value.([16]byte) // treat it as byte slice
if !ok {
src.err = fmt.Errorf("invalid UUID value %v", qValue.Value)
Expand Down
12 changes: 7 additions & 5 deletions flow/model/qvalue/avro_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func NewQValueAvroConverter(value QValue, targetDWH QDWHType, nullable bool) *QV
}

func (c *QValueAvroConverter) ToAvroValue() (interface{}, error) {
if c.Nullable && c.Value.Value == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes a lot of the code below dead, but cleaning up that can be done later

return nil, nil
}

switch c.Value.Kind {
case QValueKindInvalid:
// we will attempt to convert invalid to a string
Expand Down Expand Up @@ -457,14 +461,12 @@ func (c *QValueAvroConverter) processNullableUnion(
avroType string,
value interface{},
) (interface{}, error) {
if value == nil && c.Nullable {
return nil, nil
}

if c.Nullable {
if value == nil {
return nil, nil
}
return goavro.Union(avroType, value), nil
}

return value, nil
}

Expand Down
Loading