Binds a WINDOW expression and returns the result.
363321 {
363322 QueryErrorContext error_context(window.GetQueryLocation());
363323
363324
363325 EntryLookupInfo function_lookup(CatalogType::SCALAR_FUNCTION_ENTRY, window.function_name, error_context);
363326 auto entry = GetCatalogEntry(window.catalog, window.schema, function_lookup, OnEntryNotFound::RETURN_NULL);
363327 if (window.GetExpressionType() == ExpressionType::WINDOW_AGGREGATE && entry &&
363328 entry->type == CatalogType::MACRO_ENTRY) {
363329 auto macro = make_uniq<FunctionExpression>(window.catalog, window.schema, window.function_name,
363330 std::move(window.children), std::move(window.filter_expr), nullptr,
363331 window.distinct);
363332 auto macro_expr = window.Copy();
363333 return BindMacro(*macro, entry->Cast<ScalarMacroCatalogEntry>(), depth, macro_expr);
363334 }
363335
363336 auto name = window.alias;
363337
363338 if (inside_window) {
363339 throw BinderException(error_context, "window function calls cannot be nested");
363340 }
363341 if (depth > 0) {
363342 throw BinderException(error_context, "correlated columns in window functions not supported");
363343 }
363344
363345
363346 const auto is_range =
363347 (window.start == WindowBoundary::EXPR_PRECEDING_RANGE || window.start == WindowBoundary::EXPR_FOLLOWING_RANGE ||
363348 window.end == WindowBoundary::EXPR_PRECEDING_RANGE || window.end == WindowBoundary::EXPR_FOLLOWING_RANGE);
363349 if (is_range && window.orders.size() != 1) {
363350 throw BinderException(error_context, "RANGE frames must have only one ORDER BY expression");
363351 } else if (window.GetExpressionType() == ExpressionType::WINDOW_FILL &&
363352 (window.arg_orders.size() > 1 || (window.arg_orders.empty() && window.orders.size() != 1))) {
363353 throw BinderException(error_context, "FILL functions must have only one ORDER BY expression");
363354 }
363355
363356
363357 inside_window = true;
363358 ErrorData error;
363359 for (auto &child : window.children) {
363360 BindChild(child, depth, error);
363361 }
363362 for (auto &child : window.partitions) {
363363 BindChild(child, depth, error);
363364 }
363365 for (auto &order : window.orders) {
363366 BindChild(order.expression, depth, error);
363367
363368
363369
363370 if (!is_range || error.HasError()) {
363371 continue;
363372 }
363373 auto &order_expr = order.expression;
363374 auto &bound_order = BoundExpression::GetExpression(*order_expr);
363375 const auto type_id = bound_order->return_type.id();
363376 if (type_id == LogicalTypeId::TIME || type_id == LogicalTypeId::TIME_TZ) {
363377
363378 unique_ptr<ParsedExpression> epoch = make_uniq<ConstantExpression>(
Value::DATE(date_t::epoch()));
363379 BindChild(epoch, depth, error);
363380 BindRangeExpression(context, "+", order.expression, epoch);
363381 }
363382 }
363383 BindChild(window.filter_expr, depth, error);
363384 BindChild(window.start_expr, depth, error);
363385 BindChild(window.end_expr, depth, error);
363386 BindChild(window.offset_expr, depth, error);
363387 BindChild(window.default_expr, depth, error);
363388
363389 for (auto &order : window.arg_orders) {
363390 BindChild(order.expression, depth, error);
363391 }
363392
363393 inside_window = false;
363394 if (error.HasError()) {
363395
363396 return BindResult(std::move(error));
363397 }
363398
363399
363400 for (auto &order : window.arg_orders) {
363401 auto &order_expr = order.expression;
363402 auto &bound_order = BoundExpression::GetExpression(*order_expr);
363403 ExpressionBinder::PushCollation(context, bound_order, bound_order->return_type);
363404 }
363405 for (auto &part_expr : window.partitions) {
363406 auto &bound_partition = BoundExpression::GetExpression(*part_expr);
363407 ExpressionBinder::PushCollation(context, bound_partition, bound_partition->return_type);
363408 }
363409 for (auto &order : window.orders) {
363410 auto &order_expr = order.expression;
363411 auto &bound_order = BoundExpression::GetExpression(*order_expr);
363412 ExpressionBinder::PushCollation(context, bound_order, bound_order->return_type);
363413 }
363414
363415 vector<LogicalType> types;
363416 vector<unique_ptr<Expression>> children;
363417 for (auto &child : window.children) {
363418 D_ASSERT(child.get());
363419 D_ASSERT(child->GetExpressionClass() == ExpressionClass::BOUND_EXPRESSION);
363420 auto &bound = BoundExpression::GetExpression(*child);
363421
363422 const auto argno = children.size();
363423 switch (window.GetExpressionType()) {
363424 case ExpressionType::WINDOW_NTILE:
363425
363426 if (argno == 0) {
363428 }
363429 break;
363430 case ExpressionType::WINDOW_NTH_VALUE:
363431
363432 if (argno == 1) {
363434 }
363435 break;
363436 default:
363437 break;
363438 }
363439 types.push_back(bound->return_type);
363440 children.push_back(std::move(bound));
363441 }
363442
363443 LogicalType sql_type;
363444 unique_ptr<AggregateFunction> aggregate;
363445 unique_ptr<FunctionData> bind_info;
363446 if (window.GetExpressionType() == ExpressionType::WINDOW_AGGREGATE) {
363447
363448 if (!entry || entry->type != CatalogType::AGGREGATE_FUNCTION_ENTRY) {
363449
363451 window.function_name, error_context);
363452 }
363453 auto &func = entry->Cast<AggregateFunctionCatalogEntry>();
363454 D_ASSERT(func.type == CatalogType::AGGREGATE_FUNCTION_ENTRY);
363455
363456
363457 ErrorData error_aggr;
363458 FunctionBinder function_binder(context);
363459 auto best_function = function_binder.BindFunction(func.name, func.functions, types, error_aggr);
363460 if (!best_function.IsValid()) {
363461 error_aggr.AddQueryLocation(window);
363462 error_aggr.
Throw();
363463 }
363464
363465
363466 auto bound_function = func.functions.GetFunctionByOffset(best_function.GetIndex());
363467
363468 if (!bound_function.CanAggregate() && bound_function.CanWindow() && !window.arg_orders.empty()) {
363469
363470
363471 ErrorData err(BinderException("Function '%s' cannot be used as a window with ORDER BY arguments",
363472 bound_function.name));
363473 err.AddQueryLocation(window);
363474 err.Throw();
363475 }
363476
363477 auto window_bound_aggregate = function_binder.BindAggregateFunction(bound_function, std::move(children));
363478
363479 aggregate = make_uniq<AggregateFunction>(window_bound_aggregate->function);
363480 bind_info = std::move(window_bound_aggregate->bind_info);
363481 children = std::move(window_bound_aggregate->children);
363482 sql_type = window_bound_aggregate->return_type;
363483
363484 } else {
363485
363486 sql_type = ResolveWindowExpressionType(window.GetExpressionType(), types);
363487 }
363488 auto result = make_uniq<BoundWindowExpression>(window.GetExpressionType(), sql_type, std::move(aggregate),
363489 std::move(bind_info));
363490 result->children = std::move(children);
363491 for (auto &child : window.partitions) {
363492 result->partitions.push_back(GetExpression(child));
363493 }
363494 result->ignore_nulls = window.ignore_nulls;
363495 result->distinct = window.distinct;
363496
363497
363498
363499
363500
363501 auto &config = DBConfig::GetConfig(context);
363502 auto range_sense = OrderType::INVALID;
363503 LogicalType start_type = LogicalType::BIGINT;
363504 if (window.start == WindowBoundary::EXPR_PRECEDING_RANGE) {
363505 D_ASSERT(window.orders.size() == 1);
363506 range_sense = config.ResolveOrder(context, window.orders[0].type);
363507 const auto range_name = (range_sense == OrderType::ASCENDING) ? "-" : "+";
363508 start_type = BindRangeExpression(context, range_name, window.start_expr, window.orders[0].expression);
363509
363510 } else if (window.start == WindowBoundary::EXPR_FOLLOWING_RANGE) {
363511 D_ASSERT(window.orders.size() == 1);
363512 range_sense = config.ResolveOrder(context, window.orders[0].type);
363513 const auto range_name = (range_sense == OrderType::ASCENDING) ? "+" : "-";
363514 start_type = BindRangeExpression(context, range_name, window.start_expr, window.orders[0].expression);
363515 }
363516
363517 LogicalType end_type = LogicalType::BIGINT;
363518 if (window.end == WindowBoundary::EXPR_PRECEDING_RANGE) {
363519 D_ASSERT(window.orders.size() == 1);
363520 range_sense = config.ResolveOrder(context, window.orders[0].type);
363521 const auto range_name = (range_sense == OrderType::ASCENDING) ? "-" : "+";
363522 end_type = BindRangeExpression(context, range_name, window.end_expr, window.orders[0].expression);
363523
363524 } else if (window.end == WindowBoundary::EXPR_FOLLOWING_RANGE) {
363525 D_ASSERT(window.orders.size() == 1);
363526 range_sense = config.ResolveOrder(context, window.orders[0].type);
363527 const auto range_name = (range_sense == OrderType::ASCENDING) ? "+" : "-";
363528 end_type = BindRangeExpression(context, range_name, window.end_expr, window.orders[0].expression);
363529 }
363530
363531
363532 if (range_sense != OrderType::INVALID) {
363533 D_ASSERT(window.orders.size() == 1);
363534
363535 auto &order_expr = window.orders[0].expression;
363536 D_ASSERT(order_expr.get());
363537 D_ASSERT(order_expr->GetExpressionClass() == ExpressionClass::BOUND_EXPRESSION);
363538 auto &bound_order = BoundExpression::GetExpression(*order_expr);
363539 auto order_type = bound_order->return_type;
363540 if (window.start_expr) {
363542 }
363543 if (window.end_expr) {
363545 }
363546
363547
363549 start_type = end_type = order_type;
363550 }
363551
363552 for (auto &order : window.orders) {
363553 auto type = config.ResolveOrder(context, order.type);
363554 auto null_order = config.ResolveNullOrder(context, type, order.null_order);
363555 auto expression = GetExpression(order.expression);
363556 result->orders.emplace_back(type, null_order, std::move(expression));
363557 }
363558
363559
363560 for (auto &order : window.arg_orders) {
363561 auto type = config.ResolveOrder(context, order.type);
363562 auto null_order = config.ResolveNullOrder(context, type, order.null_order);
363563 auto expression = GetExpression(order.expression);
363564 result->arg_orders.emplace_back(type, null_order, std::move(expression));
363565 }
363566
363567
363568 if (window.GetExpressionType() == ExpressionType::WINDOW_FILL) {
363569 D_ASSERT(!result->children.empty());
363570 if (!IsFillType(result->children[0]->return_type)) {
363571 throw BinderException(error_context, "FILL argument must support subtraction");
363572 }
363573 LogicalType order_type;
363574 if (window.arg_orders.empty()) {
363575 D_ASSERT(!result->orders.empty());
363576 order_type = result->orders[0].expression->return_type;
363577 } else {
363578 order_type = result->arg_orders[0].expression->return_type;
363579 }
363580 if (!IsFillType(order_type)) {
363581 throw BinderException(error_context, "FILL ordering must support subtraction");
363582 }
363583 }
363584
363585 result->filter_expr = CastWindowExpression(window.filter_expr, LogicalType::BOOLEAN);
363586 result->start_expr = CastWindowExpression(window.start_expr, start_type);
363587 result->end_expr = CastWindowExpression(window.end_expr, end_type);
363588 result->offset_expr = CastWindowExpression(window.offset_expr, LogicalType::BIGINT);
363589 result->default_expr = CastWindowExpression(window.default_expr, result->return_type);
363590 result->start = window.start;
363591 result->end = window.end;
363592 result->exclude_clause = window.exclude_clause;
363593
363594
363595 auto colref = make_uniq<BoundColumnRefExpression>(std::move(name), result->return_type,
363596 ColumnBinding(node.window_index, node.windows.size()), depth);
363597
363598 node.windows.push_back(std::move(result));
363599 return BindResult(std::move(colref));
363600}
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
DUCKDB_API void Throw(const string &prepended_message="") const
Throw the error.
Definition duckdb.cpp:70200
static DUCKDB_API Value DATE(date_t date)
Create a date Value from a specified date.
Definition duckdb.cpp:132338
static DUCKDB_API LogicalType MaxLogicalType(ClientContext &context, const LogicalType &left, const LogicalType &right)
Returns the maximum logical type when combining the two types - or throws an exception if combining i...
Definition duckdb.cpp:140334