-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Built-in string functions implementation: ASCII, BIN, BIT_LENGTH #9793
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1679a72
Add built-in functions ascii, bin, bit_length to evalengine
wanghaoyang1995 1a1e646
Merge branch 'vitessio:main' into main
wanghaoyang1995 2a420ae
Merge branch 'vitessio:main' into main
wanghaoyang1995 2302075
cover more test case
wanghaoyang1995 71c2214
Merge branch 'vitessio:main' into main
wanghaoyang1995 6b89697
Merge branch 'main' of github.com:wanghaoyang1995/vitess into main
wanghaoyang1995 659881b
fix errors in test case
wanghaoyang1995 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 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
119 changes: 119 additions & 0 deletions
119
go/vt/vtgate/evalengine/integration/string_func_test.go
This file contains 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,119 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
|
||
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 integration | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestBuiltinAscii(t *testing.T) { | ||
var elems = []string{ | ||
"NULL", | ||
"\"\"", | ||
"\"a\"", | ||
"\"abc\"", | ||
"1", | ||
"-1", | ||
"0123", | ||
"0xAACC", | ||
"3.1415926", | ||
"\"中文测试\"", | ||
"\"日本語テスト\"", | ||
"\"한국어 시험\"", | ||
"\"😊😂🤢\"", | ||
"9223372036854775807", | ||
"-9223372036854775808", | ||
"999999999999999999999999", | ||
"-999999999999999999999999", | ||
} | ||
|
||
var conn = mysqlconn(t) | ||
defer conn.Close() | ||
|
||
t.Run("ASCII", func(t *testing.T) { | ||
for i := 0; i < len(elems); i++ { | ||
query := fmt.Sprintf("SELECT ASCII(%s)", elems[i]) | ||
compareRemoteQuery(t, conn, query) | ||
} | ||
}) | ||
} | ||
|
||
func TestBuiltinBin(t *testing.T) { | ||
var elems = []string{ | ||
"NULL", | ||
"\"\"", | ||
"\"a\"", | ||
"\"101\"", | ||
"\"-101\"", | ||
"1", | ||
"-1", | ||
"20", | ||
"-100", | ||
"\"10abc\"", | ||
"\"10a1b2c3\"", | ||
"\"中文测试\"", | ||
"\"日本語テスト\"", | ||
"\"한국어 시험\"", | ||
"\"😊😂🤢\"", | ||
"3.1415926", | ||
"9223372036854775807", | ||
"-9223372036854775808", | ||
} | ||
|
||
var conn = mysqlconn(t) | ||
defer conn.Close() | ||
|
||
t.Run("BIN", func(t *testing.T) { | ||
for i := 0; i < len(elems); i++ { | ||
query := fmt.Sprintf("SELECT BIN(%s)", elems[i]) | ||
compareRemoteQuery(t, conn, query) | ||
} | ||
}) | ||
} | ||
|
||
func TestBuiltinBitLength(t *testing.T) { | ||
var elems = []string{ | ||
"NULL", | ||
"\"\"", | ||
"\"a\"", | ||
"\"abc\"", | ||
"1", | ||
"-1", | ||
"20", | ||
"-100", | ||
"\"中文测试\"", | ||
"\"日本語テスト\"", | ||
"\"한국어 시험\"", | ||
"\"😊😂🤢\"", | ||
"3.1415926", | ||
"9223372036854775807", | ||
"-9223372036854775808", | ||
"999999999999999999999999", | ||
"-999999999999999999999999", | ||
} | ||
|
||
var conn = mysqlconn(t) | ||
defer conn.Close() | ||
|
||
t.Run("BIT_LENGTH", func(t *testing.T) { | ||
for i := 0; i < len(elems); i++ { | ||
query := fmt.Sprintf("SELECT BIT_LENGTH(%s)", elems[i]) | ||
compareRemoteQuery(t, conn, query) | ||
} | ||
}) | ||
} |
This file contains 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,111 @@ | ||
/* | ||
Copyright 2022 The Vitess Authors. | ||
|
||
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 evalengine | ||
|
||
import ( | ||
"strconv" | ||
"vitess.io/vitess/go/mysql/collations" | ||
"vitess.io/vitess/go/sqltypes" | ||
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" | ||
"vitess.io/vitess/go/vt/vterrors" | ||
) | ||
|
||
type ( | ||
builtinAscii struct{} | ||
builtinBin struct{} | ||
builtinBitLength struct{} | ||
) | ||
|
||
func (builtinAscii) call(_ *ExpressionEnv, args []EvalResult, result *EvalResult) { | ||
toascii := &args[0] | ||
if toascii.isNull() { | ||
result.setNull() | ||
return | ||
} | ||
|
||
toascii.makeBinary() | ||
bs := toascii.bytes() | ||
if len(bs) > 0 { | ||
result.setInt64(int64(bs[0])) | ||
} else { | ||
result.setInt64(0) | ||
} | ||
} | ||
|
||
func (builtinAscii) typeof(env *ExpressionEnv, args []Expr) (sqltypes.Type, flag) { | ||
if len(args) != 1 { | ||
throwArgError("ASCII") | ||
} | ||
_, f := args[0].typeof(env) | ||
return sqltypes.Int64, f | ||
} | ||
|
||
func (builtinBin) call(env *ExpressionEnv, args []EvalResult, result *EvalResult) { | ||
tobin := &args[0] | ||
if tobin.isNull() { | ||
result.setNull() | ||
return | ||
} | ||
|
||
var raw []byte | ||
switch t := tobin.typeof(); { | ||
case sqltypes.IsNumber(t): | ||
tobin.makeUnsignedIntegral() | ||
raw = strconv.AppendUint(raw, tobin.uint64(), 2) | ||
case sqltypes.IsText(t): | ||
if len(tobin.bytes()) == 0 { | ||
return | ||
} | ||
tobin.makeFloat() | ||
float, _ := tobin.coerceToFloat() | ||
raw = strconv.AppendUint(raw, uint64(float), 2) | ||
default: | ||
throwEvalError(vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "Unsupported BIN argument: %s", t.String())) | ||
} | ||
|
||
result.setRaw(sqltypes.VarChar, raw, collations.TypedCollation{ | ||
Collation: env.DefaultCollation, | ||
Coercibility: collations.CoerceCoercible, | ||
Repertoire: collations.RepertoireASCII, | ||
}) | ||
} | ||
|
||
func (builtinBin) typeof(env *ExpressionEnv, args []Expr) (sqltypes.Type, flag) { | ||
if len(args) != 1 { | ||
throwArgError("BIN") | ||
} | ||
_, f := args[0].typeof(env) | ||
return sqltypes.VarChar, f | ||
} | ||
|
||
func (builtinBitLength) call(_ *ExpressionEnv, args []EvalResult, result *EvalResult) { | ||
arg1 := &args[0] | ||
if arg1.isNull() { | ||
result.setNull() | ||
return | ||
} | ||
|
||
result.setInt64(int64(8 * len(arg1.toRawBytes()))) | ||
} | ||
|
||
func (builtinBitLength) typeof(env *ExpressionEnv, args []Expr) (sqltypes.Type, flag) { | ||
if len(args) != 1 { | ||
throwArgError("BIT_LENGTH") | ||
} | ||
_, f := args[0].typeof(env) | ||
return sqltypes.Int64, f | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job with these integration tests. I think the thing you're missing are strings with Unicode characters: it's important to test the behavior of these functions with Unicode strings. What happens if you call
ASCII()
on an emoji? What's theBIT_COUNT
of a string that contains emoji or uses a multi-byte encoding?