Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse charset introducer for string literals for expressions #960

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,32 @@ func TestCreateTable(t *testing.T, harness Harness) {

require.Equal(t, s, testTable.Schema())
})

t.Run("create table statement with charset introducer in column definition", func(t *testing.T) {
ctx := NewContext(harness)
ctx.SetCurrentDatabase("mydb")

TestQueryWithContext(t, ctx, e, `CREATE TABLE types_with_defaults (
pk int NOT NULL,
col1 blob DEFAULT (_utf8mb4'abc'),
col2 json DEFAULT (json_object(_utf8mb4'a',1)),
col3 text DEFAULT (_utf8mb4'abc'),
PRIMARY KEY (pk)
)`, []sql.Row(nil), nil, nil)

db, err := e.Analyzer.Catalog.Database(ctx, "mydb")
require.NoError(t, err)

_, ok, err := db.GetTableInsensitive(ctx, "types_with_defaults")
require.NoError(t, err)
require.True(t, ok)

RunQuery(t, e, harness, "INSERT INTO types_with_defaults (pk) VALUES (1)")
TestQueryWithContext(t, ctx, e, "SELECT * FROM types_with_defaults", []sql.Row{{1, "abc", sql.MustJSON(`{"a":1}`), "abc"}}, nil, nil)

_, _, err = e.Query(ctx, "CREATE TABLE unsupported_charset (pk int NOT NULL, col1 blob DEFAULT (_latin1'abc'))")
require.Error(t, err)
})
}

func TestDropTable(t *testing.T, harness Harness) {
Expand Down
13 changes: 13 additions & 0 deletions sql/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3386,7 +3386,20 @@ func unaryExprToExpression(ctx *sql.Context, e *sqlparser.UnaryExpr) (sql.Expres
return expression.NewLiteral(exprLiteral.Value(), sql.LongBlob), nil
}
return expr, nil
case "_utf8mb4 ", "_utf8mb3 ", "_utf8 ":
expr, err := ExprToExpression(ctx, e.Expr)
if err != nil {
return nil, err
}
// must be string type
if !sql.IsText(expr.Type()) {
return nil, sql.ErrInvalidType.New(expr.Type().String() + " after character set introducer")
}
return expr, nil
default:
if strings.HasPrefix(strings.ToLower(e.Operator), "_") {
return nil, sql.ErrUnsupportedFeature.New("unsupported character set: " + e.Operator)
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
}
return nil, sql.ErrUnsupportedFeature.New("unary operator: " + e.Operator)
}
}
Expand Down