6055 column alias in where clause results in binder error #6162
+42
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix for #6055
Alias2 references the first node in the select list, which is alias1. alias1 references the 0th index in the select list, which is cte.a. The issue is that don't keep track of what alias's we have visited when binding a where clause. The same issue happens in the following SQL query
select i as b, b as c, c as d, d as e from a where e > 5;
The solution is the following. Instead of keeping track if we are in an alias or not, we keep track of what indexes in our select list we end up referencing when binding aliases. If we follow a rabbit hole of alias binding, we will eventually be able to detect a self-referencing alias, or we will be able to find the desired column to bind to.
This doesn't happen in the SelectBinder because we have access to the BoundSelectNode instance in the select binder (i.e
node.select_list
. We can then just copy the node in the select list the alias refers to. We are assured this node is bound because it comes earlier in the select list. A where binder doesn't have access to a BoundSelectNode.