-
-
Notifications
You must be signed in to change notification settings - Fork 263
implementing CHAR() function
#2255
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
43cec5f
fix number overflow and add unit tests
07dd52f
enginetest
7e26338
[ga-format-pr] Run ./format_repo.sh to fix formatting
jycor fa865b7
test with using clause
bc33251
Merge branch 'james/char' of https://github.com/dolthub/go-mysql-serv…
38e7d05
use bitops and recursion
a5f5a27
[ga-format-pr] Run ./format_repo.sh to fix formatting
jycor d1a2b98
some comments
59d3d01
Merge branch 'main' into james/char
f201e19
support charsets
97b68c7
bump
cedd2e9
[ga-format-pr] Run ./format_repo.sh to fix formatting
jycor 88e8569
feedback
b515a23
merge with main
acd1b10
merge with main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright 2024 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 function | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/dolthub/vitess/go/sqltypes" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/types" | ||
| ) | ||
|
|
||
| // Char implements the sql function "char" which returns the character for each integer passed | ||
| type Char struct { | ||
| // TODO: support using (charset/collation) clause | ||
|
fulghum marked this conversation as resolved.
Outdated
|
||
| args []sql.Expression | ||
| Collation sql.CollationID | ||
| } | ||
|
|
||
| var _ sql.FunctionExpression = (*Char)(nil) | ||
| var _ sql.CollationCoercible = (*Char)(nil) | ||
|
|
||
| func NewChar(args ...sql.Expression) (sql.Expression, error) { | ||
| return &Char{args: args}, nil | ||
| } | ||
|
|
||
| // FunctionName implements sql.FunctionExpression | ||
| func (c *Char) FunctionName() string { | ||
| return "char" | ||
| } | ||
|
|
||
| // Resolved implements sql.FunctionExpression | ||
| func (c *Char) Resolved() bool { | ||
| for _, arg := range c.args { | ||
| if !arg.Resolved() { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // String implements sql.Expression | ||
| func (c *Char) String() string { | ||
| args := make([]string, len(c.args)) | ||
| for i, arg := range c.args { | ||
| args[i] = arg.String() | ||
| } | ||
| str := strings.Join(args, ", ") | ||
| return fmt.Sprintf("%s(%s)", c.FunctionName(), str) | ||
| } | ||
|
|
||
| // Type implements sql.Expression | ||
| func (c *Char) Type() sql.Type { | ||
| if c.Collation == sql.Collation_binary || c.Collation == sql.Collation_Unspecified { | ||
| return types.MustCreateString(sqltypes.VarBinary, int64(len(c.args)*4), sql.Collation_binary) | ||
| } | ||
| return types.MustCreateString(sqltypes.VarChar, int64(len(c.args)*16), c.Collation) | ||
| } | ||
|
|
||
| // IsNullable implements sql.Expression | ||
| func (c *Char) IsNullable() bool { | ||
| return true | ||
| } | ||
|
|
||
| // Description implements sql.FunctionExpression | ||
| func (c *Char) Description() string { | ||
| return "interprets each argument N as an integer and returns a string consisting of the characters given by the code values of those integers." | ||
| } | ||
|
|
||
| // CollationCoercibility implements the interface sql.CollationCoercible. | ||
| func (c *Char) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { | ||
| return sql.Collation_binary, 5 | ||
| } | ||
|
|
||
| // char converts num into a byte array | ||
| // This function is essentially converting the number to base 256 | ||
| func char(num uint32) []byte { | ||
| if num == 0 { | ||
| return []byte{} | ||
| } | ||
| return append(char(num>>8), byte(num&255)) | ||
| } | ||
|
|
||
| // Eval implements the sql.Expression interface | ||
| func (c *Char) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
| res := []byte{} | ||
| for _, arg := range c.args { | ||
| if arg == nil { | ||
| continue | ||
| } | ||
|
|
||
| val, err := arg.Eval(ctx, row) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if val == nil { | ||
| continue | ||
| } | ||
|
|
||
| v, _, err := types.Uint32.Convert(val) | ||
| if err != nil { | ||
| ctx.Warn(1292, "Truncated incorrect INTEGER value: '%v'", val) | ||
| res = append(res, 0) | ||
| continue | ||
| } | ||
|
|
||
| res = append(res, char(v.(uint32))...) | ||
| } | ||
|
|
||
| return res, nil | ||
| } | ||
|
|
||
| // Children implements sql.Expression | ||
| func (c *Char) Children() []sql.Expression { | ||
| return c.args | ||
| } | ||
|
|
||
| // WithChildren implements the sql.Expression interface | ||
| func (c *Char) WithChildren(children ...sql.Expression) (sql.Expression, error) { | ||
| return NewChar(children...) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Copyright 2024 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 function | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/shopspring/decimal" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/expression" | ||
| "github.com/dolthub/go-mysql-server/sql/types" | ||
| ) | ||
|
|
||
| func TestChar(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []sql.Expression | ||
| exp interface{} | ||
| err bool | ||
| skip bool | ||
| }{ | ||
| { | ||
| name: "null", | ||
| args: []sql.Expression{ | ||
| nil, | ||
| }, | ||
| exp: []byte{}, | ||
| }, | ||
| { | ||
| name: "null literal", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(nil, types.Null), | ||
| }, | ||
| exp: []byte{}, | ||
| }, | ||
| { | ||
| name: "nulls are skipped", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(int32(1), types.Int32), | ||
| expression.NewLiteral(nil, types.Null), | ||
| expression.NewLiteral(int32(300), types.Int32), | ||
| expression.NewLiteral(int32(4000), types.Int32), | ||
| }, | ||
| exp: []byte{0x1, 0x01, 0x2c, 0xf, 0xa0}, | ||
| }, | ||
| { | ||
| name: "-1", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(int32(-1), types.Int32), | ||
| }, | ||
| exp: []byte{0xff, 0xff, 0xff, 0xff}, | ||
| }, | ||
| { | ||
| name: "256", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(int32(256), types.Int32), | ||
| }, | ||
| exp: []byte{0x1, 0x0}, | ||
| }, | ||
| { | ||
| name: "512", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(int32(512), types.Int32), | ||
| }, | ||
| exp: []byte{0x2, 0x0}, | ||
| }, | ||
| { | ||
| name: "256 * 256", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(int32(256*256), types.Int32), | ||
| }, | ||
| exp: []byte{0x1, 0x0, 0x0}, | ||
| }, | ||
| { | ||
| name: "1 2 3 4", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(int32(1), types.Int32), | ||
| expression.NewLiteral(int32(2), types.Int32), | ||
| expression.NewLiteral(int32(3), types.Int32), | ||
| expression.NewLiteral(int32(4), types.Int32), | ||
| }, | ||
| exp: []byte{0x1, 0x2, 0x3, 0x4}, | ||
| }, | ||
| { | ||
| name: "1 20 300 4000", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(int32(1), types.Int32), | ||
| expression.NewLiteral(int32(20), types.Int32), | ||
| expression.NewLiteral(int32(300), types.Int32), | ||
| expression.NewLiteral(int32(4000), types.Int32), | ||
| }, | ||
| exp: []byte{0x1, 0x14, 0x1, 0x2c, 0xf, 0xa0}, | ||
| }, | ||
| { | ||
| name: "float32 1.99", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(float32(1.99), types.Float32), | ||
| }, | ||
| exp: []byte{0x2}, | ||
| }, | ||
| { | ||
| name: "float64 1.99", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(1.99, types.Float64), | ||
| }, | ||
| exp: []byte{0x2}, | ||
| }, | ||
| { | ||
| name: "decimal 1.99", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(decimal.NewFromFloat(1.99), types.DecimalType_{}), | ||
| }, | ||
| exp: []byte{0x2}, | ||
| }, | ||
| { | ||
| name: "good string", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral("12", types.Text), | ||
| }, | ||
| exp: []byte{0x0C}, | ||
| }, | ||
| { | ||
| name: "bad string", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral("abc", types.Text), | ||
| }, | ||
| exp: []byte{0x0}, | ||
| }, | ||
| { | ||
| name: "mix types", | ||
| args: []sql.Expression{ | ||
| expression.NewLiteral(1, types.Int32), | ||
| expression.NewLiteral(9999, types.Int32), | ||
| expression.NewLiteral(1.23, types.Int32), | ||
| expression.NewLiteral("78", types.Text), | ||
| expression.NewLiteral("abc", types.Text), | ||
| }, | ||
| exp: []byte{0x01, 0x27, 0x0F, 0x01, 0x4E, 0x0}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if tt.skip { | ||
| t.Skip() | ||
| } | ||
|
|
||
| ctx := sql.NewEmptyContext() | ||
| f, err := NewChar(tt.args...) | ||
| require.NoError(t, err) | ||
|
|
||
| res, err := f.Eval(ctx, nil) | ||
| if tt.err { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, tt.exp, res) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.