Skip to content

Commit 1f1064c

Browse files
committed
address comments
1 parent 60b536c commit 1f1064c

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

bttest/cmv.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bttest
33
import (
44
"encoding/json"
55
"fmt"
6+
"log"
67
"os"
78
"regexp"
89
"strconv"
@@ -54,6 +55,11 @@ func LoadCMVConfigs(path string) ([]CMVConfig, error) {
5455
if len(c.KeyMapping) == 0 {
5556
return nil, fmt.Errorf("CMV config[%d]: key_mapping is required", i)
5657
}
58+
for j, idx := range c.KeyMapping {
59+
if idx < 0 {
60+
return nil, fmt.Errorf("CMV config[%d]: key_mapping[%d] is negative (%d)", i, j, idx)
61+
}
62+
}
5763
}
5864
return configs, nil
5965
}
@@ -195,6 +201,8 @@ func (c *cmvInstance) transformKey(sourceKey string) string {
195201
if idx < len(parts) {
196202
newParts = append(newParts, parts[idx])
197203
} else {
204+
log.Printf("CMV %q: key_mapping index %d out of bounds for source key %q (%d parts) — check your config",
205+
c.config.ViewID, idx, sourceKey, len(parts))
198206
newParts = append(newParts, "")
199207
}
200208
}

bttest/cmv_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,35 @@ func TestLoadCMVConfigs(t *testing.T) {
285285
assert.True(t, configs[0].AppendSourceKey)
286286
}
287287

288+
func TestLoadCMVConfigsNegativeIndex(t *testing.T) {
289+
tmpFile, err := os.CreateTemp("", "cmv-test-*.json")
290+
require.NoError(t, err)
291+
defer os.Remove(tmpFile.Name())
292+
293+
content := `[{"source_table":"events","view_id":"events_by_account","key_separator":"#","key_mapping":[-1,0]}]`
294+
_, err = tmpFile.WriteString(content)
295+
require.NoError(t, err)
296+
tmpFile.Close()
297+
298+
_, err = LoadCMVConfigs(tmpFile.Name())
299+
require.Error(t, err)
300+
assert.Contains(t, err.Error(), "negative")
301+
}
302+
303+
func TestCMVTransformKeyOutOfBounds(t *testing.T) {
304+
inst := &cmvInstance{
305+
config: CMVConfig{
306+
SourceTable: "events",
307+
ViewID: "events_by_account",
308+
KeySeparator: "#",
309+
KeyMapping: []int{0, 99}, // index 99 is out of bounds
310+
},
311+
}
312+
// Should not panic; out-of-bounds index produces an empty component.
313+
got := inst.transformKey("only#two#parts")
314+
assert.Equal(t, "only#", got)
315+
}
316+
288317
func TestParseCMVConfigFromSQL(t *testing.T) {
289318
// Standard Bigtable CMV SQL pattern: plain SPLIT (no CAST), _key aliased,
290319
// alias appears in ORDER BY to set AppendSourceKey = true.

0 commit comments

Comments
 (0)