Skip to content

Commit

Permalink
Refactor trim functions to return MISSING on arg mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jpschorr committed Jan 9, 2023
1 parent c0368d3 commit 492a2aa
Showing 1 changed file with 59 additions and 49 deletions.
108 changes: 59 additions & 49 deletions partiql-eval/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,6 @@ where
fn evaluate(&self, bindings: &Tuple, ctx: &dyn EvalContext) -> Value {
let value = self.input().evaluate(bindings, ctx);
match value {
Null => Value::Null,
Missing => Value::Missing,
Value::String(s) => self.evaluate_str(s.as_ref()),
_ => Value::Null,
Expand Down Expand Up @@ -1223,6 +1222,35 @@ impl EvalExpr for EvalFnSubstring {
}
}

#[inline]
#[track_caller]
fn trim<FnTrim>(
value: &dyn EvalExpr,
to_trim: &dyn EvalExpr,
bindings: &Tuple,
ctx: &dyn EvalContext,
trim_fn: FnTrim,
) -> Value
where
FnTrim: Fn(&str, &str) -> Value,
{
let value = match value.evaluate(bindings, ctx) {
Value::String(s) => Some(s),
Null => None,
_ => return Value::Missing,
};
let to_trim = match to_trim.evaluate(bindings, ctx) {
Value::String(s) => s,
Null => return Value::Null,
_ => return Value::Missing,
};
if let Some(s) = value {
trim_fn(&s, &to_trim)
} else {
Value::Null
}
}

#[derive(Debug)]
pub struct EvalFnBtrim {
pub value: Box<dyn EvalExpr>,
Expand All @@ -1232,22 +1260,16 @@ pub struct EvalFnBtrim {
impl EvalExpr for EvalFnBtrim {
#[inline]
fn evaluate(&self, bindings: &Tuple, ctx: &dyn EvalContext) -> Value {
let value = match self.value.evaluate(bindings, ctx) {
Missing => return Value::Missing,
Value::String(s) => Some(s),
_ => None,
};
let to_trim = match self.to_trim.evaluate(bindings, ctx) {
Missing => return Value::Missing,
Value::String(s) => s,
_ => return Value::Null,
};
if let Some(s) = value {
let to_trim = to_trim.chars().collect_vec();
s.trim_matches(&to_trim[..]).into()
} else {
Value::Null
}
trim(
self.value.as_ref(),
self.to_trim.as_ref(),
bindings,
ctx,
|s, to_trim| {
let to_trim = to_trim.chars().collect_vec();
s.trim_matches(&to_trim[..]).into()
},
)
}
}

Expand All @@ -1260,22 +1282,16 @@ pub struct EvalFnRtrim {
impl EvalExpr for EvalFnRtrim {
#[inline]
fn evaluate(&self, bindings: &Tuple, ctx: &dyn EvalContext) -> Value {
let value = match self.value.evaluate(bindings, ctx) {
Missing => return Value::Missing,
Value::String(s) => Some(s),
_ => None,
};
let to_trim = match self.to_trim.evaluate(bindings, ctx) {
Missing => return Value::Missing,
Value::String(s) => s,
_ => return Value::Null,
};
if let Some(s) = value {
let to_trim = to_trim.chars().collect_vec();
s.trim_end_matches(&to_trim[..]).into()
} else {
Value::Null
}
trim(
self.value.as_ref(),
self.to_trim.as_ref(),
bindings,
ctx,
|s, to_trim| {
let to_trim = to_trim.chars().collect_vec();
s.trim_end_matches(&to_trim[..]).into()
},
)
}
}

Expand All @@ -1288,22 +1304,16 @@ pub struct EvalFnLtrim {
impl EvalExpr for EvalFnLtrim {
#[inline]
fn evaluate(&self, bindings: &Tuple, ctx: &dyn EvalContext) -> Value {
let value = match self.value.evaluate(bindings, ctx) {
Missing => return Value::Missing,
Value::String(s) => Some(s),
_ => None,
};
let to_trim = match self.to_trim.evaluate(bindings, ctx) {
Missing => return Value::Missing,
Value::String(s) => s,
_ => return Value::Null,
};
if let Some(s) = value {
let to_trim = to_trim.chars().collect_vec();
s.trim_start_matches(&to_trim[..]).into()
} else {
Value::Null
}
trim(
self.value.as_ref(),
self.to_trim.as_ref(),
bindings,
ctx,
|s, to_trim| {
let to_trim = to_trim.chars().collect_vec();
s.trim_start_matches(&to_trim[..]).into()
},
)
}
}

Expand Down

0 comments on commit 492a2aa

Please sign in to comment.