pingcap / tiup

A component manager for TiDB
https://tiup.io
Apache License 2.0
427 stars 313 forks source link

tiup sql client corrupts SQL column names #1597

Open morgo opened 3 years ago

morgo commented 3 years ago

Bug Report

Credit for this bug report goes to @Alkaagr81

  1. What did you do?
create table t1 (c1 int, c2 char(6), c3 int);
create table t2 (c1 int, c2 char(6));
insert into t1 values (1, "t1c2-1", 10), (2, "t1c2-2", 20);
update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1";
update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1" where t1.c3 = 10;
drop table t1, t2;
  1. What did you expect to see?

Using mysql client, no error:

tidb> create table t1 (c1 int, c2 char(6), c3 int);
Query OK, 0 rows affected (0.06 sec)

tidb> create table t2 (c1 int, c2 char(6));
Query OK, 0 rows affected (0.07 sec)

tidb> insert into t1 values (1, "t1c2-1", 10), (2, "t1c2-2", 20);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

tidb> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

tidb> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1" where t1.c3 = 10;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

tidb> drop table t1, t2;
  1. What did you see instead?

Using Tiup SQL client:

my:root@127.0.0.1:4000=> use test;
USE
my:root@127.0.0.1:4000=> create table t1 (c1 int, c2 char(6), c3 int);
CREATE TABLE
my:root@127.0.0.1:4000=> create table t2 (c1 int, c2 char(6));
CREATE TABLE
my:root@127.0.0.1:4000=> insert into t1 values (1, "t1c2-1", 10), (2, "t1c2-2", 20);
error: mysql: 1054: Unknown column 't1c2-1' in 'field list'
my:root@127.0.0.1:4000=> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1";
error: mysql: 1054: Unknown column 't2c2-1' in 'field list'
my:root@127.0.0.1:4000=> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1" where t1.c3 = 10;
error: mysql: 1054: Unknown column 't2c2-1' in 'field list'
my:root@127.0.0.1:4000=> drop table t1, t2;
  1. What version of TiUP are you using (tiup --version)?
morgo@ubuntu:~/go/src/github.com/mysql/mysql-server$ tiup --version
1.4.0 tiup
Go Version: go1.16.2
Git Ref: v1.4.0
GitHash: eb335e6ad9d0dc19d1c43db422cd409d62bc3cea
dveeden commented 2 years ago

The reason for this is that usql set the SQL Mode to "ANSI" by default to more closely mimic the behavior of psql.

https://github.com/xo/usql/blob/7132de43273ea9b6fbe695ea3229d25d8e977bbc/drivers/mysql/mysql.go#L30

my:root@127.0.0.1:4000=> USE test;
USE
my:root@127.0.0.1:4000=> \conninfo
Connected with driver mysql (root@tcp(127.0.0.1:4000)/?loc=Local&parseTime=true&sql_mode=ansi)
my:root@127.0.0.1:4000=> SELECT @@session.sql_mode;
                               @@session.sql_mode                               
--------------------------------------------------------------------------------
 REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI 
(1 row)

my:root@127.0.0.1:4000=> create table t1 (c1 int, c2 char(6), c3 int);
CREATE TABLE
my:root@127.0.0.1:4000=> create table t2 (c1 int, c2 char(6));
CREATE TABLE
my:root@127.0.0.1:4000=> insert into t1 values (1, "t1c2-1", 10), (2, "t1c2-2", 20);
error: mysql: 1054: Unknown column 't1c2-1' in 'field list'
my:root@127.0.0.1:4000=> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1";
error: mysql: 1054: Unknown column 't2c2-1' in 'field list'
my:root@127.0.0.1:4000=> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1" where t1.c3 = 10;
error: mysql: 1054: Unknown column 't2c2-1' in 'field list'
my:root@127.0.0.1:4000=> drop table t1, t2;
DROP TABLE
my:root@127.0.0.1:4000=> SET sql_mode='';
SET
my:root@127.0.0.1:4000=> create table t1 (c1 int, c2 char(6), c3 int);
CREATE TABLE
my:root@127.0.0.1:4000=> create table t2 (c1 int, c2 char(6));
CREATE TABLE
my:root@127.0.0.1:4000=> insert into t1 values (1, "t1c2-1", 10), (2, "t1c2-2", 20);
INSERT 2
my:root@127.0.0.1:4000=> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1";
UPDATE
my:root@127.0.0.1:4000=> update t1 left join t2 on t1.c1 = t2.c1 set t2.c2 = "t2c2-1" where t1.c3 = 10;
UPDATE
my:root@127.0.0.1:4000=> drop table t1, t2;
DROP TABLE

Other differences between MySQL Client and usql are:

my:root@127.0.0.1:4000=> SELECT NOW(), 'foo' || 'bar';
           NOW()           | 'foo' || 'bar' 
---------------------------+----------------
 2021-12-29T08:26:01+01:00 | foobar 
(1 row)

my:root@127.0.0.1:4000=> \timing
Timing is on.
my:root@127.0.0.1:4000=> \pset border 2
Border style is 2.
my:root@127.0.0.1:4000=> \pset time "2006-01-02 15:04:05.000000"
Time display is "2006-01-02 15:04:05.000000" ("2006-01-02 15:04:05.000000").
my:root@127.0.0.1:4000=> SET sql_mode='';
SET
my:root@127.0.0.1:4000=> SELECT NOW(), 'foo' || 'bar';
+----------------------------+----------------+
|           NOW()            | 'foo' || 'bar' |
+----------------------------+----------------+
| 2021-12-29 08:26:33.000000 |              0 |
+----------------------------+----------------+
(1 row)

Time: [18.896] ms

Another case that seems problematic:

(mysql)=> SELECT CAST("2020-11-02" AS DATE);
error: mysql: sql: Scan error on column index 0, name "CAST(\"2020-11-02\" AS DATE)": unsupported Scan, storing driver.Value type []uint8 into type *time.Time
(mysql)=> SELECT CAST("2020-11-02" AS DATE) + 0;
 CAST("2020-11-02" AS DATE) + 0 
--------------------------------
                       20201102 
(1 row)
dveeden commented 2 years ago

Please check https://github.com/xo/usql/issues/309 to see why this is happening and why changing the SQL mode might cause other issues with usql.