Skip to content

Commit 18113cf

Browse files
authored
fix: MySQL TABLE statement translation (#349)
1 parent c7abb54 commit 18113cf

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

backend/executor.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,12 @@ func (b *DuckBuilder) executeQuery(ctx *sql.Context, n sql.Node, conn *stdsql.Co
170170
)
171171

172172
// Translate the MySQL query to a DuckDB query
173-
switch n.(type) {
173+
switch n := n.(type) {
174174
case *plan.ShowTables:
175175
duckSQL = ctx.Query()
176+
case *plan.ResolvedTable:
177+
// SQLGlot cannot translate MySQL's `TABLE t` into DuckDB's `FROM t` - it produces `"table" AS t` instead.
178+
duckSQL = `FROM ` + catalog.ConnectIdentifiersANSI(n.Database().Name(), n.Name())
176179
default:
177180
duckSQL, err = transpiler.TranslateWithSQLGlot(ctx.Query())
178181
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bats
2+
bats_require_minimum_version 1.5.0
3+
4+
load helper
5+
6+
setup_file() {
7+
mysql_exec_stdin <<-'EOF'
8+
CREATE DATABASE table_statement_test;
9+
USE table_statement_test;
10+
CREATE TABLE t (id INT, name VARCHAR(255));
11+
INSERT INTO t VALUES (1, 'test1'), (2, 'test2');
12+
EOF
13+
}
14+
15+
teardown_file() {
16+
mysql_exec_stdin <<-'EOF'
17+
DROP DATABASE IF EXISTS table_statement_test;
18+
EOF
19+
}
20+
21+
@test "TABLE statement should return all rows from the table" {
22+
run -0 mysql_exec_stdin <<-'EOF'
23+
USE table_statement_test;
24+
TABLE t;
25+
EOF
26+
[ "${lines[0]}" = "1 test1" ]
27+
[ "${lines[1]}" = "2 test2" ]
28+
}

0 commit comments

Comments
 (0)