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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
package engine
import (
"context"
"fmt"
"strings"
"testing"
)
func TestSQLEngine_HybridSelectBasic(t *testing.T) {
engine := NewTestSQLEngine()
// Test SELECT with _source column to show both live and archived data
result, err := engine.ExecuteSQL(context.Background(), "SELECT *, _source FROM user_events")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result.Error != nil {
t.Fatalf("Expected no query error, got %v", result.Error)
}
if len(result.Columns) == 0 {
t.Error("Expected columns in result")
}
// In mock environment, we only get live_log data from unflushed messages
// parquet_archive data would come from parquet files in a real system
if len(result.Rows) == 0 {
t.Error("Expected rows in result")
}
// Check that we have the _source column showing data source
hasSourceColumn := false
sourceColumnIndex := -1
for i, column := range result.Columns {
if column == SW_COLUMN_NAME_SOURCE {
hasSourceColumn = true
sourceColumnIndex = i
break
}
}
if !hasSourceColumn {
t.Skip("_source column not available in fallback mode - test requires real SeaweedFS cluster")
}
// Verify we have the expected data sources (in mock environment, only live_log)
if hasSourceColumn && sourceColumnIndex >= 0 {
foundLiveLog := false
for _, row := range result.Rows {
if sourceColumnIndex < len(row) {
source := row[sourceColumnIndex].ToString()
if source == "live_log" {
foundLiveLog = true
}
// In mock environment, all data comes from unflushed messages (live_log)
// In a real system, we would also see parquet_archive from parquet files
}
}
if !foundLiveLog {
t.Error("Expected to find live_log data source in results")
}
t.Logf("Found live_log data source from unflushed messages")
}
}
func TestSQLEngine_HybridSelectWithLimit(t *testing.T) {
engine := NewTestSQLEngine()
// Test SELECT with LIMIT on hybrid data
result, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM user_events LIMIT 2")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result.Error != nil {
t.Fatalf("Expected no query error, got %v", result.Error)
}
// Should have exactly 2 rows due to LIMIT
if len(result.Rows) != 2 {
t.Errorf("Expected 2 rows with LIMIT 2, got %d", len(result.Rows))
}
}
func TestSQLEngine_HybridSelectDifferentTables(t *testing.T) {
engine := NewTestSQLEngine()
// Test both user_events and system_logs tables
tables := []string{"user_events", "system_logs"}
for _, tableName := range tables {
result, err := engine.ExecuteSQL(context.Background(), fmt.Sprintf("SELECT *, _source FROM %s", tableName))
if err != nil {
t.Errorf("Error querying hybrid table %s: %v", tableName, err)
continue
}
if result.Error != nil {
t.Errorf("Query error for hybrid table %s: %v", tableName, result.Error)
continue
}
if len(result.Columns) == 0 {
t.Errorf("No columns returned for hybrid table %s", tableName)
}
if len(result.Rows) == 0 {
t.Errorf("No rows returned for hybrid table %s", tableName)
}
// Check for _source column
hasSourceColumn := false
for _, column := range result.Columns {
if column == "_source" {
hasSourceColumn = true
break
}
}
if !hasSourceColumn {
t.Logf("Table %s missing _source column - running in fallback mode", tableName)
}
t.Logf("Table %s: %d columns, %d rows with hybrid data sources", tableName, len(result.Columns), len(result.Rows))
}
}
func TestSQLEngine_HybridDataSource(t *testing.T) {
engine := NewTestSQLEngine()
// Test that we can distinguish between live and archived data
result, err := engine.ExecuteSQL(context.Background(), "SELECT user_id, event_type, _source FROM user_events")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result.Error != nil {
t.Fatalf("Expected no query error, got %v", result.Error)
}
// Find the _source column
sourceColumnIndex := -1
eventTypeColumnIndex := -1
for i, column := range result.Columns {
switch column {
case "_source":
sourceColumnIndex = i
case "event_type":
eventTypeColumnIndex = i
}
}
if sourceColumnIndex == -1 {
t.Skip("Could not find _source column - test requires real SeaweedFS cluster")
}
if eventTypeColumnIndex == -1 {
t.Fatal("Could not find event_type column")
}
// Check the data characteristics
liveEventFound := false
archivedEventFound := false
for _, row := range result.Rows {
if sourceColumnIndex < len(row) && eventTypeColumnIndex < len(row) {
source := row[sourceColumnIndex].ToString()
eventType := row[eventTypeColumnIndex].ToString()
if source == "live_log" && strings.Contains(eventType, "live_") {
liveEventFound = true
t.Logf("Found live event: %s from %s", eventType, source)
}
if source == "parquet_archive" && strings.Contains(eventType, "archived_") {
archivedEventFound = true
t.Logf("Found archived event: %s from %s", eventType, source)
}
}
}
if !liveEventFound {
t.Error("Expected to find live events with live_ prefix")
}
if !archivedEventFound {
t.Error("Expected to find archived events with archived_ prefix")
}
}
func TestSQLEngine_HybridSystemLogs(t *testing.T) {
engine := NewTestSQLEngine()
// Test system_logs with hybrid data
result, err := engine.ExecuteSQL(context.Background(), "SELECT level, message, service, _source FROM system_logs")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result.Error != nil {
t.Fatalf("Expected no query error, got %v", result.Error)
}
// Should have both live and archived system logs
if len(result.Rows) < 2 {
t.Errorf("Expected at least 2 system log entries, got %d", len(result.Rows))
}
// Find column indices
levelIndex := -1
sourceIndex := -1
for i, column := range result.Columns {
switch column {
case "level":
levelIndex = i
case "_source":
sourceIndex = i
}
}
// Verify we have both live and archived system logs
foundLive := false
foundArchived := false
for _, row := range result.Rows {
if sourceIndex >= 0 && sourceIndex < len(row) {
source := row[sourceIndex].ToString()
if source == "live_log" {
foundLive = true
if levelIndex >= 0 && levelIndex < len(row) {
level := row[levelIndex].ToString()
t.Logf("Live system log: level=%s", level)
}
}
if source == "parquet_archive" {
foundArchived = true
if levelIndex >= 0 && levelIndex < len(row) {
level := row[levelIndex].ToString()
t.Logf("Archived system log: level=%s", level)
}
}
}
}
if !foundLive {
t.Log("No live system logs found - running in fallback mode")
}
if !foundArchived {
t.Log("No archived system logs found - running in fallback mode")
}
}
func TestSQLEngine_HybridSelectWithTimeImplications(t *testing.T) {
engine := NewTestSQLEngine()
// Test that demonstrates the time-based nature of hybrid data
// Live data should be more recent than archived data
result, err := engine.ExecuteSQL(context.Background(), "SELECT event_type, _source FROM user_events")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result.Error != nil {
t.Fatalf("Expected no query error, got %v", result.Error)
}
// This test documents that hybrid scanning provides a complete view
// of both recent (live) and historical (archived) data in a single query
liveCount := 0
archivedCount := 0
sourceIndex := -1
for i, column := range result.Columns {
if column == "_source" {
sourceIndex = i
break
}
}
if sourceIndex >= 0 {
for _, row := range result.Rows {
if sourceIndex < len(row) {
source := row[sourceIndex].ToString()
switch source {
case "live_log":
liveCount++
case "parquet_archive":
archivedCount++
}
}
}
}
t.Logf("Hybrid query results: %d live messages, %d archived messages", liveCount, archivedCount)
if liveCount == 0 && archivedCount == 0 {
t.Log("No live or archived messages found - running in fallback mode")
}
}
|