Skip to content

Commit

Permalink
Support MySQL REVERSE function (arana-db#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
raspberry-hu authored Nov 30, 2022
1 parent b5e0a21 commit 21cf52f
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkg/runtime/function/reverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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"
)

import (
gxbig "github.com/dubbogo/gost/math/big"
)

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

const FuncReverse = "REVERSE"

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

func init() {
proto.RegisterFunc(FuncReverse, reverseFunc{})
}

type reverseFunc struct{}

func reverseString(s string) string {
runes := []rune(s)
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}

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

isNull := func(val any) bool {
_, ok := val.(ast.Null)
return ok
}

if isNull(str) {
return ast.Null{}, nil
}

switch v := str.(type) {
case *gxbig.Decimal:
return reverseString(v.Value), nil
default:
return reverseString(fmt.Sprint(str)), nil
}
}

func (a reverseFunc) NumInput() int {
return 1
}
58 changes: 58 additions & 0 deletions pkg/runtime/function/reverse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 (
gxbig "github.com/dubbogo/gost/math/big"

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

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

func TestReverse(t *testing.T) {
fn := proto.MustGetFunc(FuncReverse)
assert.Equal(t, 1, fn.NumInput())

type tt struct {
in proto.Value
out string
}

for _, it := range []tt{
{1111, "1111"},
{&gxbig.Decimal{Value: "12.23"}, "32.21"},
{"arana", "anara"},
{"db-mesh", "hsem-bd"},
{20.22, "22.02"},
} {
t.Run(it.out, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(it.in))
assert.NoError(t, err)
assert.Equal(t, it.out, fmt.Sprint(out))
})
}
}

0 comments on commit 21cf52f

Please sign in to comment.