Skip to content
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

sigma: support for parsing @sigma table factors and expressions #19

Merged
merged 3 commits into from
Mar 18, 2024
Merged
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
3 changes: 3 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ pub enum Expr {
Identifier(Ident),
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
CompoundIdentifier(Vec<Ident>),
/// A reference to a Sigma scalar value, e.g. `@sigma.my_parameter`.
SigmaParameter(Ident),
/// JSON access (postgres) eg: data->'tags'
JsonAccess {
left: Box<Expr>,
Expand Down Expand Up @@ -752,6 +754,7 @@ impl fmt::Display for Expr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expr::Identifier(s) => write!(f, "{s}"),
Expr::SigmaParameter(s) => write!(f, "@sigma.{s}"),
Expr::MapAccess { column, keys } => {
write!(f, "{column}")?;
for k in keys {
Expand Down
12 changes: 12 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@ pub enum TableFactor {
/// [Partition selection](https://dev.mysql.com/doc/refman/8.0/en/partitioning-selection.html), supported by MySQL.
partitions: Vec<Ident>,
},
/// A reference to an element in a Sigma workbook.
SigmaElement {
element: Ident,
alias: Option<TableAlias>,
},
Derived {
lateral: bool,
subquery: Box<Query>,
Expand Down Expand Up @@ -887,6 +892,13 @@ impl fmt::Display for TableFactor {
}
Ok(())
}
TableFactor::SigmaElement { element, alias } => {
write!(f, "@sigma.{element}")?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
}
Ok(())
}
TableFactor::Derived {
lateral,
subquery,
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ define_keywords!(
SETS,
SHARE,
SHOW,
SIGMA,
SIMILAR,
SKIP,
SLOW,
Expand Down
19 changes: 19 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,10 @@ impl<'a> Parser<'a> {
}, // End of Token::Word
// array `[1, 2, 3]`
Token::LBracket => self.parse_array_expr(false),
Token::AtSign if self.parse_keyword(Keyword::SIGMA) => {
self.expect_token(&Token::Period)?;
Ok(Expr::SigmaParameter(self.parse_identifier(false)?))
}
tok @ Token::Minus | tok @ Token::Plus => {
let op = if tok == Token::Plus {
UnaryOperator::Plus
Expand Down Expand Up @@ -7985,6 +7989,7 @@ impl<'a> Parser<'a> {
match &mut table_and_joins.relation {
TableFactor::Derived { alias, .. }
| TableFactor::Table { alias, .. }
| TableFactor::SigmaElement { alias, .. }
| TableFactor::Function { alias, .. }
| TableFactor::UNNEST { alias, .. }
| TableFactor::JsonTable { alias, .. }
Expand Down Expand Up @@ -8062,6 +8067,11 @@ impl<'a> Parser<'a> {
columns,
alias,
})
} else if self.parse_sigma_directive() {
self.expect_token(&Token::Period)?;
let element = self.parse_identifier(true)?;
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
Ok(TableFactor::SigmaElement { element, alias })
} else {
let name = self.parse_object_name(true)?;

Expand Down Expand Up @@ -8118,6 +8128,15 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_sigma_directive(&mut self) -> bool {
self.maybe_parse(|p| {
p.expect_token(&Token::AtSign)?;
p.expect_keyword(Keyword::SIGMA)?;
Ok(())
})
.is_some()
}

/// Parse a given table version specifier.
///
/// For now it only supports timestamp versioning for BigQuery and MSSQL dialects.
Expand Down
38 changes: 38 additions & 0 deletions tests/sqlparser_sigma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![warn(clippy::all)]

use sqlparser::ast::*;
use sqlparser::dialect::SnowflakeDialect;
use test_utils::*;

#[macro_use]
mod test_utils;

fn snowflake() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SnowflakeDialect {})],
options: None,
}
}
#[test]
fn parse_sigma() {
let sql = "SELECT my_column FROM @sigma.my_element WHERE my_column <> @sigma.param_filter";
let select = snowflake().verified_only_select(sql);
assert_eq!(
select.from,
vec![TableWithJoins {
relation: TableFactor::SigmaElement {
element: Ident::new("my_element"),
alias: None
},
joins: vec![]
}]
);
assert_eq!(
select.selection,
Some(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident::new("my_column"))),
op: BinaryOperator::NotEq,
right: Box::new(Expr::SigmaParameter(Ident::new("param_filter"))),
})
)
}
Loading