1
Fork 0

Implement explain (query plan) statements, ala sqlite

This commit is contained in:
prescientmoon 2025-01-10 18:48:30 +01:00
parent c842cf50b6
commit ab82249523
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
4 changed files with 36 additions and 0 deletions

View file

@ -1712,6 +1712,7 @@ statementWithoutSemicolon =
,rollback
,grant
,revoke
,explain
,SelectStatement <$> queryExpr
]
@ -2168,6 +2169,14 @@ privilegeObject = choice
,optional (keyword_ "table") >> PrivTable <$> names "table name"
]
explain :: Parser Statement
explain = do
keyword_ "explain" >>
Explain
<$> isJust <$> optional (keywords_ ["query", "plan"])
<*> statementWithoutSemicolon
{-
----------------------------

View file

@ -681,6 +681,10 @@ statement _ (RevokeRole ao rs trs db) =
adminOptFor AdminOptionFor = texts ["admin","option","for"]
adminOptFor NoAdminOptionFor = mempty
statement dialect (Explain explainQueryPlan inner) =
pretty "explain"
<+> (if explainQueryPlan then pretty "query plan" else mempty)
<+> statement dialect inner
statement _ (StatementComment cs) = vsep $ map comment cs
statement _ EmptyStatement = mempty

View file

@ -523,10 +523,20 @@ data Statement =
| SetNames
| SetTransform
| SetCollation -}
| Explain
-- When enabled, this corresponds to the higher level
-- "EXPLAIN QUERY PLAN" statement
{esExplainQueryPlan :: Bool
-- Technically a sqlite "explain" cannot be followed by another explain
-- statement, but I think this representation is good enough...
,esStatement :: Statement
}
| StatementComment [Comment]
| EmptyStatement
deriving (Eq,Show,Read,Data,Typeable)
data DropBehaviour =
Restrict
| Cascade
@ -720,6 +730,7 @@ data PrivilegeAction =
| PrivExecute
deriving (Eq,Show,Read,Data,Typeable)
-- | Comment. Useful when generating SQL code programmatically. The
-- parser doesn't produce these.
newtype Comment = BlockComment Text

View file

@ -775,6 +775,18 @@ defintely skip
$ ColDefaultClause
$ DefaultClause $ NumLit "2"]]
False
,testStatement ansi2011
"explain create table t (a int);"
$ Explain False
$ CreateTable [Name Nothing "t"]
[TableColumnDef $ ColumnDef (Name Nothing "a") (Just (TypeName [Name Nothing "int"])) []]
False
,testStatement ansi2011
"explain query plan create table t (a int);"
$ Explain True
$ CreateTable [Name Nothing "t"]
[TableColumnDef $ ColumnDef (Name Nothing "a") (Just (TypeName [Name Nothing "int"])) []]
False
{-