Skip to content

Commit

Permalink
feat(func): add reverse function
Browse files Browse the repository at this point in the history
Signed-off-by: Dream95 <[email protected]>
  • Loading branch information
Dream95 committed Nov 5, 2024
1 parent 31e0b4c commit 9f213d8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
44 changes: 41 additions & 3 deletions etc/functions/internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -1149,10 +1149,10 @@
"category": "function",
"icon": "iconPath",
"label": {
"en_US": "Natural Logarithm",
"zh_CN": "自然对数"
}
"en_US": "Natural Logarithm",
"zh_CN": "自然对数"
}
}
}, {
"name": "log",
"example": "log(col1)",
Expand Down Expand Up @@ -2137,6 +2137,44 @@
"zh_CN": "正则匹配子串"
}
}
},{
"name": "reverse",
"example": "reverse(col1)",
"hint": {
"en_US": "Returns the reversed string.",
"zh_CN": "返回反转的字符串。"
},
"args": [
{
"name": "string",
"optional": false,
"control": "field",
"type": "string",
"hint": {
"en_US": "A String",
"zh_CN": "String 值"
},
"label": {
"en_US": "String",
"zh_CN": "String 值"
}
}
],
"return": {
"type": "string",
"hint": {
"en_US": "Reversed String",
"zh_CN": "反转的字符串"
}
},
"node": {
"category": "function",
"icon": "iconPath",
"label": {
"en_US": "Reverse",
"zh_CN": "反转字符串"
}
}
}, {
"name": "rpad",
"example": "rpad(col1, 2)",
Expand Down
13 changes: 13 additions & 0 deletions internal/binder/function/funcs_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ func registerStrFunc() {
val: ValidateTwoStrArg,
check: returnNilIfHasAnyNil,
}
builtins["reverse"] = builtinFunc{
fType: ast.FuncTypeScalar,
exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
arg0 := cast.ToStringAlways(args[0])
runes := []rune(arg0)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes), true
},
val: ValidateOneStrArg,
check: returnNilIfHasAnyNil,
}
builtins["rpad"] = builtinFunc{
fType: ast.FuncTypeScalar,
exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
Expand Down
12 changes: 12 additions & 0 deletions internal/topo/operator/str_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,18 @@ func TestStrFunc_Apply1(t *testing.T) {
},
result: []map[string]interface{}{{}},
},
{
sql: "SELECT reverse(a) AS a FROM test",
data: &xsql.Tuple{
Emitter: "test",
Message: xsql.Message{
"a": "abc",
},
},
result: []map[string]interface{}{{
"a": "cba",
}},
},
{
sql: "SELECT rpad(a, 3) AS a FROM test",
data: &xsql.Tuple{
Expand Down

0 comments on commit 9f213d8

Please sign in to comment.