mingmwang commented on code in PR #6084:
URL: https://github.com/apache/arrow-datafusion/pull/6084#discussion_r1177296225
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -484,6 +484,97 @@ impl LogicalPlan {
_ => Ok(self),
}
}
+
+ /// Returns the maximum number of rows that this plan can output.
+ pub fn max_rows(self: &LogicalPlan) -> Option<usize> {
+ match self {
+ LogicalPlan::Projection(Projection { input, .. }) =>
input.max_rows(),
+ LogicalPlan::Filter(Filter { input, .. }) => input.max_rows(),
+ LogicalPlan::Window(Window { input, .. }) => input.max_rows(),
+ LogicalPlan::Aggregate(Aggregate {
+ input, group_expr, ..
+ }) => {
+ if group_expr.is_empty() {
+ Some(1)
+ } else {
+ input.max_rows()
+ }
+ }
+ LogicalPlan::Sort(Sort { input, fetch, .. }) => {
+ match (fetch, input.max_rows()) {
+ (Some(fetch_limit), Some(input_max)) => {
+ Some(input_max.min(*fetch_limit))
+ }
+ (Some(fetch_limit), None) => Some(*fetch_limit),
+ (None, Some(input_max)) => Some(input_max),
+ (None, None) => None,
+ }
+ }
+ LogicalPlan::Join(Join {
+ left,
+ right,
+ join_type,
+ ..
+ }) => match join_type {
+ JoinType::Inner | JoinType::Left | JoinType::Right |
JoinType::Full => {
+ match (left.max_rows(), right.max_rows()) {
+ (Some(left_max), Some(right_max)) => {
+ let min_rows = match join_type {
+ JoinType::Left => left_max,
+ JoinType::Right => right_max,
+ JoinType::Full => left_max + right_max,
+ _ => 0,
+ };
+ Some((left_max * right_max).max(min_rows))
+ }
+ _ => None,
+ }
+ }
+ JoinType::LeftSemi | JoinType::LeftAnti => left.max_rows(),
+ JoinType::RightSemi | JoinType::RightAnti => right.max_rows(),
+ },
+ LogicalPlan::CrossJoin(CrossJoin { left, right, .. }) => {
+ match (left.max_rows(), right.max_rows()) {
+ (Some(left_max), Some(right_max)) => Some(left_max *
right_max),
+ _ => None,
+ }
+ }
+ LogicalPlan::Repartition(Repartition { input, .. }) =>
input.max_rows(),
+ LogicalPlan::Union(Union { inputs, .. }) => inputs
+ .iter()
+ .map(|plan| plan.max_rows())
+ .try_fold(0usize, |mut acc, input_max| {
+ if let Some(i_max) = input_max {
+ acc += i_max;
+ Some(acc)
+ } else {
+ None
+ }
+ }),
+ LogicalPlan::TableScan(TableScan { fetch, .. }) => *fetch,
+ LogicalPlan::EmptyRelation(_) => Some(0),
+ LogicalPlan::Subquery(_) => None,
+ LogicalPlan::SubqueryAlias(SubqueryAlias { input, .. }) =>
input.max_rows(),
+ LogicalPlan::Limit(Limit { fetch, .. }) => *fetch,
+ LogicalPlan::Distinct(Distinct { input }) => input.max_rows(),
+ LogicalPlan::Unnest(_) => None,
+ LogicalPlan::CreateMemoryTable(_)
+ | LogicalPlan::CreateExternalTable(_)
+ | LogicalPlan::CreateView(_)
+ | LogicalPlan::CreateCatalogSchema(_)
+ | LogicalPlan::CreateCatalog(_)
+ | LogicalPlan::DropTable(_)
+ | LogicalPlan::DropView(_)
+ | LogicalPlan::Explain(_)
+ | LogicalPlan::Analyze(_)
+ | LogicalPlan::Dml(_)
+ | LogicalPlan::DescribeTable(_)
+ | LogicalPlan::Prepare(_)
+ | LogicalPlan::Statement(_)
+ | LogicalPlan::Values(_)
Review Comment:
Will add.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]