Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::OrderBinder Class Reference

The ORDER binder is responsible for binding an expression within the ORDER BY clause of a SQL statement. More...

Collaboration diagram for duckdb::OrderBinder:

Public Member Functions

 OrderBinder (vector< reference< Binder > > binders, SelectBindState &bind_state)
 OrderBinder (vector< reference< Binder > > binders, SelectNode &node, SelectBindState &bind_state)
unique_ptr< Expression > Bind (unique_ptr< ParsedExpression > expr)
bool HasExtraList () const
const vector< reference< Binder > > & GetBinders () const
unique_ptr< Expression > CreateExtraReference (unique_ptr< ParsedExpression > expr)
void SetQueryComponent (string component=string())
 Sets the query component, for error messages.

Private Member Functions

unique_ptr< Expression > CreateProjectionReference (ParsedExpression &expr, const idx_t index)
unique_ptr< Expression > BindConstant (ParsedExpression &expr)
optional_idx TryGetProjectionReference (ParsedExpression &expr) const

Private Attributes

vector< reference< Binder > > binders
optional_ptr< vector< unique_ptr< ParsedExpression > > > extra_list
SelectBindState & bind_state
string query_component = "ORDER BY"

Detailed Description

The ORDER binder is responsible for binding an expression within the ORDER BY clause of a SQL statement.

Constructor & Destructor Documentation

◆ OrderBinder() [1/2]

duckdb::OrderBinder::OrderBinder ( vector< reference< Binder > > binders,
SelectBindState & bind_state )
378138 : binders(std::move(binders)), extra_list(nullptr), bind_state(bind_state) {
378139}

◆ OrderBinder() [2/2]

duckdb::OrderBinder::OrderBinder ( vector< reference< Binder > > binders,
SelectNode & node,
SelectBindState & bind_state )
378141 : binders(std::move(binders)), bind_state(bind_state) {
378142 this->extra_list = &node.select_list;
378143}

Member Function Documentation

◆ Bind()

unique_ptr< Expression > duckdb::OrderBinder::Bind ( unique_ptr< ParsedExpression > expr)
378256 {
378257 // in the ORDER BY clause we do not bind children
378258 // we bind ONLY to the select list
378259 // if there is no matching entry in the SELECT list already, we add the expression to the SELECT list and refer the
378260 // new expression the new entry will then be bound later during the binding of the SELECT list we also don't do type
378261 // resolution here: this only happens after the SELECT list has been bound
378262 switch (expr->GetExpressionClass()) {
378263 case ExpressionClass::CONSTANT: {
378264 // ORDER BY constant
378265 // is the ORDER BY expression a constant integer? (e.g. ORDER BY 1)
378266 return BindConstant(*expr);
378267 }
378268 case ExpressionClass::COLUMN_REF:
378269 case ExpressionClass::POSITIONAL_REFERENCE: {
378270 // COLUMN REF expression
378271 // check if we can bind it to an alias in the select list
378272 auto index = TryGetProjectionReference(*expr);
378273 if (index.IsValid()) {
378274 return CreateProjectionReference(*expr, index.GetIndex());
378275 }
378276 break;
378277 }
378278 case ExpressionClass::PARAMETER: {
378279 throw ParameterNotAllowedException("Parameter not supported in %s clause", query_component);
378280 }
378281 case ExpressionClass::COLLATE: {
378282 auto &collation = expr->Cast<CollateExpression>();
378283 auto collation_index = TryGetProjectionReference(*collation.child);
378284 if (collation_index.IsValid()) {
378285 child_list_t<Value> values;
378286 values.push_back(make_pair("index", Value::UBIGINT(collation_index.GetIndex())));
378287 values.push_back(make_pair("collation", Value(std::move(collation.collation))));
378288 return make_uniq<BoundConstantExpression>(Value::STRUCT(std::move(values)));
378289 }
378290 break;
378291 }
378292 default:
378293 break;
378294 }
378295 // general case
378296 // first bind the table names of this entry
378297 for (auto &binder : binders) {
378298 ExpressionBinder::QualifyColumnNames(binder.get(), expr);
378299 }
378300 // first check if the ORDER BY clause already points to an entry in the projection list
378301 auto entry = bind_state.projection_map.find(*expr);
378302 if (entry != bind_state.projection_map.end()) {
378303 if (entry->second == DConstants::INVALID_INDEX) {
378304 throw BinderException("Ambiguous reference to column");
378305 }
378306 // there is a matching entry in the projection list
378307 // just point to that entry
378308 return CreateProjectionReference(*expr, entry->second);
378309 }
378310 if (!extra_list) {
378311 // no extra list specified: we cannot push an extra ORDER BY clause
378312 throw BinderException("Could not ORDER BY column \"%s\": add the expression/function to every SELECT, or move "
378313 "the UNION into a FROM clause.",
378314 expr->ToString());
378315 }
378316 // otherwise we need to push the ORDER BY entry into the select list
378317 return CreateExtraReference(std::move(expr));
378318}
void QualifyColumnNames(unique_ptr< ParsedExpression > &expr, vector< unordered_set< string > > &lambda_params, const bool within_function_expression=false)
Definition duckdb.cpp:360116
static DUCKDB_API Value STRUCT(child_list_t< Value > values)
Create a struct value with given list of entries.
Definition duckdb.cpp:132434
static DUCKDB_API Value UBIGINT(uint64_t value)
Create an unsigned bigint Value from a specified value.
Definition duckdb.cpp:132202
index
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117

◆ HasExtraList()

bool duckdb::OrderBinder::HasExtraList ( ) const
inline
364084 {
364085 return extra_list;
364086 }

◆ GetBinders()

const vector< reference< Binder > > & duckdb::OrderBinder::GetBinders ( ) const
inline
364087 {
364088 return binders;
364089 }

◆ CreateExtraReference()

unique_ptr< Expression > duckdb::OrderBinder::CreateExtraReference ( unique_ptr< ParsedExpression > expr)
378160 {
378161 if (!extra_list) {
378162 throw InternalException("CreateExtraReference called without extra_list");
378163 }
378164 bind_state.projection_map[*expr] = extra_list->size();
378165 auto result = CreateProjectionReference(*expr, extra_list->size());
378166 extra_list->push_back(std::move(expr));
378167 return result;
378168}

◆ SetQueryComponent()

void duckdb::OrderBinder::SetQueryComponent ( string component = string())

Sets the query component, for error messages.

378235 {
378236 if (component.empty()) {
378237 query_component = "ORDER BY";
378238 } else {
378239 query_component = std::move(component);
378240 }
378241}

◆ CreateProjectionReference()

unique_ptr< Expression > duckdb::OrderBinder::CreateProjectionReference ( ParsedExpression & expr,
const idx_t index )
private
378145 {
378146 string alias;
378147 if (extra_list && index < extra_list->size()) {
378148 alias = extra_list->at(index)->ToString();
378149 } else {
378150 if (!expr.GetAlias().empty()) {
378151 alias = expr.GetAlias();
378152 }
378153 }
378154 auto result = make_uniq<BoundConstantExpression>(Value::UBIGINT(index));
378155 result->SetAlias(std::move(alias));
378156 result->SetQueryLocation(expr.GetQueryLocation());
378157 return std::move(result);
378158}
GOpaque< Size > size(const GMat &src)

◆ BindConstant()

unique_ptr< Expression > duckdb::OrderBinder::BindConstant ( ParsedExpression & expr)
private
378243 {
378244 auto index = TryGetProjectionReference(expr);
378245 if (!index.IsValid()) {
378246 return nullptr;
378247 }
378248 child_list_t<Value> values;
378249 values.push_back(make_pair("index", Value::UBIGINT(index.GetIndex())));
378250 auto result = make_uniq<BoundConstantExpression>(Value::STRUCT(std::move(values)));
378251 result->SetAlias(expr.GetAlias());
378252 result->SetQueryLocation(expr.GetQueryLocation());
378253 return std::move(result);
378254}

◆ TryGetProjectionReference()

optional_idx duckdb::OrderBinder::TryGetProjectionReference ( ParsedExpression & expr) const
private
378170 {
378171 switch (expr.GetExpressionClass()) {
378172 case ExpressionClass::CONSTANT: {
378173 auto &constant = expr.Cast<ConstantExpression>();
378174 // ORDER BY a constant
378175 if (!constant.value.type().IsIntegral()) {
378176 // non-integral expression
378177 // ORDER BY <constant> has no effect
378178 // this is disabled by default (matching Postgres) - but we can control this with a setting
378179 auto order_by_non_integer_literal =
378180 Settings::Get<OrderByNonIntegerLiteralSetting>(binders[0].get().context);
378181 if (!order_by_non_integer_literal) {
378182 throw BinderException(expr,
378183 "%s non-integer literal has no effect.\n* SET "
378184 "order_by_non_integer_literal=true to allow this behavior.",
378185 query_component);
378186 }
378187 break;
378188 }
378189 // INTEGER constant: we use the integer as an index into the select list (e.g. ORDER BY 1)
378190 auto order_value = constant.value.GetValue<int64_t>();
378191 return static_cast<idx_t>(order_value <= 0 ? NumericLimits<int64_t>::Maximum() : order_value - 1);
378192 }
378193 case ExpressionClass::COLUMN_REF: {
378194 auto &colref = expr.Cast<ColumnRefExpression>();
378195 if (!ExpressionBinder::IsPotentialAlias(colref)) {
378196 break;
378197 }
378198
378199 string alias_name = colref.column_names.back();
378200 // check the alias list
378201 auto entry = bind_state.alias_map.find(alias_name);
378202 if (entry != bind_state.alias_map.end()) {
378203 // this is an alias - return the index
378204 return entry->second;
378205 }
378206 // check the expression list
378207 vector<idx_t> matching_columns;
378208 for (idx_t i = 0; i < bind_state.original_expressions.size(); i++) {
378209 if (bind_state.original_expressions[i]->type != ExpressionType::COLUMN_REF) {
378210 continue;
378211 }
378212 auto &colref = bind_state.original_expressions[i]->Cast<ColumnRefExpression>();
378213 if (colref.HasAlias()) {
378214 continue;
378215 }
378216 if (colref.GetColumnName() == alias_name) {
378217 matching_columns.push_back(i);
378218 }
378219 }
378220 if (matching_columns.size() == 1) {
378221 return matching_columns[0];
378222 }
378223 break;
378224 }
378225 case ExpressionClass::POSITIONAL_REFERENCE: {
378226 auto &posref = expr.Cast<PositionalReferenceExpression>();
378227 return posref.index - 1;
378228 }
378229 default:
378230 break;
378231 }
378232 return optional_idx();
378233}
__host__ cudaTextureObject_t get() const noexcept
::int64_t int64_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

The documentation for this class was generated from the following file: