Skip to content

Commit

Permalink
Add:Support MySQL CHAR_LENGTH function.arana-db#501 (arana-db#544)
Browse files Browse the repository at this point in the history
* Add:Support MySQL CHAR_LENGTH function.arana-db#501

* Add:Support MySQL CHAR_LENGTH function.arana-db#501
  • Loading branch information
gongna-au authored Nov 25, 2022
1 parent 7c00fb9 commit f2dfe76
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/runtime/function/char_length.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 (
"context"
"fmt"
"unicode/utf8"
)

import (
"github.com/pkg/errors"
)

import (
"github.com/arana-db/arana/pkg/proto"
)

// FuncCharLength is https://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_character-length
const FuncCharLength = "CHAR_LENGTH"

var _ proto.Func = (*charlengthFunc)(nil)

func init() {
proto.RegisterFunc(FuncCharLength, charlengthFunc{})
}

type charlengthFunc struct{}

func (a charlengthFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
val, err := inputs[0].Value(ctx)
if err != nil {
return nil, errors.WithStack(err)
}

result := utf8.RuneCountInString(fmt.Sprint(val))
return int64(result), nil
}

func (a charlengthFunc) NumInput() int {
return 1
}
54 changes: 54 additions & 0 deletions pkg/runtime/function/char_length_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 (
"context"
"fmt"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/arana-db/arana/pkg/proto"
)

func TestCharLength(t *testing.T) {
fn := proto.MustGetFunc(FuncCharLength)
assert.Equal(t, 1, fn.NumInput())
type tt struct {
inFirst proto.Value
want int64
}
for _, v := range []tt{
{"Hello世界", 7},
{"Hello", 5},
{"世界", 2},
{"你好世界!", 5},
} {
t.Run(fmt.Sprint(v.inFirst), func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(v.inFirst))
assert.NoError(t, err)
assert.Equal(t, v.want, out)
})
}

}

0 comments on commit f2dfe76

Please sign in to comment.