| 1045 | } |
| 1046 | |
| 1047 | ExtensionIdRegistry::SubstraitAggregateToArrow DecodeBasicAggregate( |
| 1048 | const std::string& arrow_function_name) { |
| 1049 | return [arrow_function_name](const SubstraitCall& call) -> Result<compute::Aggregate> { |
| 1050 | std::string fixed_arrow_func; |
| 1051 | if (call.is_hash()) { |
| 1052 | fixed_arrow_func = "hash_"; |
| 1053 | } |
| 1054 | |
| 1055 | switch (call.size()) { |
| 1056 | case 0: { |
| 1057 | if (call.id().name == "count") { |
| 1058 | fixed_arrow_func += "count_all"; |
| 1059 | return compute::Aggregate{std::move(fixed_arrow_func), ""}; |
| 1060 | } |
| 1061 | return Status::Invalid("Expected aggregate call ", call.id().uri, "#", |
| 1062 | call.id().name, " to have at least one argument"); |
| 1063 | } |
| 1064 | default: { |
| 1065 | // Handles all arity > 0 |
| 1066 | |
| 1067 | std::shared_ptr<compute::FunctionOptions> options = nullptr; |
| 1068 | if (arrow_function_name == "stddev" || arrow_function_name == "variance") { |
| 1069 | // See the following URL for the spec of stddev and variance: |
| 1070 | // https://github.com/substrait-io/substrait/blob/ |
| 1071 | // 73228b4112d79eb1011af0ebb41753ce23ca180c/ |
| 1072 | // extensions/functions_arithmetic.yaml#L1240 |
| 1073 | auto maybe_dist = call.GetOption("distribution"); |
| 1074 | if (maybe_dist) { |
| 1075 | auto& prefs = **maybe_dist; |
| 1076 | if (prefs.size() != 1) { |
| 1077 | return Status::Invalid("expected a single preference for ", |
| 1078 | arrow_function_name, " but got ", prefs.size()); |
| 1079 | } |
| 1080 | int ddof; |
| 1081 | if (prefs[0] == "POPULATION") { |
| 1082 | ddof = 1; |
| 1083 | } else if (prefs[0] == "SAMPLE") { |
| 1084 | ddof = 0; |
| 1085 | } else { |
| 1086 | return Status::Invalid("unknown distribution preference ", prefs[0]); |
| 1087 | } |
| 1088 | options = std::make_shared<compute::VarianceOptions>(ddof); |
| 1089 | } |
| 1090 | } |
| 1091 | fixed_arrow_func += arrow_function_name; |
| 1092 | |
| 1093 | std::vector<FieldRef> target; |
| 1094 | for (int i = 0; i < call.size(); i++) { |
| 1095 | ARROW_ASSIGN_OR_RAISE(compute::Expression arg, call.GetValueArg(i)); |
| 1096 | const FieldRef* arg_ref = arg.field_ref(); |
| 1097 | if (!arg_ref) { |
| 1098 | return Status::Invalid("Expected an aggregate call ", call.id().uri, "#", |
| 1099 | call.id().name, " to have a direct reference"); |
| 1100 | } |
| 1101 | // Copy arg_ref here because field_ref() return const FieldRef* |
| 1102 | target.emplace_back(*arg_ref); |
| 1103 | } |
| 1104 | return compute::Aggregate{std::move(fixed_arrow_func), |