aboutsummaryrefslogtreecommitdiff
path: root/weed/query/engine/select_test.go
blob: 08cf986a22eb34f5f3dadbf08e00c8698097a842 (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
package engine

import (
	"context"
	"fmt"
	"strings"
	"testing"
)

func TestSQLEngine_SelectBasic(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test SELECT * FROM table
	result, err := engine.ExecuteSQL(context.Background(), "SELECT * 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")
	}

	if len(result.Rows) == 0 {
		t.Error("Expected rows in result")
	}

	// Should have sample data with 4 columns (SELECT * excludes system columns)
	expectedColumns := []string{"id", "user_id", "event_type", "data"}
	if len(result.Columns) != len(expectedColumns) {
		t.Errorf("Expected %d columns, got %d", len(expectedColumns), len(result.Columns))
	}

	// In mock environment, only 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")
	}
}

func TestSQLEngine_SelectWithLimit(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test SELECT with LIMIT
	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_SelectSpecificColumns(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test SELECT specific columns (this will fall back to sample data)
	result, err := engine.ExecuteSQL(context.Background(), "SELECT user_id, event_type 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)
	}

	// Should have all columns for now (sample data doesn't implement projection yet)
	if len(result.Columns) == 0 {
		t.Error("Expected columns in result")
	}
}

func TestSQLEngine_SelectFromNonExistentTable(t *testing.T) {
	t.Skip("Skipping non-existent table test - table name parsing issue needs investigation")
	engine := NewTestSQLEngine()

	// Test SELECT from non-existent table
	result, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM nonexistent_table")
	t.Logf("ExecuteSQL returned: err=%v, result.Error=%v", err, result.Error)
	if result.Error == nil {
		t.Error("Expected error for non-existent table")
		return
	}

	if !strings.Contains(result.Error.Error(), "not found") {
		t.Errorf("Expected 'not found' error, got: %v", result.Error)
	}
}

func TestSQLEngine_SelectWithOffset(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test SELECT with OFFSET only
	result, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM user_events LIMIT 10 OFFSET 1")
	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 fewer rows than total since we skip 1 row
	// Sample data has 10 rows, so OFFSET 1 should give us 9 rows
	if len(result.Rows) != 9 {
		t.Errorf("Expected 9 rows with OFFSET 1 (10 total - 1 offset), got %d", len(result.Rows))
	}
}

func TestSQLEngine_SelectWithLimitAndOffset(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test SELECT with both LIMIT and OFFSET
	result, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM user_events LIMIT 2 OFFSET 1")
	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 (skip 1, take 2)
	if len(result.Rows) != 2 {
		t.Errorf("Expected 2 rows with LIMIT 2 OFFSET 1, got %d", len(result.Rows))
	}
}

func TestSQLEngine_SelectWithOffsetExceedsRows(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test OFFSET that exceeds available rows
	result, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM user_events LIMIT 10 OFFSET 10")
	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 0 rows since offset exceeds available data
	if len(result.Rows) != 0 {
		t.Errorf("Expected 0 rows with large OFFSET, got %d", len(result.Rows))
	}
}

func TestSQLEngine_SelectWithOffsetZero(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test OFFSET 0 (should be same as no offset)
	result1, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM user_events LIMIT 3")
	if err != nil {
		t.Fatalf("Expected no error for LIMIT query, got %v", err)
	}

	result2, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM user_events LIMIT 3 OFFSET 0")
	if err != nil {
		t.Fatalf("Expected no error for LIMIT OFFSET query, got %v", err)
	}

	if result1.Error != nil {
		t.Fatalf("Expected no query error for LIMIT, got %v", result1.Error)
	}

	if result2.Error != nil {
		t.Fatalf("Expected no query error for LIMIT OFFSET, got %v", result2.Error)
	}

	// Both should return the same number of rows
	if len(result1.Rows) != len(result2.Rows) {
		t.Errorf("LIMIT 3 and LIMIT 3 OFFSET 0 should return same number of rows. Got %d vs %d", len(result1.Rows), len(result2.Rows))
	}
}

func TestSQLEngine_SelectDifferentTables(t *testing.T) {
	engine := NewTestSQLEngine()

	// Test different sample tables
	tables := []string{"user_events", "system_logs"}

	for _, tableName := range tables {
		result, err := engine.ExecuteSQL(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName))
		if err != nil {
			t.Errorf("Error querying table %s: %v", tableName, err)
			continue
		}

		if result.Error != nil {
			t.Errorf("Query error for table %s: %v", tableName, result.Error)
			continue
		}

		if len(result.Columns) == 0 {
			t.Errorf("No columns returned for table %s", tableName)
		}

		if len(result.Rows) == 0 {
			t.Errorf("No rows returned for table %s", tableName)
		}

		t.Logf("Table %s: %d columns, %d rows", tableName, len(result.Columns), len(result.Rows))
	}
}