Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Param as column alias can't be parsed in group statement #53872

Closed
pcqz opened this issue Jun 7, 2024 · 4 comments · Fixed by #54205
Closed

Param as column alias can't be parsed in group statement #53872

pcqz opened this issue Jun 7, 2024 · 4 comments · Fixed by #54205

Comments

@pcqz
Copy link

pcqz commented Jun 7, 2024

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

create table test(id int, col int);
prepare stmt from "select id, ? as col1 from test where col=? group by id,col1";
set @a=100, @b=100;
execute stmt using @a,@b;

2. What did you expect to see? (Required)

No error

3. What did you see instead (Required)

ERROR 1105 (HY000): Unknown column '100' in 'group statement'

4. What is your TiDB version? (Required)

v8.1.0

@pcqz pcqz added the type/bug The issue is confirmed as a bug. label Jun 7, 2024
@YangKeao
Copy link
Member

YangKeao commented Jun 7, 2024

I think it's caused by the special behavior of group by ?, which will group by the N-th field. Ref #8153 🤦 . It's referred as *ast.PositionExpr in TiDB.

Every day I can learn a new mystery behavior of SQL.

@YangKeao YangKeao added the sig/planner SIG: Planner label Jun 7, 2024
@qw4990 qw4990 self-assigned this Jun 7, 2024
@qw4990
Copy link
Contributor

qw4990 commented Jun 19, 2024

A workaround:

mysql>   prepare stmt from "select id, col1 from (select id, ? as col1 from test where col=?) t group by id,col1";
Query OK, 0 rows affected (0.00 sec)

mysql> set @a=100, @b=100;
Query OK, 0 rows affected (0.00 sec)

mysql> execute stmt using @a,@b;
Empty set (0.01 sec)

@elsa0520
Copy link
Contributor

Maybe order by will have the same problem ~ @qw4990

@qw4990
Copy link
Contributor

qw4990 commented Jun 28, 2024

Maybe order by will have the same problem ~ @qw4990

I tested "order-by" as well and it doesn't have this problem since the optimizer doesn't replace order by col to order by ? directly. So this seems a special "optimization" for Agg only.

ti-chi-bot bot pushed a commit that referenced this issue Jun 28, 2024
@ti-chi-bot ti-chi-bot added affects-6.1 affects-5.4 This bug affects 5.4.x versions. labels Jun 28, 2024
ti-chi-bot bot pushed a commit that referenced this issue Jul 1, 2024
ti-chi-bot bot pushed a commit that referenced this issue Jul 2, 2024
ti-chi-bot bot pushed a commit that referenced this issue Aug 1, 2024
ti-chi-bot bot pushed a commit that referenced this issue Sep 3, 2024
ti-chi-bot bot pushed a commit that referenced this issue Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants