Skip to content
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ Cargo.lock
.vscode

*.swp

# Mac DS_Store
**/*.DS_Store
93 changes: 93 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@
let mut expr = self.parse_prefix()?;
debug!("prefix: {:?}", expr);
loop {
expr = self.parse_within_filter(expr)?;
expr = self.parse_range_expr(expr)?;
let next_precedence = self.get_next_precedence()?;
debug!("next precedence: {:?}", next_precedence);
Expand All @@ -880,6 +881,30 @@
Ok(expr)
}

/// Parse within syntax like `select * from monitor where ts within '2025-01'`
fn parse_within_filter(&mut self, expr: Expr) -> Result<Expr, ParserError> {
if !self.parse_keyword(Keyword::WITHIN) {
return Ok(expr);
}
let Expr::Identifier(ident) = expr else {
return Ok(expr);
};
let value = self.parse_value()?;
Ok(Expr::Function(Function {
name: ObjectName(vec![Ident::new("within_filter")]),
args: vec![
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(ident))),
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(value))),
],
filter: None,
null_treatment: None,
over: None,
distinct: false,
special: false,
order_by: vec![],
}))
}

/// Parse Range clause with format `RANGE [ Duration literal | (INTERVAL [interval expr]) ] FILL [ NULL | PREV .....]`
fn parse_range_expr(&mut self, expr: Expr) -> Result<Expr, ParserError> {
let index = self.index;
Expand Down Expand Up @@ -10965,4 +10990,72 @@

assert!(Parser::parse_sql(&MySqlDialect {}, sql).is_err());
}

#[test]
fn test_within_filter() {
let sql = "select * from monitors where ts within '2024';";
let mut parser = Parser::new(&GenericDialect {}).try_with_sql(sql).unwrap();
let result = parser.parse_query().unwrap();
if let SetExpr::Select(select) = result.body.as_ref() {
let expected = &select.selection;
assert_eq!(
expected,
&Some(Expr::Function(Function {
name: ObjectName(vec![Ident::new("within_filter")]),
args: vec![
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(Ident::new(
"ts"
)))),
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
Value::SingleQuotedString("2024".to_string())
)))
],
filter: None,
null_treatment: None,
over: None,
distinct: false,
special: false,
order_by: vec![]
}))
);
} else {
unreachable!();
}
let sql = "select * from monitors where ts within '1915' and memory < 1024;";
let mut parser = Parser::new(&GenericDialect {}).try_with_sql(sql).unwrap();
let result = parser.parse_query().unwrap();
if let SetExpr::Select(select) = result.body.as_ref() {
let expected = &select.selection;
let within_expr = Expr::Function(Function {
name: ObjectName(vec![Ident::new("within_filter")]),
args: vec![
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(Ident::new("ts")))),
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
Value::SingleQuotedString("1915".to_string()),
))),
],
filter: None,
null_treatment: None,
over: None,
distinct: false,
special: false,
order_by: vec![],
});
let right = Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident::new("memory"))),
op: BinaryOperator::Lt,
right: Box::new(Expr::Value(Value::Number("1024".to_string(), false))),

Check failure on line 11047 in src/parser/mod.rs

View workflow job for this annotation

GitHub Actions / compile

mismatched types

Check failure on line 11047 in src/parser/mod.rs

View workflow job for this annotation

GitHub Actions / lint

mismatched types

Check failure on line 11047 in src/parser/mod.rs

View workflow job for this annotation

GitHub Actions / test (stable)

mismatched types

Check failure on line 11047 in src/parser/mod.rs

View workflow job for this annotation

GitHub Actions / test (beta)

mismatched types

Check failure on line 11047 in src/parser/mod.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

mismatched types
};
assert_eq!(
expected,
&Some(Expr::BinaryOp {
left: Box::new(within_expr),
op: BinaryOperator::And,
right: Box::new(right)
})
);
} else {
unreachable!();
}
}
}
Loading