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
|
package engine
import (
"strconv"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
"github.com/stretchr/testify/assert"
)
// TestTimestampQueryFixes tests all the timestamp query fixes comprehensively
func TestTimestampQueryFixes(t *testing.T) {
engine := NewTestSQLEngine()
// Test timestamps from the actual failing cases
largeTimestamp1 := int64(1756947416566456262) // Original failing query
largeTimestamp2 := int64(1756947416566439304) // Second failing query
largeTimestamp3 := int64(1756913789829292386) // Current data timestamp
t.Run("Fix1_PrecisionLoss", func(t *testing.T) {
// Test that large int64 timestamps don't lose precision in comparisons
testRecord := &schema_pb.RecordValue{
Fields: map[string]*schema_pb.Value{
"_ts_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: largeTimestamp1}},
"id": {Kind: &schema_pb.Value_Int64Value{Int64Value: 12345}},
},
}
// Test equality comparison
result := engine.valuesEqual(testRecord.Fields["_ts_ns"], largeTimestamp1)
assert.True(t, result, "Large timestamp equality should work without precision loss")
// Test inequality comparison
result = engine.valuesEqual(testRecord.Fields["_ts_ns"], largeTimestamp1+1)
assert.False(t, result, "Large timestamp inequality should be detected accurately")
// Test less than comparison
result = engine.valueLessThan(testRecord.Fields["_ts_ns"], largeTimestamp1+1)
assert.True(t, result, "Large timestamp less-than should work without precision loss")
// Test greater than comparison
result = engine.valueGreaterThan(testRecord.Fields["_ts_ns"], largeTimestamp1-1)
assert.True(t, result, "Large timestamp greater-than should work without precision loss")
})
t.Run("Fix2_TimeFilterExtraction", func(t *testing.T) {
// Test that equality queries don't set stopTimeNs (which causes premature termination)
equalitySQL := "SELECT * FROM test WHERE _ts_ns = " + strconv.FormatInt(largeTimestamp2, 10)
stmt, err := ParseSQL(equalitySQL)
assert.NoError(t, err)
selectStmt := stmt.(*SelectStatement)
startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
assert.Equal(t, largeTimestamp2-1, startTimeNs, "Equality query should set startTimeNs to target-1")
assert.Equal(t, int64(0), stopTimeNs, "Equality query should NOT set stopTimeNs to avoid early termination")
})
t.Run("Fix3_RangeBoundaryFix", func(t *testing.T) {
// Test that range queries with equal boundaries don't cause premature termination
rangeSQL := "SELECT * FROM test WHERE _ts_ns >= " + strconv.FormatInt(largeTimestamp3, 10) +
" AND _ts_ns <= " + strconv.FormatInt(largeTimestamp3, 10)
stmt, err := ParseSQL(rangeSQL)
assert.NoError(t, err)
selectStmt := stmt.(*SelectStatement)
startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
// Should be treated like an equality query to avoid premature termination
assert.NotEqual(t, int64(0), startTimeNs, "Range with equal boundaries should set startTimeNs")
assert.Equal(t, int64(0), stopTimeNs, "Range with equal boundaries should NOT set stopTimeNs")
})
t.Run("Fix4_DifferentRangeBoundaries", func(t *testing.T) {
// Test that normal range queries still work correctly
rangeSQL := "SELECT * FROM test WHERE _ts_ns >= " + strconv.FormatInt(largeTimestamp1, 10) +
" AND _ts_ns <= " + strconv.FormatInt(largeTimestamp2, 10)
stmt, err := ParseSQL(rangeSQL)
assert.NoError(t, err)
selectStmt := stmt.(*SelectStatement)
startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
assert.Equal(t, largeTimestamp1, startTimeNs, "Range query should set correct startTimeNs")
assert.Equal(t, largeTimestamp2, stopTimeNs, "Range query should set correct stopTimeNs")
})
t.Run("Fix5_PredicateAccuracy", func(t *testing.T) {
// Test that predicates correctly evaluate large timestamp equality
equalitySQL := "SELECT * FROM test WHERE _ts_ns = " + strconv.FormatInt(largeTimestamp1, 10)
stmt, err := ParseSQL(equalitySQL)
assert.NoError(t, err)
selectStmt := stmt.(*SelectStatement)
predicate, err := engine.buildPredicate(selectStmt.Where.Expr)
assert.NoError(t, err)
// Test with matching record
matchingRecord := &schema_pb.RecordValue{
Fields: map[string]*schema_pb.Value{
"_ts_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: largeTimestamp1}},
"id": {Kind: &schema_pb.Value_Int64Value{Int64Value: 897795}},
},
}
result := predicate(matchingRecord)
assert.True(t, result, "Predicate should match record with exact timestamp")
// Test with non-matching record
nonMatchingRecord := &schema_pb.RecordValue{
Fields: map[string]*schema_pb.Value{
"_ts_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: largeTimestamp1 + 1}},
"id": {Kind: &schema_pb.Value_Int64Value{Int64Value: 12345}},
},
}
result = predicate(nonMatchingRecord)
assert.False(t, result, "Predicate should NOT match record with different timestamp")
})
t.Run("Fix6_ComparisonOperators", func(t *testing.T) {
// Test all comparison operators work correctly with large timestamps
testRecord := &schema_pb.RecordValue{
Fields: map[string]*schema_pb.Value{
"_ts_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: largeTimestamp2}},
},
}
operators := []struct {
sql string
expected bool
}{
{"_ts_ns = " + strconv.FormatInt(largeTimestamp2, 10), true},
{"_ts_ns = " + strconv.FormatInt(largeTimestamp2+1, 10), false},
{"_ts_ns > " + strconv.FormatInt(largeTimestamp2-1, 10), true},
{"_ts_ns > " + strconv.FormatInt(largeTimestamp2, 10), false},
{"_ts_ns >= " + strconv.FormatInt(largeTimestamp2, 10), true},
{"_ts_ns >= " + strconv.FormatInt(largeTimestamp2+1, 10), false},
{"_ts_ns < " + strconv.FormatInt(largeTimestamp2+1, 10), true},
{"_ts_ns < " + strconv.FormatInt(largeTimestamp2, 10), false},
{"_ts_ns <= " + strconv.FormatInt(largeTimestamp2, 10), true},
{"_ts_ns <= " + strconv.FormatInt(largeTimestamp2-1, 10), false},
}
for _, op := range operators {
sql := "SELECT * FROM test WHERE " + op.sql
stmt, err := ParseSQL(sql)
assert.NoError(t, err, "Should parse SQL: %s", op.sql)
selectStmt := stmt.(*SelectStatement)
predicate, err := engine.buildPredicate(selectStmt.Where.Expr)
assert.NoError(t, err, "Should build predicate for: %s", op.sql)
result := predicate(testRecord)
assert.Equal(t, op.expected, result, "Operator test failed for: %s", op.sql)
}
})
t.Run("Fix7_EdgeCases", func(t *testing.T) {
// Test edge cases and boundary conditions
// Maximum int64 value
maxInt64 := int64(9223372036854775807)
testRecord := &schema_pb.RecordValue{
Fields: map[string]*schema_pb.Value{
"_ts_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: maxInt64}},
},
}
// Test equality with maximum int64
result := engine.valuesEqual(testRecord.Fields["_ts_ns"], maxInt64)
assert.True(t, result, "Should handle maximum int64 value correctly")
// Test with zero timestamp
zeroRecord := &schema_pb.RecordValue{
Fields: map[string]*schema_pb.Value{
"_ts_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: 0}},
},
}
result = engine.valuesEqual(zeroRecord.Fields["_ts_ns"], int64(0))
assert.True(t, result, "Should handle zero timestamp correctly")
})
}
// TestOriginalFailingQueries tests the specific queries that were failing before the fixes
func TestOriginalFailingQueries(t *testing.T) {
engine := NewTestSQLEngine()
failingQueries := []struct {
name string
sql string
timestamp int64
id int64
}{
{
name: "OriginalQuery1",
sql: "select id, _ts_ns from ecommerce.user_events where _ts_ns = 1756947416566456262",
timestamp: 1756947416566456262,
id: 897795,
},
{
name: "OriginalQuery2",
sql: "select id, _ts_ns from ecommerce.user_events where _ts_ns = 1756947416566439304",
timestamp: 1756947416566439304,
id: 715356,
},
{
name: "CurrentDataQuery",
sql: "select id, _ts_ns from ecommerce.user_events where _ts_ns = 1756913789829292386",
timestamp: 1756913789829292386,
id: 82460,
},
}
for _, query := range failingQueries {
t.Run(query.name, func(t *testing.T) {
// Parse the SQL
stmt, err := ParseSQL(query.sql)
assert.NoError(t, err, "Should parse the failing query")
selectStmt := stmt.(*SelectStatement)
// Test time filter extraction
startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
assert.Equal(t, query.timestamp-1, startTimeNs, "Should set startTimeNs to timestamp-1")
assert.Equal(t, int64(0), stopTimeNs, "Should not set stopTimeNs for equality")
// Test predicate building and evaluation
predicate, err := engine.buildPredicate(selectStmt.Where.Expr)
assert.NoError(t, err, "Should build predicate")
// Test with matching record
matchingRecord := &schema_pb.RecordValue{
Fields: map[string]*schema_pb.Value{
"_ts_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: query.timestamp}},
"id": {Kind: &schema_pb.Value_Int64Value{Int64Value: query.id}},
},
}
result := predicate(matchingRecord)
assert.True(t, result, "Predicate should match the target record for query: %s", query.name)
})
}
}
|