spiceai / spiceai

A self-hostable CDN for databases. Spice provides a unified SQL query interface and portable runtime to locally materialize, accelerate, and query datasets across databases, data warehouses, and data lakes.
https://docs.spiceai.org
Apache License 2.0
1.8k stars 70 forks source link

Bug: MySQL error `Every derived table must have its own alias` #1952

Closed sgrebnov closed 1 month ago

sgrebnov commented 1 month ago

Describe the bug

When running TPCH query 13 with MySQL, the following rewritten query is generated and is failed to execute with the following error 'Every derived table must have its own alias'

SELECT
    `c_orders`.`c_count`,
    COUNT(1) AS `custdist`
FROM
    (
    SELECT
        `customer`.`c_custkey` AS `c_custkey`,
        `COUNT(orders.o_orderkey)` AS `c_count`
    FROM
        (
        SELECT
            `customer`.`c_custkey`,
            COUNT(`orders`.`o_orderkey`)
        FROM
            `customer`
        LEFT JOIN `orders` ON
            ((`customer`.`c_custkey` = `orders`.`o_custkey`)
                AND `orders`.`o_comment` NOT LIKE CAST('%special%requests%' AS CHAR))
        GROUP BY
            `customer`.`c_custkey`)) AS `c_orders`
GROUP BY
    `c_orders`.`c_count`
ORDER BY
    `custdist` DESC,
    `c_orders`.`c_count` DESC

Note: CAST('%special%requests%' AS TEXT)) was manually replaced with CAST('%special%requests%' AS CHAR)) due to another but;

peasee commented 1 month ago

Postgres over ODBC has a similar error:

ERROR: subquery in FROM must have an alias;\nNo query has been executed with that handle
Sevenannn commented 1 month ago

Postgres data connector over Postgres, Aurora, Supabase has the same error

sgrebnov commented 1 month ago

This has been fixed (at least for MySQL) by the following PR: https://github.com/spiceai/spiceai/pull/2001