Skip to content

Commit

Permalink
Fix char casts
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Jul 26, 2024
1 parent 472bb30 commit 30b4168
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
17 changes: 11 additions & 6 deletions server/cast/internal_char.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"strconv"
"strings"
"unicode"

"github.com/dolthub/go-mysql-server/sql"

Expand All @@ -44,14 +45,18 @@ func internalCharExplicit() {
FromType: pgtypes.InternalChar,
ToType: pgtypes.Int32,
Function: func(ctx *sql.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
s := val.(string)
if len(s) == 0 {
return int32(0), nil
}
if out > 2147483647 || out < -2147483648 {
return nil, fmt.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
if unicode.IsLetter(rune(s[0])) {
return int32(s[0]), nil
}
return int32(out), nil
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return 0, err
}
return int32(i), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
Expand Down
8 changes: 2 additions & 6 deletions server/cast/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ func handleStringCast(str string, targetType pgtypes.DoltgresType) (string, erro
}
}
case pgtypes.InternalCharType:
str, runeLength := truncateString(str, pgtypes.InternalCharLength)
if runeLength > pgtypes.InternalCharLength {
return str, fmt.Errorf("value too long for type %s", targetType.String())
} else {
return str, nil
}
str, _ := truncateString(str, pgtypes.InternalCharLength)
return str, nil
case pgtypes.NameType:
// Name seems to never throw an error, regardless of the context or how long the input is
str, _ := truncateString(str, targetType.Length)
Expand Down
2 changes: 1 addition & 1 deletion server/types/internal_char.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (b InternalCharType) Zero() any {
// SerializeType implements the DoltgresType interface.
func (b InternalCharType) SerializeType() ([]byte, error) {
t := make([]byte, serializationIDHeaderSize+4)
copy(t, SerializationID_Char.ToByteSlice(0))
copy(t, SerializationID_InternalChar.ToByteSlice(0))
binary.LittleEndian.PutUint32(t[serializationIDHeaderSize:], InternalCharLength)
return t, nil
}
Expand Down
1 change: 0 additions & 1 deletion testing/go/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestSmokeTests(t *testing.T) {
},
},
{
// Skip: true, // TODO: specifying tables in column names fails with `table not found` error
Query: "SELECT test2.pk FROM test2;",
Expected: []sql.Row{
{3},
Expand Down
15 changes: 6 additions & 9 deletions testing/go/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,17 @@ var typesTests = []ScriptTest{
},
{
Name: "Internal char type",
Skip: true, // TODO: During insert t_char schema is using Char(1) instead of InternalChar
SetUpScript: []string{
`CREATE TABLE t_char (id INTEGER primary key, v1 "char");`,
`INSERT INTO t_char VALUES (1, 'abcde'), (2, 'vwxyz'), (3, '123'), (4, ''), (5, NULL);`,
},
Assertions: []ScriptTestAssertion{
{
Query: `INSERT INTO t_char VALUES (1, 'abcde'), (2, 'vwxyz'), (3, 'ghi'), (4, ''), (5, NULL);`,
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM t_char ORDER BY id;",
Expected: []sql.Row{
{1, "a"},
{2, "v"},
{3, "g"},
{3, "1"},
{4, ""},
{5, nil},
},
Expand All @@ -303,21 +299,22 @@ var typesTests = []ScriptTest{
ExpectedErr: `target is of type "char" but expression is of type boolean`,
},
{
Skip: true, // TODO: Why is this not erroring?
Query: `SELECT true::"char";`,
ExpectedErr: `cannot cast type boolean to "char"`,
},
{
Query: `SELECT 'abc'::"char", 'def'::name::"char", 'ghi'::varchar(3)::"char";`,
Query: `SELECT 'abc'::"char", 'def'::name::"char", '123'::varchar(3)::"char";`,
Expected: []sql.Row{
{"a", "d", "g"},
{"a", "d", "1"},
},
},
{
Query: `SELECT id, v1::int, v1::text FROM t_char;`,
Expected: []sql.Row{
{1, 97, "a"},
{2, 118, "v"},
{3, 103, "g"},
{3, 1, "1"},
{4, 0, ""},
{5, nil, nil},
},
Expand Down

0 comments on commit 30b4168

Please sign in to comment.