From 5f9032bf0ee1128032e3c074b56d8f4f67b9655d Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Tue, 30 Apr 2024 12:59:59 -0700 Subject: [PATCH] More testing --- server/cast/bool.go | 22 +++--- server/cast/float32.go | 44 +++++------ server/cast/float64.go | 44 +++++------ server/cast/init.go | 2 +- server/cast/int16.go | 25 +++--- server/cast/int32.go | 25 +++--- server/cast/int64.go | 25 +++--- server/cast/numeric.go | 44 +++++------ server/cast/oid.go | 28 +++---- testing/go/types_test.go | 162 +++++++++++++++++++++------------------ 10 files changed, 211 insertions(+), 210 deletions(-) diff --git a/server/cast/bool.go b/server/cast/bool.go index 4864823750..ce56d607da 100644 --- a/server/cast/bool.go +++ b/server/cast/bool.go @@ -100,17 +100,17 @@ func boolExplicit() { return handleCharExplicitCast(str, targetType) }, }) - framework.MustAddExplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Bool, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - if val.(bool) { - return uint32(1), nil - } else { - return uint32(0), nil - } - }, - }) + // framework.MustAddExplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Bool, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // if val.(bool) { + // return uint32(1), nil + // } else { + // return uint32(0), nil + // } + // }, + // }) } // boolImplicit registers all implicit casts. This comprises only the "From" types. diff --git a/server/cast/float32.go b/server/cast/float32.go index 225bf1a8fd..6f40066d22 100644 --- a/server/cast/float32.go +++ b/server/cast/float32.go @@ -129,17 +129,17 @@ func float32Explicit() { return handleCharExplicitCast(str, targetType) }, }) - framework.MustAddExplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Float32, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { - val := float32(math.RoundToEven(float64(valInterface.(float32)))) - if val > 2147483647 || val < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val), nil - }, - }) + // framework.MustAddExplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Float32, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { + // val := float32(math.RoundToEven(float64(valInterface.(float32)))) + // if val > 2147483647 || val < 0 { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(val), nil + // }, + // }) } // float32Implicit registers all implicit casts. This comprises only the "From" types. @@ -240,15 +240,15 @@ func float32Implicit() { return handleCharImplicitCast(str, targetType) }, }) - framework.MustAddImplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Float32, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { - val := float32(math.RoundToEven(float64(valInterface.(float32)))) - if val > 2147483647 || val < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val), nil - }, - }) + // framework.MustAddImplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Float32, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { + // val := float32(math.RoundToEven(float64(valInterface.(float32)))) + // if val > 2147483647 || val < 0 { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(val), nil + // }, + // }) } diff --git a/server/cast/float64.go b/server/cast/float64.go index f751fbc8f2..eb0292f3db 100644 --- a/server/cast/float64.go +++ b/server/cast/float64.go @@ -129,17 +129,17 @@ func float64Explicit() { return handleCharExplicitCast(str, targetType) }, }) - framework.MustAddExplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Float64, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { - val := math.RoundToEven(valInterface.(float64)) - if val > 2147483647 || val < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val), nil - }, - }) + // framework.MustAddExplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Float64, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { + // val := math.RoundToEven(valInterface.(float64)) + // if val > 2147483647 || val < 0 { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(val), nil + // }, + // }) } // float64Implicit registers all implicit casts. This comprises only the "From" types. @@ -240,15 +240,15 @@ func float64Implicit() { return handleCharImplicitCast(str, targetType) }, }) - framework.MustAddImplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Float64, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { - val := math.RoundToEven(valInterface.(float64)) - if val > 2147483647 || val < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val), nil - }, - }) + // framework.MustAddImplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Float64, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) { + // val := math.RoundToEven(valInterface.(float64)) + // if val > 2147483647 || val < 0 { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(val), nil + // }, + // }) } diff --git a/server/cast/init.go b/server/cast/init.go index bcc953ca21..4a9d9d7592 100644 --- a/server/cast/init.go +++ b/server/cast/init.go @@ -30,5 +30,5 @@ func Init() { initText() initUuid() initVarChar() - initXid() + // initXid() } diff --git a/server/cast/int16.go b/server/cast/int16.go index 89ab8cc26c..795240eeda 100644 --- a/server/cast/int16.go +++ b/server/cast/int16.go @@ -119,10 +119,7 @@ func int16Explicit() { FromType: pgtypes.Int16, ToType: pgtypes.Xid, Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - if val.(int16) < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val.(int16)), nil + return nil, fmt.Errorf("cannot cast type %s to %s", pgtypes.Int16.String(), pgtypes.Xid.String()) }, }) } @@ -212,14 +209,14 @@ func int16Implicit() { return handleCharImplicitCast(str, targetType) }, }) - framework.MustAddImplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Int16, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - if val.(int16) < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val.(int16)), nil - }, - }) + // framework.MustAddImplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Int16, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // if val.(int16) < 0 { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(val.(int16)), nil + // }, + // }) } diff --git a/server/cast/int32.go b/server/cast/int32.go index 4432c3f089..92777e2fd6 100644 --- a/server/cast/int32.go +++ b/server/cast/int32.go @@ -122,10 +122,7 @@ func int32Explicit() { FromType: pgtypes.Int32, ToType: pgtypes.Xid, Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - if val.(int32) < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val.(int32)), nil + return nil, fmt.Errorf("cannot cast type %s to %s", pgtypes.Int32.String(), pgtypes.Xid.String()) }, }) } @@ -218,14 +215,14 @@ func int32Implicit() { return handleCharImplicitCast(str, targetType) }, }) - framework.MustAddImplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Int32, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - if val.(int32) < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val.(int32)), nil - }, - }) + // framework.MustAddImplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Int32, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // if val.(int32) < 0 { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(val.(int32)), nil + // }, + // }) } diff --git a/server/cast/int64.go b/server/cast/int64.go index 750887e0b5..f75aa55577 100644 --- a/server/cast/int64.go +++ b/server/cast/int64.go @@ -125,10 +125,7 @@ func int64Explicit() { FromType: pgtypes.Int64, ToType: pgtypes.Xid, Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - if val.(int64) > 2147483647 || val.(int64) < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val.(int64)), nil + return nil, fmt.Errorf("cannot cast type %s to %s", pgtypes.Int64.String(), pgtypes.Xid.String()) }, }) } @@ -224,14 +221,14 @@ func int64Implicit() { return handleCharImplicitCast(str, targetType) }, }) - framework.MustAddImplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Int64, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - if val.(int64) > 2147483647 || val.(int64) < 0 { - return nil, fmt.Errorf("XID out of range") - } - return uint32(val.(int64)), nil - }, - }) + // framework.MustAddImplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Int64, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // if val.(int64) > 2147483647 || val.(int64) < 0 { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(val.(int64)), nil + // }, + // }) } diff --git a/server/cast/numeric.go b/server/cast/numeric.go index b20647b91f..29474c24fa 100644 --- a/server/cast/numeric.go +++ b/server/cast/numeric.go @@ -126,17 +126,17 @@ func numericExplicit() { return handleCharExplicitCast(val.(decimal.Decimal).String(), targetType) }, }) - framework.MustAddExplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Numeric, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - d := val.(decimal.Decimal) - if d.LessThan(decimal.NewFromInt(0)) || d.GreaterThan(pgtypes.NumericValueMaxInt32) { - return nil, fmt.Errorf("XID out of range") - } - return uint32(d.IntPart()), nil - }, - }) + // framework.MustAddExplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Numeric, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // d := val.(decimal.Decimal) + // if d.LessThan(decimal.NewFromInt(0)) || d.GreaterThan(pgtypes.NumericValueMaxInt32) { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(d.IntPart()), nil + // }, + // }) } // numericImplicit registers all implicit casts. This comprises only the "From" types. @@ -236,15 +236,15 @@ func numericImplicit() { return handleCharImplicitCast(val.(decimal.Decimal).String(), targetType) }, }) - framework.MustAddImplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Numeric, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - d := val.(decimal.Decimal) - if d.LessThan(decimal.NewFromInt(0)) || d.GreaterThan(pgtypes.NumericValueMaxInt32) { - return nil, fmt.Errorf("XID out of range") - } - return uint32(d.IntPart()), nil - }, - }) + // framework.MustAddImplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Numeric, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // d := val.(decimal.Decimal) + // if d.LessThan(decimal.NewFromInt(0)) || d.GreaterThan(pgtypes.NumericValueMaxInt32) { + // return nil, fmt.Errorf("XID out of range") + // } + // return uint32(d.IntPart()), nil + // }, + // }) } diff --git a/server/cast/oid.go b/server/cast/oid.go index e1f83caf62..5aff9d289d 100644 --- a/server/cast/oid.go +++ b/server/cast/oid.go @@ -107,13 +107,13 @@ func oidExplicit() { return handleCharExplicitCast(str, targetType) }, }) - framework.MustAddExplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Oid, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - return val, nil - }, - }) + // framework.MustAddExplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Oid, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // return val, nil + // }, + // }) } // oidImplicit registers all implicit casts. This comprises only the "From" types. @@ -193,11 +193,11 @@ func oidImplicit() { return handleCharImplicitCast(str, targetType) }, }) - framework.MustAddImplicitTypeCast(framework.TypeCast{ - FromType: pgtypes.Oid, - ToType: pgtypes.Xid, - Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { - return val, nil - }, - }) + // framework.MustAddImplicitTypeCast(framework.TypeCast{ + // FromType: pgtypes.Oid, + // ToType: pgtypes.Xid, + // Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) { + // return val, nil + // }, + // }) } diff --git a/testing/go/types_test.go b/testing/go/types_test.go index 23fba7344c..9783907025 100644 --- a/testing/go/types_test.go +++ b/testing/go/types_test.go @@ -1123,96 +1123,106 @@ var typesTests = []ScriptTest{ { Name: "Xid type", SetUpScript: []string{ - "CREATE TABLE t_xid (id INTEGER primary key, v1 XID, v2 INTEGER);", - "INSERT INTO t_xid VALUES (1, 1234, 100), (2, 5678, -100);", + "CREATE TABLE t_xid (id INTEGER primary key, v1 XID, v2 VARCHAR(20));", }, Assertions: []ScriptTestAssertion{ { - Query: "SELECT * FROM t_xid ORDER BY id;", - Expected: []sql.Row{ - {1, 1234, 100}, - {2, 5678, -100}, - }, - }, - { - Query: "SELECT * FROM t_xid ORDER BY v1 DESC;", - Expected: []sql.Row{ - {2, 5678, -100}, - {1, 1234, 100}, - }, - }, - { - Query: "SELECT v1::char(1) FROM t_xid WHERE v1=5678;", - Expected: []sql.Row{ - {"5"}, - }, - }, - { - Query: "SELECT v1::smallint FROM t_xid WHERE v1=5678;", - Expected: []sql.Row{ - {5678}, - }, - }, - { - Query: "UPDATE t_xid SET v1=9012 WHERE id=2;", - Expected: []sql.Row{}, - }, - { - Query: "DELETE FROM t_xid WHERE v1=1234;", - Expected: []sql.Row{}, - }, - { - Query: "SELECT * FROM t_xid ORDER BY id;", - Expected: []sql.Row{ - {2, 9012, -100}, - }, - }, - { - Query: "SELECT v2::xid FROM t_xid ORDER BY id;", - Skip: true, // TODO: Cast currently returns error for negative values - Expected: []sql.Row{ - {4294967196}, - }, + Query: "INSERT INTO t_xid VALUES (1, 1234, 100);", + Skip: true, // TODO: Should return 'column "v1" is of type xid but expression is of type integer' error + ExpectedErr: true, }, { - Query: "INSERT INTO t_xid VALUES (3, 2147483647000, 100);", - Skip: true, // TODO: Should return an XID out of range error + Query: "INSERT INTO t_xid VALUES (1, 1234::xid, 100);", ExpectedErr: true, }, { - Query: "INSERT INTO t_xid VALUES (3, '2345', 100);", + Query: "INSERT INTO t_xid VALUES (1, NULL, 100);", Expected: []sql.Row{}, }, { - Query: "SELECT * FROM t_xid ORDER BY id;", - Expected: []sql.Row{ - {2, 9012, -100}, - {3, 2345, 100}, - }, - }, - }, - }, - { - Name: "Xid array type", - SetUpScript: []string{ - "CREATE TABLE t_xid (id INTEGER primary key, v1 XID[], v2 CHARACTER(100), v3 BOOLEAN);", - "INSERT INTO t_xid VALUES (1, ARRAY[123, 456, 789, 101], '1234567890', true);", - }, - Assertions: []ScriptTestAssertion{ - { - Query: `SELECT v1::varchar(1)[] FROM t_xid;`, - Expected: []sql.Row{ - {"{1,4,7,1}"}, - }, - }, - { - Query: `SELECT v2::xid, v3::xid FROM t_xid;`, - Expected: []sql.Row{ - {1234567890, 1}, - }, + Query: "SELECT v2::xid FROM t_xid ORDER BY id;", + Expected: []sql.Row{}, }, + // { + // Query: "SELECT * FROM t_xid ORDER BY v1 DESC;", + // Expected: []sql.Row{ + // {2, 5678, -100}, + // {1, 1234, 100}, + // }, + // }, + // { + // Query: "SELECT v1::char(1) FROM t_xid WHERE v1=5678;", + // Expected: []sql.Row{ + // {"5"}, + // }, + // }, + // { + // Query: "SELECT v1::smallint FROM t_xid WHERE v1=5678;", + // Expected: []sql.Row{ + // {5678}, + // }, + // }, + // { + // Query: "UPDATE t_xid SET v1=9012 WHERE id=2;", + // Expected: []sql.Row{}, + // }, + // { + // Query: "DELETE FROM t_xid WHERE v1=1234;", + // Expected: []sql.Row{}, + // }, + // { + // Query: "SELECT * FROM t_xid ORDER BY id;", + // Expected: []sql.Row{ + // {2, 9012, -100}, + // }, + // }, + // { + // Query: "SELECT v2::xid FROM t_xid ORDER BY id;", + // Skip: true, // TODO: Cast currently returns error for negative values + // Expected: []sql.Row{ + // {4294967196}, + // }, + // }, + // { + // Query: "INSERT INTO t_xid VALUES (3, 2147483647000, 100);", + // Skip: true, // TODO: Should return an XID out of range error + // ExpectedErr: true, + // }, + // { + // Query: "INSERT INTO t_xid VALUES (3, '2345', 100);", + // Expected: []sql.Row{}, + // }, + // { + // Query: "SELECT * FROM t_xid ORDER BY id;", + // Expected: []sql.Row{ + // {2, 9012, -100}, + // {3, 2345, 100}, + // }, + // }, + // }, }, }, + // { + // Name: "Xid array type", + // SetUpScript: []string{ + // "CREATE TABLE t_xid (id INTEGER primary key, v1 XID[], v2 CHARACTER(100), v3 BOOLEAN);", + // "INSERT INTO t_xid VALUES (1, ARRAY[123, 456, 789, 101], '1234567890', true);", + // }, + // Assertions: []ScriptTestAssertion{ + // { + // Query: `SELECT v1::varchar(1)[] FROM t_xid;`, + // Expected: []sql.Row{ + // {"{1,4,7,1}"}, + // }, + // }, + // { + // Query: `SELECT v2::xid, v3::xid FROM t_xid;`, + // Expected: []sql.Row{ + // {1234567890, 1}, + // }, + // }, + // }, + // }, { Name: "Xml type", Skip: true,