Skip to content

Commit

Permalink
Only bind to SQL value functions if there is no alias with this name …
Browse files Browse the repository at this point in the history
…present we can bind to instead (#13925)

Fixes an issue where aliases like `CURRENT_TIMESTAMP` would behave
differently from regular aliases in the select list or oder by clause
  • Loading branch information
Mytherin authored Sep 13, 2024
2 parents 8ffd8ad + 87d33a3 commit b369bcb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/include/duckdb/planner/expression_binder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ExpressionBinder {
const optional_ptr<bind_lambda_function_t> bind_lambda_function,
const LogicalType &list_child_type);

static unique_ptr<ParsedExpression> GetSQLValueFunction(const string &column_name);
virtual unique_ptr<ParsedExpression> GetSQLValueFunction(const string &column_name);

LogicalType ResolveOperatorType(OperatorExpression &op, vector<unique_ptr<Expression>> &children);
LogicalType ResolveCoalesceType(OperatorExpression &op, vector<unique_ptr<Expression>> &children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SelectBinder : public BaseSelectBinder {
BindResult BindColumnRef(unique_ptr<ParsedExpression> &expr_ptr, idx_t depth, bool root_expression) override;

bool QualifyColumnAlias(const ColumnRefExpression &colref) override;
unique_ptr<ParsedExpression> GetSQLValueFunction(const string &column_name) override;

protected:
idx_t unnest_level = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/planner/expression_binder/select_binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ SelectBinder::SelectBinder(Binder &binder, ClientContext &context, BoundSelectNo
: BaseSelectBinder(binder, context, node, info) {
}

unique_ptr<ParsedExpression> SelectBinder::GetSQLValueFunction(const string &column_name) {
auto alias_entry = node.bind_state.alias_map.find(column_name);
if (alias_entry != node.bind_state.alias_map.end()) {
// don't replace SQL value functions if they are in the alias map
return nullptr;
}
return ExpressionBinder::GetSQLValueFunction(column_name);
}

BindResult SelectBinder::BindColumnRef(unique_ptr<ParsedExpression> &expr_ptr, idx_t depth, bool root_expression) {
// first try to bind the column reference regularly
auto result = BaseSelectBinder::BindColumnRef(expr_ptr, depth, root_expression);
Expand Down
13 changes: 13 additions & 0 deletions test/sql/parser/test_value_functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ group by one
having max(cast('1000-05-01 00:00:00' as timestamp)) <= current_timestamp;
----
1 1000-05-01 00:00:00

query II
select a as "b", "b" + 1 from (VALUES (84), (42)) t(a) ORDER BY ALL;
----
42 43
84 85

# value function conflict in ORDER BY
query I
select a as "CURRENT_TIMESTAMP" from (VALUES (84), (42)) t(a) order by "CURRENT_TIMESTAMP" + 1;
----
42
84

0 comments on commit b369bcb

Please sign in to comment.