Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andygrove committed May 24, 2024
1 parent 28169e7 commit b3f47b7
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions core/src/execution/datafusion/expressions/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

use crate::execution::datafusion::expressions::utils::down_cast_any_ref;
use crate::{errors::CometError, execution::datafusion::expressions::utils::down_cast_any_ref};
use arrow_array::{builder::BooleanBuilder, Array, RecordBatch, StringArray};
use arrow_schema::{DataType, Schema};
use datafusion::logical_expr::ColumnarValue;
Expand Down Expand Up @@ -80,32 +80,42 @@ impl PhysicalExpr for RLike {
if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(pattern))) =
self.pattern.evaluate(batch)?
{
// TODO cache Regex across invocations of evaluate or create it in constructor
let re = Regex::new(&pattern).unwrap(); // TODO error handling
let inputs = v
.as_any()
.downcast_ref::<StringArray>()
.expect("string array");
let mut builder = BooleanBuilder::with_capacity(inputs.len());
if inputs.is_nullable() {
for i in 0..inputs.len() {
if inputs.is_null(i) {
builder.append_null();
// TODO cache Regex across invocations of evaluate() or create it in constructor
match Regex::new(&pattern) {
Ok(re) => {
let inputs = v
.as_any()
.downcast_ref::<StringArray>()
.expect("string array");
let mut builder = BooleanBuilder::with_capacity(inputs.len());
if inputs.is_nullable() {
for i in 0..inputs.len() {
if inputs.is_null(i) {
builder.append_null();
} else {
builder.append_value(re.is_match(inputs.value(i)));
}
}
} else {
builder.append_value(re.is_match(inputs.value(i)));
for i in 0..inputs.len() {
builder.append_value(re.is_match(inputs.value(i)));
}
}
Ok(ColumnarValue::Array(Arc::new(builder.finish())))
}
} else {
for i in 0..inputs.len() {
builder.append_value(re.is_match(inputs.value(i)));
}
Err(e) => Err(CometError::Internal(format!(
"Failed to compile regular expression: {e:?}"
))
.into()),
}
Ok(ColumnarValue::Array(Arc::new(builder.finish())))
} else {
todo!()
Err(
CometError::Internal("Only scalar regex patterns are supported".to_string())
.into(),
)
}
} else {
todo!()
Err(CometError::Internal("Only columnar inputs are supported".to_string()).into())
}
}

Expand Down

0 comments on commit b3f47b7

Please sign in to comment.