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

Mihai Budiu commented on CALCITE-6508:
--------------------------------------

The parser grammar looks like this:
{code}
    <ARRAY> { s = span(); }
    (
        (
            // nullary array function call: "array()" (Apache Spark)
            LOOKAHEAD(2)
            <LPAREN> <RPAREN> { args = SqlNodeList.EMPTY; }
        |
            args = ParenthesizedQueryOrCommaList(ExprContext.ACCEPT_ALL)
        )
        {
            if (args.size() == 1 && args.get(0).isA(SqlKind.QUERY)) {
                // Array query constructor, 'ARRAY (SELECT * FROM t)'
                return SqlStdOperatorTable.ARRAY_QUERY.createCall(s.end(this), 
args.get(0));
            } else {
                // Spark ARRAY function, 'ARRAY(1, 2)',
                // equivalent to standard 'ARRAY [1, 2]'
                return SqlLibraryOperators.ARRAY.createCall(s.end(this), 
args.getList());
            }
        }
    |
        // by enumeration "ARRAY[e0, e1, ..., eN]"
        <LBRACKET> // TODO: do trigraph as well ??( ??)
        (
            args = ExpressionCommaList(s, ExprContext.ACCEPT_NON_QUERY)
        |
            { args = SqlNodeList.EMPTY; }
        )
        <RBRACKET>
        {
            return SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR.createCall(
                s.end(this), args.getList());
        }
{code}

So it looks like the form with () should accept queries, while the form with [] 
does not.
Maybe it's enough to change this in the grammar?

> Parse error when using scalar sub-query as operant to Array constructor 
> function
> --------------------------------------------------------------------------------
>
>                 Key: CALCITE-6508
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6508
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Ian Bertolacci
>            Priority: Major
>
> Using a scalar subquery in the {{`ARRAY[ ... ]`}} function throws "parse 
> failed: Query expression encountered in illegal context"
> (Calcite version 1.36)
> For example:
> {code:sql}
> select ARRAY[ (select 1) ]
> select ARRAY[ (select max(x) from table) ]
> {code}
> Is this something that can be configured or easily altered?



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

Reply via email to