@@ -5,6 +5,7 @@ use crate::splitter::common::SplitterResult;
55use super :: {
66 Splitter ,
77 common:: { parenthesis, unknown} ,
8+ ddl:: create,
89} ;
910
1011pub ( crate ) fn cte ( p : & mut Splitter ) -> SplitterResult {
@@ -39,6 +40,38 @@ pub(crate) fn cte(p: &mut Splitter) -> SplitterResult {
3940 Ok ( ( ) )
4041}
4142
43+ /// `EXPLAIN [ ANALYZE ] [ VERBOSE ] <statement>` and
44+ /// `EXPLAIN ( option [, ...] ) <statement>`
45+ ///
46+ /// EXPLAIN is a prefix to the statement it explains, so after consuming the
47+ /// prefix we delegate to the splitter function of the inner statement instead
48+ /// of treating its leading keyword as a new statement start.
49+ pub ( crate ) fn explain ( p : & mut Splitter ) -> SplitterResult {
50+ p. expect ( SyntaxKind :: EXPLAIN_KW ) ?;
51+
52+ // EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON, ...) <statement>
53+ if p. current ( ) == SyntaxKind :: L_PAREN {
54+ parenthesis ( p) ?;
55+ } else {
56+ // legacy EXPLAIN [ANALYZE | ANALYSE] [VERBOSE] <statement>
57+ p. eat ( SyntaxKind :: ANALYZE_KW ) ?;
58+ p. eat ( SyntaxKind :: ANALYSE_KW ) ?;
59+ p. eat ( SyntaxKind :: VERBOSE_KW ) ?;
60+ }
61+
62+ match p. current ( ) {
63+ SyntaxKind :: WITH_KW => cte ( p) ,
64+ SyntaxKind :: SELECT_KW => select ( p) ,
65+ SyntaxKind :: INSERT_KW => insert ( p) ,
66+ SyntaxKind :: UPDATE_KW => update ( p) ,
67+ SyntaxKind :: DELETE_KW => delete ( p) ,
68+ // EXPLAIN CREATE TABLE AS / CREATE MATERIALIZED VIEW AS
69+ SyntaxKind :: CREATE_KW => create ( p) ,
70+ // MERGE, VALUES, EXECUTE, DECLARE
71+ _ => unknown ( p, & [ ] ) ,
72+ }
73+ }
74+
4275pub ( crate ) fn select ( p : & mut Splitter ) -> SplitterResult {
4376 p. expect ( SyntaxKind :: SELECT_KW ) ?;
4477
0 commit comments