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::SelectBinder Class Reference

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

Inheritance diagram for duckdb::SelectBinder:
Collaboration diagram for duckdb::SelectBinder:

Public Member Functions

 SelectBinder (Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info)
bool TryResolveAliasReference (ColumnRefExpression &colref, idx_t depth, bool root_expression, BindResult &result, unique_ptr< ParsedExpression > &expr_ptr) override
bool DoesColumnAliasExist (const ColumnRefExpression &colref) override
Public Member Functions inherited from duckdb::BaseSelectBinder
 BaseSelectBinder (Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info)
bool BoundAggregates ()
void ResetBindings ()
Public Member Functions inherited from duckdb::ExpressionBinder
 ExpressionBinder (Binder &binder, ClientContext &context, bool replace_binder=false)
unique_ptr< Expression > Bind (unique_ptr< ParsedExpression > &expr, optional_ptr< LogicalType > result_type=nullptr, bool root_expression=true)
bool HasBoundColumns ()
 Returns whether or not any columns have been bound by the expression binder.
const vector< BoundColumnReferenceInfo > & GetBoundColumns ()
void SetCatalogLookupCallback (catalog_entry_callback_t callback)
ErrorData Bind (unique_ptr< ParsedExpression > &expr, idx_t depth, bool root_expression=false)
unique_ptr< ParsedExpression > CreateStructExtract (unique_ptr< ParsedExpression > base, const string &field_name)
 Returns the STRUCT_EXTRACT operator expression.
unique_ptr< ParsedExpression > CreateStructPack (ColumnRefExpression &col_ref)
 Returns a STRUCT_PACK function expression.
BindResult BindQualifiedColumnName (ColumnRefExpression &colref, const string &table_name)
unique_ptr< ParsedExpression > QualifyColumnName (const ParsedExpression &expr, const string &column_name, ErrorData &error)
 Returns a qualified column reference from a column name.
unique_ptr< ParsedExpression > QualifyColumnNameWithManyDots (ColumnRefExpression &col_ref, ErrorData &error)
 Returns a qualified column reference from a column reference with column_names.size() > 2.
virtual unique_ptr< ParsedExpression > QualifyColumnName (ColumnRefExpression &col_ref, ErrorData &error)
 Returns a qualified column reference from a column reference.
void QualifyColumnNamesInLambda (FunctionExpression &function, vector< unordered_set< string > > &lambda_params)
 Enables special-handling of lambda parameters by tracking them in the lambda_params vector.
void QualifyColumnNames (unique_ptr< ParsedExpression > &expr, vector< unordered_set< string > > &lambda_params, const bool within_function_expression=false)
BindResult BindCorrelatedColumns (unique_ptr< ParsedExpression > &expr, ErrorData error_message)
void BindChild (unique_ptr< ParsedExpression > &expr, idx_t depth, ErrorData &error)
void ReplaceMacroParameters (unique_ptr< ParsedExpression > &expr, vector< unordered_set< string > > &lambda_params)
void ReplaceMacroParametersInLambda (FunctionExpression &function, vector< unordered_set< string > > &lambda_params)

Protected Member Functions

void ThrowIfUnnestInLambda (const ColumnBinding &column_binding) override
BindResult BindUnnest (FunctionExpression &function, idx_t depth, bool root_expression) override
unique_ptr< ParsedExpression > GetSQLValueFunction (const string &column_name) override
Protected Member Functions inherited from duckdb::BaseSelectBinder
BindResult BindExpression (unique_ptr< ParsedExpression > &expr_ptr, idx_t depth, bool root_expression=false) override
BindResult BindAggregate (FunctionExpression &expr, AggregateFunctionCatalogEntry &function, idx_t depth) override
BindResult BindGroupingFunction (OperatorExpression &op, idx_t depth) override
virtual BindResult BindWindow (WindowExpression &expr, idx_t depth)
 Binds a WINDOW expression and returns the result.
virtual BindResult BindColumnRef (unique_ptr< ParsedExpression > &expr_ptr, idx_t depth, bool root_expression)
idx_t TryBindGroup (ParsedExpression &expr)
BindResult BindGroup (ParsedExpression &expr, idx_t depth, idx_t group_index)
Protected Member Functions inherited from duckdb::ExpressionBinder
BindResult BindExpression (BetweenExpression &expr, idx_t depth)
BindResult BindExpression (CaseExpression &expr, idx_t depth)
BindResult BindExpression (CollateExpression &expr, idx_t depth)
BindResult BindExpression (CastExpression &expr, idx_t depth)
BindResult BindExpression (ColumnRefExpression &expr, idx_t depth, bool root_expression, unique_ptr< ParsedExpression > &expr_ptr)
BindResult BindExpression (LambdaRefExpression &expr, idx_t depth)
BindResult BindExpression (ComparisonExpression &expr, idx_t depth)
BindResult BindExpression (ConjunctionExpression &expr, idx_t depth)
BindResult BindExpression (ConstantExpression &expr, idx_t depth)
BindResult BindExpression (FunctionExpression &expr, idx_t depth, unique_ptr< ParsedExpression > &expr_ptr)
BindResult BindExpression (TypeExpression &expr, idx_t depth)
BindResult BindExpression (LambdaExpression &expr, idx_t depth, const vector< LogicalType > &function_child_types, optional_ptr< bind_lambda_function_t > bind_lambda_function)
BindResult BindExpression (OperatorExpression &expr, idx_t depth)
BindResult BindExpression (ParameterExpression &expr, idx_t depth)
BindResult BindExpression (SubqueryExpression &expr, idx_t depth)
BindResult BindPositionalReference (unique_ptr< ParsedExpression > &expr, idx_t depth, bool root_expression)
void TransformCapturedLambdaColumn (unique_ptr< Expression > &original, unique_ptr< Expression > &replacement, BoundLambdaExpression &bound_lambda_expr, const optional_ptr< bind_lambda_function_t > bind_lambda_function, const vector< LogicalType > &function_child_types)
void CaptureLambdaColumns (BoundLambdaExpression &bound_lambda_expr, unique_ptr< Expression > &expr, const optional_ptr< bind_lambda_function_t > bind_lambda_function, const vector< LogicalType > &function_child_types)
LogicalType ResolveOperatorType (OperatorExpression &op, vector< unique_ptr< Expression > > &children)
LogicalType ResolveCoalesceType (OperatorExpression &op, vector< unique_ptr< Expression > > &children)
LogicalType ResolveNotType (OperatorExpression &op, vector< unique_ptr< Expression > > &children)
BindResult BindUnsupportedExpression (ParsedExpression &expr, idx_t depth, const string &message)
optional_ptr< CatalogEntry > BindAndQualifyFunction (FunctionExpression &function, bool allow_throw)
virtual BindResult BindFunction (FunctionExpression &expr, ScalarFunctionCatalogEntry &function, idx_t depth)
virtual BindResult BindLambdaFunction (FunctionExpression &expr, ScalarFunctionCatalogEntry &function, idx_t depth)
virtual BindResult BindMacro (FunctionExpression &expr, ScalarMacroCatalogEntry &macro, idx_t depth, unique_ptr< ParsedExpression > &expr_ptr)
void UnfoldMacroExpression (FunctionExpression &function, ScalarMacroCatalogEntry &macro_func, unique_ptr< ParsedExpression > &expr, idx_t depth)
virtual string UnsupportedAggregateMessage ()
virtual string UnsupportedUnnestMessage ()
optional_ptr< CatalogEntry > GetCatalogEntry (const string &catalog, const string &schema, const EntryLookupInfo &lookup_info, OnEntryNotFound on_entry_not_found)
BindResult TryBindLambdaOrJson (FunctionExpression &function, idx_t depth, CatalogEntry &func, const LambdaSyntaxType syntax_type)
unique_ptr< ParsedExpression > QualifyColumnNameWithManyDotsInternal (ColumnRefExpression &col_ref, ErrorData &error, idx_t &struct_extract_start)

Protected Attributes

idx_t unnest_level = 0
Protected Attributes inherited from duckdb::BaseSelectBinder
bool inside_window
bool bound_aggregate = false
BoundSelectNode & node
BoundGroupInformation & info
Protected Attributes inherited from duckdb::ExpressionBinder
Binder & binder
ClientContext & context
optional_ptr< ExpressionBinder > stored_binder
vector< BoundColumnReferenceInfo > bound_columns

Additional Inherited Members

Static Public Member Functions inherited from duckdb::ExpressionBinder
static bool IsPotentialAlias (const ColumnRefExpression &colref)
static void QualifyColumnNames (Binder &binder, unique_ptr< ParsedExpression > &expr)
 Entry point for qualifying the column references of the expression.
static void QualifyColumnNames (ExpressionBinder &binder, unique_ptr< ParsedExpression > &expr)
static bool PushCollation (ClientContext &context, unique_ptr< Expression > &source, const LogicalType &sql_type, CollationType type=CollationType::ALL_COLLATIONS)
static void TestCollation (ClientContext &context, const string &collation)
static void ExtractCorrelatedExpressions (Binder &binder, Expression &expr)
static bool ContainsNullType (const LogicalType &type)
static LogicalType ExchangeNullType (const LogicalType &type)
static bool ContainsType (const LogicalType &type, LogicalTypeId target)
static LogicalType ExchangeType (const LogicalType &type, LogicalTypeId target, LogicalType new_type)
static LogicalType GetExpressionReturnType (const Expression &expr)
static bool IsUnnestFunction (const string &function_name)
 Returns true if the function name is an alias for the UNNEST function.
Public Attributes inherited from duckdb::ExpressionBinder
LogicalType target_type
optional_ptr< DummyBinding > macro_binding
optional_ptr< vector< DummyBinding > > lambda_bindings

Detailed Description

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

Constructor & Destructor Documentation

◆ SelectBinder()

duckdb::SelectBinder::SelectBinder ( Binder & binder,
ClientContext & context,
BoundSelectNode & node,
BoundGroupInformation & info )
378551 : BaseSelectBinder(binder, context, node, info) {
378552}

Member Function Documentation

◆ TryResolveAliasReference()

bool duckdb::SelectBinder::TryResolveAliasReference ( ColumnRefExpression & colref,
idx_t depth,
bool root_expression,
BindResult & result,
unique_ptr< ParsedExpression > & expr_ptr )
overridevirtual

Reimplemented from duckdb::ExpressionBinder.

378555 {
378556 // must be a qualified alias.<name>
378557 if (!ExpressionBinder::IsPotentialAlias(colref)) {
378558 return false;
378559 }
378560
378561 const auto &alias_name = colref.column_names.back();
378562 auto entry = node.bind_state.alias_map.find(alias_name);
378563 if (entry == node.bind_state.alias_map.end()) {
378564 return false;
378565 }
378566
378567 auto alias_index = entry->second;
378568 // Simple way to prevent circular aliasing (`SELECT alias.y as x, alias.x as y;`)
378569 if (alias_index >= node.bound_column_count) {
378570 throw BinderException("Column \"%s\" referenced that exists in the SELECT clause - but this column "
378571 "cannot be referenced before it is defined",
378572 colref.column_names.back());
378573 }
378574
378575 if (node.bind_state.AliasHasSubquery(alias_index)) {
378576 throw BinderException(colref,
378577 "Alias \"%s\" referenced in a SELECT clause - but the expression has a subquery. This is "
378578 "not yet supported.",
378579 alias_name);
378580 }
378581 auto copied_unbound = node.bind_state.BindAlias(alias_index);
378582 result = BindExpression(copied_unbound, depth, false);
378583 return true;
378584}
BindResult BindExpression(unique_ptr< ParsedExpression > &expr_ptr, idx_t depth, bool root_expression=false) override
Definition duckdb.cpp:377336

◆ DoesColumnAliasExist()

bool duckdb::SelectBinder::DoesColumnAliasExist ( const ColumnRefExpression & colref)
overridevirtual

Reimplemented from duckdb::ExpressionBinder.

378586 {
378587 // Using `back()` to support both qualified and unqualified aliasing
378588 auto alias_name = colref.column_names.back();
378589 return node.bind_state.alias_map.find(alias_name) != node.bind_state.alias_map.end();
378590}

◆ ThrowIfUnnestInLambda()

void duckdb::SelectBinder::ThrowIfUnnestInLambda ( const ColumnBinding & column_binding)
overrideprotectedvirtual

Reimplemented from duckdb::ExpressionBinder.

362829 {
362830 // Extract the unnests and check if any match the column index.
362831 for (auto &node_pair : node.unnests) {
362832 auto &unnest_node = node_pair.second;
362833
362834 if (unnest_node.index == column_binding.table_index) {
362835 if (column_binding.column_index < unnest_node.expressions.size()) {
362836 throw BinderException("UNNEST in lambda expressions is not supported");
362837 }
362838 }
362839 }
362840}

◆ BindUnnest()

BindResult duckdb::SelectBinder::BindUnnest ( FunctionExpression & function,
idx_t depth,
bool root_expression )
overrideprotectedvirtual

Reimplemented from duckdb::ExpressionBinder.

362842 {
362843 // bind the children of the function expression
362844 if (depth > 0) {
362845 return BindResult(BinderException(function, "UNNEST() for correlated expressions is not supported yet"));
362846 }
362847
362848 ErrorData error;
362849 if (function.children.empty()) {
362850 return BindResult(BinderException(function, "UNNEST() requires at lease one argument"));
362851 }
362852 if (inside_window) {
362853 return BindResult(BinderException(function, UnsupportedUnnestMessage()));
362854 }
362855
362856 if (function.distinct || function.filter || !function.order_bys->orders.empty()) {
362857 throw InvalidInputException("\"DISTINCT\", \"FILTER\", and \"ORDER BY\" are not "
362858 "applicable to \"UNNEST\"");
362859 }
362860
362861 idx_t max_depth = 1;
362862 bool keep_parent_names = false;
362863 if (function.children.size() != 1) {
362864 bool supported_argument = false;
362865 for (idx_t i = 1; i < function.children.size(); i++) {
362866 if (function.children[i]->HasParameter()) {
362867 throw ParameterNotAllowedException("Parameter not allowed in unnest parameter");
362868 }
362869 if (!function.children[i]->IsScalar()) {
362870 break;
362871 }
362872 auto alias = StringUtil::Lower(function.children[i]->GetAlias());
362873 BindChild(function.children[i], depth, error);
362874 if (error.HasError()) {
362875 return BindResult(std::move(error));
362876 }
362877 auto &const_child = BoundExpression::GetExpression(*function.children[i]);
362878 auto value = ExpressionExecutor::EvaluateScalar(context, *const_child, true);
362879 if (alias == "recursive") {
362880 auto recursive = value.GetValue<bool>();
362881 if (recursive) {
362882 max_depth = NumericLimits<idx_t>::Maximum();
362883 }
362884 } else if (alias == "max_depth") {
362885 max_depth = value.GetValue<uint32_t>();
362886 if (max_depth == 0) {
362887 throw BinderException("UNNEST cannot have a max depth of 0");
362888 }
362889 } else if (alias == "keep_parent_names") {
362890 keep_parent_names = value.GetValue<bool>();
362891 } else if (!alias.empty()) {
362892 throw BinderException("Unsupported parameter \"%s\" for unnest", alias);
362893 } else {
362894 break;
362895 }
362896 supported_argument = true;
362897 }
362898 if (!supported_argument) {
362899 return BindResult(BinderException(
362900 function, "UNNEST - unsupported extra argument, unnest only supports "
362901 "recursive := [true/false], max_depth := # or keep_parent_names := [true/false]"));
362902 }
362903 }
362904 unnest_level++;
362905 BindChild(function.children[0], depth, error);
362906 if (error.HasError()) {
362907 // failed to bind
362908 // try to bind correlated columns manually
362909 auto result = BindCorrelatedColumns(function.children[0], error);
362910 if (result.HasError()) {
362911 return BindResult(result.error);
362912 }
362913 auto &bound_expr = BoundExpression::GetExpression(*function.children[0]);
362914 ExtractCorrelatedExpressions(binder, *bound_expr);
362915 }
362916 auto &child = BoundExpression::GetExpression(*function.children[0]);
362917 child = BoundCastExpression::AddArrayCastToList(context, std::move(child));
362918 auto &child_type = child->return_type;
362919 unnest_level--;
362920
362921 if (unnest_level > 0) {
362922 throw BinderException(
362923 function,
362924 "Nested UNNEST calls are not supported - use UNNEST(x, recursive := true) to unnest multiple levels");
362925 }
362926
362927 switch (child_type.id()) {
362928 case LogicalTypeId::UNKNOWN:
362929 throw ParameterNotResolvedException();
362930 case LogicalTypeId::LIST:
362931 case LogicalTypeId::STRUCT:
362932 case LogicalTypeId::SQLNULL:
362933 break;
362934 default:
362935 return BindResult(BinderException(function, "UNNEST() can only be applied to lists, structs and NULL, not %s",
362936 child_type.ToString()));
362937 }
362938
362939 idx_t list_unnests;
362940 idx_t struct_unnests = 0;
362941
362942 auto unnest_expr = std::move(child);
362943 if (child_type.id() == LogicalTypeId::SQLNULL) {
362944 list_unnests = 1;
362945 } else {
362946 // perform all LIST unnests
362947 auto type = child_type;
362948 list_unnests = 0;
362949 while (type.id() == LogicalTypeId::LIST) {
362950 type = ListType::GetChildType(type);
362951 list_unnests++;
362952 if (list_unnests >= max_depth) {
362953 break;
362954 }
362955 }
362956 // unnest structs
362957 if (type.id() == LogicalTypeId::STRUCT) {
362958 struct_unnests = max_depth - list_unnests;
362959 }
362960 }
362961 if (struct_unnests > 0 && !root_expression) {
362962 child = std::move(unnest_expr);
362963 return BindResult(BinderException(
362964 function, "UNNEST() on a struct column can only be applied as the root element of a SELECT expression"));
362965 }
362966 // perform all LIST unnests
362967 auto return_type = child_type;
362968 for (idx_t current_depth = 0; current_depth < list_unnests; current_depth++) {
362969 if (return_type.id() == LogicalTypeId::LIST) {
362970 return_type = ListType::GetChildType(return_type);
362971 }
362972 auto result = make_uniq<BoundUnnestExpression>(return_type);
362973 result->child = std::move(unnest_expr);
362974 auto alias = function.GetAlias().empty() ? result->ToString() : function.GetAlias();
362975
362976 auto current_level = unnest_level + list_unnests - current_depth - 1;
362977 auto entry = node.unnests.find(current_level);
362978 idx_t unnest_table_index;
362979 idx_t unnest_column_index;
362980 if (entry == node.unnests.end()) {
362981 BoundUnnestNode unnest_node;
362982 unnest_node.index = binder.GenerateTableIndex();
362983 unnest_node.expressions.push_back(std::move(result));
362984 unnest_table_index = unnest_node.index;
362985 unnest_column_index = 0;
362986 node.unnests.insert(make_pair(current_level, std::move(unnest_node)));
362987 } else {
362988 unnest_table_index = entry->second.index;
362989 unnest_column_index = entry->second.expressions.size();
362990 entry->second.expressions.push_back(std::move(result));
362991 }
362992 // now create a column reference referring to the unnest
362993 unnest_expr = make_uniq<BoundColumnRefExpression>(
362994 std::move(alias), return_type, ColumnBinding(unnest_table_index, unnest_column_index), depth);
362995 }
362996 // now perform struct unnests, if any
362997 if (struct_unnests > 0) {
362998 vector<unique_ptr<Expression>> struct_expressions;
362999 struct_expressions.push_back(std::move(unnest_expr));
363000 for (idx_t i = 0; i < struct_unnests; i++) {
363001 vector<unique_ptr<Expression>> new_expressions;
363002 // check if there are any structs left
363003 bool has_structs = false;
363004 for (auto &expr : struct_expressions) {
363005 if (expr->return_type.id() == LogicalTypeId::STRUCT) {
363006 // struct! push a struct_extract
363007 auto &child_types = StructType::GetChildTypes(expr->return_type);
363008 if (StructType::IsUnnamed(expr->return_type)) {
363009 for (idx_t child_index = 0; child_index < child_types.size(); child_index++) {
363010 new_expressions.push_back(
363011 CreateBoundStructExtractIndex(context, expr->Copy(), child_index + 1));
363012 }
363013 } else {
363014 for (auto &entry : child_types) {
363015 vector<string> current_key_path;
363016 // During recursive expansion, not all expressions are BoundFunctionExpression
363017 if (keep_parent_names && expr->type == ExpressionType::BOUND_FUNCTION) {
363018 current_key_path.push_back(expr->alias);
363019 }
363020 current_key_path.push_back(entry.first);
363021 new_expressions.push_back(
363022 CreateBoundStructExtract(context, expr->Copy(), current_key_path, keep_parent_names));
363023 }
363024 }
363025 has_structs = true;
363026 } else {
363027 // not a struct - push as-is
363028 new_expressions.push_back(std::move(expr));
363029 }
363030 }
363031 struct_expressions = std::move(new_expressions);
363032 if (!has_structs) {
363033 break;
363034 }
363035 }
363036 unnest_expr = make_uniq<BoundExpandedExpression>(std::move(struct_expressions));
363037 }
363038 return BindResult(std::move(unnest_expr));
363039}
static DUCKDB_API unique_ptr< Expression > AddArrayCastToList(ClientContext &context, unique_ptr< Expression > expr)
If the expression returns an array, cast it to return a list with the same child type....
Definition duckdb.cpp:375877
static DUCKDB_API Value EvaluateScalar(ClientContext &context, const Expression &expr, bool allow_unfoldable=false)
Evaluate a scalar expression and fold it into a single value.
Definition duckdb.cpp:149993
static DUCKDB_API string Lower(const string &str)
Convert a string to lowercase.
Definition duckdb.cpp:109630
::uint32_t uint32_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ GetSQLValueFunction()

unique_ptr< ParsedExpression > duckdb::SelectBinder::GetSQLValueFunction ( const string & column_name)
overrideprotectedvirtual

Reimplemented from duckdb::ExpressionBinder.

378592 {
378593 auto alias_entry = node.bind_state.alias_map.find(column_name);
378594 if (alias_entry != node.bind_state.alias_map.end()) {
378595 // don't replace SQL value functions if they are in the alias map
378596 return nullptr;
378597 }
378598 return ExpressionBinder::GetSQLValueFunction(column_name);
378599}

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