You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Maxime Sechehaye <ms...@activeviam.com.INVALID> on 2025/09/16 07:37:02 UTC
Support for field access on a table expression
Hello,
We are currently using Calcite to enable SQL queries on our database
application. Our application emulates a Postgres server, so all frontend
tools send Postgres-like queries.
We are encountering an issue with a query sent by Tableau that contains:
(information_schema._pg_expandarray(i.indkey)).n
We have implemented a SqlUserDefinedTableFunction for _pg_expandarray. The
implementation works correctly, except when it comes to field access, as in
the expression above. The return type of _pg_expandarray must be a cursor,
but it does not seem to be compatible with the dot operator.
Do you know if it is currently possible to support field access on a table
expression in Calcite, or should we wait for a potential update?
Attached is the full query sent by Tableau.
Best regards,
*Maxime Sechehaye*
Junior Software Engineer [image: ActiveViam] <https://activeviam.com>
*A:* ActiveViam France, 46 rue de l'Arbre Sec, 75001 Paris
Follow us on LinkedIn <https://www.linkedin.com/company/activeviam/>
[image: Both Awards]
Re: Support for field access on a table expression
Posted by Julian Hyde <jh...@gmail.com>.
The problem might be how Postgres handles table functions in the SELECT clause. They have a special behavior that if the function returns N rows then multiple rows are emitted. Effectively the flatMap operator (aka SelectMany).
This behavior is non-standard and Calcite doesn’t support it. We could. I don’t recall whether there is a jira.
Julian
> On Sep 23, 2025, at 11:54, Mihai Budiu <mb...@gmail.com> wrote:
>
> UNNEST has a surprising behavior when the argument is an array of ROW values: it will actually expand each element of the ROW into a separate column. pg_expandarray doesn't do that, it produces a single column with a ROW. That's why you can use the dot syntax to access a field.
>
> UNNEST(ARRAY[ROW(2, 3), ROW(3, 4)] WITH ORDINALITY produces as result a table with 3 columns, the last being the ordinality:
>
> 2 | 3 | 1
> 3 | 4 | 2
>
> However, the postgres function would produce a table with 2 columns, the last being the ordinality:
>
> ROW(2, 3) | 1
> ROW(3, 4) | 2
>
> You can name the columns produced by UNNEST using this syntax:
>
> UNNEST(array) WITH ORDINALITY AS R(c0, c1, o)
>
> This will produce a table with three named columns:
>
> c0 | c1 | o
>
> And then you can refer to the field you need by name.
>
> So you should be able to emulate this function if you rewrite your programs manually.
>
> I am pretty sure that with some work you can convince Calcite to accept this function using the postgres syntax. I think the trick is to parse the program using the function as a normal function call (no changes needed in the parser), then rewrite the SqlNode IR by replacing this RexCall with a call to the SqlUnnestOperator, before validating the program.
>
> Mihai
>
> ________________________________
> From: Maxime Sechehaye <ms...@activeviam.com.INVALID>
> Sent: Tuesday, September 23, 2025 12:51 AM
> To: dev@calcite.apache.org <de...@calcite.apache.org>
> Subject: Re: Support for field access on a table expression
>
> Hello Mihai,
>
> Thank you for your quick response! I tried redirecting the _pg_expandarray
> operator to UNNEST WITH ORDINALITY, but it didn’t work—particularly when
> used with the dot operator.
>
> Here’s what I attempted:
>
> -
>
> Creating a custom parsing rule: didn’t work because Calcite’s default
> parser handles it first
> -
>
> Manually rewriting the query to replace _pg_expandarray with UNNEST
> -
>
> Implementing a custom SqlUserDefinedTableFunction _pg_expandarray whose
> implementation is UNNEST
>
> At the moment, I can’t think of any other solutions.
>
> Best regards,
> Maxime
>
>>
Re: Support for field access on a table expression
Posted by Mihai Budiu <mb...@gmail.com>.
UNNEST has a surprising behavior when the argument is an array of ROW values: it will actually expand each element of the ROW into a separate column. pg_expandarray doesn't do that, it produces a single column with a ROW. That's why you can use the dot syntax to access a field.
UNNEST(ARRAY[ROW(2, 3), ROW(3, 4)] WITH ORDINALITY produces as result a table with 3 columns, the last being the ordinality:
2 | 3 | 1
3 | 4 | 2
However, the postgres function would produce a table with 2 columns, the last being the ordinality:
ROW(2, 3) | 1
ROW(3, 4) | 2
You can name the columns produced by UNNEST using this syntax:
UNNEST(array) WITH ORDINALITY AS R(c0, c1, o)
This will produce a table with three named columns:
c0 | c1 | o
And then you can refer to the field you need by name.
So you should be able to emulate this function if you rewrite your programs manually.
I am pretty sure that with some work you can convince Calcite to accept this function using the postgres syntax. I think the trick is to parse the program using the function as a normal function call (no changes needed in the parser), then rewrite the SqlNode IR by replacing this RexCall with a call to the SqlUnnestOperator, before validating the program.
Mihai
________________________________
From: Maxime Sechehaye <ms...@activeviam.com.INVALID>
Sent: Tuesday, September 23, 2025 12:51 AM
To: dev@calcite.apache.org <de...@calcite.apache.org>
Subject: Re: Support for field access on a table expression
Hello Mihai,
Thank you for your quick response! I tried redirecting the _pg_expandarray
operator to UNNEST WITH ORDINALITY, but it didn’t work—particularly when
used with the dot operator.
Here’s what I attempted:
-
Creating a custom parsing rule: didn’t work because Calcite’s default
parser handles it first
-
Manually rewriting the query to replace _pg_expandarray with UNNEST
-
Implementing a custom SqlUserDefinedTableFunction _pg_expandarray whose
implementation is UNNEST
At the moment, I can’t think of any other solutions.
Best regards,
Maxime
>
Re: Support for field access on a table expression
Posted by Maxime Sechehaye <ms...@activeviam.com.INVALID>.
Hello Mihai,
Thank you for your quick response! I tried redirecting the _pg_expandarray
operator to UNNEST WITH ORDINALITY, but it didn’t work—particularly when
used with the dot operator.
Here’s what I attempted:
-
Creating a custom parsing rule: didn’t work because Calcite’s default
parser handles it first
-
Manually rewriting the query to replace _pg_expandarray with UNNEST
-
Implementing a custom SqlUserDefinedTableFunction _pg_expandarray whose
implementation is UNNEST
At the moment, I can’t think of any other solutions.
Best regards,
Maxime
>
Re: Support for field access on a table expression
Posted by Mihai Budiu <mb...@gmail.com>.
Calcite has UNNEST WITH ORDINALITY instead of pg_expandarray.
Notice that if an array of records is expanded, each field becomes a separate column.
Would that work for you?
Mihai
________________________________
From: Maxime Sechehaye <ms...@activeviam.com.INVALID>
Sent: Tuesday, September 16, 2025 12:37 AM
To: dev@calcite.apache.org <de...@calcite.apache.org>
Subject: Support for field access on a table expression
Hello,
We are currently using Calcite to enable SQL queries on our database application. Our application emulates a Postgres server, so all frontend tools send Postgres-like queries.
We are encountering an issue with a query sent by Tableau that contains:
(information_schema._pg_expandarray(i.indkey)).n
We have implemented a SqlUserDefinedTableFunction for _pg_expandarray. The implementation works correctly, except when it comes to field access, as in the expression above. The return type of _pg_expandarray must be a cursor, but it does not seem to be compatible with the dot operator.
Do you know if it is currently possible to support field access on a table expression in Calcite, or should we wait for a potential update?
Attached is the full query sent by Tableau.
Best regards,
Maxime Sechehaye
Junior Software Engineer
[ActiveViam]<https://activeviam.com>
A:
ActiveViam France, 46 rue de l'Arbre Sec, 75001 Paris
Follow us on LinkedIn<https://www.linkedin.com/company/activeviam/>
[Both Awards]