| 1043 | } |
| 1044 | |
| 1045 | void ExpressionActionsChain::finalize() |
| 1046 | { |
| 1047 | /// Finalize all steps. Right to left to define unnecessary input columns. |
| 1048 | for (int i = static_cast<int>(steps.size()) - 1; i >= 0; --i) |
| 1049 | { |
| 1050 | auto & required_output = steps[i]->required_output; |
| 1051 | NameSet required_names; |
| 1052 | for (const auto & output : required_output) |
| 1053 | required_names.insert(output.first); |
| 1054 | |
| 1055 | if (i + 1 < static_cast<int>(steps.size())) |
| 1056 | { |
| 1057 | const NameSet & additional_input = steps[i + 1]->additional_input; |
| 1058 | for (const auto & it : steps[i + 1]->getRequiredColumns()) |
| 1059 | { |
| 1060 | if (!additional_input.contains(it.name)) |
| 1061 | { |
| 1062 | auto iter = required_output.find(it.name); |
| 1063 | if (iter == required_output.end()) |
| 1064 | required_names.insert(it.name); |
| 1065 | else |
| 1066 | iter->second = false; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | steps[i]->finalize(required_names); |
| 1071 | } |
| 1072 | |
| 1073 | /// Adding the ejection of unnecessary columns to the beginning of each step. |
| 1074 | for (size_t i = 1; i < steps.size(); ++i) |
| 1075 | { |
| 1076 | size_t columns_from_previous = steps[i - 1]->getResultColumns().size(); |
| 1077 | |
| 1078 | /// If unnecessary columns are formed at the output of the previous step, we'll add them to the beginning of this step. |
| 1079 | /// Except when we drop all the columns and lose the number of rows in the block. |
| 1080 | if (!steps[i]->getResultColumns().empty() |
| 1081 | && columns_from_previous > steps[i]->getRequiredColumns().size()) |
| 1082 | steps[i]->prependProjectInput(); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | ExpressionActionsChainSteps::ExpressionActionsStep * ExpressionActionsChain::getLastExpressionStep(bool allow_empty) |
| 1087 | { |