Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 16, 2024
1 parent 2f7d64b commit 5d9a624
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 14 deletions.
13 changes: 13 additions & 0 deletions lib/typing/columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ func (c *Columns) UpsertColumn(colName string, arg UpsertColumnArg) error {
}

if arg.StringPrecision != nil {
var currentPrecision int32
if col.KindDetails.OptionalStringPrecision != nil {
currentPrecision = *col.KindDetails.OptionalStringPrecision
}

if currentPrecision > *arg.StringPrecision {
return fmt.Errorf("cannot decrease string precision from %d to %d", currentPrecision, *arg.StringPrecision)
}

col.KindDetails.OptionalStringPrecision = arg.StringPrecision
}

Expand All @@ -151,6 +160,10 @@ func (c *Columns) UpsertColumn(colName string, arg UpsertColumnArg) error {
_col.backfilled = *arg.Backfilled
}

if arg.StringPrecision != nil {
_col.KindDetails.OptionalStringPrecision = arg.StringPrecision
}

c.AddColumn(_col)
}

Expand Down
79 changes: 65 additions & 14 deletions lib/typing/columns/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,40 +192,91 @@ func TestColumns_UpsertColumns(t *testing.T) {
{
// Now update a and b to be toast columns
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)
}
}
{
// Play with string precision
cols.UpsertColumn("string_precision_a", UpsertColumnArg{})

colA, _ := cols.GetColumn("string_precision_a")
assert.Nil(t, colA.KindDetails.OptionalStringPrecision)

cols.UpsertColumn("string_precision_a", UpsertColumnArg{StringPrecision: typing.ToPtr(int32(55))})
colA, _ = cols.GetColumn("string_precision_a")
assert.Equal(t, int32(55), *colA.KindDetails.OptionalStringPrecision)
// Increase string precision
{
// Valid - Current column does not have string precision set
assert.NoError(t, cols.UpsertColumn("string_precision_a", UpsertColumnArg{}))

colA, _ := cols.GetColumn("string_precision_a")
assert.Nil(t, colA.KindDetails.OptionalStringPrecision)

assert.NoError(t,
cols.UpsertColumn("string_precision_a",
UpsertColumnArg{
StringPrecision: typing.ToPtr(int32(55)),
},
),
)
colA, _ = cols.GetColumn("string_precision_a")
assert.Equal(t, int32(55), *colA.KindDetails.OptionalStringPrecision)
}
{
// Valid - Current column does have string precision set (but it's less)
assert.NoError(t,
cols.UpsertColumn("string_precision_b",
UpsertColumnArg{
StringPrecision: typing.ToPtr(int32(5)),
},
),
)

colB, _ := cols.GetColumn("string_precision_b")
assert.Equal(t, int32(5), *colB.KindDetails.OptionalStringPrecision)
assert.NoError(t,
cols.UpsertColumn("string_precision_b",
UpsertColumnArg{
StringPrecision: typing.ToPtr(int32(100)),
},
),
)

colB, _ = cols.GetColumn("string_precision_b")
assert.Equal(t, int32(100), *colB.KindDetails.OptionalStringPrecision)
}
{
// Invalid - Cannot decrease string precision
assert.NoError(t,
cols.UpsertColumn("string_precision_b",
UpsertColumnArg{
StringPrecision: typing.ToPtr(int32(500)),
},
),
)

assert.ErrorContains(t,
cols.UpsertColumn("string_precision_b",
UpsertColumnArg{
StringPrecision: typing.ToPtr(int32(100)),
},
),
"cannot decrease string precision from 500 to 100",
)
}
}
{
// Create a new column zzz
cols.UpsertColumn("zzz", 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)
}
{
// Create a new column aaa
cols.UpsertColumn("aaa", UpsertColumnArg{
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)
Expand Down

0 comments on commit 5d9a624

Please sign in to comment.