timestamp_integration_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package engine
  2. import (
  3. "strconv"
  4. "testing"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. // TestTimestampIntegrationScenarios tests complete end-to-end scenarios
  9. func TestTimestampIntegrationScenarios(t *testing.T) {
  10. engine := NewTestSQLEngine()
  11. // Simulate the exact timestamps that were failing in production
  12. timestamps := []struct {
  13. timestamp int64
  14. id int64
  15. name string
  16. }{
  17. {1756947416566456262, 897795, "original_failing_1"},
  18. {1756947416566439304, 715356, "original_failing_2"},
  19. {1756913789829292386, 82460, "current_data"},
  20. }
  21. t.Run("EndToEndTimestampEquality", func(t *testing.T) {
  22. for _, ts := range timestamps {
  23. t.Run(ts.name, func(t *testing.T) {
  24. // Create a test record
  25. record := &schema_pb.RecordValue{
  26. Fields: map[string]*schema_pb.Value{
  27. "_timestamp_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: ts.timestamp}},
  28. "id": {Kind: &schema_pb.Value_Int64Value{Int64Value: ts.id}},
  29. },
  30. }
  31. // Build SQL query
  32. sql := "SELECT id, _timestamp_ns FROM test WHERE _timestamp_ns = " + strconv.FormatInt(ts.timestamp, 10)
  33. stmt, err := ParseSQL(sql)
  34. assert.NoError(t, err)
  35. selectStmt := stmt.(*SelectStatement)
  36. // Test time filter extraction (Fix #2 and #5)
  37. startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
  38. assert.Equal(t, ts.timestamp-1, startTimeNs, "Should set startTimeNs to avoid scan boundary bug")
  39. assert.Equal(t, int64(0), stopTimeNs, "Should not set stopTimeNs to avoid premature termination")
  40. // Test predicate building (Fix #1)
  41. predicate, err := engine.buildPredicate(selectStmt.Where.Expr)
  42. assert.NoError(t, err)
  43. // Test predicate evaluation (Fix #1 - precision)
  44. result := predicate(record)
  45. assert.True(t, result, "Should match exact timestamp without precision loss")
  46. // Test that close but different timestamps don't match
  47. closeRecord := &schema_pb.RecordValue{
  48. Fields: map[string]*schema_pb.Value{
  49. "_timestamp_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: ts.timestamp + 1}},
  50. "id": {Kind: &schema_pb.Value_Int64Value{Int64Value: ts.id}},
  51. },
  52. }
  53. result = predicate(closeRecord)
  54. assert.False(t, result, "Should not match timestamp that differs by 1 nanosecond")
  55. })
  56. }
  57. })
  58. t.Run("ComplexRangeQueries", func(t *testing.T) {
  59. // Test range queries that combine multiple fixes
  60. testCases := []struct {
  61. name string
  62. sql string
  63. shouldSet struct{ start, stop bool }
  64. }{
  65. {
  66. name: "RangeWithDifferentBounds",
  67. sql: "SELECT * FROM test WHERE _timestamp_ns >= 1756913789829292386 AND _timestamp_ns <= 1756947416566456262",
  68. shouldSet: struct{ start, stop bool }{true, true},
  69. },
  70. {
  71. name: "RangeWithSameBounds",
  72. sql: "SELECT * FROM test WHERE _timestamp_ns >= 1756913789829292386 AND _timestamp_ns <= 1756913789829292386",
  73. shouldSet: struct{ start, stop bool }{true, false}, // Fix #4: equal bounds should not set stop
  74. },
  75. {
  76. name: "OpenEndedRange",
  77. sql: "SELECT * FROM test WHERE _timestamp_ns >= 1756913789829292386",
  78. shouldSet: struct{ start, stop bool }{true, false},
  79. },
  80. }
  81. for _, tc := range testCases {
  82. t.Run(tc.name, func(t *testing.T) {
  83. stmt, err := ParseSQL(tc.sql)
  84. assert.NoError(t, err)
  85. selectStmt := stmt.(*SelectStatement)
  86. startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
  87. if tc.shouldSet.start {
  88. assert.NotEqual(t, int64(0), startTimeNs, "Should set startTimeNs for range query")
  89. } else {
  90. assert.Equal(t, int64(0), startTimeNs, "Should not set startTimeNs")
  91. }
  92. if tc.shouldSet.stop {
  93. assert.NotEqual(t, int64(0), stopTimeNs, "Should set stopTimeNs for bounded range")
  94. } else {
  95. assert.Equal(t, int64(0), stopTimeNs, "Should not set stopTimeNs")
  96. }
  97. })
  98. }
  99. })
  100. t.Run("ProductionScenarioReproduction", func(t *testing.T) {
  101. // This test reproduces the exact production scenario that was failing
  102. // Original failing query: WHERE _timestamp_ns = 1756947416566456262
  103. sql := "SELECT id, _timestamp_ns FROM ecommerce.user_events WHERE _timestamp_ns = 1756947416566456262"
  104. stmt, err := ParseSQL(sql)
  105. assert.NoError(t, err, "Should parse the production query that was failing")
  106. selectStmt := stmt.(*SelectStatement)
  107. // Verify time filter extraction works correctly (fixes scan termination issue)
  108. startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
  109. assert.Equal(t, int64(1756947416566456261), startTimeNs, "Should set startTimeNs to target-1") // Fix #5
  110. assert.Equal(t, int64(0), stopTimeNs, "Should not set stopTimeNs") // Fix #2
  111. // Verify predicate handles the large timestamp correctly
  112. predicate, err := engine.buildPredicate(selectStmt.Where.Expr)
  113. assert.NoError(t, err, "Should build predicate for production query")
  114. // Test with the actual record that exists in production
  115. productionRecord := &schema_pb.RecordValue{
  116. Fields: map[string]*schema_pb.Value{
  117. "_timestamp_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: 1756947416566456262}},
  118. "id": {Kind: &schema_pb.Value_Int64Value{Int64Value: 897795}},
  119. },
  120. }
  121. result := predicate(productionRecord)
  122. assert.True(t, result, "Should match the production record that was failing before") // Fix #1
  123. // Verify precision - test that a timestamp differing by just 1 nanosecond doesn't match
  124. slightlyDifferentRecord := &schema_pb.RecordValue{
  125. Fields: map[string]*schema_pb.Value{
  126. "_timestamp_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: 1756947416566456263}},
  127. "id": {Kind: &schema_pb.Value_Int64Value{Int64Value: 897795}},
  128. },
  129. }
  130. result = predicate(slightlyDifferentRecord)
  131. assert.False(t, result, "Should NOT match record with timestamp differing by 1 nanosecond")
  132. })
  133. }
  134. // TestRegressionPrevention ensures the fixes don't break normal cases
  135. func TestRegressionPrevention(t *testing.T) {
  136. engine := NewTestSQLEngine()
  137. t.Run("SmallTimestamps", func(t *testing.T) {
  138. // Ensure small timestamps still work normally
  139. smallTimestamp := int64(1234567890)
  140. record := &schema_pb.RecordValue{
  141. Fields: map[string]*schema_pb.Value{
  142. "_timestamp_ns": {Kind: &schema_pb.Value_Int64Value{Int64Value: smallTimestamp}},
  143. },
  144. }
  145. result := engine.valuesEqual(record.Fields["_timestamp_ns"], smallTimestamp)
  146. assert.True(t, result, "Small timestamps should continue to work")
  147. })
  148. t.Run("NonTimestampColumns", func(t *testing.T) {
  149. // Ensure non-timestamp columns aren't affected by timestamp fixes
  150. sql := "SELECT * FROM test WHERE id = 12345"
  151. stmt, err := ParseSQL(sql)
  152. assert.NoError(t, err)
  153. selectStmt := stmt.(*SelectStatement)
  154. startTimeNs, stopTimeNs := engine.extractTimeFilters(selectStmt.Where.Expr)
  155. assert.Equal(t, int64(0), startTimeNs, "Non-timestamp queries should not set startTimeNs")
  156. assert.Equal(t, int64(0), stopTimeNs, "Non-timestamp queries should not set stopTimeNs")
  157. })
  158. t.Run("StringComparisons", func(t *testing.T) {
  159. // Ensure string comparisons aren't affected
  160. record := &schema_pb.RecordValue{
  161. Fields: map[string]*schema_pb.Value{
  162. "name": {Kind: &schema_pb.Value_StringValue{StringValue: "test"}},
  163. },
  164. }
  165. result := engine.valuesEqual(record.Fields["name"], "test")
  166. assert.True(t, result, "String comparisons should continue to work")
  167. })
  168. }