I had attempted this once before in the case of non-virtual columns, and
can only second what Julian mentioned regarding starting from the tests and
working backwards through the Validator APIs specifically in a debugger.

This does remind me though that I filed this addressing a bug I found while
doing that awhile back:  Maybe possible to get a review and merge? :)

https://github.com/apache/calcite/pull/2683
https://issues.apache.org/jira/browse/CALCITE-4972


On Thu, Sep 8, 2022 at 8:54 AM Jiajun Xie <[email protected]> wrote:

> My real goal is to know which virtual columns are used in query.
>
> - I tried to use `getColumnOrigins`, here is one test in RelMetadataTest
> ```
> @Test void testVirtualColumnOrigins() {
> final String sql = "select E from VIRTUALCOLUMNS.VC_T1 "
> + "for system_time as of TIMESTAMP '2011-01-02 00:00:00'";
> // Actual column name is A, but I need E.
> sql(sql).withCatalogReaderFactory(MockCatalogReaderExtended::create)
> .assertColumnOriginSingle("VC_T1", "E", false);
> // I know that A is reasonable value because E = A + 1. (The root column
> origin should be A.)
> }
> ```
>
> - Visit SqlNode is another solution, but there are some fields not be
> trimmed.
>
> I want to know if anyone has suggestions to get virtual column that query
> used.
>
> Thank you for your reply.
>
> On Thu, 8 Sept 2022 at 14:55, Julian Hyde <[email protected]> wrote:
>
> > You know, when people ask ‘Does Calcite support lineage?’, I’m never
> quite
> > sure. People mean different things by lineage, and it takes a bit of
> effort
> > to set up the required APIs.
> >
> > I think the way to solve this question is with unit tests.
> >
> > Can write a unit test that fails, or point to an existing test that
> almost
> > does what you need? For example, is there a test to get the lineage of a
> > view column?
> >
> > Julian
> >
> >
> > > On Sep 5, 2022, at 2:52 AM, Jiajun Xie <[email protected]>
> > wrote:
> > >
> > > Hi, all:
> > > I want to know which virtual column is in use. For example,
> > > ```
> > > CREATE TABLE peoples(age int, virtual_age int as (age + 1) virtual);
> > > SELECT virtual_age from peoples;
> > > ```
> > > After converting, the virtual column is expanded to expression.
> > > ```
> > > LogicalProject(virtual_age=[+($0, 1)])
> > >  LogicalTableScan(table=[[default_ns, default, peoples]])
> > > ```
> > > So `RelMetadataQuery#getColumnOrigins` return RelColumnOrigin that is
> > > age(not is virtual_age).
> > >
> > > I try to rewrite code in the implementation class of
> > > `InitializerExpressionFactory`, but I can't know which column is in
> use.
> > > When I confirm which column is in use, I can't know if the expression
> is
> > > from a virtual column.
> > >
> > > Does anyone have relevant experience?  Thanks~
> >
> >
>

Reply via email to