Skip to content

Implement explain (query plan) statements, ala sqlite #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Language/SQL/SimpleSQL/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ statementWithoutSemicolon =
,rollback
,grant
,revoke
,explain
,SelectStatement <$> queryExpr
]

Expand Down Expand Up @@ -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



{-
----------------------------
Expand Down
4 changes: 4 additions & 0 deletions Language/SQL/SimpleSQL/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Language/SQL/SimpleSQL/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/Language/SQL/SimpleSQL/SQL2011Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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


{-
Expand Down