| 43 | namespace impala { |
| 44 | |
| 45 | Status AnalyticEvalPlanNode::Init(const TPlanNode& tnode, FragmentState* state) { |
| 46 | RETURN_IF_ERROR(PlanNode::Init(tnode, state)); |
| 47 | |
| 48 | const TAnalyticNode& analytic_node = tnode.analytic_node; |
| 49 | TupleDescriptor* intermediate_tuple_desc = |
| 50 | state->desc_tbl().GetTupleDescriptor(tnode.analytic_node.intermediate_tuple_id); |
| 51 | TupleDescriptor* result_tuple_desc = |
| 52 | state->desc_tbl().GetTupleDescriptor(tnode.analytic_node.output_tuple_id); |
| 53 | bool has_lead_fn = false; |
| 54 | |
| 55 | for (int i = 0; i < analytic_node.analytic_functions.size(); ++i) { |
| 56 | AggFn* analytic_fn; |
| 57 | RETURN_IF_ERROR(AggFn::Create(analytic_node.analytic_functions[i], |
| 58 | *children_[0]->row_descriptor_, *(intermediate_tuple_desc->slots()[i]), |
| 59 | *(result_tuple_desc->slots()[i]), state, &analytic_fn)); |
| 60 | analytic_fns_.push_back(analytic_fn); |
| 61 | DCHECK(!analytic_fn->is_merge()); |
| 62 | const TFunction& fn = analytic_node.analytic_functions[i].nodes[0].fn; |
| 63 | const bool is_lead_fn = fn.name.function_name == "lead"; |
| 64 | is_lead_fn_.push_back(is_lead_fn); |
| 65 | has_lead_fn |= is_lead_fn; |
| 66 | } |
| 67 | const TAnalyticWindow& window = tnode.analytic_node.window; |
| 68 | DCHECK(!has_lead_fn || !window.__isset.window_start); |
| 69 | DCHECK(window.__isset.window_end || !window.__isset.window_start) |
| 70 | << "UNBOUNDED FOLLOWING is only supported with UNBOUNDED PRECEDING."; |
| 71 | |
| 72 | if (analytic_node.__isset.partition_by_eq || analytic_node.__isset.order_by_eq) { |
| 73 | DCHECK(analytic_node.__isset.buffered_tuple_id); |
| 74 | TupleDescriptor* buffered_tuple_desc = |
| 75 | state->desc_tbl().GetTupleDescriptor(tnode.analytic_node.buffered_tuple_id); |
| 76 | DCHECK(buffered_tuple_desc != nullptr); |
| 77 | vector<TTupleId> tuple_ids; |
| 78 | tuple_ids.push_back(children_[0]->row_descriptor_->tuple_descriptors()[0]->id()); |
| 79 | tuple_ids.push_back(buffered_tuple_desc->id()); |
| 80 | RowDescriptor cmp_row_desc(state->desc_tbl(), tuple_ids, vector<bool>(2, false)); |
| 81 | |
| 82 | if (analytic_node.__isset.partition_by_eq) { |
| 83 | RETURN_IF_ERROR(ScalarExpr::Create( |
| 84 | analytic_node.partition_by_eq, cmp_row_desc, state, &partition_by_eq_expr_)); |
| 85 | } |
| 86 | if (analytic_node.__isset.order_by_eq) { |
| 87 | RETURN_IF_ERROR(ScalarExpr::Create( |
| 88 | analytic_node.order_by_eq, cmp_row_desc, state, &order_by_eq_expr_)); |
| 89 | } |
| 90 | } |
| 91 | return Status::OK(); |
| 92 | } |
| 93 | |
| 94 | void AnalyticEvalPlanNode::Close() { |
| 95 | AggFn::Close(analytic_fns_); |