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

Update UpsertColumn to include an error response #963

Merged
merged 3 commits into from
Oct 16, 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
7 changes: 6 additions & 1 deletion clients/shared/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ func Merge(ctx context.Context, dwh destination.DataWarehouse, tableData *optimi
for attempts := 0; attempts < backfillMaxRetries; attempts++ {
backfillErr = BackfillColumn(dwh, col, tableID)
if backfillErr == nil {
tableConfig.Columns().UpsertColumn(col.Name(), columns.UpsertColumnArg{
err = tableConfig.Columns().UpsertColumn(col.Name(), columns.UpsertColumnArg{
Backfilled: typing.ToPtr(true),
})

if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could inline this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although with the multiple lines that could make it hard to read, so I'm also good with leaving it as is.

return fmt.Errorf("failed to update column backfilled status: %w", err)
}

break
}

Expand Down
36 changes: 19 additions & 17 deletions lib/typing/columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package columns

import (
"errors"
"fmt"
"slices"
"strings"
"sync"
Expand Down Expand Up @@ -108,9 +109,9 @@ type UpsertColumnArg struct {

// UpsertColumn - just a wrapper around UpdateColumn and AddColumn
// If it doesn't find a column, it'll add one where the kind = Invalid.
func (c *Columns) UpsertColumn(colName string, arg UpsertColumnArg) {
func (c *Columns) UpsertColumn(colName string, arg UpsertColumnArg) error {
if colName == "" {
return
return fmt.Errorf("column name is empty")
}

if col, isOk := c.GetColumn(colName); isOk {
Expand All @@ -127,27 +128,28 @@ func (c *Columns) UpsertColumn(colName string, arg UpsertColumnArg) {
}

c.UpdateColumn(col)
return
}
} else {
_col := Column{
name: colName,
KindDetails: typing.Invalid,
}

col := Column{
name: colName,
KindDetails: typing.Invalid,
}
if arg.ToastCol != nil {
_col.ToastColumn = *arg.ToastCol
}

if arg.ToastCol != nil {
col.ToastColumn = *arg.ToastCol
}
if arg.PrimaryKey != nil {
_col.primaryKey = *arg.PrimaryKey
}

if arg.PrimaryKey != nil {
col.primaryKey = *arg.PrimaryKey
}
if arg.Backfilled != nil {
_col.backfilled = *arg.Backfilled
}

if arg.Backfilled != nil {
col.backfilled = *arg.Backfilled
c.AddColumn(_col)
}

c.AddColumn(col)
return nil
}

func (c *Columns) AddColumn(col Column) {
Expand Down
43 changes: 20 additions & 23 deletions lib/typing/columns/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,36 +194,33 @@ func TestColumns_UpsertColumns(t *testing.T) {

// Now selectively update only a, b
for _, key := range []string{"a", "b"} {
cols.UpsertColumn(key, UpsertColumnArg{
assert.NoError(t, cols.UpsertColumn(key, UpsertColumnArg{
ToastCol: typing.ToPtr(true),
})
}))

// Now inspect.
col, _ := cols.GetColumn(key)
assert.True(t, col.ToastColumn)
}

cols.UpsertColumn("zzz", UpsertColumnArg{})
zzzCol, _ := cols.GetColumn("zzz")
assert.False(t, zzzCol.ToastColumn)
assert.False(t, zzzCol.primaryKey)
assert.Equal(t, zzzCol.KindDetails, typing.Invalid)

cols.UpsertColumn("aaa", UpsertColumnArg{
ToastCol: typing.ToPtr(true),
PrimaryKey: typing.ToPtr(true),
})
aaaCol, _ := cols.GetColumn("aaa")
assert.True(t, aaaCol.ToastColumn)
assert.True(t, aaaCol.primaryKey)
assert.Equal(t, aaaCol.KindDetails, typing.Invalid)

length := len(cols.columns)
for i := 0; i < 500; i++ {
cols.UpsertColumn("", UpsertColumnArg{})
{
assert.NoError(t, cols.UpsertColumn("zzz", UpsertColumnArg{}))
zzzCol, _ := cols.GetColumn("zzz")
assert.False(t, zzzCol.ToastColumn)
assert.False(t, zzzCol.primaryKey)
assert.Equal(t, zzzCol.KindDetails, typing.Invalid)
}

assert.Equal(t, length, len(cols.columns))
{
assert.NoError(t, cols.UpsertColumn("aaa", UpsertColumnArg{
ToastCol: typing.ToPtr(true),
PrimaryKey: typing.ToPtr(true),
}))

aaaCol, _ := cols.GetColumn("aaa")
assert.True(t, aaaCol.ToastColumn)
assert.True(t, aaaCol.primaryKey)
assert.Equal(t, aaaCol.KindDetails, typing.Invalid)
}
assert.ErrorContains(t, cols.UpsertColumn("", UpsertColumnArg{}), "column name is empty")
}

func TestColumns_Add_Duplicate(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions models/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ func ToMemoryEvent(event cdc.Event, pkMap map[string]any, tc kafkalib.TopicConfi
// Now iterate over pkMap and tag each column that is a primary key
if cols != nil {
for primaryKey := range pkMap {
cols.UpsertColumn(
err = cols.UpsertColumn(
// We need to escape the column name similar to have parity with event.GetColumns()
columns.EscapeName(primaryKey),
columns.UpsertColumnArg{
PrimaryKey: typing.ToPtr(true),
},
)

if err != nil {
return Event{}, fmt.Errorf("failed to upsert column: %w", err)
}
}
}

Expand Down Expand Up @@ -223,9 +227,13 @@ func (e *Event) Save(cfg config.Config, inMemDB *models.DatabaseData, tc kafkali
}

if toastedCol {
inMemoryColumns.UpsertColumn(newColName, columns.UpsertColumnArg{
err := inMemoryColumns.UpsertColumn(newColName, columns.UpsertColumnArg{
ToastCol: typing.ToPtr(true),
})

if err != nil {
return false, "", fmt.Errorf("failed to upsert column: %w", err)
}
} else {
retrievedColumn, isOk := inMemoryColumns.GetColumn(newColName)
if !isOk {
Expand Down
Loading