parsing_debug_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package engine
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. // TestBasicParsing tests basic SQL parsing
  7. func TestBasicParsing(t *testing.T) {
  8. testCases := []string{
  9. "SELECT * FROM user_events",
  10. "SELECT id FROM user_events",
  11. "SELECT id FROM user_events WHERE id = 123",
  12. "SELECT id FROM user_events WHERE id > 123",
  13. "SELECT id FROM user_events WHERE status = 'active'",
  14. }
  15. for i, sql := range testCases {
  16. t.Run(fmt.Sprintf("Query_%d", i+1), func(t *testing.T) {
  17. t.Logf("Testing SQL: %s", sql)
  18. stmt, err := ParseSQL(sql)
  19. if err != nil {
  20. t.Errorf("Parse error: %v", err)
  21. return
  22. }
  23. t.Logf("Parsed statement type: %T", stmt)
  24. if selectStmt, ok := stmt.(*SelectStatement); ok {
  25. t.Logf("SelectStatement details:")
  26. t.Logf(" SelectExprs count: %d", len(selectStmt.SelectExprs))
  27. t.Logf(" From count: %d", len(selectStmt.From))
  28. t.Logf(" WHERE clause exists: %v", selectStmt.Where != nil)
  29. if selectStmt.Where != nil {
  30. t.Logf(" WHERE expression type: %T", selectStmt.Where.Expr)
  31. } else {
  32. t.Logf(" ❌ WHERE clause is NIL - this is the bug!")
  33. }
  34. } else {
  35. t.Errorf("Expected SelectStatement, got %T", stmt)
  36. }
  37. })
  38. }
  39. }
  40. // TestCockroachParserDirectly tests the CockroachDB parser directly
  41. func TestCockroachParserDirectly(t *testing.T) {
  42. // Test if the issue is in our ParseSQL function or CockroachDB parser
  43. sql := "SELECT id FROM user_events WHERE id > 123"
  44. t.Logf("Testing CockroachDB parser directly with: %s", sql)
  45. // First test our ParseSQL function
  46. stmt, err := ParseSQL(sql)
  47. if err != nil {
  48. t.Fatalf("Our ParseSQL failed: %v", err)
  49. }
  50. t.Logf("Our ParseSQL returned: %T", stmt)
  51. if selectStmt, ok := stmt.(*SelectStatement); ok {
  52. if selectStmt.Where == nil {
  53. t.Errorf("❌ Our ParseSQL is not extracting WHERE clauses!")
  54. t.Errorf("This means the issue is in our CockroachDB AST conversion")
  55. } else {
  56. t.Logf("✅ Our ParseSQL extracted WHERE clause: %T", selectStmt.Where.Expr)
  57. }
  58. }
  59. }
  60. // TestParseMethodComparison tests different parsing paths
  61. func TestParseMethodComparison(t *testing.T) {
  62. sql := "SELECT id FROM user_events WHERE id > 123"
  63. t.Logf("Comparing parsing methods for: %s", sql)
  64. // Test 1: Our global ParseSQL function
  65. stmt1, err1 := ParseSQL(sql)
  66. t.Logf("Global ParseSQL: %T, error: %v", stmt1, err1)
  67. if selectStmt, ok := stmt1.(*SelectStatement); ok {
  68. t.Logf(" WHERE clause: %v", selectStmt.Where != nil)
  69. }
  70. // Test 2: Check if we have different parsing paths
  71. // This will help identify if the issue is in our custom parser vs CockroachDB parser
  72. engine := NewTestSQLEngine()
  73. _, err2 := engine.ExecuteSQL(nil, sql)
  74. t.Logf("ExecuteSQL error (helps identify parsing path): %v", err2)
  75. }