Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
cfde3c5
Implement values.Wrapper and values.AnyWrapper
nicktobey Mar 5, 2025
a92ff64
Propagate context parameter.
nicktobey Mar 14, 2025
91f1c0d
[ga-format-pr] Run ./format_repo.sh to fix formatting
nicktobey Mar 17, 2025
12d340f
Add extra checks to wrapper test
nicktobey Mar 19, 2025
2ce4c01
Amend toast implementation
nicktobey Mar 20, 2025
b49ea60
Merge remote-tracking branch 'origin/main' into nicktobey/wrapper
nicktobey Mar 21, 2025
48e833f
Unwrap values before casting them to strings or bytes.
nicktobey Mar 24, 2025
d8edd4c
Merge remote-tracking branch 'origin/main' into nicktobey/wrapper
nicktobey Mar 24, 2025
d4589a3
Unwrap wrapped values when computing hashes for keyless tables.
nicktobey Mar 25, 2025
9f11af5
Migrate explicit string casts to calls to sql.Unwrap
nicktobey Mar 25, 2025
8a2ead9
Merge remote-tracking branch 'origin/main' into nicktobey/wrapper
nicktobey Mar 26, 2025
ba2722f
Make NewUpdateResult public
nicktobey Mar 27, 2025
1867e61
Allow tests that inspect the encoding of a wrapped value without unwr…
nicktobey Mar 27, 2025
f5b659e
Disable the short circuiting of comparing wrapped values until we con…
nicktobey Mar 27, 2025
0dc98df
Merge remote-tracking branch 'origin/main' into nicktobey/wrapper
nicktobey Mar 27, 2025
92b9799
Respond to PR feedback.
nicktobey Mar 28, 2025
a17f203
[ga-format-pr] Run ./format_repo.sh to fix formatting
nicktobey Mar 28, 2025
543ddd0
Unwrap values when inserting them into a key cache.
nicktobey Mar 28, 2025
e17df0b
more precision fixes for `unix_timestamp` (#2918)
jycor Mar 28, 2025
9170b3c
Unwrap wrapped values in Like expressions.
nicktobey Apr 1, 2025
2ac2dec
Merge remote-tracking branch 'origin/main' into nicktobey/wrapper
nicktobey Apr 1, 2025
9988aef
Propagate context parameter.
nicktobey Apr 1, 2025
54ed687
Propagate context parameter.
nicktobey Apr 1, 2025
4569796
Update tests
nicktobey Apr 1, 2025
1d90344
Propogate context in StringType::Compare
nicktobey Apr 1, 2025
822f120
Add context parameter to DatetimeType::Compare
nicktobey Apr 1, 2025
8c2fddb
WIP propogate context parameter
nicktobey Apr 1, 2025
f950002
Propogate context parameter
nicktobey Apr 1, 2025
56c0371
Allow nil ctx parameter in globalStatusVariables::SetGlobal
nicktobey Apr 1, 2025
e5862b5
Merge remote-tracking branch 'origin/main' into nicktobey/wrapper
nicktobey Apr 2, 2025
d169367
[ga-format-pr] Run ./format_repo.sh to fix formatting
nicktobey Apr 2, 2025
5585f96
Don't create sql.Context in MySQLRangeCut.Compare
nicktobey Apr 4, 2025
00fc297
Propogate context parameter.
nicktobey Apr 4, 2025
b3f3563
Propogate context parameter, and remove redundant context instantiati…
nicktobey Apr 7, 2025
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
4 changes: 3 additions & 1 deletion enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,9 @@ func TestOrderByGroupBy(t *testing.T, harness Harness) {
panic(fmt.Sprintf("unexpected type %T", v))
}

team := row[1].(string)
team, ok, err := sql.Unwrap[string](ctx, row[1])
require.NoError(t, err)
require.True(t, ok)
switch team {
case "red":
require.True(t, val == 3 || val == 4)
Expand Down
83 changes: 46 additions & 37 deletions enginetest/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package enginetest

import (
"context"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -679,8 +680,8 @@ func checkResults(
q string,
e QueryEngine,
) {
widenedRows := WidenRows(sch, rows)
widenedExpected := WidenRows(sch, expected)
widenedRows := WidenRows(t, sch, rows)
widenedExpected := WidenRows(t, sch, expected)

upperQuery := strings.ToUpper(q)
orderBy := strings.Contains(upperQuery, "ORDER BY ")
Expand Down Expand Up @@ -795,62 +796,70 @@ func simplifyResultSchema(s sql.Schema) []resultSchemaCol {
return fields
}

// WidenRows returns a slice of rows with all values widened to their widest type.
// WidenRows returns a slice of rows with all values widened to their widest type, and wrapper types unwrapped.
// For a variety of reasons, the widths of various primitive types can vary when passed through different SQL queries
// (and different database implementations). We may eventually decide that this undefined behavior is a problem, but
// for now it's mostly just an issue when comparing results in tests. To get around this, we widen every type to its
// widest value in actual and expected results.
func WidenRows(sch sql.Schema, rows []sql.Row) []sql.Row {
func WidenRows(t *testing.T, sch sql.Schema, rows []sql.Row) []sql.Row {
widened := make([]sql.Row, len(rows))
for i, row := range rows {
widened[i] = WidenRow(sch, row)
widened[i] = WidenRow(t, sch, row)
}
return widened
}

// WidenRow returns a row with all values widened to their widest type
func WidenRow(sch sql.Schema, row sql.Row) sql.Row {
// WidenRow returns a row with all values widened to their widest type, and wrapper types unwrapped.
func WidenRow(t *testing.T, sch sql.Schema, row sql.Row) sql.Row {
widened := make(sql.Row, len(row))
for i, v := range row {

var vw interface{}
if i < len(sch) && types.IsJSON(sch[i].Type) {
widened[i] = widenJSONValues(v)
continue
}

switch x := v.(type) {
case int:
vw = int64(x)
case int8:
vw = int64(x)
case int16:
vw = int64(x)
case int32:
vw = int64(x)
case uint:
vw = uint64(x)
case uint8:
vw = uint64(x)
case uint16:
vw = uint64(x)
case uint32:
vw = uint64(x)
case float32:
// casting it to float64 causes approximation, which doesn't work for server engine results.
vw, _ = strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
case decimal.Decimal:
// The exact expected decimal type value cannot be defined in enginetests,
// so convert the result to string format, which is the value we get on sql shell.
vw = x.StringFixed(x.Exponent() * -1)
default:
vw = v
}
widened[i] = vw
widened[i] = widenValue(t, v)
}
return widened
}

// widenValue normalizes the input by widening it to its widest type and unwrapping any wrappers.
func widenValue(t *testing.T, v interface{}) (vw interface{}) {
var err error
switch x := v.(type) {
case int:
vw = int64(x)
case int8:
vw = int64(x)
case int16:
vw = int64(x)
case int32:
vw = int64(x)
case uint:
vw = uint64(x)
case uint8:
vw = uint64(x)
case uint16:
vw = uint64(x)
case uint32:
vw = uint64(x)
case float32:
// casting it to float64 causes approximation, which doesn't work for server engine results.
vw, _ = strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
case decimal.Decimal:
// The exact expected decimal type value cannot be defined in enginetests,
// so convert the result to string format, which is the value we get on sql shell.
vw = x.StringFixed(x.Exponent() * -1)
case sql.AnyWrapper:
vw, err = x.UnwrapAny(context.Background())
vw = widenValue(t, vw)
require.NoError(t, err)
default:
vw = v
}
return vw
}

func widenJSONValues(val interface{}) sql.JSONWrapper {
if val == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion enginetest/mysqlshim/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ sql.CheckTable = Table{}
var _ sql.StatisticsTable = Table{}
var _ sql.PrimaryKeyAlterableTable = Table{}

func (t Table) IndexedAccess(sql.IndexLookup) sql.IndexedTable {
func (t Table) IndexedAccess(*sql.Context, sql.IndexLookup) sql.IndexedTable {
panic("not implemented")
}

Expand Down
2 changes: 1 addition & 1 deletion enginetest/mysqlshim/table_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type tableEditor struct {
sch sql.Schema
}

func (t *tableEditor) IndexedAccess(lookup sql.IndexLookup) sql.IndexedTable {
func (t *tableEditor) IndexedAccess(ctx *sql.Context, lookup sql.IndexLookup) sql.IndexedTable {
//TODO implement me
panic("implement me")
}
Expand Down
180 changes: 180 additions & 0 deletions enginetest/wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright 2025 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package enginetest

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"

sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/planbuilder"
"github.com/dolthub/go-mysql-server/sql/types"
)

// ErrorWrapper is a wrapped type that errors when unwrapped. This can be used to test that certain operations
// won't trigger an unwrap.
type ErrorWrapper[T any] struct {
maxByteLength int64
isExactLength bool
}

func (w ErrorWrapper[T]) Compare(ctx context.Context, other interface{}) (cmp int, comparable bool, err error) {
return 0, false, nil
}

var textErrorWrapper = ErrorWrapper[string]{maxByteLength: types.Text.MaxByteLength(), isExactLength: false}
var longTextErrorWrapper = ErrorWrapper[string]{maxByteLength: types.LongText.MaxByteLength(), isExactLength: false}

func exactLengthErrorWrapper(maxByteLength int64) ErrorWrapper[string] {
return ErrorWrapper[string]{maxByteLength: maxByteLength, isExactLength: true}
}

func (w ErrorWrapper[T]) assertInterfaces() {
var _ sql.Wrapper[T] = w
}

func (w ErrorWrapper[T]) Unwrap(ctx context.Context) (result T, err error) {
return result, fmt.Errorf("unwrap failed")
}

func (w ErrorWrapper[T]) UnwrapAny(ctx context.Context) (result interface{}, err error) {
return result, fmt.Errorf("unwrap failed")
}

func (w ErrorWrapper[T]) MaxByteLength() int64 {
return w.maxByteLength
}

func (w ErrorWrapper[T]) IsExactLength() bool {
return w.isExactLength
}

func setupWrapperTests(t *testing.T) (*sql.Context, *memory.Database, *MemoryHarness, *sqle.Engine) {
db := memory.NewDatabase("mydb")
pro := memory.NewDBProvider(db)
harness := NewDefaultMemoryHarness().WithProvider(pro)
ctx := NewContext(harness)
e := NewEngineWithProvider(t, harness, pro)
return ctx, db, harness, e
}

// TestWrapperCopyInKey tests that copying a wrapped value in the primary key doesn't require the value to be unwrapped.
// This is skipped because inserting into tables requires comparisons between primary keys, which currently requires
// unwrapping. But in the future, we may be able to skip fully unwrapping values for specific operations.
func TestWrapperCopyInKey(t *testing.T) {
t.Skip()
ctx, db, harness, e := setupWrapperTests(t)

schema := sql.NewPrimaryKeySchema(sql.Schema{
&sql.Column{Name: "col1", Source: "test", Type: types.LongText, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `""`, types.Text, false)},
})
table := memory.NewTable(db.BaseDatabase, "test", schema, nil)

require.NoError(t, table.Insert(ctx, sql.Row{"brave"}))
require.NoError(t, table.Insert(ctx, sql.Row{longTextErrorWrapper}))
require.NoError(t, table.Insert(ctx, sql.Row{"!"}))

TestQueryWithContext(t, ctx, e, harness, "CREATE TABLE t2 AS SELECT 1, col1, 2 FROM test;", nil, nil, nil, nil)
}

// TestWrapperCopyInKey tests that copying a wrapped value not in the primary key doesn't require the value to be unwrapped.
func TestWrapperCopyNotInKey(t *testing.T) {
ctx, db, harness, e := setupWrapperTests(t)

schema := sql.NewPrimaryKeySchema(sql.Schema{
&sql.Column{Name: "pk", Source: "test", Type: types.Int64, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `1`, types.Int64, false), PrimaryKey: true},
&sql.Column{Name: "col1", Source: "test", Type: types.LongText, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `""`, types.Text, false), PrimaryKey: false},
})

testTable := memory.NewTable(db.BaseDatabase, "test", schema, nil)
db.AddTable("test", testTable)

require.NoError(t, testTable.Insert(ctx, sql.Row{int64(1), "brave"}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(2), longTextErrorWrapper}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(3), "!"}))

copySchema := sql.NewPrimaryKeySchema(sql.Schema{
&sql.Column{Name: "one", Source: "t2", Type: types.Int64, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `1`, types.Int64, false), PrimaryKey: true},
&sql.Column{Name: "pk", Source: "t2", Type: types.Int64, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `1`, types.Int64, false), PrimaryKey: true},
&sql.Column{Name: "col1", Source: "t2", Type: types.LongText, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `""`, types.Text, false), PrimaryKey: false},
&sql.Column{Name: "two", Source: "t2", Type: types.Int64, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `1`, types.Int64, false), PrimaryKey: false},
})

testTable2 := memory.NewTable(db.BaseDatabase, "t2", copySchema, nil)
db.AddTable("t2", testTable2)

TestQueryWithContext(t, ctx, e, harness, "INSERT INTO t2 SELECT 1, pk, col1, 2 FROM test;", nil, nil, nil, nil)
}

// TestWrapperCopyWhenWideningColumn tests that widening a column doesn't cause values to be unwrapped.
func TestWrapperCopyWhenWideningColumn(t *testing.T) {
ctx, db, harness, e := setupWrapperTests(t)

schema := sql.NewPrimaryKeySchema(sql.Schema{
&sql.Column{Name: "pk", Source: "test", Type: types.Int64, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `1`, types.Int64, false), PrimaryKey: true},
&sql.Column{Name: "col1", Source: "test", Type: types.Text, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `""`, types.Text, false), PrimaryKey: false},
})

testTable := memory.NewTable(db.BaseDatabase, "test", schema, nil)
db.AddTable("test", testTable)

require.NoError(t, testTable.Insert(ctx, sql.Row{int64(1), "brave"}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(2), textErrorWrapper}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(3), "!"}))

TestQueryWithContext(t, ctx, e, harness, "ALTER TABLE test MODIFY COLUMN col1 LONGTEXT;", nil, nil, nil, nil)
}

// TestWrapperCopyWhenWideningColumn tests that widening a column doesn't cause values to be unwrapped.
func TestWrapperCopyWhenNarrowingColumn(t *testing.T) {
ctx, db, harness, e := setupWrapperTests(t)

schema := sql.NewPrimaryKeySchema(sql.Schema{
&sql.Column{Name: "pk", Source: "test", Type: types.Int64, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `1`, types.Int64, false), PrimaryKey: true},
&sql.Column{Name: "col1", Source: "test", Type: types.LongText, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `""`, types.Text, false), PrimaryKey: false},
})

testTable := memory.NewTable(db.BaseDatabase, "test", schema, nil)
db.AddTable("test", testTable)

require.NoError(t, testTable.Insert(ctx, sql.Row{int64(1), "brave"}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(2), longTextErrorWrapper}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(3), "!"}))

AssertErrWithCtx(t, e, harness, ctx, "ALTER TABLE test MODIFY COLUMN col1 TEXT;", nil, nil, "unwrap failed")
}

func TestWrapperCopyWithExactLengthWhenNarrowingColumn(t *testing.T) {
ctx, db, harness, e := setupWrapperTests(t)

schema := sql.NewPrimaryKeySchema(sql.Schema{
&sql.Column{Name: "pk", Source: "test", Type: types.Int64, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `1`, types.Int64, false), PrimaryKey: true},
&sql.Column{Name: "col1", Source: "test", Type: types.LongText, Nullable: false, Default: planbuilder.MustStringToColumnDefaultValue(sql.NewEmptyContext(), `""`, types.Text, false), PrimaryKey: false},
})

testTable := memory.NewTable(db.BaseDatabase, "test", schema, nil)
db.AddTable("test", testTable)

require.NoError(t, testTable.Insert(ctx, sql.Row{int64(1), "brave"}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(2), exactLengthErrorWrapper(64)}))
require.NoError(t, testTable.Insert(ctx, sql.Row{int64(3), "!"}))

TestQueryWithContext(t, ctx, e, harness, "ALTER TABLE test MODIFY COLUMN col1 TEXT;", nil, nil, nil, nil)
}
2 changes: 1 addition & 1 deletion memory/required_lookup_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s RequiredLookupTable) Database() sql.Database {
return s.db
}

func (s RequiredLookupTable) IndexedAccess(lookup sql.IndexLookup) sql.IndexedTable {
func (s RequiredLookupTable) IndexedAccess(ctx *sql.Context, lookup sql.IndexLookup) sql.IndexedTable {
return RequiredLookupTable{indexOk: true, IntSequenceTable: s.IntSequenceTable}
}

Expand Down
2 changes: 1 addition & 1 deletion memory/sequence_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (s IntSequenceTable) LookupPartitions(context *sql.Context, lookup sql.Inde
return sql.PartitionsToPartitionIter(&sequencePartition{min: min.(int64), max: max.(int64)}), nil
}

func (s IntSequenceTable) IndexedAccess(lookup sql.IndexLookup) sql.IndexedTable {
func (s IntSequenceTable) IndexedAccess(ctx *sql.Context, lookup sql.IndexLookup) sql.IndexedTable {
return s
}

Expand Down
2 changes: 1 addition & 1 deletion memory/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ func (t *IndexedTable) PartitionRows(ctx *sql.Context, partition sql.Partition)
return iter, nil
}

func (t *Table) IndexedAccess(lookup sql.IndexLookup) sql.IndexedTable {
func (t *Table) IndexedAccess(ctx *sql.Context, lookup sql.IndexLookup) sql.IndexedTable {
return &IndexedTable{Table: t, Lookup: lookup}
}

Expand Down
2 changes: 1 addition & 1 deletion memory/table_data.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (td TableData) copy() *TableData {

// partition returns the partition for the row given. Uses the primary key columns if they exist, or all columns
// otherwise
func (td TableData) partition(row sql.Row) (int, error) {
func (td TableData) partition(ctx *sql.Context, row sql.Row) (int, error) {
var keyColumns []int
if len(td.schema.PkOrdinals) > 0 {
keyColumns = td.schema.PkOrdinals
Expand Down
Loading
Loading