Skip to content

Commit

Permalink
feat : implement basic translate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ChobobDev committed Dec 16, 2023
1 parent c40eddc commit 63f0bbe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/src/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
super::{
Aggregate, AstLiteral, BinaryOperator, DataType, DateTimeField, Function, Query, ToSql,
Aggregate, AstLiteral, BinaryOperator, DataType, DateTimeField, Function, Query,ToSql,
ToSqlUnquoted, UnaryOperator,
},
serde::{Deserialize, Serialize},
Expand Down Expand Up @@ -77,6 +77,7 @@ pub enum Expr {
leading_field: Option<DateTimeField>,
last_field: Option<DateTimeField>,
},
Array(Vec<Expr>)
}

impl ToSql for Expr {
Expand Down Expand Up @@ -239,6 +240,10 @@ impl Expr {
.join("");
format!("{obj}{indexes}")
}
Expr::Array(elem)=>{
let elem = elem.iter().map(|e| format!("[{}]", e.to_sql_with(quoted))).collect::<Vec<_>>().join("");
format!("{elem}")
}
Expr::Subquery(query) => format!("({})", query.to_sql()),
Expr::Interval {
expr,
Expand Down Expand Up @@ -659,6 +664,16 @@ mod tests {
.to_sql()
);

assert_eq!(
r#"['GlueSQL','Rust']"#,
Expr::Array {
elem: vec![
Expr::Literal(AstLiteral::QuotedString("GlueSQL".to_owned())),
Expr::Literal(AstLiteral::QuotedString("Rust".to_owned()))
]
}.to_sql()
);

assert_eq!(
r#"INTERVAL "col1" + 3 DAY"#,
&Expr::Interval {
Expand Down
3 changes: 3 additions & 0 deletions core/src/plan/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ impl<'a> From<&'a Expr> for PlanExpr<'a> {
let exprs = indexes.iter().chain(once(obj.as_ref())).collect();
PlanExpr::MultiExprs(exprs)
}
Expr::Array(array) => {
//fix
},
Expr::Function(function) => PlanExpr::MultiExprs(function.as_exprs().collect()),
Expr::Subquery(subquery) | Expr::Exists { subquery, .. } => PlanExpr::Query(subquery),
Expr::InSubquery {
Expand Down
7 changes: 7 additions & 0 deletions core/src/plan/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ pub trait Planner<'a> {
let obj = Box::new(self.subquery_expr(outer_context, *obj));
Expr::ArrayIndex { obj, indexes }
}
Expr::Array(elem)=>{
let elem = elem
.into_iter()
.map(|expr| self.subquery_expr(outer_context.as_ref().map(Rc::clone), expr))
.collect();
Expr::Array(elem)
}
Expr::Interval {
expr,
leading_field,
Expand Down
5 changes: 5 additions & 0 deletions core/src/translate/expr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use sqlparser::ast::Array;

use {
super::{
ast_literal::{translate_ast_literal, translate_datetime_field},
Expand Down Expand Up @@ -167,6 +169,9 @@ pub fn translate_expr(sql_expr: &SqlExpr) -> Result<Expr> {
obj: translate_expr(obj).map(Box::new)?,
indexes: indexes.iter().map(translate_expr).collect::<Result<_>>()?,
}),
SqlExpr::Array(Array { elem, named }) => Ok(Expr::Array(
elem.iter().map(translate_expr).collect::<Result<_>>()?,
)),
SqlExpr::Position { expr, r#in } => translate_position(expr, r#in),
SqlExpr::Interval(SqlInterval {
value,
Expand Down

0 comments on commit 63f0bbe

Please sign in to comment.