File: registry_test.go

package info (click to toggle)
golang-github-apache-arrow-go 18.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,200 kB
  • sloc: asm: 477,547; ansic: 5,369; cpp: 759; sh: 585; makefile: 319; python: 190; sed: 5
file content (182 lines) | stat: -rw-r--r-- 6,115 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// 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.

//go:build go1.18

package compute_test

import (
	"context"
	"errors"
	"testing"

	"github.com/apache/arrow-go/v18/arrow"
	"github.com/apache/arrow-go/v18/arrow/compute"
	"github.com/apache/arrow-go/v18/arrow/compute/exec"
	"github.com/stretchr/testify/assert"
	"golang.org/x/exp/slices"
)

var registry compute.FunctionRegistry

func init() {
	// make tests fail if there's a problem initializing the global
	// function registry
	registry = compute.GetFunctionRegistry()
}

type mockFn struct {
	name string
}

func (m *mockFn) Name() string           { return m.name }
func (*mockFn) Kind() compute.FuncKind   { return compute.FuncScalar }
func (*mockFn) Arity() compute.Arity     { return compute.Unary() }
func (*mockFn) Doc() compute.FunctionDoc { return compute.EmptyFuncDoc }
func (*mockFn) NumKernels() int          { return 0 }
func (*mockFn) Execute(context.Context, compute.FunctionOptions, ...compute.Datum) (compute.Datum, error) {
	return nil, errors.New("not implemented")
}
func (*mockFn) DefaultOptions() compute.FunctionOptions              { return nil }
func (*mockFn) Validate() error                                      { return nil }
func (*mockFn) DispatchExact(...arrow.DataType) (exec.Kernel, error) { return nil, nil }
func (*mockFn) DispatchBest(...arrow.DataType) (exec.Kernel, error)  { return nil, nil }

func TestRegistryBasics(t *testing.T) {
	tests := []struct {
		name          string
		factory       func() compute.FunctionRegistry
		nfuncs        int
		expectedNames []string
	}{
		{"default", compute.NewRegistry, 0, []string{}},
		{"nested", func() compute.FunctionRegistry {
			return compute.NewChildRegistry(registry)
		}, registry.NumFunctions(), registry.GetFunctionNames()},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			registry := tt.factory()
			assert.Equal(t, tt.nfuncs, registry.NumFunctions())

			fn := &mockFn{name: "f1"}
			assert.True(t, registry.AddFunction(fn, false))
			assert.Equal(t, tt.nfuncs+1, registry.NumFunctions())

			f1, ok := registry.GetFunction("f1")
			assert.True(t, ok)
			assert.Same(t, fn, f1)

			// nonexistent
			_, ok = registry.GetFunction("f2")
			assert.False(t, ok)

			// name collision
			f2 := &mockFn{name: "f1"}
			assert.False(t, registry.AddFunction(f2, false))

			// allow overwriting
			assert.True(t, registry.AddFunction(f2, true))
			f1, ok = registry.GetFunction("f1")
			assert.True(t, ok)
			assert.Same(t, f2, f1)

			expected := append(tt.expectedNames, "f1")
			slices.Sort(expected)
			assert.Equal(t, expected, registry.GetFunctionNames())

			// aliases
			assert.False(t, registry.AddAlias("f33", "f3")) // doesn't exist
			assert.True(t, registry.AddAlias("f11", "f1"))
			f1, ok = registry.GetFunction("f11")
			assert.True(t, ok)
			assert.Same(t, f2, f1)
		})
	}
}

func TestRegistry(t *testing.T) {
	defaultRegistry := registry
	t.Run("RegisterTempFunctions", func(t *testing.T) {
		const rounds = 3
		for i := 0; i < rounds; i++ {
			registry := compute.NewChildRegistry(registry)
			for _, v := range []string{"f1", "f2"} {
				fn := &mockFn{name: v}
				assert.True(t, registry.CanAddFunction(fn, false))
				assert.True(t, registry.AddFunction(fn, false))
				assert.False(t, registry.CanAddFunction(fn, false))
				assert.False(t, registry.AddFunction(fn, false))
				assert.True(t, defaultRegistry.CanAddFunction(fn, false))
			}
		}
	})

	t.Run("RegisterTempAliases", func(t *testing.T) {
		funcNames := defaultRegistry.GetFunctionNames()
		const rounds = 3
		for i := 0; i < rounds; i++ {
			registry := compute.NewChildRegistry(registry)
			for _, funcName := range funcNames {
				alias := "alias_of_" + funcName
				_, ok := registry.GetFunction(alias)
				assert.False(t, ok)
				assert.True(t, registry.CanAddAlias(alias, funcName))
				assert.True(t, registry.AddAlias(alias, funcName))
				_, ok = registry.GetFunction(alias)
				assert.True(t, ok)
				_, ok = defaultRegistry.GetFunction(funcName)
				assert.True(t, ok)
				_, ok = defaultRegistry.GetFunction(alias)
				assert.False(t, ok)
			}
		}
	})
}

func TestRegistryRegisterNestedFunction(t *testing.T) {
	defaultRegistry := registry
	func1 := &mockFn{name: "f1"}
	func2 := &mockFn{name: "f2"}

	const rounds = 3
	for i := 0; i < rounds; i++ {
		registry1 := compute.NewChildRegistry(defaultRegistry)

		assert.True(t, registry1.CanAddFunction(func1, false))
		assert.True(t, registry1.AddFunction(func1, false))
		for j := 0; j < rounds; j++ {
			registry2 := compute.NewChildRegistry(registry1)
			assert.False(t, registry2.CanAddFunction(func1, false))
			assert.False(t, registry2.AddFunction(func1, false))

			assert.True(t, registry2.CanAddFunction(func2, false))
			assert.True(t, registry2.AddFunction(func2, false))
			assert.False(t, registry2.CanAddFunction(func2, false))
			assert.False(t, registry2.AddFunction(func2, false))
			assert.True(t, defaultRegistry.CanAddFunction(func2, false))

			assert.False(t, registry2.CanAddAlias("f1", "f2"))
			assert.False(t, registry2.AddAlias("f1", "f2"))
			assert.False(t, registry2.AddAlias("f1", "f1"))
		}
		assert.False(t, registry1.CanAddFunction(func1, false))
		assert.False(t, registry1.AddFunction(func1, false))
		assert.True(t, registry1.CanAddAlias("f2", "f1"))
		assert.True(t, defaultRegistry.CanAddFunction(func1, false))
	}
}