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

feat(expr): support substring function #792

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 48 additions & 0 deletions src/array/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,34 @@ impl ArrayImpl {
})
}

pub fn substring(&self, start: &Self, length: &Self) -> Result<Self, ConvertError> {
let (A::Utf8(a), A::Int32(b), A::Int32(c)) = (self, start, length) else {
return Err(ConvertError::NoTernaryOp("substring".into(), self.type_string(), start.type_string(), length.type_string()));
};
Ok(A::new_utf8(ternary_op(
a.as_ref(),
b.as_ref(),
c.as_ref(),
|a, b, c| {
let chars = a.chars().count() as i32;
let mut start = match *b {
0.. => *b - 1,
_ => chars + *b,
};
let mut end = start.saturating_add(*c);
if start > end {
(start, end) = (end, start);
}
let skip = start.max(0);
let take = (end - skip).max(0);
a.chars()
.skip(skip as usize)
.take(take as usize)
.collect::<String>()
},
)))
}

/// Perform binary operation.
pub fn binary_op(
&self,
Expand Down Expand Up @@ -608,6 +636,26 @@ where
Ok(builder.finish())
}

fn ternary_op<A, B, C, O, F, V>(a: &A, b: &B, c: &C, f: F) -> O
where
A: Array,
B: Array,
C: Array,
O: Array,
V: Borrow<O::Item>,
F: Fn(&A::Item, &B::Item, &C::Item) -> V,
{
let mut builder = O::Builder::with_capacity(a.len());
for e in a.iter().zip(b.iter()).zip(c.iter()) {
if let ((Some(a), Some(b)), Some(c)) = e {
builder.push(Some(f(a, b, c).borrow()));
} else {
builder.push(None);
}
}
builder.finish()
}

/// Optimized operations.
///
/// Assume both bitvecs have the same length.
Expand Down
23 changes: 23 additions & 0 deletions src/binder/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl Binder {
} => self.bind_between(*expr, negated, *low, *high),
Expr::Interval(interval) => self.bind_interval(interval),
Expr::Extract { field, expr } => self.bind_extract(field, *expr),
Expr::Substring {
expr,
substring_from,
substring_for,
} => self.bind_substring(*expr, substring_from, substring_for),
_ => todo!("bind expression: {:?}", expr),
}?;
self.check_type(id)?;
Expand Down Expand Up @@ -194,6 +199,24 @@ impl Binder {
Ok(self.egraph.add(Node::Extract([field, expr])))
}

fn bind_substring(
&mut self,
expr: Expr,
from: Option<Box<Expr>>,
for_: Option<Box<Expr>>,
) -> Result {
let expr = self.bind_expr(expr)?;
let from = match from {
Some(expr) => self.bind_expr(*expr)?,
None => self.egraph.add(Node::Constant(DataValue::Int32(1))),
};
let for_ = match for_ {
Some(expr) => self.bind_expr(*expr)?,
None => self.egraph.add(Node::Constant(DataValue::Int32(i32::MAX))),
};
Ok(self.egraph.add(Node::Substring([expr, from, for_])))
}

fn bind_function(&mut self, func: Function) -> Result {
let mut args = vec![];
for arg in func.args {
Expand Down
6 changes: 6 additions & 0 deletions src/executor/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ impl<'a> Evaluator<'a> {
let Expr::Field(field) = self.expr[*field] else { panic!("not a field") };
a.extract(field)
}
Substring([str, start, length]) => {
let str = self.next(*str).eval(chunk)?;
let start = self.next(*start).eval(chunk)?;
let length = self.next(*length).eval(chunk)?;
str.substring(&start, &length)
}
Desc(a) | Ref(a) => self.next(*a).eval(chunk),
// for aggs, evaluate its children
RowCount => Ok(ArrayImpl::new_null(
Expand Down
8 changes: 8 additions & 0 deletions src/planner/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ impl<'a> Explain<'a> {
("to", self.expr(c).pretty()),
],
),
Substring([str, start, len]) => Pretty::childless_record(
"Substring",
vec![
("str", self.expr(str).pretty()),
("start", self.expr(start).pretty()),
("length", self.expr(len).pretty()),
],
),

// aggregations
RowCount | RowNumber => enode.to_string().into(),
Expand Down
1 change: 1 addition & 0 deletions src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ define_language! {
"extract" = Extract([Id; 2]), // (extract field expr)
Field(DateTimeField),
"replace" = Replace([Id; 3]), // (replace expr pattern replacement)
"substring" = Substring([Id; 3]), // (substring expr start length)

// aggregations
"max" = Max(Id),
Expand Down
6 changes: 6 additions & 0 deletions src/planner/rules/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ pub fn analyze_type(enode: &Expr, x: impl Fn(&Id) -> Type, catalog: &RootCatalog
Extract([_, a]) => merge(enode, [x(a)?], |[a]| {
matches!(a, Kind::Date | Kind::Interval).then_some(Kind::Int32)
}),
Substring([str, start, len]) => {
merge(enode, [x(str)?, x(start)?, x(len)?], |[str, start, len]| {
(str == Kind::String && start == Kind::Int32 && len == Kind::Int32)
.then_some(Kind::String)
})
}

// number agg
Max(a) | Min(a) => x(a),
Expand Down
4 changes: 3 additions & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl From<&crate::parser::DataType> for DataTypeKind {
// Real => Self::Float32,
Float(_) | Double => Self::Float64,
SmallInt(_) => Self::Int16,
Int(_) => Self::Int32,
Int(_) | Integer(_) => Self::Int32,
BigInt(_) => Self::Int64,
Boolean => Self::Bool,
Decimal(info) => match info {
Expand Down Expand Up @@ -293,6 +293,8 @@ pub enum ConvertError {
NoUnaryOp(String, &'static str),
#[error("no function {0}({1}, {2})")]
NoBinaryOp(String, &'static str, &'static str),
#[error("no function {0}({1}, {2}, {3})")]
NoTernaryOp(String, &'static str, &'static str, &'static str),
#[error("no cast {0} -> {1}")]
NoCast(&'static str, DataTypeKind),
}
Expand Down
Loading
Loading