linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//! Full-Text Parser
//!
//! This module implements the parser for full-text search SQL statements,
//! including CREATE FULLTEXT INDEX, SEARCH, and related queries.

use crate::parser::ast::fulltext::{
    AlterFulltextIndex, AlterIndexAction, CreateFulltextIndex, DescribeFulltextIndex,
    DropFulltextIndex, FulltextMatchCondition, FulltextQueryExpr, FulltextYieldClause,
    FulltextYieldItem, IndexFieldDef, IndexOptions, LookupFulltext, MatchFulltext, SearchStatement,
    ShowFulltextIndex,
};
use crate::parser::ast::stmt::Stmt;
use crate::parser::ast::stmt::{OrderByClause, OrderByItem};
use crate::parser::ast::types::{LimitClause, OrderDirection, SkipClause};
use crate::parser::parsing::expr_parser::parse_expression_with_context;
use crate::parser::parsing::parse_context::ParseContext;
use crate::parser::TokenKind;
use linkrs_core::types::expr::{create_contextual_expression, Expression};
use linkrs_core::types::FulltextEngineType;
use std::collections::HashMap;

/// Parse full-text search statements from ParseContext
pub fn parse_fulltext(ctx: &mut ParseContext) -> Result<Stmt, crate::parser::ParseError> {
    if ctx.check_keyword("CREATE") {
        return parse_create_fulltext_index(ctx);
    } else if ctx.check_keyword("DROP") {
        return parse_drop_fulltext_index(ctx);
    } else if ctx.check_keyword("ALTER") {
        return parse_alter_fulltext_index(ctx);
    } else if ctx.check_keyword("SHOW") {
        return parse_show_fulltext_index(ctx);
    } else if ctx.check_keyword("DESCRIBE") || ctx.check_keyword("DESC") {
        return parse_describe_fulltext_index(ctx);
    } else if ctx.check_keyword("SEARCH") {
        // Check if it's SEARCH VECTOR or SEARCH INDEX
        if ctx.check_keyword_sequence(&["SEARCH", "VECTOR"]) {
            // Forward to vector parser
            return crate::parser::parsing::vector_parser::parse_vector(ctx);
        }
        // Check if it's SEARCH INDEX - if so, we need to consume INDEX here
        let is_search_index = ctx.check_keyword_sequence(&["SEARCH", "INDEX"]);
        // Consume SEARCH and continue with fulltext parsing
        ctx.consume_keyword("SEARCH")?;
        if is_search_index {
            // Also consume INDEX here before calling parse_search_statement_after_search
            ctx.consume_keyword("INDEX")?;
        }
        return parse_search_statement_after_search(ctx);
    } else if ctx.check_keyword("LOOKUP") {
        // Check if it's LOOKUP VECTOR
        if ctx.check_keyword_sequence(&["LOOKUP", "VECTOR"]) {
            // Forward to vector parser
            return crate::parser::parsing::vector_parser::parse_vector(ctx);
        }
        return parse_lookup_fulltext(ctx);
    } else if ctx.check_keyword("MATCH") {
        return parse_match_fulltext(ctx);
    }

    Err(crate::parser::ParseError::new(
        crate::parser::core::error::ParseErrorKind::SyntaxError,
        "Not a full-text search statement".to_string(),
        ctx.current_position(),
    ))
}

pub fn parse_create_fulltext_index(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("CREATE")?;
    parse_create_fulltext_index_after_create(ctx)
}

pub fn parse_create_fulltext_index_after_create(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("FULLTEXT")?;
    ctx.consume_keyword("INDEX")?;

    let if_not_exists = if ctx.check_keyword("IF") {
        ctx.consume_keyword("IF")?;
        ctx.consume_keyword("NOT")?;
        ctx.consume_keyword("EXISTS")?;
        true
    } else {
        false
    };

    let index_name = ctx.consume_identifier()?;
    ctx.consume_keyword("ON")?;
    let schema_name = ctx.consume_identifier()?;

    ctx.expect_token(TokenKind::LParen)?;
    let mut fields = Vec::new();

    loop {
        let field_name = ctx.consume_identifier()?;

        let field_def = IndexFieldDef::new(field_name);

        if ctx.check_keyword("ANALYZER") {
            return Err(crate::parser::ParseError::new(
                crate::parser::core::error::ParseErrorKind::SyntaxError,
                "ANALYZER is not supported yet; create the fulltext index without per-field analyzers"
                    .to_string(),
                ctx.current_position(),
            ));
        }

        if ctx.check_keyword("BOOST") {
            return Err(crate::parser::ParseError::new(
                crate::parser::core::error::ParseErrorKind::SyntaxError,
                "BOOST is not supported yet; create the fulltext index without per-field weights"
                    .to_string(),
                ctx.current_position(),
            ));
        }

        fields.push(field_def);

        if !ctx.consume_optional_token(",") {
            break;
        }
    }

    ctx.expect_token(TokenKind::RParen)?;

    ctx.consume_keyword("ENGINE")?;
    if ctx.current_token().kind == TokenKind::Assign {
        ctx.next_token();
    }
    if ctx.check_keyword("BM25") {
        ctx.consume_keyword("BM25")?;
    } else {
        return Err(crate::parser::ParseError::new(
            crate::parser::core::error::ParseErrorKind::SyntaxError,
            "Expected BM25 engine type".to_string(),
            ctx.current_position(),
        ));
    }
    let engine_type = FulltextEngineType::Bm25;

    if ctx.check_keyword("OPTIONS") {
        return Err(crate::parser::ParseError::new(
            crate::parser::core::error::ParseErrorKind::SyntaxError,
            "OPTIONS (...) is not supported yet; create the fulltext index without BM25 option overrides (defaults k1=1.5, b=0.75 apply)"
                .to_string(),
            ctx.current_position(),
        ));
    }

    let options = IndexOptions {
        bm25_config: None,
        common_options: HashMap::new(),
    };

    let mut create = CreateFulltextIndex::new(
        ctx.current_span(),
        index_name,
        schema_name,
        fields,
        engine_type,
    );
    create.if_not_exists = if_not_exists;
    create.options = options;

    Ok(Stmt::CreateFulltextIndex(create))
}

pub fn parse_drop_fulltext_index(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("DROP")?;
    parse_drop_fulltext_index_after_drop(ctx)
}

pub fn parse_drop_fulltext_index_after_drop(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("FULLTEXT")?;
    ctx.consume_keyword("INDEX")?;

    let if_exists = if ctx.check_keyword("IF") {
        ctx.consume_keyword("IF")?;
        ctx.consume_keyword("EXISTS")?;
        true
    } else {
        false
    };

    let index_name = ctx.consume_identifier()?;

    let drop = DropFulltextIndex {
        span: ctx.current_span(),
        index_name,
        if_exists,
    };

    Ok(Stmt::DropFulltextIndex(drop))
}

pub fn parse_alter_fulltext_index(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("ALTER")?;
    parse_alter_fulltext_index_after_alter(ctx)
}

pub fn parse_alter_fulltext_index_after_alter(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("FULLTEXT")?;
    ctx.consume_keyword("INDEX")?;

    let index_name = ctx.consume_identifier()?;
    let mut actions = Vec::new();

    loop {
        if ctx.check_keyword("ADD") {
            ctx.consume_keyword("ADD")?;
            ctx.consume_keyword("FIELD")?;

            let field_name = ctx.consume_identifier()?;

            if ctx.check_keyword("ANALYZER") {
                return Err(crate::parser::ParseError::new(
                    crate::parser::core::error::ParseErrorKind::SyntaxError,
                    "ANALYZER is not supported yet; add the field without a per-field analyzer"
                        .to_string(),
                    ctx.current_position(),
                ));
            }

            let field_def = IndexFieldDef::new(field_name);

            actions.push(AlterIndexAction::AddField(field_def));
        } else if ctx.check_keyword("DROP") {
            ctx.consume_keyword("DROP")?;
            ctx.consume_keyword("FIELD")?;
            let field_name = ctx.consume_identifier()?;
            actions.push(AlterIndexAction::DropField(field_name));
        } else if ctx.check_keyword("SET") {
            ctx.consume_keyword("SET")?;
            let key = ctx.consume_identifier()?;
            ctx.expect_token(TokenKind::Eq)?;
            let value = ctx.consume_value()?;
            actions.push(AlterIndexAction::SetOption(key, value));
        } else if ctx.check_keyword("REBUILD") {
            ctx.consume_keyword("REBUILD")?;
            actions.push(AlterIndexAction::Rebuild);
        } else if ctx.check_keyword("OPTIMIZE") {
            ctx.consume_keyword("OPTIMIZE")?;
            actions.push(AlterIndexAction::Optimize);
        } else {
            return Err(crate::parser::ParseError::new(
                crate::parser::core::error::ParseErrorKind::SyntaxError,
                "Expected ALTER INDEX action".to_string(),
                ctx.current_position(),
            ));
        }

        if !ctx.consume_optional_token(",") {
            break;
        }
    }

    let alter = AlterFulltextIndex {
        span: ctx.current_span(),
        index_name,
        actions,
    };

    Ok(Stmt::AlterFulltextIndex(alter))
}

fn parse_show_fulltext_index(ctx: &mut ParseContext) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("SHOW")?;
    ctx.consume_keyword("FULLTEXT")?;
    ctx.consume_keyword("INDEX")?;

    let mut pattern = None;
    let mut from_schema = None;

    if ctx.check_keyword("LIKE") {
        ctx.consume_keyword("LIKE")?;
        pattern = Some(ctx.consume_string()?);
    }

    if ctx.check_keyword("FROM") || ctx.check_keyword("IN") {
        ctx.consume_keyword("FROM")?;
        from_schema = Some(ctx.consume_identifier()?);
    }

    let show = ShowFulltextIndex {
        span: ctx.current_span(),
        pattern,
        from_schema,
    };

    Ok(Stmt::ShowFulltextIndex(show))
}

fn parse_describe_fulltext_index(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("DESCRIBE")?;
    ctx.consume_keyword("FULLTEXT")?;
    ctx.consume_keyword("INDEX")?;

    let index_name = ctx.consume_identifier()?;

    let describe = DescribeFulltextIndex {
        span: ctx.current_span(),
        index_name,
    };

    Ok(Stmt::DescribeFulltextIndex(describe))
}

fn parse_search_statement_after_search(
    ctx: &mut ParseContext,
) -> Result<Stmt, crate::parser::ParseError> {
    // INDEX keyword has already been consumed by the caller
    let index_name = ctx.consume_identifier()?;
    ctx.consume_keyword("MATCH")?;

    let query = parse_fulltext_query_expr(ctx)?;

    let mut search = SearchStatement::new(index_name, query);

    if ctx.check_keyword("YIELD") {
        ctx.consume_keyword("YIELD")?;
        let yield_clause = parse_yield_clause(ctx)?;
        search.yield_clause = Some(yield_clause);
    }

    if ctx.check_keyword("WHERE") {
        ctx.consume_keyword("WHERE")?;
        let where_clause = parse_expression_with_context(ctx, ctx.expression_context_clone())?;
        search.where_clause = Some(where_clause);
    }

    if ctx.check_keyword("ORDER") {
        ctx.consume_keyword("ORDER")?;
        ctx.consume_keyword("BY")?;
        let order_clause = parse_order_clause(ctx)?;
        search.order_clause = Some(order_clause);
    }

    if ctx.check_keyword("LIMIT") {
        ctx.consume_keyword("LIMIT")?;
        let count = ctx.consume_int()? as usize;
        search.limit = Some(LimitClause {
            span: ctx.current_span(),
            count,
        });
    }

    if ctx.check_keyword("OFFSET") {
        ctx.consume_keyword("OFFSET")?;
        let count = ctx.consume_int()? as usize;
        search.skip = Some(SkipClause {
            span: ctx.current_span(),
            count,
        });
    }

    Ok(Stmt::Search(search))
}

fn parse_fulltext_query_expr(
    ctx: &mut ParseContext,
) -> Result<FulltextQueryExpr, crate::parser::ParseError> {
    if let Some(text) = ctx.try_consume_string() {
        return Ok(FulltextQueryExpr::Simple(text));
    }

    if ctx.is_identifier_token() {
        let field = ctx.consume_identifier()?;
        if ctx.consume_optional_token(":") {
            let query = ctx.consume_string()?;
            return Ok(FulltextQueryExpr::Field(field, query));
        }
    }

    if let Some(text) = ctx.try_consume_quoted_string() {
        return Ok(FulltextQueryExpr::Phrase(text));
    }

    Err(crate::parser::ParseError::new(
        crate::parser::core::error::ParseErrorKind::SyntaxError,
        "Expected full-text query expression".to_string(),
        ctx.current_position(),
    ))
}

fn parse_yield_clause(
    ctx: &mut ParseContext,
) -> Result<FulltextYieldClause, crate::parser::ParseError> {
    let mut items = Vec::new();

    loop {
        let expr = if ctx.check_keyword("score") && ctx.peek_token().kind != TokenKind::LParen {
            ctx.consume_identifier()?;
            create_contextual_expression(Expression::Function {
                name: "score".to_string(),
                args: vec![],
            })
        } else if ctx.check_keyword("matched_fields") && ctx.peek_token().kind != TokenKind::LParen
        {
            ctx.consume_identifier()?;
            create_contextual_expression(Expression::Function {
                name: "matched_fields".to_string(),
                args: vec![],
            })
        } else if ctx.consume_optional_token("*") {
            create_contextual_expression(Expression::variable("*"))
        } else {
            parse_expression_with_context(ctx, ctx.expression_context_clone())?
        };

        let alias = if ctx.check_keyword("AS") {
            ctx.consume_keyword("AS")?;
            Some(ctx.consume_identifier()?)
        } else {
            None
        };

        items.push(FulltextYieldItem { expr, alias });

        if !ctx.consume_optional_token(",") {
            break;
        }
    }

    Ok(FulltextYieldClause { items })
}

fn parse_order_clause(ctx: &mut ParseContext) -> Result<OrderByClause, crate::parser::ParseError> {
    let span = ctx.current_span();
    let mut items = Vec::new();

    loop {
        let expression = parse_expression_with_context(ctx, ctx.expression_context_clone())?;
        let direction = if ctx.check_keyword("ASC") {
            ctx.consume_keyword("ASC")?;
            OrderDirection::Asc
        } else if ctx.check_keyword("DESC") {
            ctx.consume_keyword("DESC")?;
            OrderDirection::Desc
        } else {
            OrderDirection::Asc
        };

        items.push(OrderByItem {
            expression,
            direction,
        });

        if !ctx.consume_optional_token(",") {
            break;
        }
    }

    Ok(OrderByClause { span, items })
}

fn parse_lookup_fulltext(ctx: &mut ParseContext) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("LOOKUP")?;
    ctx.consume_keyword("ON")?;

    let schema_name = ctx.consume_identifier()?;
    ctx.consume_keyword("INDEX")?;
    let index_name = ctx.consume_identifier()?;

    ctx.consume_keyword("WHERE")?;
    let query = ctx.consume_string()?;

    let mut lookup = LookupFulltext {
        span: ctx.current_span(),
        schema_name,
        index_name,
        query,
        yield_clause: None,
        limit: None,
    };

    if ctx.check_keyword("YIELD") {
        ctx.consume_keyword("YIELD")?;
        lookup.yield_clause = Some(parse_yield_clause(ctx)?);
    }

    if ctx.check_keyword("LIMIT") {
        ctx.consume_keyword("LIMIT")?;
        lookup.limit = Some(ctx.consume_int()? as usize);
    }

    Ok(Stmt::LookupFulltext(lookup))
}

fn parse_match_fulltext(ctx: &mut ParseContext) -> Result<Stmt, crate::parser::ParseError> {
    ctx.consume_keyword("MATCH")?;
    let pattern = ctx.consume_string()?;

    ctx.consume_keyword("WHERE")?;
    ctx.consume_keyword("FULLTEXT_MATCH")?;
    ctx.expect_token(TokenKind::LParen)?;
    let field = ctx.consume_identifier()?;
    ctx.expect_token(TokenKind::Comma)?;
    let query = ctx.consume_string()?;
    ctx.expect_token(TokenKind::RParen)?;

    let condition = FulltextMatchCondition {
        field,
        query,
        index_name: None,
    };

    let mut match_stmt = MatchFulltext {
        span: ctx.current_span(),
        pattern,
        fulltext_condition: condition,
        yield_clause: None,
    };

    if ctx.check_keyword("YIELD") {
        ctx.consume_keyword("YIELD")?;
        match_stmt.yield_clause = Some(parse_yield_clause(ctx)?);
    }

    Ok(Stmt::MatchFulltext(match_stmt))
}

impl IndexFieldDef {
    fn new(field_name: String) -> Self {
        Self {
            field_name,
            analyzer: None,
            boost: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::parser::parsing::parser::Parser;

    #[test]
    fn test_parse_create_fulltext_index() {
        let sql = r#"CREATE FULLTEXT INDEX idx_article_content
                     ON article(title, content)
                     ENGINE BM25"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_create_fulltext_index_with_equals_engine() {
        let sql = r#"CREATE FULLTEXT INDEX idx_article_content
                     ON article(content)
                     ENGINE = BM25"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_create_fulltext_index_with_analyzer_rejected() {
        let sql = r#"CREATE FULLTEXT INDEX idx_article_content
                     ON article(content ANALYZER 'standard')
                     ENGINE = BM25"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_err());
        let collected = format!("{:?}", parser.take_errors());
        assert!(
            collected.contains("ANALYZER"),
            "rejection must name ANALYZER: {}",
            collected
        );
    }

    #[test]
    fn test_parse_create_fulltext_index_with_boost_rejected() {
        let sql = r#"CREATE FULLTEXT INDEX idx_article_content
                     ON article(content BOOST 2.0)
                     ENGINE = BM25"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_err());
        let collected = format!("{:?}", parser.take_errors());
        assert!(
            collected.contains("BOOST"),
            "rejection must name BOOST: {}",
            collected
        );
    }

    #[test]
    fn test_parse_create_fulltext_index_with_options_rejected() {
        let sql = r#"CREATE FULLTEXT INDEX idx_article_content
                     ON article(content)
                     ENGINE = BM25 OPTIONS (k1=1.2, b=0.8)"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_err());
        let collected = format!("{:?}", parser.take_errors());
        assert!(
            collected.contains("OPTIONS"),
            "rejection must name OPTIONS: {}",
            collected
        );
    }

    #[test]
    fn test_parse_alter_fulltext_add_field_analyzer_rejected() {
        let sql = "ALTER FULLTEXT INDEX idx_article ADD FIELD summary ANALYZER 'standard'";

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_err());
        let collected = format!("{:?}", parser.take_errors());
        assert!(
            collected.contains("ANALYZER"),
            "rejection must name ANALYZER: {}",
            collected
        );
    }

    #[test]
    fn test_parse_search_statement() {
        let sql = r#"SEARCH INDEX idx_article MATCH 'database'
                     YIELD doc_id, score() AS s
                     LIMIT 10"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_search_statement_where_and_order() {
        let sql = r#"SEARCH INDEX idx_article MATCH 'database'
                     YIELD doc_id, title, score
                     WHERE score > 0.5 AND doc_id != 'art001'
                     ORDER BY score DESC
                     LIMIT 10"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse().expect("SEARCH should parse");
        let stmt = result.ast.stmt();
        let search = match stmt {
            crate::parser::ast::stmt::Stmt::Search(stmt) => stmt,
            _ => panic!("expected SEARCH statement"),
        };

        assert!(search.where_clause.is_some());
        assert!(search.where_clause.as_ref().unwrap().is_binary());
        assert!(search.order_clause.is_some());
        assert_eq!(search.order_clause.as_ref().unwrap().items.len(), 1);

        let yield_items = search.yield_clause.as_ref().expect("YIELD should parse");
        assert_eq!(yield_items.items.len(), 3);
    }

    #[test]
    fn test_parse_search_statement_highlight() {
        let sql = r#"SEARCH INDEX idx_article MATCH 'database'
                     YIELD highlight(title), matched_fields, score"#;

        let mut parser = Parser::new(sql);
        let result = parser.parse().expect("SEARCH should parse");
        let stmt = result.ast.stmt();
        match stmt {
            crate::parser::ast::stmt::Stmt::Search(search) => {
                let items = search
                    .yield_clause
                    .as_ref()
                    .expect("YIELD should parse")
                    .items
                    .clone();
                assert_eq!(items.len(), 3);
                assert!(items[0].expr.is_function());
                assert!(items[1].expr.is_function());
                assert!(items[2].expr.is_function());
            }
            _ => panic!("expected SEARCH statement"),
        }
    }

    #[test]
    fn test_parse_drop_index() {
        let sql = "DROP FULLTEXT INDEX idx_article";

        let mut parser = Parser::new(sql);
        let result = parser.parse();
        assert!(result.is_ok());
    }
}