use serde_json::Value;
const OP_UNION_ALL: &str = "union_all";
const OP_UNION: &str = "union";
const OP_INTERSECT: &str = "intersect";
const OP_EXCEPT: &str = "except";
#[must_use]
pub fn canonical_op(raw: &str) -> Option<&'static str> {
let folded: String = raw
.chars()
.map(|c| {
if c == '_' || c.is_whitespace() {
' '
} else {
c.to_ascii_lowercase()
}
})
.collect();
let collapsed: String = folded.split_whitespace().collect::<Vec<_>>().join(" ");
match collapsed.as_str() {
"union all" | "unionall" => Some(OP_UNION_ALL),
"union" => Some(OP_UNION),
"intersect" | "intersection" => Some(OP_INTERSECT),
"except" | "minus" | "difference" => Some(OP_EXCEPT),
_ => None,
}
}
#[must_use]
pub fn normalize(val: &mut Value) -> bool {
let Some(obj) = val.as_object_mut() else {
return false;
};
let mut changed = false;
for op_key in [
"union_all",
"unionall",
"union",
"intersect",
"intersection",
"except",
"minus",
] {
if let Some(operand) = obj.remove(op_key) {
let op = canonical_op(op_key).unwrap_or(OP_UNION_ALL);
if !obj.contains_key("set_operation") {
obj.insert(
"set_operation".to_owned(),
serde_json::json!({"operation": op, "right": operand}),
);
changed = true;
}
}
}
if let Some(set_op_val) = obj.get_mut("set_operation") {
changed |= normalize_set_operation(set_op_val);
}
changed
}
fn normalize_set_operation(set_op_val: &mut Value) -> bool {
if let Some(s) = set_op_val.as_str() {
if let Some(canonical) = canonical_op(s) {
*set_op_val = Value::String(canonical.to_owned());
return true;
}
return false;
}
if let Some(arr) = set_op_val.as_array_mut() {
if arr.is_empty() {
return false;
}
let first = arr.remove(0);
let op_str = first
.as_object()
.and_then(|o| o.get("operation").or_else(|| o.get("op")))
.and_then(|v| v.as_str())
.unwrap_or("union_all");
let op = canonical_op(op_str).unwrap_or(OP_UNION_ALL);
let right = if !arr.is_empty() {
arr.remove(0)
} else {
Value::Null
};
let mut new_obj = serde_json::Map::new();
new_obj.insert("operation".to_owned(), Value::String(op.to_owned()));
new_obj.insert("right".to_owned(), right);
*set_op_val = Value::Object(new_obj);
return true;
}
let Some(obj) = set_op_val.as_object_mut() else {
return false;
};
let mut changed = false;
if let Some(op_val) = obj.remove("op") {
obj.entry("operation").or_insert(op_val);
changed = true;
}
if let Some(op_val) = obj.get_mut("operation")
&& let Some(s) = op_val.as_str()
&& let Some(canonical) = canonical_op(s)
&& canonical != s
{
*op_val = Value::String(canonical.to_owned());
changed = true;
}
for alt in ["right_query", "right_plan", "rhs", "second"] {
if let Some(v) = obj.remove(alt) {
obj.entry("right").or_insert(v);
changed = true;
}
}
changed
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn canonical_op_handles_variants() {
assert_eq!(canonical_op("union_all"), Some(OP_UNION_ALL));
assert_eq!(canonical_op("UNION ALL"), Some(OP_UNION_ALL));
assert_eq!(canonical_op("UnionAll"), Some(OP_UNION_ALL));
assert_eq!(canonical_op("union"), Some(OP_UNION));
assert_eq!(canonical_op("INTERSECT"), Some(OP_INTERSECT));
assert_eq!(canonical_op("intersection"), Some(OP_INTERSECT));
assert_eq!(canonical_op("except"), Some(OP_EXCEPT));
assert_eq!(canonical_op("minus"), Some(OP_EXCEPT));
assert_eq!(canonical_op("unknown"), None);
}
#[test]
fn lifts_bare_union_all_key() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"union_all": {"select": [{"type": "star"}], "from": {"table": "b"}}
});
assert!(normalize(&mut val));
let set_op = val.get("set_operation").expect("set_operation lifted");
assert_eq!(set_op["operation"], "union_all");
assert_eq!(set_op["right"]["from"]["table"], "b");
}
#[test]
fn lifts_bare_union_key() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"union": {"select": [{"type": "star"}], "from": {"table": "b"}}
});
assert!(normalize(&mut val));
assert_eq!(val["set_operation"]["operation"], "union");
}
#[test]
fn does_not_overwrite_existing_set_operation() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"set_operation": {"operation": "intersect", "right": {"select": [{"type": "star"}], "from": {"table": "b"}}},
"union_all": {"select": [{"type": "star"}], "from": {"table": "c"}}
});
let _ = normalize(&mut val);
assert_eq!(val["set_operation"]["operation"], "intersect");
assert_eq!(val["set_operation"]["right"]["from"]["table"], "b");
}
#[test]
fn renames_op_to_operation() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"set_operation": {"op": "UNION ALL", "right": {"select": [{"type": "star"}], "from": {"table": "b"}}}
});
assert!(normalize(&mut val));
let set_op = &val["set_operation"];
assert!(set_op.get("op").is_none(), "`op` should be renamed");
assert_eq!(set_op["operation"], "union_all");
}
#[test]
fn canonicalizes_uppercase_operation() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"set_operation": {"operation": "UNION ALL", "right": {"select": [{"type": "star"}], "from": {"table": "b"}}}
});
assert!(normalize(&mut val));
assert_eq!(val["set_operation"]["operation"], "union_all");
}
#[test]
fn renames_right_query_to_right() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"set_operation": {"operation": "union_all", "right_query": {"select": [{"type": "star"}], "from": {"table": "b"}}}
});
assert!(normalize(&mut val));
assert!(val["set_operation"].get("right_query").is_none());
assert_eq!(val["set_operation"]["right"]["from"]["table"], "b");
}
#[test]
fn array_of_operands_promotes_first() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"set_operation": [
{"operation": "UNION ALL"},
{"select": [{"type": "star"}], "from": {"table": "b"}}
]
});
assert!(normalize(&mut val));
let set_op = &val["set_operation"];
assert_eq!(set_op["operation"], "union_all");
assert_eq!(set_op["right"]["from"]["table"], "b");
}
#[test]
fn bare_string_operation_canonicalized() {
let mut val = json!({"set_operation": "UNION ALL"});
assert!(normalize(&mut val));
assert_eq!(val["set_operation"], "union_all");
}
#[test]
fn no_change_for_canonical() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "a"},
"set_operation": {"operation": "union_all", "right": {"select": [{"type": "star"}], "from": {"table": "b"}}}
});
assert!(!normalize(&mut val));
}
}