Skip to content

Commit

Permalink
pg: leading zeros in pginterp
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Sep 5, 2024
1 parent 3864569 commit 05a1873
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
8 changes: 6 additions & 2 deletions internal/sql/pg/histo.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func interpNum[T Num](f float64, a, b T) T {
}

func interpBig(f float64, a, b *big.Int) *big.Int {
if b.Cmp(a) <= 0 {
if b.Cmp(a) == -1 {
panic("b must not be less than a")
}

Expand Down Expand Up @@ -372,7 +372,11 @@ func interpBts(f float64, a, b []byte) []byte {
}

func interpUUID(f float64, a, b types.UUID) types.UUID {
return types.UUID(interpBts(f, a[:], b[:]))
bts := interpBts(f, a[:], b[:])
if len(bts) != 16 {
bts = append(make([]byte, 16-len(bts)), bts...)
}
return types.UUID(bts)
}

func interpUUIDPtr(f float64, a, b *types.UUID) *types.UUID {
Expand Down
66 changes: 65 additions & 1 deletion internal/sql/pg/histo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"strings"
"testing"

"github.com/kwilteam/kwil-db/common/sql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/kwilteam/kwil-db/common/sql"
"github.com/kwilteam/kwil-db/core/types"
)

func Test_histo_explicit_int(t *testing.T) {
Expand Down Expand Up @@ -699,3 +701,65 @@ func Test_restoreHisto(t *testing.T) {
require.NotNil(t, ht.comp)
assert.Equal(t, ht.comp(1, 2), -1)
}

func TestInterpUUID(t *testing.T) {
tests := []struct {
name string
f float64
a types.UUID
b types.UUID
expected types.UUID
}{
{
name: "result with leading zeros",
f: 0.5,
a: types.UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
b: types.UUID{0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
expected: types.UUID{0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
{
name: "Zero interpolation",
f: 0,
a: types.UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
b: types.UUID{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
expected: types.UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
name: "Full interpolation",
f: 1,
a: types.UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
b: types.UUID{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
expected: types.UUID{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
{
name: "Mid interpolation",
f: 0.5,
a: types.UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
b: types.UUID{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
expected: types.UUID{0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
{
name: "Quarter interpolation",
f: 0.25,
a: types.UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
b: types.UUID{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
expected: types.UUID{0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
{
name: "Interpolation with same UUIDs",
f: 0.5,
a: types.UUID{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF},
b: types.UUID{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF},
expected: types.UUID{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := interpUUID(tt.f, tt.a, tt.b)
if result != tt.expected {
t.Errorf("interpUUID(%v, %v, %v) = %v, want %v", tt.f, tt.a, tt.b, result, tt.expected)
}
})
}
}

0 comments on commit 05a1873

Please sign in to comment.