Skip to content

Apache DataFusion SQL Query Engine

ARROW-6666: [Rust] Datafusion parquet string literal support

Max Burkee939978873df
diff --git a/.gitignore b/.gitignore
index 0e3df7234b..aa064342d1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -64,6 +64,8 @@ docker_cache
.gdb_history
.DS_Store
*.orig
+.*.swp
+.*.swo
site/
diff --git a/rust/datafusion/Cargo.toml b/rust/datafusion/Cargo.toml
index 1702832039..76ef878795 100644
--- a/rust/datafusion/Cargo.toml
+++ b/rust/datafusion/Cargo.toml
@@ -52,6 +52,7 @@ clap = "2.33.0"
prettytable-rs = "0.8.0"
rustyline = {version = "4.1.0", optional = true}
crossbeam = "0.7.1"
+paste = "0.1.6"
[dev-dependencies]
criterion = "0.2.0"
diff --git a/rust/datafusion/src/execution/physical_plan/expressions.rs b/rust/datafusion/src/execution/physical_plan/expressions.rs
index b3efa23e23..4f101e0eaf 100644
--- a/rust/datafusion/src/execution/physical_plan/expressions.rs
+++ b/rust/datafusion/src/execution/physical_plan/expressions.rs
@@ -27,17 +27,22 @@ use crate::execution::physical_plan::{Accumulator, AggregateExpr, PhysicalExpr};
use crate::logicalplan::{Operator, ScalarValue};
use arrow::array::{
ArrayRef, BooleanArray, Float32Array, Float64Array, Int16Array, Int32Array,
- Int64Array, Int8Array, UInt16Array, UInt32Array, UInt64Array, UInt8Array,
+ Int64Array, Int8Array, StringArray, UInt16Array, UInt32Array, UInt64Array,
+ UInt8Array,
};
use arrow::array::{
Float32Builder, Float64Builder, Int16Builder, Int32Builder, Int64Builder,
- Int8Builder, UInt16Builder, UInt32Builder, UInt64Builder, UInt8Builder,
+ Int8Builder, StringBuilder, UInt16Builder, UInt32Builder, UInt64Builder,
+ UInt8Builder,
};
use arrow::compute;
use arrow::compute::kernels::arithmetic::{add, divide, multiply, subtract};
use arrow::compute::kernels::boolean::{and, or};
use arrow::compute::kernels::cast::cast;
use arrow::compute::kernels::comparison::{eq, gt, gt_eq, lt, lt_eq, neq};
+use arrow::compute::kernels::comparison::{
+ eq_utf8, gt_eq_utf8, gt_utf8, like_utf8, lt_eq_utf8, lt_utf8, neq_utf8, nlike_utf8,
+};
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
@@ -849,6 +854,21 @@ pub fn count(expr: Arc<dyn PhysicalExpr>) -> Arc<dyn AggregateExpr> {
Arc::new(Count::new(expr))
}
+/// Invoke a compute kernel on a pair of binary data arrays
+macro_rules! compute_utf8_op {
+ ($LEFT:expr, $RIGHT:expr, $OP:ident, $DT:ident) => {{
+ let ll = $LEFT
+ .as_any()
+ .downcast_ref::<$DT>()
+ .expect("compute_op failed to downcast array");
+ let rr = $RIGHT
+ .as_any()
+ .downcast_ref::<$DT>()
+ .expect("compute_op failed to downcast array");
+ Ok(Arc::new(paste::expr! {[<$OP _utf8>]}(&ll, &rr)?))
+ }};
+}
+
/// Invoke a compute kernel on a pair of arrays
macro_rules! compute_op {
($LEFT:expr, $RIGHT:expr, $OP:ident, $DT:ident) => {{
@@ -864,7 +884,44 @@ macro_rules! compute_op {
}};
}
+macro_rules! binary_string_array_op {
+ ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+ match $LEFT.data_type() {
+ DataType::Utf8 => compute_utf8_op!($LEFT, $RIGHT, $OP, StringArray),
+ other => Err(ExecutionError::General(format!(
+ "Unsupported data type {:?}",
+ other
+ ))),
+ }
+ }};
+}
+
/// Invoke a compute kernel on a pair of arrays
+/// The binary_primitive_array_op macro only evaluates for primitive types
+/// like integers and floats.
+macro_rules! binary_primitive_array_op {
+ ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+ match $LEFT.data_type() {
+ DataType::Int8 => compute_op!($LEFT, $RIGHT, $OP, Int8Array),
+ DataType::Int16 => compute_op!($LEFT, $RIGHT, $OP, Int16Array),
+ DataType::Int32 => compute_op!($LEFT, $RIGHT, $OP, Int32Array),
+ DataType::Int64 => compute_op!($LEFT, $RIGHT, $OP, Int64Array),
+ DataType::UInt8 => compute_op!($LEFT, $RIGHT, $OP, UInt8Array),
+ DataType::UInt16 => compute_op!($LEFT, $RIGHT, $OP, UInt16Array),
+ DataType::UInt32 => compute_op!($LEFT, $RIGHT, $OP, UInt32Array),
+ DataType::UInt64 => compute_op!($LEFT, $RIGHT, $OP, UInt64Array),
+ DataType::Float32 => compute_op!($LEFT, $RIGHT, $OP, Float32Array),
+ DataType::Float64 => compute_op!($LEFT, $RIGHT, $OP, Float64Array),
+ other => Err(ExecutionError::General(format!(
+ "Unsupported data type {:?}",
+ other
+ ))),
+ }
+ }};
+}
+
+/// The binary_array_op macro includes types that extend beyond the primitive,
+/// such as Utf8 strings.
macro_rules! binary_array_op {
($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
match $LEFT.data_type() {
@@ -878,6 +935,7 @@ macro_rules! binary_array_op {
DataType::UInt64 => compute_op!($LEFT, $RIGHT, $OP, UInt64Array),
DataType::Float32 => compute_op!($LEFT, $RIGHT, $OP, Float32Array),
DataType::Float64 => compute_op!($LEFT, $RIGHT, $OP, Float64Array),
+ DataType::Utf8 => compute_utf8_op!($LEFT, $RIGHT, $OP, StringArray),
other => Err(ExecutionError::General(format!(
"Unsupported data type {:?}",
other
@@ -939,16 +997,18 @@ impl PhysicalExpr for BinaryExpr {
)));
}
match &self.op {
+ Operator::Like => binary_string_array_op!(left, right, like),
+ Operator::NotLike => binary_string_array_op!(left, right, nlike),
Operator::Lt => binary_array_op!(left, right, lt),
Operator::LtEq => binary_array_op!(left, right, lt_eq),
Operator::Gt => binary_array_op!(left, right, gt),
Operator::GtEq => binary_array_op!(left, right, gt_eq),
Operator::Eq => binary_array_op!(left, right, eq),
Operator::NotEq => binary_array_op!(left, right, neq),
- Operator::Plus => binary_array_op!(left, right, add),
- Operator::Minus => binary_array_op!(left, right, subtract),
- Operator::Multiply => binary_array_op!(left, right, multiply),
- Operator::Divide => binary_array_op!(left, right, divide),
+ Operator::Plus => binary_primitive_array_op!(left, right, add),
+ Operator::Minus => binary_primitive_array_op!(left, right, subtract),
+ Operator::Multiply => binary_primitive_array_op!(left, right, multiply),
+ Operator::Divide => binary_primitive_array_op!(left, right, divide),
Operator::And => {
if left.data_type() == &DataType::Boolean {
boolean_op!(left, right, and)
@@ -1149,6 +1209,9 @@ impl PhysicalExpr for Literal {
ScalarValue::Float64(value) => {
build_literal_array!(batch, Float64Builder, *value)
}
+ ScalarValue::Utf8(value) => {
+ build_literal_array!(batch, StringBuilder, &*value)
+ }
other => Err(ExecutionError::General(format!(
"Unsupported literal type {:?}",
other