Skip to content
Snippets Groups Projects
Commit 65f45353 authored by Denis Smirnov's avatar Denis Smirnov Committed by Igor Kuznetsov
Browse files

feat: introduce grammar for our SQL subset

parent dfd14dd6
Branches
Tags
1 merge request!1414sbroad import
......@@ -9,6 +9,8 @@ edition = "2018"
[dependencies]
decimal = "2.1.0"
fasthash = "0.4.0"
pest = "2.0"
pest_derive = "2.0"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
sqlparser = "0.11.0"
......
Command = _{ SOI ~ Query ~ EOF }
Keys = { ^"all" | ^"and" | ^"as" | ^"false" | ^"from"
| ^"in" | ^"inner" | ^"insert" | ^"into" | ^"join" | ^"null"
| ^"on" | ^"or" | ^"row" | ^"select" | ^"true" | ^"union"
| ^"where" | ^"values" }
Value = _{ Row | Bool | Null | QuotedName | Number | String }
Bool = @{ ^"true" | ^"false" }
Null = @{ ^"null" }
Number = @{ Int ~ ("." ~ ASCII_DIGIT*)? ~ (^"e" ~ Int)? }
Int = @{ ("+" | "-")? ~ ASCII_DIGIT+ }
String = @{ !(Keys) ~ ('a'..'z' | "_" | Number)+ }
QuotedName = @{ "\"" ~ String ~ "\"" ~ ("." ~ "\"" ~ String ~ "\"")? }
Row = { ("(" ~ Value ~ ("," ~ Value)* ~ ")") | (^"row(" ~ Value ~ ("," ~ Value)* ~ ")") }
BinaryOp = _{ BoolOp }
BoolOp = { In | NotEq | GtEq | LtEq | Eq | Gt | Lt | And | Or }
And = @{ ^"and" }
Eq = @{ "=" }
In = @{ ^"in" }
Gt = @{ ">" }
GtEq = @{ ">=" }
Lt = @{ "<" }
LtEq = @{ "<=" }
NotEq = @{ "!=" | "<>" }
Or = @{ ^"or" }
PrimaryExpr = _{ SubQuery | Value }
Expr = { PrimaryExpr ~ BinaryOp ~ (Expr | PrimaryExpr) }
ParenthesesExpr = { Expr | ("(" ~ Expr ~ ")") }
ChainExpr = { ParenthesesExpr ~ BinaryOp ~ ChainExpr | ("(" ~ ChainExpr ~ ")") | ParenthesesExpr }
Query = _{ Select | UnionAll | Values | Insert }
Select = {
^"select" ~ Projection ~ ^"from" ~ Scan ~
(((^"inner" ~ ^"join") | ^"join") ~ InnerJoin)? ~
(^"on" ~ Condition)? ~ (^"where" ~ Selection)?
}
Projection = { (Row | Column | SubQuery ) ~ ("," ~ (Row | Column | SubQuery ))*? }
Column = { (Asterisk | Alias | Value) }
Alias = {Value ~ ^"as" ~ (String | QuotedName) }
Asterisk = @{ "*" }
Selection = { ChainExpr* }
Scan = { SubQuery | Table }
Table = @{ String | QuotedName }
InnerJoin = { Scan }
Condition = { Selection }
UnionAll = { (SubQuery | Select) ~ ^"union" ~ ^"all" ~ (UnionAll | SubQuery | Select) }
SubQuery = { "(" ~ (UnionAll | Select) ~ ")" ~ (^"as" ~ (String | QuotedName))? }
Insert = { ^"insert" ~ ^"into" ~ Table ~ Values }
Values = { ^"values" ~ Row ~ ("," ~ Row)*?}
EOF = { EOI | ";" }
WHITESPACE = _{ " " | "\t" | "\n" | "\n\r" }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment