aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/schema/flat_schema_utils_test.go
blob: 779d3705f38ca5b8f11e9e1041e19770a52051d4 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package schema

import (
	"reflect"
	"testing"

	"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)

func TestSplitFlatSchemaToKeyValue(t *testing.T) {
	// Create a test flat schema
	flatSchema := &schema_pb.RecordType{
		Fields: []*schema_pb.Field{
			{
				Name:       "user_id",
				FieldIndex: 0,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}},
			},
			{
				Name:       "session_id",
				FieldIndex: 1,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}},
			},
			{
				Name:       "event_type",
				FieldIndex: 2,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}},
			},
			{
				Name:       "timestamp",
				FieldIndex: 3,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}},
			},
		},
	}

	keyColumns := []string{"user_id", "session_id"}

	keySchema, valueSchema, err := SplitFlatSchemaToKeyValue(flatSchema, keyColumns)
	if err != nil {
		t.Fatalf("SplitFlatSchemaToKeyValue failed: %v", err)
	}

	// Verify key schema
	if keySchema == nil {
		t.Fatal("Expected key schema, got nil")
	}
	if len(keySchema.Fields) != 2 {
		t.Errorf("Expected 2 key fields, got %d", len(keySchema.Fields))
	}
	if keySchema.Fields[0].Name != "user_id" || keySchema.Fields[1].Name != "session_id" {
		t.Errorf("Key field names incorrect: %v", []string{keySchema.Fields[0].Name, keySchema.Fields[1].Name})
	}

	// Verify value schema
	if valueSchema == nil {
		t.Fatal("Expected value schema, got nil")
	}
	if len(valueSchema.Fields) != 2 {
		t.Errorf("Expected 2 value fields, got %d", len(valueSchema.Fields))
	}
	if valueSchema.Fields[0].Name != "event_type" || valueSchema.Fields[1].Name != "timestamp" {
		t.Errorf("Value field names incorrect: %v", []string{valueSchema.Fields[0].Name, valueSchema.Fields[1].Name})
	}

	// Verify field indices are reindexed
	for i, field := range keySchema.Fields {
		if field.FieldIndex != int32(i) {
			t.Errorf("Key field %s has incorrect index %d, expected %d", field.Name, field.FieldIndex, i)
		}
	}
	for i, field := range valueSchema.Fields {
		if field.FieldIndex != int32(i) {
			t.Errorf("Value field %s has incorrect index %d, expected %d", field.Name, field.FieldIndex, i)
		}
	}
}

func TestSplitFlatSchemaToKeyValueMissingColumns(t *testing.T) {
	flatSchema := &schema_pb.RecordType{
		Fields: []*schema_pb.Field{
			{Name: "field1", Type: &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}}},
		},
	}

	keyColumns := []string{"field1", "missing_field"}

	_, _, err := SplitFlatSchemaToKeyValue(flatSchema, keyColumns)
	if err == nil {
		t.Error("Expected error for missing key column, got nil")
	}
	if !contains(err.Error(), "missing_field") {
		t.Errorf("Error should mention missing_field: %v", err)
	}
}

func TestCombineFlatSchemaFromKeyValue(t *testing.T) {
	keySchema := &schema_pb.RecordType{
		Fields: []*schema_pb.Field{
			{
				Name:       "user_id",
				FieldIndex: 0,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}},
			},
			{
				Name:       "session_id",
				FieldIndex: 1,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}},
			},
		},
	}

	valueSchema := &schema_pb.RecordType{
		Fields: []*schema_pb.Field{
			{
				Name:       "event_type",
				FieldIndex: 0,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}},
			},
			{
				Name:       "timestamp",
				FieldIndex: 1,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}},
			},
		},
	}

	flatSchema, keyColumns := CombineFlatSchemaFromKeyValue(keySchema, valueSchema)

	// Verify combined schema
	if flatSchema == nil {
		t.Fatal("Expected flat schema, got nil")
	}
	if len(flatSchema.Fields) != 4 {
		t.Errorf("Expected 4 fields, got %d", len(flatSchema.Fields))
	}

	// Verify key columns
	expectedKeyColumns := []string{"user_id", "session_id"}
	if !reflect.DeepEqual(keyColumns, expectedKeyColumns) {
		t.Errorf("Expected key columns %v, got %v", expectedKeyColumns, keyColumns)
	}

	// Verify field order (key fields first)
	expectedNames := []string{"user_id", "session_id", "event_type", "timestamp"}
	actualNames := make([]string, len(flatSchema.Fields))
	for i, field := range flatSchema.Fields {
		actualNames[i] = field.Name
	}
	if !reflect.DeepEqual(actualNames, expectedNames) {
		t.Errorf("Expected field names %v, got %v", expectedNames, actualNames)
	}

	// Verify field indices are sequential
	for i, field := range flatSchema.Fields {
		if field.FieldIndex != int32(i) {
			t.Errorf("Field %s has incorrect index %d, expected %d", field.Name, field.FieldIndex, i)
		}
	}
}

func TestExtractKeyColumnsFromCombinedSchema(t *testing.T) {
	// Create a combined schema with key_ prefixes (as created by CreateCombinedRecordType)
	combinedSchema := &schema_pb.RecordType{
		Fields: []*schema_pb.Field{
			{
				Name:       "key_user_id",
				FieldIndex: 0,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}},
			},
			{
				Name:       "key_session_id",
				FieldIndex: 1,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}},
			},
			{
				Name:       "event_type",
				FieldIndex: 2,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}},
			},
			{
				Name:       "timestamp",
				FieldIndex: 3,
				Type:       &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}},
			},
		},
	}

	flatSchema, keyColumns := ExtractKeyColumnsFromCombinedSchema(combinedSchema)

	// Verify flat schema
	if flatSchema == nil {
		t.Fatal("Expected flat schema, got nil")
	}
	if len(flatSchema.Fields) != 4 {
		t.Errorf("Expected 4 fields, got %d", len(flatSchema.Fields))
	}

	// Verify key columns (should be sorted)
	expectedKeyColumns := []string{"session_id", "user_id"}
	if !reflect.DeepEqual(keyColumns, expectedKeyColumns) {
		t.Errorf("Expected key columns %v, got %v", expectedKeyColumns, keyColumns)
	}

	// Verify field names (key_ prefixes removed)
	expectedNames := []string{"user_id", "session_id", "event_type", "timestamp"}
	actualNames := make([]string, len(flatSchema.Fields))
	for i, field := range flatSchema.Fields {
		actualNames[i] = field.Name
	}
	if !reflect.DeepEqual(actualNames, expectedNames) {
		t.Errorf("Expected field names %v, got %v", expectedNames, actualNames)
	}
}

func TestValidateKeyColumns(t *testing.T) {
	schema := &schema_pb.RecordType{
		Fields: []*schema_pb.Field{
			{Name: "field1", Type: &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}}},
			{Name: "field2", Type: &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}}},
		},
	}

	// Valid key columns
	err := ValidateKeyColumns(schema, []string{"field1"})
	if err != nil {
		t.Errorf("Expected no error for valid key columns, got: %v", err)
	}

	// Invalid key columns
	err = ValidateKeyColumns(schema, []string{"field1", "missing_field"})
	if err == nil {
		t.Error("Expected error for invalid key columns, got nil")
	}

	// Nil schema should not error
	err = ValidateKeyColumns(nil, []string{"any_field"})
	if err != nil {
		t.Errorf("Expected no error for nil schema, got: %v", err)
	}

	// Empty key columns should not error
	err = ValidateKeyColumns(schema, []string{})
	if err != nil {
		t.Errorf("Expected no error for empty key columns, got: %v", err)
	}
}

// Helper function to check if string contains substring
func contains(str, substr string) bool {
	return len(str) >= len(substr) &&
		(len(substr) == 0 || str[len(str)-len(substr):] == substr ||
			str[:len(substr)] == substr ||
			len(str) > len(substr) && (str[len(str)-len(substr)-1:len(str)-len(substr)] == " " || str[len(str)-len(substr)-1] == ' ') && str[len(str)-len(substr):] == substr ||
			findInString(str, substr))
}

func findInString(str, substr string) bool {
	for i := 0; i <= len(str)-len(substr); i++ {
		if str[i:i+len(substr)] == substr {
			return true
		}
	}
	return false
}