Skip to content

Commit

Permalink
Sql util for cleaning pg data type
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Nov 1, 2024
1 parent 7b5b082 commit e282c74
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/benthos/benthos-builder/builders/sql-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,8 @@ func getAdditionalJobMappings(
return output, nil
}
func getJmTransformerByPostgresDataType(colInfo *sqlmanager_shared.DatabaseSchemaRow) (*mgmtv1alpha1.JobMappingTransformer, error) {
switch colInfo.DataType {
cleanedDataType := cleanPostgresType(colInfo.DataType)
switch cleanedDataType {
case "smallint":
return &mgmtv1alpha1.JobMappingTransformer{
Config: &mgmtv1alpha1.TransformerConfig{
Expand Down Expand Up @@ -902,3 +903,11 @@ func getJmTransformerByPostgresDataType(colInfo *sqlmanager_shared.DatabaseSchem
)
}
}

func cleanPostgresType(dataType string) string {
parenIndex := strings.Index(dataType, "(")
if parenIndex == -1 {
return dataType
}
return strings.TrimSpace(dataType[:parenIndex])
}
135 changes: 135 additions & 0 deletions internal/benthos/benthos-builder/builders/sql-util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package benthosbuilder_builders

import "testing"

func Test_cleanPostgresType(t *testing.T) {
t.Run("simple type without params", func(t *testing.T) {
result := cleanPostgresType("integer")
expected := "integer"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("type with single parameter", func(t *testing.T) {
result := cleanPostgresType("varchar(255)")
expected := "varchar"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("type with multiple parameters", func(t *testing.T) {
result := cleanPostgresType("numeric(10,2)")
expected := "numeric"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("character varying with parameter", func(t *testing.T) {
result := cleanPostgresType("character varying(50)")
expected := "character varying"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("type with spaces and parameter", func(t *testing.T) {
result := cleanPostgresType("bit varying(8)")
expected := "bit varying"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("array type", func(t *testing.T) {
result := cleanPostgresType("integer[]")
expected := "integer[]"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("type with time zone", func(t *testing.T) {
result := cleanPostgresType("timestamp with time zone")
expected := "timestamp with time zone"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("type with trailing space", func(t *testing.T) {
result := cleanPostgresType("numeric(10,2) ")
expected := "numeric"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("type with leading space", func(t *testing.T) {
result := cleanPostgresType(" decimal(10,2)")
expected := "decimal"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("empty string", func(t *testing.T) {
result := cleanPostgresType("")
expected := ""
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("just parentheses", func(t *testing.T) {
result := cleanPostgresType("()")
expected := ""
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("multiple parentheses", func(t *testing.T) {
result := cleanPostgresType("foo(bar(baz))")
expected := "foo"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("json type", func(t *testing.T) {
result := cleanPostgresType("json")
expected := "json"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})

t.Run("jsonb type", func(t *testing.T) {
result := cleanPostgresType("jsonb")
expected := "jsonb"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
})
}

// Benchmark remains the same as it's already using discrete cases
func BenchmarkCleanPostgresType(b *testing.B) {
inputs := []string{
"integer",
"character varying(50)",
"numeric(10,2)",
"timestamp with time zone",
"text[]",
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, input := range inputs {
cleanPostgresType(input)
}
}
}

0 comments on commit e282c74

Please sign in to comment.