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

Built-in string functions implementation: ASCII, BIN, BIT_LENGTH #9793

Closed
wants to merge 7 commits into from
Closed
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
15 changes: 9 additions & 6 deletions go/vt/vtgate/evalengine/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ import (
)

var builtinFunctions = map[string]builtin{
"coalesce": builtinCoalesce{},
"greatest": &builtinMultiComparison{name: "GREATEST", cmp: 1},
"least": &builtinMultiComparison{name: "LEAST", cmp: -1},
"collation": builtinCollation{},
"bit_count": builtinBitCount{},
"hex": builtinHex{},
"coalesce": builtinCoalesce{},
"greatest": &builtinMultiComparison{name: "GREATEST", cmp: 1},
"least": &builtinMultiComparison{name: "LEAST", cmp: -1},
"collation": builtinCollation{},
"bit_count": builtinBitCount{},
"hex": builtinHex{},
"ascii": builtinAscii{},
"bin": builtinBin{},
"bit_length": builtinBitLength{},
}

var builtinFunctionsRewrite = map[string]builtinRewrite{
Expand Down
119 changes: 119 additions & 0 deletions go/vt/vtgate/evalengine/integration/string_func_test.go
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) {
Copy link
Collaborator

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 the BIT_COUNT of a string that contains emoji or uses a multi-byte encoding?

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)
}
})
}
111 changes: 111 additions & 0 deletions go/vt/vtgate/evalengine/string.go
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
}