use serde_json::Value;
use tracing;
use super::literal::{fix_literal_type_aliases, repair_expression_value};
const PRED_KEYS: &[&str] = &["not", "exists", "and", "or"];
const PREDICATE_TYPES: &[&str] = &["like", "in", "between", "is_null", "exists"];
#[must_use]
pub(super) fn repair_predicate_type(val: &mut Value) -> bool {
let obj = match val.as_object_mut() {
Some(o) => o,
None => return false,
};
let existing_type = obj.get("type").and_then(|t| t.as_str()).unwrap_or("");
let has_valid_type = !existing_type.is_empty()
&& existing_type != "and"
&& existing_type != "or"
&& existing_type != "not"
&& existing_type != "exists";
for &key in PRED_KEYS {
if obj.contains_key(key) {
if has_valid_type {
continue;
}
if let Some(v) = obj.get(key)
&& (v.is_null()
|| v.as_str().is_some()
|| (v.as_object().is_some_and(|o| o.is_empty())))
{
continue;
}
if let Some(v) = obj.remove(key) {
obj.insert("type".to_owned(), Value::String(key.to_owned()));
if key == "not" || key == "exists" {
obj.insert("child".to_owned(), v);
} else {
obj.insert("left".to_owned(), v);
if !obj.contains_key("right") {
obj.insert("right".to_owned(), Value::Null);
}
}
return true;
}
}
}
if let Some(type_str) = obj.get("type").and_then(|t| t.as_str())
&& (type_str == "and" || type_str == "or")
&& obj.contains_key("op")
{
obj.insert("type".to_owned(), Value::String("comparison".to_owned()));
return true;
}
if obj.contains_key("type") {
return false;
}
if obj.contains_key("left") && obj.contains_key("op") {
obj.insert("type".to_owned(), Value::String("comparison".to_owned()));
return true;
}
for &key in PRED_KEYS {
if let Some(v) = obj.remove(key) {
obj.insert("type".to_owned(), Value::String(key.to_owned()));
if key == "not" || key == "exists" {
if key == "not"
&& let Some(child_obj) = v.as_object()
&& child_obj.contains_key("child")
{
if let Some(child_val) = child_obj.get("child").cloned() {
obj.insert("child".to_owned(), child_val);
}
} else {
obj.insert("child".to_owned(), v);
}
} else {
obj.insert("left".to_owned(), v);
if !obj.contains_key("right") {
obj.insert("right".to_owned(), Value::Null);
}
}
return true;
}
}
false
}
#[must_use]
pub(super) fn unwrap_array_sides(val: &mut Value) -> bool {
let obj = match val.as_object_mut() {
Some(o) => o,
None => return false,
};
let pred_type = obj
.get("type")
.and_then(|t| t.as_str())
.unwrap_or("")
.to_owned();
let mut changed = false;
if pred_type == "and" || pred_type == "or" {
let split: Option<(Value, Value)> = {
let arr = obj.get("left").and_then(|v| v.as_array());
match arr {
Some(arr) if arr.len() >= 2 && (obj.get("right").is_none_or(|v| v.is_null())) => {
Some((arr[0].clone(), arr[1].clone()))
}
_ => None,
}
};
if let Some((new_left, new_right)) = split {
obj.insert("left".to_owned(), new_left);
obj.insert("right".to_owned(), new_right);
changed = true;
}
for side in &["left", "right"] {
changed |= unwrap_side(obj, side);
}
}
if pred_type == "not" {
changed |= unwrap_side(obj, "child");
}
if pred_type == "comparison"
|| pred_type == "between"
|| pred_type == "in"
|| pred_type == "like"
|| pred_type == "is_null"
{
for field in &["left", "right", "expr", "low", "high"] {
changed |= unwrap_array_field(obj, field);
}
if pred_type == "comparison" {
changed |= unwrap_operator_field(obj);
}
}
changed
}
fn unwrap_side(obj: &mut serde_json::Map<String, Value>, side: &str) -> bool {
if let Some(arr) = obj.get(side).and_then(|v| v.as_array()) {
if arr.is_empty() {
obj.remove(side);
true
} else {
obj.insert(side.to_string(), arr[0].clone());
true
}
} else {
false
}
}
fn unwrap_array_field(obj: &mut serde_json::Map<String, Value>, field: &str) -> bool {
if let Some(arr) = obj.get(field).and_then(|v| v.as_array())
&& !arr.is_empty()
{
obj.insert((*field).to_string(), arr[0].clone());
return true;
}
false
}
#[must_use]
fn unwrap_operator_field(obj: &mut serde_json::Map<String, Value>) -> bool {
let arr = match obj.get("op").and_then(|v| v.as_array()) {
Some(a) if !a.is_empty() => a.clone(),
_ => return false,
};
let op_name = arr[0].as_str().or_else(|| {
arr[0]
.as_object()
.and_then(|o| o.get("name").and_then(|v| v.as_str()))
});
match op_name {
Some("between" | "not_between") => {
let expr = obj
.get("left")
.cloned()
.or_else(|| obj.get("expr").cloned())
.unwrap_or(Value::Null);
let (low, high) = arr[0]
.as_object()
.and_then(|o| o.get("args"))
.and_then(|a| a.as_array())
.map(|a| {
let low = a.first().cloned().unwrap_or(Value::Null);
let high = a.get(1).cloned().unwrap_or(Value::Null);
(low, high)
})
.unwrap_or((Value::Null, Value::Null));
obj.clear();
obj.insert("type".to_owned(), Value::String("between".to_owned()));
obj.insert("expr".to_owned(), expr);
obj.insert("low".to_owned(), low);
obj.insert("high".to_owned(), high);
true
}
Some(name) => {
obj.insert("op".to_owned(), Value::String(name.to_owned()));
true
}
None => {
obj.insert("op".to_owned(), Value::String("eq".to_owned()));
true
}
}
}
#[must_use]
pub(super) fn inject_missing_right(val: &mut Value) -> bool {
let obj = match val.as_object_mut() {
Some(o) => o,
None => return false,
};
let pred_type = obj.get("type").and_then(|t| t.as_str()).unwrap_or("");
if pred_type == "comparison"
&& !obj.contains_key("right")
&& obj.contains_key("left")
&& obj.contains_key("op")
{
obj.insert(
"right".to_owned(),
serde_json::json!({"type": "literal", "value": null, "data_type": "null"}),
);
return true;
}
false
}
#[must_use]
pub(super) fn simplify_single_child(val: &mut Value) -> bool {
let obj = match val.as_object_mut() {
Some(o) => o,
None => return false,
};
let pred_type = obj.get("type").and_then(|t| t.as_str()).unwrap_or("");
if (pred_type == "and" || pred_type == "or")
&& obj.contains_key("left")
&& (obj.get("right").is_none_or(|v| v.is_null()))
&& let Some(left_val) = obj.remove("left")
{
obj.remove("right");
*val = left_val;
return true;
}
false
}
#[must_use]
pub(super) fn normalize_predicate(val: &mut Value) -> bool {
let mut changed = false;
changed |= fix_literal_type_aliases(val);
if let Some(obj) = val.as_object()
&& obj.get("type").and_then(|t| t.as_str()) == Some("expr")
&& obj.contains_key("expression")
&& let Some(inner) = obj.get("expression").cloned()
{
let op = obj
.get("op")
.and_then(|v| v.as_str())
.unwrap_or("gt")
.to_owned();
let right = obj.get("right").cloned().unwrap_or_else(
|| serde_json::json!({"type": "literal", "value": 0, "data_type": "int"}),
);
let comparison = serde_json::json!({
"type": "comparison",
"left": inner,
"op": op,
"right": right,
});
*val = comparison;
changed = true;
changed |= normalize_predicate(val);
return changed;
}
if let Some(obj) = val.as_object()
&& obj.get("type").and_then(|t| t.as_str()) == Some("function_call")
{
let comparison = serde_json::json!({
"type": "comparison",
"left": val.clone(),
"op": "gt",
"right": {"type": "literal", "value": 0, "data_type": "int"}
});
*val = comparison;
changed = true;
changed |= normalize_predicate(val);
return changed;
}
if let Some(obj) = val.as_object()
&& obj.get("type").and_then(|t| t.as_str()) == Some("none")
&& obj.len() == 1
{
*val = serde_json::json!({
"type": "comparison",
"left": {"type": "literal", "value": 1, "data_type": "int"},
"op": "eq",
"right": {"type": "literal", "value": 0, "data_type": "int"}
});
changed = true;
return changed;
}
if let Some(obj) = val.as_object()
&& obj.get("type").and_then(|t| t.as_str()) == Some("case")
{
let comparison = serde_json::json!({
"type": "comparison",
"left": val.clone(),
"op": "ne",
"right": {"type": "literal", "value": null, "data_type": "null"}
});
*val = comparison;
changed = true;
return changed;
}
if let Some(obj) = val.as_object()
&& obj.get("type").and_then(|t| t.as_str()) == Some("distinct")
&& let Some(on_arr) = obj.get("on").and_then(|v| v.as_array())
&& !on_arr.is_empty()
{
*val = on_arr[0].clone();
changed = true;
changed |= normalize_predicate(val);
return changed;
}
if let Some(obj) = val.as_object()
&& let Some(type_str) = obj.get("type").and_then(|t| t.as_str())
&& type_str.starts_with("comparison")
&& type_str != "comparison"
{
let mut tmp = val.clone();
if let Some(tmp_obj) = tmp.as_object_mut() {
tmp_obj.insert("type".to_owned(), Value::String("comparison".to_owned()));
}
*val = tmp;
changed = true;
changed |= normalize_predicate(val);
return changed;
}
changed |= repair_predicate_type(val);
changed |= unwrap_array_sides(val);
if let Some(obj) = val.as_object_mut() {
let pred_type = obj
.get("type")
.and_then(|t| t.as_str())
.unwrap_or("")
.to_owned();
if pred_type == "comparison"
|| pred_type == "between"
|| pred_type == "in"
|| pred_type == "like"
|| pred_type == "is_null"
{
for field in &["left", "right", "expr", "low", "high"] {
if let Some(v) = obj.get_mut(*field) {
changed |= repair_expression_value(v);
}
}
}
if (pred_type == "like" || pred_type == "is_null" || pred_type == "between")
&& obj.contains_key("left")
&& !obj.contains_key("expr")
&& let Some(left) = obj.remove("left")
{
obj.insert("expr".to_owned(), left);
changed = true;
}
if pred_type == "like" && !obj.contains_key("pattern") {
if let Some(expr) = obj.get("expr").and_then(|v| v.as_object()) {
let table = expr.get("table").and_then(|v| v.as_str()).unwrap_or("");
let column = expr.get("column").and_then(|v| v.as_str()).unwrap_or("");
let known_tables = ["users", "orders", "products", "order_items", "employees"];
if !known_tables.contains(&table) && !column.is_empty() {
let pattern = format!("%{column}");
let corrected_expr = if table.is_empty() || known_tables.contains(&table) {
Value::Object(expr.clone())
} else {
serde_json::json!({"type": "column_ref", "column": table})
};
obj.insert("pattern".to_owned(), Value::String(pattern));
obj.insert("expr".to_owned(), corrected_expr);
changed = true;
}
}
if !obj.contains_key("pattern") {
obj.insert("pattern".to_owned(), Value::String("%".to_owned()));
changed = true;
}
}
if pred_type == "between" {
if !obj.contains_key("low") {
obj.insert(
"low".to_owned(),
serde_json::json!({"type": "literal", "value": 0, "data_type": "int"}),
);
changed = true;
}
if !obj.contains_key("high") {
obj.insert(
"high".to_owned(),
serde_json::json!({"type": "literal", "value": null, "data_type": "null"}),
);
changed = true;
}
}
if pred_type == "comparison" {
let left_is_pred = obj
.get("left")
.and_then(|v| v.as_object())
.and_then(|o| o.get("type"))
.and_then(|t| t.as_str())
.is_some_and(|t| PREDICATE_TYPES.contains(&t));
if left_is_pred && let Some(left_val) = obj.remove("left") {
*val = left_val;
changed = true;
changed |= normalize_predicate(val);
return changed;
}
let right_is_pred = obj
.get("right")
.and_then(|v| v.as_object())
.and_then(|o| o.get("type"))
.and_then(|t| t.as_str())
.is_some_and(|t| PREDICATE_TYPES.contains(&t));
if right_is_pred
&& let Some(right_val) = obj.remove("right")
&& let Some(right_obj) = right_val.as_object()
{
let right_type = right_obj
.get("type")
.and_then(|t| t.as_str())
.unwrap_or("like");
let left_expr = obj.remove("left").or_else(|| obj.remove("expr"));
obj.clear();
match right_type {
"like" => {
obj.insert("type".to_owned(), Value::String("like".to_owned()));
let expr = left_expr.unwrap_or(Value::Null);
let pattern = right_obj
.get("pattern")
.cloned()
.unwrap_or(Value::String("%".to_owned()));
obj.insert("expr".to_owned(), expr);
obj.insert("pattern".to_owned(), pattern);
}
"is_null" => {
obj.insert("type".to_owned(), Value::String("is_null".to_owned()));
let expr = left_expr.unwrap_or_else(|| {
right_obj.get("expr").cloned().unwrap_or(Value::Null)
});
obj.insert("expr".to_owned(), expr);
}
other => {
obj.insert("type".to_owned(), Value::String(other.to_owned()));
if let Some(expr) = left_expr {
obj.insert("expr".to_owned(), expr);
}
for (k, v) in right_obj.iter() {
if k != "type" && !obj.contains_key(k) {
obj.insert(k.clone(), v.clone());
}
}
}
}
changed = true;
changed |= normalize_predicate(val);
return changed;
}
if !obj.contains_key("op") {
let promoted = obj
.get("left")
.and_then(|v| v.as_object())
.and_then(|left_obj| {
let op_val = left_obj.get("op").and_then(|v| v.as_str())?;
if !left_obj.contains_key("right") {
return None;
}
Some((
op_val.to_owned(),
left_obj.get("right").cloned().unwrap_or(Value::Null),
))
});
if let Some((op_str, right_val)) = promoted {
if let Some(left) = obj.get_mut("left").and_then(|v| v.as_object_mut()) {
left.remove("op");
left.remove("right");
}
obj.insert("op".to_owned(), Value::String(op_str));
obj.insert("right".to_owned(), right_val);
changed = true;
}
}
if matches!(obj.get("op"), Some(Value::String(s)) if s.is_empty() || s == "unknown")
|| obj.get("op").is_some_and(|v| v.is_null())
{
obj.insert("op".to_owned(), Value::String("eq".to_owned()));
changed = true;
}
if let Some(op_val) = obj.get("op").and_then(|v| v.as_str()) {
if op_val == "is_null" || op_val == "is null" {
let expr = obj
.remove("left")
.or_else(|| obj.remove("expr"))
.unwrap_or(Value::Null);
obj.clear();
obj.insert("type".to_owned(), Value::String("is_null".to_owned()));
obj.insert("expr".to_owned(), expr);
changed = true;
} else if op_val == "is_not_null"
|| op_val == "is not null"
|| op_val == "isnotnull"
{
let expr = obj
.remove("left")
.or_else(|| obj.remove("expr"))
.unwrap_or(Value::Null);
obj.clear();
obj.insert("type".to_owned(), Value::String("not".to_owned()));
obj.insert(
"child".to_owned(),
serde_json::json!({
"type": "is_null",
"expr": expr
}),
);
changed = true;
} else if op_val == "like" || op_val == "ilike" {
let expr = obj
.remove("left")
.or_else(|| obj.remove("expr"))
.unwrap_or(Value::Null);
let pattern = obj
.remove("right")
.and_then(|r| r.get("value").cloned())
.and_then(|v| v.as_str().map(|s| s.to_owned()))
.unwrap_or_else(|| "%".to_owned());
obj.clear();
obj.insert("type".to_owned(), Value::String("like".to_owned()));
obj.insert("expr".to_owned(), expr);
obj.insert("pattern".to_owned(), Value::String(pattern));
changed = true;
}
}
}
if pred_type == "in"
&& let Some(target) = obj.get("target")
&& target.is_object()
&& !target.as_object().is_some_and(|o| o.contains_key("select"))
{
let wrapped = serde_json::json!([target.clone()]);
obj.insert("target".to_owned(), wrapped);
changed = true;
}
changed |= inject_missing_right(val);
changed |= simplify_single_child(val);
}
if let Some(obj) = val.as_object_mut() {
let pred_type = obj
.get("type")
.and_then(|t| t.as_str())
.unwrap_or("")
.to_owned();
match pred_type.as_str() {
"and" | "or" => {
for side in &["left", "right"] {
if let Some(v) = obj.get_mut(*side) {
changed |= normalize_predicate(v);
}
}
}
"not" => {
if let Some(v) = obj.get_mut("child") {
changed |= normalize_predicate(v);
}
}
"comparison" | "between" | "in" | "like" | "is_null" | "exists" | "true" | "false" => {}
other => {
tracing::debug!("normalize_predicate: unknown predicate type `{other}`");
}
}
}
changed
}