321698 {
321699 if (!aggr.order_bys) {
321700
321701 return nullptr;
321702 }
321703 if (aggr.function.GetOrderDependent() == AggregateOrderDependent::NOT_ORDER_DEPENDENT) {
321704
321705 aggr.order_bys.reset();
321706 changes_made = true;
321707 return nullptr;
321708 }
321709
321710
321711 if (aggr.order_bys->Simplify(groups, grouping_sets)) {
321712 aggr.order_bys.reset();
321713 changes_made = true;
321714 return nullptr;
321715 }
321716
321717
321718 const auto &aggr_name = aggr.function.name;
321719 string arg_xxx_name;
321720 if (aggr_name == "last") {
321721 arg_xxx_name = "arg_max_null";
321722 } else if (aggr_name == "first" || aggr_name == "arbitrary") {
321723 arg_xxx_name = "arg_min_null";
321724 } else if (aggr_name == "any_value") {
321725 arg_xxx_name = "arg_min";
321726 } else {
321727 return nullptr;
321728 }
321729
321730 FunctionBinder binder(context);
321731 vector<unique_ptr<Expression>> sort_children;
321732 for (auto &order : aggr.order_bys->orders) {
321733 sort_children.emplace_back(std::move(order.expression));
321734 sort_children.emplace_back(make_uniq<BoundConstantExpression>(Value(order.GetOrderModifier())));
321735 }
321736 aggr.order_bys.reset();
321737
321738 ErrorData error;
321739 auto sort_key = binder.BindScalarFunction(DEFAULT_SCHEMA, "create_sort_key", std::move(sort_children), error);
321740 if (!sort_key) {
321741 error.Throw();
321742 }
321743
321744 auto &children = aggr.children;
321745 children.emplace_back(std::move(sort_key));
321746
321747
321748 QueryErrorContext error_context;
321750 error_context);
321751 D_ASSERT(func.type == CatalogType::AGGREGATE_FUNCTION_ENTRY);
321752
321753
321754 vector<LogicalType> types;
321755 for (const auto &child : children) {
321756 types.emplace_back(child->return_type);
321757 }
321758 auto best_function = binder.BindFunction(func.name, func.functions, types, error);
321759 if (!best_function.IsValid()) {
321760 error.Throw();
321761 }
321762
321763 auto bound_function = func.functions.GetFunctionByOffset(best_function.GetIndex());
321764 return binder.BindAggregateFunction(bound_function, std::move(children), std::move(aggr.filter),
321765 aggr.IsDistinct() ? AggregateType::DISTINCT : AggregateType::NON_DISTINCT);
321766}
DUCKDB_API optional_ptr< CatalogEntry > GetEntry(ClientContext &context, const string &schema, const EntryLookupInfo &lookup_info, OnEntryNotFound if_not_found)
Gets the "schema.name" entry of the specified type, if entry does not exist behavior depends on OnEnt...
Definition duckdb.cpp:8156