-
Notifications
You must be signed in to change notification settings - Fork 30
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
[bigquery] Add scaffolding for the Storage Write API #720
Changes from 4 commits
a28c02d
b5c9fec
6b505bd
351aa0e
0f825c9
4826202
e446373
a43498e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,28 +65,14 @@ func castColVal(colVal any, colKind columns.Column, additionalDateFmts []string) | |
return extTime.String(dialect.BQStreamingTimeFormat), nil | ||
} | ||
case typing.Struct.Kind: | ||
// TODO: See if we can improve this eval and find a better location, see: https://github.com/artie-labs/transfer/pull/697#discussion_r1609280164 | ||
if strings.Contains(fmt.Sprint(colVal), constants.ToastUnavailableValuePlaceholder) { | ||
return fmt.Sprintf(`{"key":"%s"}`, constants.ToastUnavailableValuePlaceholder), nil | ||
} | ||
|
||
// Structs from relational and Mongo are different. | ||
// MongoDB will return the native objects back such as `map[string]any{"hello": "world"}` | ||
// Relational will return a string representation of the struct such as `{"hello": "world"}` | ||
if colValString, isOk := colVal.(string); isOk { | ||
if colValString == "" { | ||
return nil, nil | ||
} | ||
|
||
return colValString, nil | ||
} | ||
|
||
colValBytes, err := json.Marshal(colVal) | ||
stringValue, err := EncodeStructToJSONString(colVal) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal colVal: %w", err) | ||
return nil, err | ||
} else if stringValue == "" { | ||
return nil, nil | ||
} else { | ||
return stringValue, nil | ||
} | ||
|
||
return string(colValBytes), nil | ||
case typing.Array.Kind: | ||
arrayString, err := array.InterfaceToArrayString(colVal, true) | ||
if err != nil { | ||
|
@@ -104,3 +90,27 @@ func castColVal(colVal any, colKind columns.Column, additionalDateFmts []string) | |
slog.Error("Unexpected BigQuery Data Type", slog.Any("colKind", colKind.KindDetails.Kind), slog.Any("colVal", colVal)) | ||
return fmt.Sprint(colVal), nil | ||
} | ||
|
||
// EncodeStructToJSONString takes a struct as either a string or Go object and encodes it into a JSON string. | ||
// Structs from relational and Mongo are different. | ||
// MongoDB will return the native objects back such as `map[string]any{"hello": "world"}` | ||
// Relational will return a string representation of the struct such as `{"hello": "world"}` | ||
func EncodeStructToJSONString(value any) (string, error) { | ||
if stringValue, isOk := value.(string); isOk { | ||
if strings.Contains(stringValue, constants.ToastUnavailableValuePlaceholder) { | ||
return fmt.Sprintf(`{"key":"%s"}`, constants.ToastUnavailableValuePlaceholder), nil | ||
} | ||
return stringValue, nil | ||
} | ||
|
||
bytes, err := json.Marshal(value) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to marshal colVal: %w", err) | ||
} | ||
|
||
stringValue := string(bytes) | ||
if strings.Contains(stringValue, constants.ToastUnavailableValuePlaceholder) { | ||
slog.Error("encoded JSON value contains the toast unavailable value placeholder") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should return this too return fmt.Sprintf(`{"key":"%s"}`, constants.ToastUnavailableValuePlaceholder), nil |
||
} | ||
return stringValue, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,12 @@ import "fmt" | |
type BigQuery struct { | ||
// PathToCredentials is _optional_ if you have GOOGLE_APPLICATION_CREDENTIALS set as an env var | ||
// Links to credentials: https://cloud.google.com/docs/authentication/application-default-credentials#GAC | ||
PathToCredentials string `yaml:"pathToCredentials"` | ||
DefaultDataset string `yaml:"defaultDataset"` | ||
ProjectID string `yaml:"projectID"` | ||
Location string `yaml:"location"` | ||
BatchSize int `yaml:"batchSize"` | ||
PathToCredentials string `yaml:"pathToCredentials"` | ||
DefaultDataset string `yaml:"defaultDataset"` | ||
ProjectID string `yaml:"projectID"` | ||
Location string `yaml:"location"` | ||
BatchSize int `yaml:"batchSize"` | ||
UseStorageWriteAPI bool `yaml:"__useStorageWriteAPI"` // Not officially supported yet. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For config settings that are not intended for production use I think it's nice to prefix them with "__" to indicate that the are not officially supported, that way we can get rid of them without breaking backwards compatibility and having to bump the minor version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
} | ||
|
||
func (b *BigQuery) LoadDefaultValues() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will need this for the Storage Write API.