File tree 4 files changed +46
-0
lines changed
4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -3111,6 +3111,11 @@ pub enum Statement {
3111
3111
analyze : bool ,
3112
3112
// Display additional information regarding the plan.
3113
3113
verbose : bool ,
3114
+ /// `EXPLAIN QUERY PLAN`
3115
+ /// Display the query plan without running the query.
3116
+ ///
3117
+ /// [SQLite](https://sqlite.org/lang_explain.html)
3118
+ query_plan : bool ,
3114
3119
/// A SQL query that specifies what to explain
3115
3120
statement : Box < Statement > ,
3116
3121
/// Optional output format of explain
@@ -3302,12 +3307,16 @@ impl fmt::Display for Statement {
3302
3307
describe_alias,
3303
3308
verbose,
3304
3309
analyze,
3310
+ query_plan,
3305
3311
statement,
3306
3312
format,
3307
3313
options,
3308
3314
} => {
3309
3315
write ! ( f, "{describe_alias} " ) ?;
3310
3316
3317
+ if * query_plan {
3318
+ write ! ( f, "QUERY PLAN " ) ?;
3319
+ }
3311
3320
if * analyze {
3312
3321
write ! ( f, "ANALYZE " ) ?;
3313
3322
}
Original file line number Diff line number Diff line change @@ -572,6 +572,7 @@ define_keywords!(
572
572
PERSISTENT ,
573
573
PIVOT ,
574
574
PLACING ,
575
+ PLAN ,
575
576
PLANS ,
576
577
POLICY ,
577
578
PORTION ,
Original file line number Diff line number Diff line change @@ -8662,6 +8662,7 @@ impl<'a> Parser<'a> {
8662
8662
) -> Result < Statement , ParserError > {
8663
8663
let mut analyze = false ;
8664
8664
let mut verbose = false ;
8665
+ let mut query_plan = false ;
8665
8666
let mut format = None ;
8666
8667
let mut options = None ;
8667
8668
@@ -8672,6 +8673,8 @@ impl<'a> Parser<'a> {
8672
8673
&& self . peek_token ( ) . token == Token :: LParen
8673
8674
{
8674
8675
options = Some ( self . parse_utility_options ( ) ?)
8676
+ } else if self . parse_keywords ( & [ Keyword :: QUERY , Keyword :: PLAN ] ) {
8677
+ query_plan = true ;
8675
8678
} else {
8676
8679
analyze = self . parse_keyword ( Keyword :: ANALYZE ) ;
8677
8680
verbose = self . parse_keyword ( Keyword :: VERBOSE ) ;
@@ -8688,6 +8691,7 @@ impl<'a> Parser<'a> {
8688
8691
describe_alias,
8689
8692
analyze,
8690
8693
verbose,
8694
+ query_plan,
8691
8695
statement : Box :: new ( statement) ,
8692
8696
format,
8693
8697
options,
Original file line number Diff line number Diff line change @@ -4295,6 +4295,7 @@ fn run_explain_analyze(
4295
4295
describe_alias : _,
4296
4296
analyze,
4297
4297
verbose,
4298
+ query_plan,
4298
4299
statement,
4299
4300
format,
4300
4301
options,
@@ -4303,6 +4304,7 @@ fn run_explain_analyze(
4303
4304
assert_eq ! ( analyze, expected_analyze) ;
4304
4305
assert_eq ! ( format, expected_format) ;
4305
4306
assert_eq ! ( options, exepcted_options) ;
4307
+ assert ! ( !query_plan) ;
4306
4308
assert_eq ! ( "SELECT sqrt(id) FROM foo" , statement. to_string( ) ) ;
4307
4309
}
4308
4310
_ => panic ! ( "Unexpected Statement, must be Explain" ) ,
@@ -4417,6 +4419,36 @@ fn parse_explain_analyze_with_simple_select() {
4417
4419
) ;
4418
4420
}
4419
4421
4422
+ #[ test]
4423
+ fn parse_explain_query_plan ( ) {
4424
+ match all_dialects ( ) . verified_stmt ( "EXPLAIN QUERY PLAN SELECT sqrt(id) FROM foo" ) {
4425
+ Statement :: Explain {
4426
+ query_plan,
4427
+ analyze,
4428
+ verbose,
4429
+ statement,
4430
+ ..
4431
+ } => {
4432
+ assert ! ( query_plan) ;
4433
+ assert ! ( !analyze) ;
4434
+ assert ! ( !verbose) ;
4435
+ assert_eq ! ( "SELECT sqrt(id) FROM foo" , statement. to_string( ) ) ;
4436
+ }
4437
+ _ => unreachable ! ( ) ,
4438
+ }
4439
+
4440
+ // omit QUERY PLAN should be good
4441
+ all_dialects ( ) . verified_stmt ( "EXPLAIN SELECT sqrt(id) FROM foo" ) ;
4442
+
4443
+ // missing PLAN keyword should return error
4444
+ assert_eq ! (
4445
+ ParserError :: ParserError ( "Expected: end of statement, found: SELECT" . to_string( ) ) ,
4446
+ all_dialects( )
4447
+ . parse_sql_statements( "EXPLAIN QUERY SELECT sqrt(id) FROM foo" )
4448
+ . unwrap_err( )
4449
+ ) ;
4450
+ }
4451
+
4420
4452
#[ test]
4421
4453
fn parse_named_argument_function ( ) {
4422
4454
let sql = "SELECT FUN(a => '1', b => '2') FROM foo" ;
You can’t perform that action at this time.
0 commit comments