use super::TableFunctionRows;
use uqa_core::{
memory::{Produced, ProductionControl},
Value,
};
use uqa_sql::{
expr::{RegexMatches, RegexSplit},
SQLError,
};
pub(super) fn regexp_matches_row_stream(
evaluated: &[Value],
collation: u32,
column: String,
) -> Result<TableFunctionRows, SQLError> {
let cursor = RegexMatches::new(evaluated, collation, &ProductionControl::uncontrolled())?;
Ok(one_column_rows(cursor, column))
}
pub(super) fn regexp_split_to_table_row_stream(
evaluated: &[Value],
collation: u32,
column: String,
) -> Result<TableFunctionRows, SQLError> {
let cursor = RegexSplit::new(
"regexp_split_to_table",
evaluated,
collation,
&ProductionControl::uncontrolled(),
)?;
Ok(one_column_rows(cursor, column))
}
trait ValueCursor: Send + 'static {
fn next_value(
&mut self,
control: &ProductionControl<'_>,
) -> Result<Option<Produced<Value>>, SQLError>;
}
impl ValueCursor for RegexMatches {
fn next_value(
&mut self,
control: &ProductionControl<'_>,
) -> Result<Option<Produced<Value>>, SQLError> {
self.next_row(control)
}
}
impl ValueCursor for RegexSplit {
fn next_value(
&mut self,
control: &ProductionControl<'_>,
) -> Result<Option<Produced<Value>>, SQLError> {
self.next_piece(control)
}
}
fn one_column_rows(mut cursor: Option<impl ValueCursor>, column: String) -> TableFunctionRows {
TableFunctionRows::new(
vec![column],
Box::new(std::iter::from_fn(move || {
let current = cursor.as_mut()?;
match current.next_value(&ProductionControl::uncontrolled()) {
Ok(Some(value)) => Some(Ok(crate::PhysicalRow::from_values(vec![
value.into_parts().0,
]))),
Ok(None) => {
cursor = None;
None
}
Err(error) => {
cursor = None;
Some(Err(error.into()))
}
}
})),
)
}
#[cfg(test)]
mod tests;