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

[sql] Add Dialect.EscapeStruct method #537

Merged
merged 2 commits into from
May 2, 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
18 changes: 18 additions & 0 deletions lib/sql/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"strings"

"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/stringutil"
)

type Dialect interface {
NeedsEscaping(identifier string) bool // TODO: Remove this when we escape everything
QuoteIdentifier(identifier string) string
EscapeStruct(value any) string
}

type BigQueryDialect struct{}
Expand All @@ -24,6 +26,10 @@ func (BigQueryDialect) QuoteIdentifier(identifier string) string {
return fmt.Sprintf("`%s`", identifier)
}

func (BigQueryDialect) EscapeStruct(value any) string {
return "JSON" + stringutil.Wrap(value, false)
}

type MSSQLDialect struct{}

func (MSSQLDialect) NeedsEscaping(_ string) bool { return true }
Expand All @@ -32,6 +38,10 @@ func (MSSQLDialect) QuoteIdentifier(identifier string) string {
return fmt.Sprintf(`"%s"`, identifier)
}

func (MSSQLDialect) EscapeStruct(value any) string {
panic("not implemented") // We don't currently support backfills for MS SQL.
}

type RedshiftDialect struct{}

func (RedshiftDialect) NeedsEscaping(_ string) bool { return true }
Expand All @@ -41,6 +51,10 @@ func (rd RedshiftDialect) QuoteIdentifier(identifier string) string {
return fmt.Sprintf(`"%s"`, strings.ToLower(identifier))
}

func (RedshiftDialect) EscapeStruct(value any) string {
return fmt.Sprintf("JSON_PARSE(%s)", stringutil.Wrap(value, false))
}

type SnowflakeDialect struct {
UppercaseEscNames bool
}
Expand Down Expand Up @@ -75,3 +89,7 @@ func (sd SnowflakeDialect) QuoteIdentifier(identifier string) string {

return fmt.Sprintf(`"%s"`, identifier)
}

func (SnowflakeDialect) EscapeStruct(value any) string {
return stringutil.Wrap(value, false)
}
12 changes: 1 addition & 11 deletions lib/typing/columns/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ func (c *Column) DefaultValue(dialect sql.Dialect, additionalDateFmts []string)

switch c.KindDetails.Kind {
case typing.Struct.Kind, typing.Array.Kind:
switch dialect.(type) {
case sql.BigQueryDialect:
return "JSON" + stringutil.Wrap(c.defaultValue, false), nil
case sql.RedshiftDialect:
return fmt.Sprintf("JSON_PARSE(%s)", stringutil.Wrap(c.defaultValue, false)), nil
case sql.SnowflakeDialect:
return stringutil.Wrap(c.defaultValue, false), nil
default:
// Note that we don't currently support backfills for MS SQL.
return nil, fmt.Errorf("not implemented for %v dialect", dialect)
}
return dialect.EscapeStruct(c.defaultValue), nil
case typing.ETime.Kind:
if c.KindDetails.ExtendedTimeDetails == nil {
return nil, fmt.Errorf("column kind details for extended time is nil")
Expand Down