Skip to content

Commit fcd8c5d

Browse files
committed
mtrlz/sql: extract BETWEEN planning into its own function
1 parent c1d6975 commit fcd8c5d

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

src/materialize/sql/mod.rs

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@ impl Planner {
10531053
ASTNode::SQLBinaryExpr { op, left, right } => {
10541054
self.plan_binary_expr(ctx, op, left, right, plan)
10551055
}
1056+
ASTNode::SQLBetween {
1057+
expr,
1058+
low,
1059+
high,
1060+
negated,
1061+
} => self.plan_between(ctx, expr, low, high, *negated, plan),
10561062
ASTNode::SQLNested(expr) => self.plan_expr(ctx, expr, plan),
10571063
ASTNode::SQLFunction {
10581064
name,
@@ -1061,41 +1067,6 @@ impl Planner {
10611067
all,
10621068
distinct,
10631069
} => self.plan_function(ctx, name, args, over, *all, *distinct, plan),
1064-
ASTNode::SQLBetween {
1065-
expr,
1066-
low,
1067-
high,
1068-
negated,
1069-
} => {
1070-
let low = ASTNode::SQLBinaryExpr {
1071-
left: expr.clone(),
1072-
op: if *negated {
1073-
SQLOperator::Lt
1074-
} else {
1075-
SQLOperator::GtEq
1076-
},
1077-
right: low.clone(),
1078-
};
1079-
let high = ASTNode::SQLBinaryExpr {
1080-
left: expr.clone(),
1081-
op: if *negated {
1082-
SQLOperator::Gt
1083-
} else {
1084-
SQLOperator::LtEq
1085-
},
1086-
right: high.clone(),
1087-
};
1088-
let both = ASTNode::SQLBinaryExpr {
1089-
left: Box::new(low),
1090-
op: if *negated {
1091-
SQLOperator::Or
1092-
} else {
1093-
SQLOperator::And
1094-
},
1095-
right: Box::new(high),
1096-
};
1097-
self.plan_expr(ctx, &both, plan)
1098-
}
10991070
_ => bail!(
11001071
"complicated expressions are not yet supported: {}",
11011072
e.to_string()
@@ -1301,6 +1272,45 @@ impl Planner {
13011272
Ok((expr, typ))
13021273
}
13031274

1275+
fn plan_between<'a>(
1276+
&self,
1277+
ctx: &ExprContext,
1278+
expr: &'a ASTNode,
1279+
low: &'a ASTNode,
1280+
high: &'a ASTNode,
1281+
negated: bool,
1282+
plan: &SQLPlan,
1283+
) -> Result<(Expr, Type), failure::Error> {
1284+
let low = ASTNode::SQLBinaryExpr {
1285+
left: Box::new(expr.clone()),
1286+
op: if negated {
1287+
SQLOperator::Lt
1288+
} else {
1289+
SQLOperator::GtEq
1290+
},
1291+
right: Box::new(low.clone()),
1292+
};
1293+
let high = ASTNode::SQLBinaryExpr {
1294+
left: Box::new(expr.clone()),
1295+
op: if negated {
1296+
SQLOperator::Gt
1297+
} else {
1298+
SQLOperator::LtEq
1299+
},
1300+
right: Box::new(high.clone()),
1301+
};
1302+
let both = ASTNode::SQLBinaryExpr {
1303+
left: Box::new(low),
1304+
op: if negated {
1305+
SQLOperator::Or
1306+
} else {
1307+
SQLOperator::And
1308+
},
1309+
right: Box::new(high),
1310+
};
1311+
self.plan_expr(ctx, &both, plan)
1312+
}
1313+
13041314
fn plan_literal<'a>(&self, l: &'a Value) -> Result<(Expr, Type), failure::Error> {
13051315
let (datum, ftype) = match l {
13061316
Value::Long(i) => (Datum::Int64(*i), FType::Int64),

0 commit comments

Comments
 (0)