hybrid_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package engine
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. )
  8. func TestSQLEngine_HybridSelectBasic(t *testing.T) {
  9. engine := NewTestSQLEngine()
  10. // Test SELECT with _source column to show both live and archived data
  11. result, err := engine.ExecuteSQL(context.Background(), "SELECT *, _source FROM user_events")
  12. if err != nil {
  13. t.Fatalf("Expected no error, got %v", err)
  14. }
  15. if result.Error != nil {
  16. t.Fatalf("Expected no query error, got %v", result.Error)
  17. }
  18. if len(result.Columns) == 0 {
  19. t.Error("Expected columns in result")
  20. }
  21. // In mock environment, we only get live_log data from unflushed messages
  22. // parquet_archive data would come from parquet files in a real system
  23. if len(result.Rows) == 0 {
  24. t.Error("Expected rows in result")
  25. }
  26. // Check that we have the _source column showing data source
  27. hasSourceColumn := false
  28. sourceColumnIndex := -1
  29. for i, column := range result.Columns {
  30. if column == SW_COLUMN_NAME_SOURCE {
  31. hasSourceColumn = true
  32. sourceColumnIndex = i
  33. break
  34. }
  35. }
  36. if !hasSourceColumn {
  37. t.Skip("_source column not available in fallback mode - test requires real SeaweedFS cluster")
  38. }
  39. // Verify we have the expected data sources (in mock environment, only live_log)
  40. if hasSourceColumn && sourceColumnIndex >= 0 {
  41. foundLiveLog := false
  42. for _, row := range result.Rows {
  43. if sourceColumnIndex < len(row) {
  44. source := row[sourceColumnIndex].ToString()
  45. if source == "live_log" {
  46. foundLiveLog = true
  47. }
  48. // In mock environment, all data comes from unflushed messages (live_log)
  49. // In a real system, we would also see parquet_archive from parquet files
  50. }
  51. }
  52. if !foundLiveLog {
  53. t.Error("Expected to find live_log data source in results")
  54. }
  55. t.Logf("Found live_log data source from unflushed messages")
  56. }
  57. }
  58. func TestSQLEngine_HybridSelectWithLimit(t *testing.T) {
  59. engine := NewTestSQLEngine()
  60. // Test SELECT with LIMIT on hybrid data
  61. result, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM user_events LIMIT 2")
  62. if err != nil {
  63. t.Fatalf("Expected no error, got %v", err)
  64. }
  65. if result.Error != nil {
  66. t.Fatalf("Expected no query error, got %v", result.Error)
  67. }
  68. // Should have exactly 2 rows due to LIMIT
  69. if len(result.Rows) != 2 {
  70. t.Errorf("Expected 2 rows with LIMIT 2, got %d", len(result.Rows))
  71. }
  72. }
  73. func TestSQLEngine_HybridSelectDifferentTables(t *testing.T) {
  74. engine := NewTestSQLEngine()
  75. // Test both user_events and system_logs tables
  76. tables := []string{"user_events", "system_logs"}
  77. for _, tableName := range tables {
  78. result, err := engine.ExecuteSQL(context.Background(), fmt.Sprintf("SELECT *, _source FROM %s", tableName))
  79. if err != nil {
  80. t.Errorf("Error querying hybrid table %s: %v", tableName, err)
  81. continue
  82. }
  83. if result.Error != nil {
  84. t.Errorf("Query error for hybrid table %s: %v", tableName, result.Error)
  85. continue
  86. }
  87. if len(result.Columns) == 0 {
  88. t.Errorf("No columns returned for hybrid table %s", tableName)
  89. }
  90. if len(result.Rows) == 0 {
  91. t.Errorf("No rows returned for hybrid table %s", tableName)
  92. }
  93. // Check for _source column
  94. hasSourceColumn := false
  95. for _, column := range result.Columns {
  96. if column == "_source" {
  97. hasSourceColumn = true
  98. break
  99. }
  100. }
  101. if !hasSourceColumn {
  102. t.Logf("Table %s missing _source column - running in fallback mode", tableName)
  103. }
  104. t.Logf("Table %s: %d columns, %d rows with hybrid data sources", tableName, len(result.Columns), len(result.Rows))
  105. }
  106. }
  107. func TestSQLEngine_HybridDataSource(t *testing.T) {
  108. engine := NewTestSQLEngine()
  109. // Test that we can distinguish between live and archived data
  110. result, err := engine.ExecuteSQL(context.Background(), "SELECT user_id, event_type, _source FROM user_events")
  111. if err != nil {
  112. t.Fatalf("Expected no error, got %v", err)
  113. }
  114. if result.Error != nil {
  115. t.Fatalf("Expected no query error, got %v", result.Error)
  116. }
  117. // Find the _source column
  118. sourceColumnIndex := -1
  119. eventTypeColumnIndex := -1
  120. for i, column := range result.Columns {
  121. switch column {
  122. case "_source":
  123. sourceColumnIndex = i
  124. case "event_type":
  125. eventTypeColumnIndex = i
  126. }
  127. }
  128. if sourceColumnIndex == -1 {
  129. t.Skip("Could not find _source column - test requires real SeaweedFS cluster")
  130. }
  131. if eventTypeColumnIndex == -1 {
  132. t.Fatal("Could not find event_type column")
  133. }
  134. // Check the data characteristics
  135. liveEventFound := false
  136. archivedEventFound := false
  137. for _, row := range result.Rows {
  138. if sourceColumnIndex < len(row) && eventTypeColumnIndex < len(row) {
  139. source := row[sourceColumnIndex].ToString()
  140. eventType := row[eventTypeColumnIndex].ToString()
  141. if source == "live_log" && strings.Contains(eventType, "live_") {
  142. liveEventFound = true
  143. t.Logf("Found live event: %s from %s", eventType, source)
  144. }
  145. if source == "parquet_archive" && strings.Contains(eventType, "archived_") {
  146. archivedEventFound = true
  147. t.Logf("Found archived event: %s from %s", eventType, source)
  148. }
  149. }
  150. }
  151. if !liveEventFound {
  152. t.Error("Expected to find live events with live_ prefix")
  153. }
  154. if !archivedEventFound {
  155. t.Error("Expected to find archived events with archived_ prefix")
  156. }
  157. }
  158. func TestSQLEngine_HybridSystemLogs(t *testing.T) {
  159. engine := NewTestSQLEngine()
  160. // Test system_logs with hybrid data
  161. result, err := engine.ExecuteSQL(context.Background(), "SELECT level, message, service, _source FROM system_logs")
  162. if err != nil {
  163. t.Fatalf("Expected no error, got %v", err)
  164. }
  165. if result.Error != nil {
  166. t.Fatalf("Expected no query error, got %v", result.Error)
  167. }
  168. // Should have both live and archived system logs
  169. if len(result.Rows) < 2 {
  170. t.Errorf("Expected at least 2 system log entries, got %d", len(result.Rows))
  171. }
  172. // Find column indices
  173. levelIndex := -1
  174. sourceIndex := -1
  175. for i, column := range result.Columns {
  176. switch column {
  177. case "level":
  178. levelIndex = i
  179. case "_source":
  180. sourceIndex = i
  181. }
  182. }
  183. // Verify we have both live and archived system logs
  184. foundLive := false
  185. foundArchived := false
  186. for _, row := range result.Rows {
  187. if sourceIndex >= 0 && sourceIndex < len(row) {
  188. source := row[sourceIndex].ToString()
  189. if source == "live_log" {
  190. foundLive = true
  191. if levelIndex >= 0 && levelIndex < len(row) {
  192. level := row[levelIndex].ToString()
  193. t.Logf("Live system log: level=%s", level)
  194. }
  195. }
  196. if source == "parquet_archive" {
  197. foundArchived = true
  198. if levelIndex >= 0 && levelIndex < len(row) {
  199. level := row[levelIndex].ToString()
  200. t.Logf("Archived system log: level=%s", level)
  201. }
  202. }
  203. }
  204. }
  205. if !foundLive {
  206. t.Log("No live system logs found - running in fallback mode")
  207. }
  208. if !foundArchived {
  209. t.Log("No archived system logs found - running in fallback mode")
  210. }
  211. }
  212. func TestSQLEngine_HybridSelectWithTimeImplications(t *testing.T) {
  213. engine := NewTestSQLEngine()
  214. // Test that demonstrates the time-based nature of hybrid data
  215. // Live data should be more recent than archived data
  216. result, err := engine.ExecuteSQL(context.Background(), "SELECT event_type, _source FROM user_events")
  217. if err != nil {
  218. t.Fatalf("Expected no error, got %v", err)
  219. }
  220. if result.Error != nil {
  221. t.Fatalf("Expected no query error, got %v", result.Error)
  222. }
  223. // This test documents that hybrid scanning provides a complete view
  224. // of both recent (live) and historical (archived) data in a single query
  225. liveCount := 0
  226. archivedCount := 0
  227. sourceIndex := -1
  228. for i, column := range result.Columns {
  229. if column == "_source" {
  230. sourceIndex = i
  231. break
  232. }
  233. }
  234. if sourceIndex >= 0 {
  235. for _, row := range result.Rows {
  236. if sourceIndex < len(row) {
  237. source := row[sourceIndex].ToString()
  238. switch source {
  239. case "live_log":
  240. liveCount++
  241. case "parquet_archive":
  242. archivedCount++
  243. }
  244. }
  245. }
  246. }
  247. t.Logf("Hybrid query results: %d live messages, %d archived messages", liveCount, archivedCount)
  248. if liveCount == 0 && archivedCount == 0 {
  249. t.Log("No live or archived messages found - running in fallback mode")
  250. }
  251. }