-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BigQuery - Better handle on Arrays (#104)
- Loading branch information
Showing
7 changed files
with
259 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package bigquery | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/artie-labs/transfer/lib/array" | ||
|
||
"github.com/artie-labs/transfer/lib/config/constants" | ||
"github.com/artie-labs/transfer/lib/stringutil" | ||
"github.com/artie-labs/transfer/lib/typing/ext" | ||
|
||
"github.com/artie-labs/transfer/lib/typing" | ||
) | ||
|
||
func CastColVal(colVal interface{}, colKind typing.Column) (string, error) { | ||
if colVal != nil { | ||
switch colKind.KindDetails.Kind { | ||
case typing.ETime.Kind: | ||
extTime, err := ext.ParseFromInterface(colVal) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: %v, err: %v", colVal, err) | ||
} | ||
|
||
switch extTime.NestedKind.Type { | ||
case ext.DateTimeKindType: | ||
colVal = fmt.Sprintf("PARSE_DATETIME('%s', '%v')", RFC3339Format, extTime.String(time.RFC3339Nano)) | ||
case ext.DateKindType: | ||
colVal = fmt.Sprintf("PARSE_DATE('%s', '%v')", PostgresDateFormat, extTime.String(ext.Date.Format)) | ||
case ext.TimeKindType: | ||
colVal = fmt.Sprintf("PARSE_TIME('%s', '%v')", PostgresTimeFormatNoTZ, extTime.String(ext.PostgresTimeFormatNoTZ)) | ||
} | ||
// All the other types do not need string wrapping. | ||
case typing.String.Kind, typing.Struct.Kind: | ||
colVal = stringutil.Wrap(colVal) | ||
colVal = stringutil.LineBreaksToCarriageReturns(fmt.Sprint(colVal)) | ||
if colKind.KindDetails == typing.Struct { | ||
if strings.Contains(fmt.Sprint(colVal), constants.ToastUnavailableValuePlaceholder) { | ||
colVal = typing.BigQueryJSON(fmt.Sprintf(`{"key": "%s"}`, constants.ToastUnavailableValuePlaceholder)) | ||
} else { | ||
// This is how you cast string -> JSON | ||
colVal = fmt.Sprintf("JSON %s", colVal) | ||
} | ||
} | ||
case typing.Array.Kind: | ||
var err error | ||
colVal, err = array.InterfaceToArrayStringEscaped(colVal) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
} else { | ||
if colKind.KindDetails == typing.String { | ||
// BigQuery does not like null as a string for CTEs. | ||
// It throws this error: Value of type INT64 cannot be assigned to column name, which has type STRING | ||
colVal = "''" | ||
} else { | ||
colVal = "null" | ||
} | ||
} | ||
|
||
return fmt.Sprint(colVal), nil | ||
} |
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,53 @@ | ||
package bigquery | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/artie-labs/transfer/lib/typing" | ||
) | ||
|
||
func TestCastColVal(t *testing.T) { | ||
type _testCase struct { | ||
name string | ||
colVal interface{} | ||
colKind typing.Column | ||
|
||
expectedErr error | ||
expectedString string | ||
} | ||
|
||
testCases := []_testCase{ | ||
{ | ||
name: "escaping string", | ||
colVal: "foo", | ||
colKind: typing.Column{KindDetails: typing.String}, | ||
expectedString: "'foo'", | ||
}, | ||
{ | ||
name: "123 as int", | ||
colVal: 123, | ||
colKind: typing.Column{KindDetails: typing.Integer}, | ||
expectedString: "123", | ||
}, | ||
{ | ||
name: "struct", | ||
colVal: `{"hello": "world"}`, | ||
colKind: typing.Column{KindDetails: typing.Struct}, | ||
expectedString: `JSON '{"hello": "world"}'`, | ||
}, | ||
{ | ||
name: "array", | ||
colVal: []int{1, 2, 3, 4, 5}, | ||
colKind: typing.Column{KindDetails: typing.Array}, | ||
expectedString: `['1','2','3','4','5']`, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
actualString, actualErr := CastColVal(testCase.colVal, testCase.colKind) | ||
assert.Equal(t, testCase.expectedErr, actualErr, testCase.name) | ||
assert.Equal(t, testCase.expectedString, actualString, testCase.name) | ||
} | ||
} |
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
Oops, something went wrong.