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

More MySQL rewrite rules #1661

Merged
merged 4 commits into from
May 2, 2024
Merged
Changes from 1 commit
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
42 changes: 38 additions & 4 deletions nexus/peer-mysql/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
use std::ops::ControlFlow;

use peer_ast::flatten_expr_to_in_list;
use serde_json::{self, Value as JsonValue};
use sqlparser::ast::{
visit_expressions_mut, visit_function_arg_mut, visit_relations_mut, Array, BinaryOperator,
DataType, Expr, FunctionArgExpr, Query,
DataType, Expr, FunctionArgExpr, Query, Value,
};

fn json_to_expr(val: JsonValue) -> Expr {
match val {
JsonValue::Null => Expr::Value(Value::Null),
JsonValue::Bool(x) => Expr::Value(Value::Boolean(x)),
JsonValue::Number(x) => Expr::Value(Value::Number(x.to_string(), false)),
JsonValue::String(x) => Expr::Value(Value::SingleQuotedString(x)),
JsonValue::Array(x) => Expr::Array(Array {
elem: x.into_iter().map(json_to_expr).collect::<Vec<_>>(),
named: false
}),
JsonValue::Object(x) => Expr::Cast {
data_type: DataType::JSON,
expr: Box::new(Expr::Value(Value::SingleQuotedString(JsonValue::Object(x).to_string()))),
format: None,
},
}
}

pub fn rewrite_query(peername: &str, query: &mut Query) {
visit_relations_mut(query, |table| {
// if peer name is first part of table name, remove first part
if peername.eq_ignore_ascii_case(&table.0[0].value) {
table.0.remove(0);
if table.0.len() > 1 {
// if peer name is first part of table name, remove first part
if peername.eq_ignore_ascii_case(&table.0[0].value) {
table.0.remove(0);
} else if table.0[0].value == "public" {
table.0.remove(0);
}
}
ControlFlow::<()>::Continue(())
});
Expand All @@ -34,6 +57,17 @@ pub fn rewrite_query(peername: &str, query: &mut Query) {
named: true,
};
*node = FunctionArgExpr::Expr(Expr::Array(rewritten_array));
} else if let Expr::Cast { data_type: DataType::JSONB, expr, .. } = arg_expr {
*node = match **expr {
Expr::Value(Value::SingleQuotedString(ref s)) => {
if let Ok(val) = serde_json::from_str::<JsonValue>(s) {
FunctionArgExpr::Expr(json_to_expr(val))
} else {
FunctionArgExpr::Expr((**expr).clone())
}
},
_ => FunctionArgExpr::Expr((**expr).clone())
};
}
}

Expand Down
Loading