TestParser_LateralBasic tests basic LATERAL subquery syntax
(t *testing.T)
| 23 | |
| 24 | // TestParser_LateralBasic tests basic LATERAL subquery syntax |
| 25 | func TestParser_LateralBasic(t *testing.T) { |
| 26 | sql := "SELECT u.name, o.order_date FROM users u LEFT JOIN LATERAL (SELECT order_date FROM orders WHERE user_id = u.id LIMIT 1) AS o ON true" |
| 27 | |
| 28 | // Get tokenizer from pool |
| 29 | tkz := tokenizer.GetTokenizer() |
| 30 | defer tokenizer.PutTokenizer(tkz) |
| 31 | |
| 32 | // Tokenize SQL |
| 33 | tokens, err := tkz.Tokenize([]byte(sql)) |
| 34 | if err != nil { |
| 35 | t.Fatalf("Failed to tokenize: %v", err) |
| 36 | } |
| 37 | |
| 38 | // Convert tokens for parser |
| 39 | |
| 40 | // Parse tokens |
| 41 | parser := GetParser() |
| 42 | defer PutParser(parser) |
| 43 | |
| 44 | astObj, err := parser.ParseFromModelTokensWithPositions(tokens) |
| 45 | if err != nil { |
| 46 | t.Fatalf("Failed to parse: %v", err) |
| 47 | } |
| 48 | defer ast.ReleaseAST(astObj) |
| 49 | |
| 50 | // Verify we have a SELECT statement |
| 51 | if len(astObj.Statements) == 0 { |
| 52 | t.Fatal("No statements parsed") |
| 53 | } |
| 54 | |
| 55 | selectStmt, ok := astObj.Statements[0].(*ast.SelectStatement) |
| 56 | if !ok { |
| 57 | t.Fatal("Expected SELECT statement") |
| 58 | } |
| 59 | |
| 60 | // Verify JOIN contains LATERAL flag |
| 61 | if len(selectStmt.Joins) == 0 { |
| 62 | t.Fatal("Expected at least one JOIN") |
| 63 | } |
| 64 | |
| 65 | join := selectStmt.Joins[0] |
| 66 | if !join.Right.Lateral { |
| 67 | t.Error("Expected LATERAL flag to be true on joined table") |
| 68 | } |
| 69 | |
| 70 | if join.Right.Subquery == nil { |
| 71 | t.Error("Expected subquery in LATERAL join") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // TestParser_LateralFlagFalseWithoutKeyword tests that LATERAL flag is false when keyword is absent |
| 76 | func TestParser_LateralFlagFalseWithoutKeyword(t *testing.T) { |
nothing calls this directly
no test coverage detected