Skip to content

Commit

Permalink
Add internal_err! error macro (apache#7293)
Browse files Browse the repository at this point in the history
* Add `internal_err!` error macro

* doc
  • Loading branch information
comphead authored Aug 16, 2023
1 parent 7d77448 commit 518efca
Show file tree
Hide file tree
Showing 67 changed files with 483 additions and 579 deletions.
10 changes: 4 additions & 6 deletions datafusion/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

//! Runtime configuration, via [`ConfigOptions`]

use crate::error::_internal_err;
use crate::{DataFusionError, Result};
use std::any::Any;
use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -65,10 +65,10 @@ use std::fmt::Display;
/// "field1" => self.field1.set(rem, value),
/// "field2" => self.field2.set(rem, value),
/// "field3" => self.field3.set(rem, value),
/// _ => Err(DataFusionError::Internal(format!(
/// _ => _internal_err!(
/// "Config value \"{}\" not found on MyConfig",
/// key
/// ))),
/// ),
/// }
/// }
///
Expand Down Expand Up @@ -510,9 +510,7 @@ impl ConfigField for ConfigOptions {
"optimizer" => self.optimizer.set(rem, value),
"explain" => self.explain.set(rem, value),
"sql_parser" => self.sql_parser.set(rem, value),
_ => Err(DataFusionError::Internal(format!(
"Config value \"{key}\" not found on ConfigOptions"
))),
_ => _internal_err!("Config value \"{key}\" not found on ConfigOptions"),
}
}

Expand Down
8 changes: 7 additions & 1 deletion datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ macro_rules! unwrap_or_internal_err {
};
}

#[macro_export]
macro_rules! with_dollar_sign {
($($body:tt)*) => {
macro_rules! __with_dollar_sign { $($body)* }
Expand Down Expand Up @@ -449,6 +448,13 @@ macro_rules! make_error {
// Exposes a macro to create `DataFusionError::Plan`
make_error!(plan_err, Plan);

// Exposes a macro to create `DataFusionError::Internal`
make_error!(internal_err, Internal);

// To avoid compiler error when using macro in the same crate:
// macros from the current crate cannot be referred to by absolute paths
pub use internal_err as _internal_err;

#[cfg(test)]
mod test {
use std::sync::Arc;
Expand Down
86 changes: 42 additions & 44 deletions datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::cast::{
as_fixed_size_binary_array, as_fixed_size_list_array, as_list_array, as_struct_array,
};
use crate::delta::shift_months;
use crate::error::{DataFusionError, Result};
use crate::error::{DataFusionError, Result, _internal_err};
use arrow::buffer::NullBuffer;
use arrow::compute::nullif;
use arrow::datatypes::{i256, FieldRef, Fields, SchemaBuilder};
Expand Down Expand Up @@ -699,9 +699,9 @@ macro_rules! primitive_right {
Ok(ScalarValue::$SCALAR(Some($TERM.recip())))
};
($TERM:expr, /, $SCALAR:ident) => {
Err(DataFusionError::Internal(format!(
internal_err!(
"Can not divide an uninitialized value to a non-floating point value",
)))
)
};
($TERM:expr, &, $SCALAR:ident) => {
Ok(ScalarValue::$SCALAR(Some($TERM)))
Expand Down Expand Up @@ -834,12 +834,12 @@ macro_rules! impl_bit_op_arithmetic {
(ScalarValue::Int8(lhs), ScalarValue::Int8(rhs)) => {
primitive_op!(lhs, rhs, Int8, $OPERATION)
}
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Operator {} is not implemented for types {:?} and {:?}",
stringify!($OPERATION),
$LHS,
$RHS
))),
),
}
};
}
Expand All @@ -850,12 +850,12 @@ macro_rules! impl_bool_op_arithmetic {
(ScalarValue::Boolean(lhs), ScalarValue::Boolean(rhs)) => {
primitive_op!(lhs, rhs, Boolean, $OPERATION)
}
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Operator {} is not implemented for types {:?} and {:?}",
stringify!($OPERATION),
$LHS,
$RHS
))),
),
}
};
}
Expand Down Expand Up @@ -1020,12 +1020,12 @@ macro_rules! impl_op_arithmetic {
true,
)))),
// todo: Add Decimal256 support
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Operator {} is not implemented for types {:?} and {:?}",
stringify!($OPERATION),
$LHS,
$RHS
))),
),
}
};
}
Expand Down Expand Up @@ -1790,9 +1790,9 @@ impl ScalarValue {
if precision <= DECIMAL128_MAX_PRECISION && scale.unsigned_abs() <= precision {
return Ok(ScalarValue::Decimal128(Some(value), precision, scale));
}
Err(DataFusionError::Internal(format!(
_internal_err!(
"Can not new a decimal type ScalarValue for precision {precision} and scale {scale}"
)))
)
}

/// Returns a [`ScalarValue::Utf8`] representing `val`
Expand Down Expand Up @@ -2051,9 +2051,9 @@ impl ScalarValue {
ScalarValue::Decimal256(Some(v), precision, scale) => Ok(
ScalarValue::Decimal256(Some(v.neg_wrapping()), *precision, *scale),
),
value => Err(DataFusionError::Internal(format!(
value => _internal_err!(
"Can not run arithmetic negative on scalar value {value:?}"
))),
),
}
}

Expand Down Expand Up @@ -2257,11 +2257,11 @@ impl ScalarValue {
if let ScalarValue::$SCALAR_TY(v) = sv {
Ok(v)
} else {
Err(DataFusionError::Internal(format!(
_internal_err!(
"Inconsistent types in ScalarValue::iter_to_array. \
Expected {:?}, got {:?}",
data_type, sv
)))
)
}
})
.collect::<Result<$ARRAY_TY>>()?;
Expand All @@ -2277,11 +2277,11 @@ impl ScalarValue {
if let ScalarValue::$SCALAR_TY(v, _) = sv {
Ok(v)
} else {
Err(DataFusionError::Internal(format!(
_internal_err!(
"Inconsistent types in ScalarValue::iter_to_array. \
Expected {:?}, got {:?}",
data_type, sv
)))
)
}
})
.collect::<Result<$ARRAY_TY>>()?;
Expand All @@ -2299,11 +2299,11 @@ impl ScalarValue {
if let ScalarValue::$SCALAR_TY(v) = sv {
Ok(v)
} else {
Err(DataFusionError::Internal(format!(
_internal_err!(
"Inconsistent types in ScalarValue::iter_to_array. \
Expected {:?}, got {:?}",
data_type, sv
)))
)
}
})
.collect::<Result<$ARRAY_TY>>()?;
Expand Down Expand Up @@ -2352,11 +2352,11 @@ impl ScalarValue {
builder.values().append_null();
}
sv => {
return Err(DataFusionError::Internal(format!(
return _internal_err!(
"Inconsistent types in ScalarValue::iter_to_array. \
Expected Utf8, got {:?}",
sv
)))
)
}
}
}
Expand All @@ -2366,11 +2366,11 @@ impl ScalarValue {
builder.append(false);
}
sv => {
return Err(DataFusionError::Internal(format!(
return _internal_err!(
"Inconsistent types in ScalarValue::iter_to_array. \
Expected List, got {:?}",
sv
)))
)
}
}
}
Expand Down Expand Up @@ -2524,9 +2524,7 @@ impl ScalarValue {
}
};
} else {
return Err(DataFusionError::Internal(format!(
"Expected Struct but found: {scalar}"
)));
return _internal_err!("Expected Struct but found: {scalar}");
};
}

Expand Down Expand Up @@ -2554,9 +2552,9 @@ impl ScalarValue {
}
}
_ => {
Err(DataFusionError::Internal(format!(
_internal_err!(
"Expected scalar of type {value_type} but found: {scalar} {scalar:?}"
)))
)
}
})
.collect::<Result<Vec<_>>>()?;
Expand All @@ -2582,10 +2580,10 @@ impl ScalarValue {
if let ScalarValue::FixedSizeBinary(_, v) = sv {
Ok(v)
} else {
Err(DataFusionError::Internal(format!(
_internal_err!(
"Inconsistent types in ScalarValue::iter_to_array. \
Expected {data_type:?}, got {sv:?}"
)))
)
}
})
.collect::<Result<Vec<_>>>()?;
Expand All @@ -2611,11 +2609,11 @@ impl ScalarValue {
| DataType::Union(_, _)
| DataType::Map(_, _)
| DataType::RunEndEncoded(_, _) => {
return Err(DataFusionError::Internal(format!(
return _internal_err!(
"Unsupported creation of {:?} array from ScalarValue {:?}",
data_type,
scalars.peek()
)));
);
}
};

Expand Down Expand Up @@ -2703,9 +2701,9 @@ impl ScalarValue {
}
}
} else {
return Err(DataFusionError::Internal(format!(
return _internal_err!(
"Expected ScalarValue::List element. Received {scalar:?}"
)));
);
}
}

Expand Down Expand Up @@ -3641,11 +3639,11 @@ macro_rules! impl_try_from {
fn try_from(value: ScalarValue) -> Result<Self> {
match value {
ScalarValue::$SCALAR(Some(inner_value)) => Ok(inner_value),
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Cannot convert {:?} to {}",
value,
std::any::type_name::<Self>()
))),
),
}
}
}
Expand All @@ -3665,11 +3663,11 @@ impl TryFrom<ScalarValue> for i32 {
| ScalarValue::Date32(Some(inner_value))
| ScalarValue::Time32Second(Some(inner_value))
| ScalarValue::Time32Millisecond(Some(inner_value)) => Ok(inner_value),
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Cannot convert {:?} to {}",
value,
std::any::type_name::<Self>()
))),
),
}
}
}
Expand All @@ -3688,11 +3686,11 @@ impl TryFrom<ScalarValue> for i64 {
| ScalarValue::TimestampMicrosecond(Some(inner_value), _)
| ScalarValue::TimestampMillisecond(Some(inner_value), _)
| ScalarValue::TimestampSecond(Some(inner_value), _) => Ok(inner_value),
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Cannot convert {:?} to {}",
value,
std::any::type_name::<Self>()
))),
),
}
}
}
Expand All @@ -3704,11 +3702,11 @@ impl TryFrom<ScalarValue> for i128 {
fn try_from(value: ScalarValue) -> Result<Self> {
match value {
ScalarValue::Decimal128(Some(inner_value), _, _) => Ok(inner_value),
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Cannot convert {:?} to {}",
value,
std::any::type_name::<Self>()
))),
),
}
}
}
Expand All @@ -3720,11 +3718,11 @@ impl TryFrom<ScalarValue> for i256 {
fn try_from(value: ScalarValue) -> Result<Self> {
match value {
ScalarValue::Decimal256(Some(inner_value), _, _) => Ok(inner_value),
_ => Err(DataFusionError::Internal(format!(
_ => _internal_err!(
"Cannot convert {:?} to {}",
value,
std::any::type_name::<Self>()
))),
),
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions datafusion/core/src/physical_optimizer/join_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::physical_plan::projection::ProjectionExec;
use crate::physical_plan::ExecutionPlan;

use arrow_schema::Schema;
use datafusion_common::internal_err;
use datafusion_common::tree_node::{Transformed, TreeNode};
use datafusion_common::{DataFusionError, JoinType};
use datafusion_physical_expr::expressions::Column;
Expand Down Expand Up @@ -537,9 +538,7 @@ fn swap_join_according_to_unboundedness(
(
_,
JoinType::Right | JoinType::RightSemi | JoinType::RightAnti | JoinType::Full,
) => Err(DataFusionError::Internal(format!(
"{join_type} join cannot be swapped for unbounded input."
))),
) => internal_err!("{join_type} join cannot be swapped for unbounded input."),
(PartitionMode::Partitioned, _) => {
swap_hash_join(hash_join, PartitionMode::Partitioned)
}
Expand Down
Loading

0 comments on commit 518efca

Please sign in to comment.