[ 
https://issues.apache.org/jira/browse/CALCITE-6838?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17927330#comment-17927330
 ] 

xiong duan commented on CALCITE-6838:
-------------------------------------

The exception in BigQuery is clear:
{code:java}
The argument to UNNEST is an expression, not a query; to use a query as an 
expression, the query must be wrapped with additional parentheses to make it a 
scalar subquery expression at [9:19] {code}
The SQL(exception):
{code:java}
WITH  Sequences AS (    SELECT 1 AS id, [0, 1, 1, 2, 3, 5] AS some_numbers    
UNION ALL SELECT 2 AS id, [2, 4, 8, 16, 32] AS some_numbers    UNION ALL SELECT 
3 AS id, [5, 10] AS some_numbers  )
SELECT id, flattened_numbersFROM SequencesINNER JOIN UNNEST(select 
Sequences.some_numbers) AS flattened_numbers; {code}
The SQL(success):
{code:java}
WITH Sequences AS ( SELECT 1 AS id, [0, 1, 1, 2, 3, 5] AS some_numbers UNION 
ALL SELECT 2 AS id, [2, 4, 8, 16, 32] AS some_numbers UNION ALL SELECT 3 AS id, 
[5, 10] AS some_numbers )
SELECT id, flattened_numbersFROM SequencesINNER JOIN UNNEST((select 
Sequences.some_numbers)) AS flattened_numbers;  {code}

> ReloSqlConverter should generate double parentheses when the input to Unnest 
> is a query statement
> -------------------------------------------------------------------------------------------------
>
>                 Key: CALCITE-6838
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6838
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: xiong duan
>            Assignee: xiong duan
>            Priority: Major
>
> ReloSqlConverter should generate double parentheses when the input to Unnest 
> is a query statement.
> For unit tests in RelToSqlConverterTest:
> {code:java}
> @Test void testUnnest() {
>   final String sql = "select * from UNNEST(array [1, 2, 3])";
>   final String expected = "SELECT *\n" +
>       "FROM UNNEST (SELECT ARRAY[1, 2, 3]\n" +
>       "FROM (VALUES (0)) AS \"t\" (\"ZERO\")) AS \"t0\" (\"col_0\")";
>   sql(sql).ok(expected).withPostgresql().ok(expected);
> }{code}
> The generated SQL is valid in CALCITE but not in PG.
> In PostgreSQL:
> {code:java}
> CREATE TABLE IF NOT EXISTS public."unnestTable"
> (
>     array_int integer[],
>     "valueInt" integer
> );
> SELECT "valueInt","t10"."col_0" FROM public."unnestTable",unnest(( SELECT 
> "array_int" FROM (VALUES (0)) AS "t" ("ZERO")))  AS "t10" ("col_0");
> select * from unnest((SELECT "array_int" from  public."unnestTable"))  as "t" 
> ("ZERO");
> select * from unnest((SELECT ARRAY_AGG("valueInt") from  
> public."unnestTable"))  as "t" ("ZERO");
> select * from unnest((SELECT ARRAY_AGG("valueInt") from  
> public."unnestTable"))  WITH ORDINALITY AS  "t" ("ZERO","ZERO1");{code}
> The PG always needs double parentheses.
> The Calcite supports double parentheses too.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to