Skip to content

Commit

Permalink
Spacing working as expected. (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Mar 5, 2023
1 parent 834f86c commit 68f2869
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/stringutil/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ func Empty(vals ...string) bool {

return false
}

func EscapeSpaces(col string) (escaped bool, newString string) {
subStr := " "
return strings.Contains(col, subStr), strings.ReplaceAll(col, subStr, "__")
}
14 changes: 14 additions & 0 deletions lib/stringutil/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ func TestEmpty(t *testing.T) {
assert.True(t, Empty("robin", "jacqueline", "charlie", ""))
assert.True(t, Empty(""))
}

func TestEscapeSpaces(t *testing.T) {
colsToExpectation := map[string]map[string]interface{}{
"columnA": {"escaped": "columnA", "space": false},
"column_a": {"escaped": "column_a", "space": false},
"column a": {"escaped": "column__a", "space": true},
}

for col, expected := range colsToExpectation {
containsSpace, escapedString := EscapeSpaces(col)
assert.Equal(t, expected["escaped"], escapedString)
assert.Equal(t, expected["space"], containsSpace)
}
}
13 changes: 13 additions & 0 deletions models/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/kafkalib"
"github.com/artie-labs/transfer/lib/optimization"
"github.com/artie-labs/transfer/lib/stringutil"
"github.com/artie-labs/transfer/lib/typing"
"github.com/segmentio/kafka-go"
"sync"
Expand Down Expand Up @@ -67,6 +68,18 @@ func (e *Event) Save(topicConfig *kafkalib.TopicConfig, message kafka.Message) (

// Update col if necessary
for col, val := range e.Data {
// Columns here could contain spaces. Every destination treats spaces in a column differently.
// So far, Snowflake accepts them when escaped properly, however BigQuery does not accept it.
// Instead of making this more complicated for future destinations, we will escape the spaces by having double underscore `__`
// So, if customers want to retrieve spaces again, they can replace `__`.

var containsSpace bool
containsSpace, col = stringutil.EscapeSpaces(col)
if containsSpace {
// Write the message back if the column has changed.
e.Data[col] = val
}

if val == "__debezium_unavailable_value" {
// This is an edge case within Postgres & ORCL
// TL;DR - Sometimes a column that is unchanged within a DML will not be emitted
Expand Down

0 comments on commit 68f2869

Please sign in to comment.