Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
10 changes: 7 additions & 3 deletions enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestBrokenQueries(t *testing.T, harness Harness) {
// queries during debugging.
func RunQueryTests(t *testing.T, harness Harness, queries []queries.QueryTest) {
for _, tt := range queries {
TestQuery(t, harness, tt.Query, tt.Expected, tt.ExpectedColumns, nil)
testQuery(t, harness, tt.Query, tt.Expected, tt.ExpectedColumns, nil, !tt.DontUnwrap)
}
}

Expand Down 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 Expand Up @@ -846,7 +848,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.True(t, ok)
require.NoError(t, err)
switch team {
case "red":
require.True(t, val == 3 || val == 4)
Expand Down
139 changes: 93 additions & 46 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 @@ -325,6 +326,10 @@ func TestTransactionScriptWithEngine(t *testing.T, e QueryEngine, harness Harnes
// TestQuery runs a query on the engine given and asserts that results are as expected.
// TODO: this should take en engine
func TestQuery(t *testing.T, harness Harness, q string, expected []sql.Row, expectedCols []*sql.Column, bindings map[string]sqlparser.Expr) {
testQuery(t, harness, q, expected, expectedCols, bindings, true)
}

func testQuery(t *testing.T, harness Harness, q string, expected []sql.Row, expectedCols []*sql.Column, bindings map[string]sqlparser.Expr, unwrapValues bool) {
t.Run(q, func(t *testing.T) {
if sh, ok := harness.(SkippingHarness); ok {
if sh.SkipQueryTest(q) {
Expand All @@ -335,7 +340,7 @@ func TestQuery(t *testing.T, harness Harness, q string, expected []sql.Row, expe
e := mustNewEngine(t, harness)
defer e.Close()
ctx := NewContext(harness)
TestQueryWithContext(t, ctx, e, harness, q, expected, expectedCols, bindings, nil)
testQueryWithContext(t, ctx, e, harness, q, expected, expectedCols, bindings, nil, unwrapValues)
})
}

Expand Down Expand Up @@ -377,6 +382,21 @@ func TestQueryWithContext(
expectedCols []*sql.Column,
bindings map[string]sqlparser.Expr,
qFlags *sql.QueryFlags,
) {
testQueryWithContext(t, ctx, e, harness, q, expected, expectedCols, bindings, qFlags, true)
}

func testQueryWithContext(
t *testing.T,
ctx *sql.Context,
e QueryEngine,
harness Harness,
q string,
expected []sql.Row,
expectedCols []*sql.Column,
bindings map[string]sqlparser.Expr,
qFlags *sql.QueryFlags,
unwrapValues bool,
) {
ctx = ctx.WithQuery(q)
require := require.New(t)
Expand All @@ -392,7 +412,7 @@ func TestQueryWithContext(
require.NoError(err, "Unexpected error for query %s: %s", q, err)

if expected != nil {
CheckResults(t, harness, expected, expectedCols, sch, rows, q, e)
checkResults(t, harness, expected, expectedCols, sch, rows, q, e, unwrapValues)
}

require.Equal(
Expand Down Expand Up @@ -501,7 +521,6 @@ func TestPreparedQueryWithContext(t *testing.T, ctx *sql.Context, e QueryEngine,
validateEngine(t, ctx, h, e)
}

// CheckResults compares the
func CheckResults(
t *testing.T,
h Harness,
Expand All @@ -511,11 +530,25 @@ func CheckResults(
rows []sql.Row,
q string,
e QueryEngine,
) {
checkResults(t, h, expected, expectedCols, sch, rows, q, e, true)
}

func checkResults(
t *testing.T,
h Harness,
expected []sql.Row,
expectedCols []*sql.Column,
sch sql.Schema,
rows []sql.Row,
q string,
e QueryEngine,
unwrapValues bool,
) {
if reh, ok := h.(ResultEvaluationHarness); ok {
reh.EvaluateQueryResults(t, expected, expectedCols, sch, rows, q)
reh.EvaluateQueryResults(t, expected, expectedCols, sch, rows, q, unwrapValues)
} else {
checkResults(t, expected, expectedCols, sch, rows, q, e)
checkResultsDefault(t, expected, expectedCols, sch, rows, q, e, unwrapValues)
}
}

Expand Down Expand Up @@ -668,19 +701,22 @@ func toSQL(c *sql.Column, expected any, isZeroTime bool) (any, error) {
}
}

// checkResults is the default implementation for checking the results of a test query assertion for harnesses that
// checkResultsDefault is the default implementation for checking the results of a test query assertion for harnesses that
// don't implement ResultEvaluationHarness. All numerical values are widened to their widest type before comparison.
func checkResults(
// Based on the value of |unwrapValues|, this either normalized wrapped values by unwrapping them, or replaces them
// with their hash so the test caller can assert that the values are wrapped and have a certain hash.
func checkResultsDefault(
t *testing.T,
expected []sql.Row,
expectedCols []*sql.Column,
sch sql.Schema,
rows []sql.Row,
q string,
e QueryEngine,
unwrapValues bool,
) {
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 @@ -714,6 +750,14 @@ func checkResults(
widenedRow[i] = el
}
}
case sql.AnyWrapper:
if unwrapValues {
var err error
widenedRow[i], err = sql.UnwrapAny(context.Background(), v)
require.NoError(t, err)
} else {
widenedRow[i] = v.Hash()
}
}
}
}
Expand Down Expand Up @@ -795,62 +839,65 @@ 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{}) {
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
}
return vw
}

func widenJSONValues(val interface{}) sql.JSONWrapper {
if val == nil {
return nil
Expand Down Expand Up @@ -1151,12 +1198,12 @@ func RunWriteQueryTestWithEngine(t *testing.T, harness Harness, e QueryEngine, t
}

ctx := NewContext(harness)
TestQueryWithContext(t, ctx, e, harness, tt.WriteQuery, tt.ExpectedWriteResult, nil, nil, nil)
testQueryWithContext(t, ctx, e, harness, tt.WriteQuery, tt.ExpectedWriteResult, nil, nil, nil, !tt.DontUnwrap)
expectedSelect := tt.ExpectedSelect
if IsServerEngine(e) && tt.SkipServerEngine {
expectedSelect = nil
}
TestQueryWithContext(t, ctx, e, harness, tt.SelectQuery, expectedSelect, nil, nil, nil)
testQueryWithContext(t, ctx, e, harness, tt.SelectQuery, expectedSelect, nil, nil, nil, !tt.DontUnwrap)
}

func supportedDialect(harness Harness, dialect string) bool {
Expand Down
1 change: 1 addition & 0 deletions enginetest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type ResultEvaluationHarness interface {
expectdSch sql.Schema,
actualRows []sql.Row,
query string,
unwrapValues bool,
)

// EvaluateExpectedError compares expected error strings to actual errors and emits failed test assertions in the
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
4 changes: 2 additions & 2 deletions enginetest/queries/blob_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var BlobWriteQueries = []WriteQueryTest{
},
{
WriteQuery: "update blobt set b = '100000000' where i = 1",
ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}},
ExpectedWriteResult: []sql.Row{{NewUpdateResult(1, 1)}},
SelectQuery: "select * from blobt where i = 1",
ExpectedSelect: []sql.Row{{1, []byte("100000000")}},
},
Expand Down Expand Up @@ -130,7 +130,7 @@ var BlobWriteQueries = []WriteQueryTest{
},
{
WriteQuery: "update textt set t = '100000000' where i = 1",
ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}},
ExpectedWriteResult: []sql.Row{{NewUpdateResult(1, 1)}},
SelectQuery: "select * from textt where i = 1",
ExpectedSelect: []sql.Row{{1, "100000000"}},
},
Expand Down
4 changes: 2 additions & 2 deletions enginetest/queries/column_default_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var ColumnDefaultTests = []ScriptTest{
Assertions: []ScriptTestAssertion{
{
Query: "update t1 n inner join t2 m on n.name = m.name set n.cnt =m.cnt+1;",
Expected: []sql.Row{{newUpdateResult(2, 2)}},
Expected: []sql.Row{{NewUpdateResult(2, 2)}},
},
{
Query: "select name, cnt from t1",
Expand All @@ -48,7 +48,7 @@ var ColumnDefaultTests = []ScriptTest{
Assertions: []ScriptTestAssertion{
{
Query: "update t1 n inner join t2 m on n.y = m.y set n.x =n.y where n.x = 3;",
Expected: []sql.Row{{newUpdateResult(1, 1)}},
Expected: []sql.Row{{NewUpdateResult(1, 1)}},
},
{
Query: "select * from t1",
Expand Down
10 changes: 5 additions & 5 deletions enginetest/queries/generated_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var GeneratedColumnTests = []ScriptTest{
},
{
Query: "update t1 set a = 10 where a = 1",
Expected: []sql.Row{{newUpdateResult(1, 1)}},
Expected: []sql.Row{{NewUpdateResult(1, 1)}},
},
{
Query: "select * from t1 order by a",
Expand Down Expand Up @@ -149,7 +149,7 @@ var GeneratedColumnTests = []ScriptTest{
},
{
Query: "update t1 set a = 10 where a = 1",
Expected: []sql.Row{{newUpdateResult(1, 1)}},
Expected: []sql.Row{{NewUpdateResult(1, 1)}},
},
{
Query: "select * from t1 where b = 11 order by a",
Expand Down Expand Up @@ -466,7 +466,7 @@ var GeneratedColumnTests = []ScriptTest{
},
{
Query: "update t1 set a = 5, c = 10 where b = 2 and c = 3",
Expected: []sql.Row{{newUpdateResult(1, 1)}},
Expected: []sql.Row{{NewUpdateResult(1, 1)}},
},
{
Query: "select * from t1 where b = 6 and c = 10 order by a",
Expand Down Expand Up @@ -1006,7 +1006,7 @@ var GeneratedColumnTests = []ScriptTest{
},
{
Query: "update t1 set b = 5 where c = 3",
Expected: []sql.Row{{newUpdateResult(1, 1)}},
Expected: []sql.Row{{NewUpdateResult(1, 1)}},
},
{
Query: "select * from t1 order by a",
Expand Down Expand Up @@ -1046,7 +1046,7 @@ var GeneratedColumnTests = []ScriptTest{
},
{
Query: "update t1 set j = '{\"a\": 5}' where v = 2",
Expected: []sql.Row{{newUpdateResult(1, 1)}},
Expected: []sql.Row{{NewUpdateResult(1, 1)}},
},
{
Query: "select * from t1 order by v",
Expand Down
Loading
Loading