Skip to content

Commit

Permalink
fix: MySQL TABLE statement translation (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 authored Jan 6, 2025
1 parent c7abb54 commit 18113cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion backend/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ func (b *DuckBuilder) executeQuery(ctx *sql.Context, n sql.Node, conn *stdsql.Co
)

// Translate the MySQL query to a DuckDB query
switch n.(type) {
switch n := n.(type) {
case *plan.ShowTables:
duckSQL = ctx.Query()
case *plan.ResolvedTable:
// SQLGlot cannot translate MySQL's `TABLE t` into DuckDB's `FROM t` - it produces `"table" AS t` instead.
duckSQL = `FROM ` + catalog.ConnectIdentifiersANSI(n.Database().Name(), n.Name())
default:
duckSQL, err = transpiler.TranslateWithSQLGlot(ctx.Query())
}
Expand Down
28 changes: 28 additions & 0 deletions test/bats/mysql/table_statement.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bats
bats_require_minimum_version 1.5.0

load helper

setup_file() {
mysql_exec_stdin <<-'EOF'
CREATE DATABASE table_statement_test;
USE table_statement_test;
CREATE TABLE t (id INT, name VARCHAR(255));
INSERT INTO t VALUES (1, 'test1'), (2, 'test2');
EOF
}

teardown_file() {
mysql_exec_stdin <<-'EOF'
DROP DATABASE IF EXISTS table_statement_test;
EOF
}

@test "TABLE statement should return all rows from the table" {
run -0 mysql_exec_stdin <<-'EOF'
USE table_statement_test;
TABLE t;
EOF
[ "${lines[0]}" = "1 test1" ]
[ "${lines[1]}" = "2 test2" ]
}

0 comments on commit 18113cf

Please sign in to comment.