Skip to content

Commit 420e683

Browse files
committed
fix namespace function error
1 parent 5598b97 commit 420e683

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

node/engine/functions.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,25 @@ var (
279279
},
280280
"array_length": &ScalarFunctionDefinition{
281281
ValidateArgsFunc: func(args []*types.DataType) (*types.DataType, error) {
282-
if len(args) != 1 {
283-
return nil, wrapErrArgumentNumber(1, len(args))
282+
if len(args) < 1 || len(args) > 2 {
283+
return nil, fmt.Errorf("invalid number of arguments: expected 1 or 2, got %d", len(args))
284284
}
285285

286286
if !args[0].IsArray {
287287
return nil, fmt.Errorf("%w: expected argument to be an array, got %s", ErrType, args[0].String())
288288
}
289289

290+
if len(args) == 2 && !args[1].Equals(types.IntType) {
291+
return nil, wrapErrArgumentType(types.IntType, args[1])
292+
}
293+
290294
return types.IntType, nil
291295
},
292296
PGFormatFunc: func(inputs []string) (string, error) {
293-
return fmt.Sprintf("array_length(%s, 1)", inputs[0]), nil
297+
if len(inputs) == 1 {
298+
return fmt.Sprintf("array_length(%s, 1)", inputs[0]), nil
299+
}
300+
return fmt.Sprintf("array_length(%s, %s)", inputs[0], inputs[1]), nil
294301
},
295302
},
296303
"array_remove": &ScalarFunctionDefinition{

node/engine/interpreter/interpreter_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,17 @@ func Test_Actions(t *testing.T) {
17611761
action: "call_get_null",
17621762
},
17631763
// TODO: test that actions returning nulls to other actions does not error
1764+
{
1765+
// this is a regression test
1766+
name: "special functions called in new namespaces works as expected",
1767+
stmt: []string{
1768+
`CREATE NAMESPACE test;`,
1769+
`{test} create action get_uuid() public view returns (uuid) {return uuid_generate_kwil('a');}`,
1770+
},
1771+
namespace: "test",
1772+
action: "get_uuid",
1773+
results: [][]any{{mustUUID("819ab751-e64c-5259-bbae-4d36f25bdd84")}},
1774+
},
17641775
}
17651776

17661777
db, err := newTestDB()

node/engine/interpreter/planner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ func (i *interpreterPlanner) VisitCreateNamespaceStatement(p0 *parse.CreateNames
19561956
}
19571957

19581958
exec.interpreter.namespaces[p0.Namespace] = &namespace{
1959-
availableFunctions: make(map[string]*executable),
1959+
availableFunctions: copyBuiltinExecutables(),
19601960
tables: make(map[string]*engine.Table),
19611961
onDeploy: func(*executionContext) error { return nil },
19621962
onUndeploy: func(*executionContext) error { return nil },

0 commit comments

Comments
 (0)