forked from arana-db/arana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support MySQL REVERSE function (arana-db#549)
- Loading branch information
1 parent
b5e0a21
commit 21cf52f
Showing
2 changed files
with
135 additions
and
0 deletions.
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
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 | ||
} |
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,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)) | ||
}) | ||
} | ||
} |