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

UNION with more than two statements produces an incorrect query according to MySQL/MariaDB #432

Open
panta opened this issue Dec 12, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@panta
Copy link

panta commented Dec 12, 2024

Describe the bug
When using UNION on the result of another UNION, the resulting query makes MySQL/MariaDB complain.

Environment (please complete the following information):

  • OS: macos
  • Database: MariaDB 10.3
  • Database driver: go-sql-driver/mysql
  • Jet version v2.10.1

Code snippet

	stmt1 := SELECT(Int(1).AS("value"))
	stmt2 := SELECT(Int(2).AS("value"))
	stmt3 := SELECT(Int(3).AS("value"))
	stmt := UNION(stmt1, stmt2)
	stmt = UNION(stmt, stmt3)
	debugSql := stmt.DebugSql()
	log.Printf("SQL: %s", debugSql)

	var res []struct {
		Value int `db:"value" alias:"value"`
	}

	if err := stmt.Query(db, &res); err != nil {
		panic(err)
	}
	log.Printf("results: %#v", res)

Resulting query:

(
     (
          SELECT 1 AS "value"
     )
     UNION
     (
          SELECT 2 AS "value"
     )
)
UNION
(
     SELECT 3 AS "value"
);

Resulting error:

jet: Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION\n     (\n          SELECT ? AS \"value\"\n     )\n)\nUNION\n(\n     SELECT ? AS ...' at line 5

Expected behavior
I'd expect a query that doesn't return error, like:

(
     SELECT 1 AS "value"
)
UNION
(
     SELECT 2 AS "value"
)
UNION
(
     SELECT 3 AS "value"
);
@panta panta added the bug Something isn't working label Dec 12, 2024
@go-jet
Copy link
Owner

go-jet commented Dec 13, 2024

It seems to be an issue with MariaDB 10.3, as it works as expected in MariaDB 11.3.

For the expected query, why not:

stmt1 := SELECT(Int(1).AS("value"))
stmt2 := SELECT(Int(2).AS("value"))
stmt3 := SELECT(Int(3).AS("value"))
stmt := UNION(stmt1, stmt2, stmt3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants