From cacdf6dd49e6eac9d71535693746bb49cc8b0695 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 12 Dec 2017 00:54:18 +0000 Subject: [PATCH] Add SQL function for creating an array --- sql/funcs.go | 1 + util/fncs/array.go | 27 +++++++++++++++++++++++++++ util/fncs/array_test.go | 39 +++++++++++++++++++++++++++++++++++++++ util/fncs/fnc.go | 2 ++ 4 files changed, 69 insertions(+) create mode 100644 util/fncs/array.go create mode 100644 util/fncs/array_test.go diff --git a/sql/funcs.go b/sql/funcs.go index de638cd6..cdec2277 100644 --- a/sql/funcs.go +++ b/sql/funcs.go @@ -98,6 +98,7 @@ var aggrs = map[string]bool{ var funcs = map[string]map[int]interface{}{ + "array": {-1: nil}, "batch": {2: nil}, "binary": {1: nil}, "difference": {-1: nil}, diff --git a/util/fncs/array.go b/util/fncs/array.go new file mode 100644 index 00000000..85bea801 --- /dev/null +++ b/util/fncs/array.go @@ -0,0 +1,27 @@ +// Copyright © 2016 Abcum Ltd +// +// Licensed 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 fncs + +import ( + "context" +) + +func array(ctx context.Context, args ...interface{}) ([]interface{}, error) { + out := make([]interface{}, len(args)) + for i, arg := range args { + out[i] = arg + } + return out, nil +} diff --git a/util/fncs/array_test.go b/util/fncs/array_test.go new file mode 100644 index 00000000..24636711 --- /dev/null +++ b/util/fncs/array_test.go @@ -0,0 +1,39 @@ +// Copyright © 2016 Abcum Ltd +// +// Licensed 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 fncs + +import ( + "time" + + "context" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestArray(t *testing.T) { + + var res interface{} + + timer := time.Now() + + Convey("array() works properly", t, func() { + res, _ = Run(context.Background(), "array", 1, 2, 3) + So(res, ShouldResemble, []interface{}{1, 2, 3}) + res, _ = Run(context.Background(), "array", nil, true, "test", timer) + So(res, ShouldResemble, []interface{}{nil, true, "test", timer}) + }) + +} diff --git a/util/fncs/fnc.go b/util/fncs/fnc.go index fcd15ae2..1eb37864 100644 --- a/util/fncs/fnc.go +++ b/util/fncs/fnc.go @@ -20,6 +20,8 @@ func Run(ctx context.Context, name string, args ...interface{}) (interface{}, er switch name { + case "array": + return array(ctx, args...) case "batch": return batch(ctx, args...) case "difference":