You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by kant kodali <ka...@gmail.com> on 2021/01/05 01:33:44 UTC

How to rewrite a query using calcite. Any Example?

Hi All,

is there a way to parse only the where clause using calcite (any sample
code)? meaning,  for example, assume the input is something like

view1.col1 = value1 or view2.col2 = value2 and view3.col3 = value3

I want to parse this expression and replace view1.col1 with a, view2.col2
with b and view3.col3 = c such that the output should be

a = value1 or b = value2 and c = value3.

This is just an example but I can get more complex boolean expressions as
input.

Thanks!

Re: How to rewrite a query using calcite. Any Example?

Posted by kant kodali <ka...@gmail.com>.
Hi Julian,

Thanks for that response. I have not hit those cases yet so I am probably
going with an assumption that sqlshuttle would work however, it would be
great to know how relnode helps for the cases you mentioned. Are there any
examples you could point me to?

On Fri, Jan 8, 2021 at 5:16 PM Julian Hyde <jh...@apache.org> wrote:

> SqlShuttle is probably the easiest thing, and it works for simple
> cases. If it works for you, use it.
>
> There are cases where you need to know the type of the columns being
> referenced, or to deal with ambiguous references, or to make
> structural changes to the query. For these complex cases, people
> translate to relational algebra (RelNode) and transform the algebra
> expression. This is where Calcite really excels.
>
> Julian
>
> On Wed, Jan 6, 2021 at 6:44 PM JiaTao Tao <ta...@gmail.com> wrote:
> >
> > Hi
> > IMO visitor is fine.
> >
> > Regards!
> >
> > Aron Tao
> >
> >
> > kant kodali <ka...@gmail.com> 于2021年1月6日周三 下午8:11写道:
> >
> > > Hi,
> > >
> > > Thanks, that works. Got another question.
> > >
> > > Lets say, I want to expose a model to users that contains a bunch of
> fields
> > > and each field has a corresponding database column. now a user sends
> me a
> > > query which contains some fields and I want to replace them with
> columns.
> > > Is there any inbuilt way in calcite to do this rather than me
> implementing
> > > a visitor? If so, any example?
> > >
> > > I already did it with a SqlShuttle visitor and it works but want to
> know if
> > > there is an easier and out of the box way to do this?
> > >
> > >
> > > Thanks.
> > >
> > >
> > > On Tue, Jan 5, 2021 at 6:43 PM JiaTao Tao <ta...@gmail.com> wrote:
> > >
> > > > Hi
> > > > Try this:  SqlParser.create(expr).parseExpression();
> > > >
> > > > Regards!
> > > >
> > > > Aron Tao
> > > >
> > > >
> > > > kant kodali <ka...@gmail.com> 于2021年1月5日周二 上午9:34写道:
> > > >
> > > > > Hi All,
> > > > >
> > > > > is there a way to parse only the where clause using calcite (any
> sample
> > > > > code)? meaning,  for example, assume the input is something like
> > > > >
> > > > > view1.col1 = value1 or view2.col2 = value2 and view3.col3 = value3
> > > > >
> > > > > I want to parse this expression and replace view1.col1 with a,
> > > view2.col2
> > > > > with b and view3.col3 = c such that the output should be
> > > > >
> > > > > a = value1 or b = value2 and c = value3.
> > > > >
> > > > > This is just an example but I can get more complex boolean
> expressions
> > > as
> > > > > input.
> > > > >
> > > > > Thanks!
> > > > >
> > > >
> > >
>

Re: How to rewrite a query using calcite. Any Example?

Posted by Julian Hyde <jh...@apache.org>.
SqlShuttle is probably the easiest thing, and it works for simple
cases. If it works for you, use it.

There are cases where you need to know the type of the columns being
referenced, or to deal with ambiguous references, or to make
structural changes to the query. For these complex cases, people
translate to relational algebra (RelNode) and transform the algebra
expression. This is where Calcite really excels.

Julian

On Wed, Jan 6, 2021 at 6:44 PM JiaTao Tao <ta...@gmail.com> wrote:
>
> Hi
> IMO visitor is fine.
>
> Regards!
>
> Aron Tao
>
>
> kant kodali <ka...@gmail.com> 于2021年1月6日周三 下午8:11写道:
>
> > Hi,
> >
> > Thanks, that works. Got another question.
> >
> > Lets say, I want to expose a model to users that contains a bunch of fields
> > and each field has a corresponding database column. now a user sends me a
> > query which contains some fields and I want to replace them with columns.
> > Is there any inbuilt way in calcite to do this rather than me implementing
> > a visitor? If so, any example?
> >
> > I already did it with a SqlShuttle visitor and it works but want to know if
> > there is an easier and out of the box way to do this?
> >
> >
> > Thanks.
> >
> >
> > On Tue, Jan 5, 2021 at 6:43 PM JiaTao Tao <ta...@gmail.com> wrote:
> >
> > > Hi
> > > Try this:  SqlParser.create(expr).parseExpression();
> > >
> > > Regards!
> > >
> > > Aron Tao
> > >
> > >
> > > kant kodali <ka...@gmail.com> 于2021年1月5日周二 上午9:34写道:
> > >
> > > > Hi All,
> > > >
> > > > is there a way to parse only the where clause using calcite (any sample
> > > > code)? meaning,  for example, assume the input is something like
> > > >
> > > > view1.col1 = value1 or view2.col2 = value2 and view3.col3 = value3
> > > >
> > > > I want to parse this expression and replace view1.col1 with a,
> > view2.col2
> > > > with b and view3.col3 = c such that the output should be
> > > >
> > > > a = value1 or b = value2 and c = value3.
> > > >
> > > > This is just an example but I can get more complex boolean expressions
> > as
> > > > input.
> > > >
> > > > Thanks!
> > > >
> > >
> >

Re: How to rewrite a query using calcite. Any Example?

Posted by JiaTao Tao <ta...@gmail.com>.
Hi
IMO visitor is fine.

Regards!

Aron Tao


kant kodali <ka...@gmail.com> 于2021年1月6日周三 下午8:11写道:

> Hi,
>
> Thanks, that works. Got another question.
>
> Lets say, I want to expose a model to users that contains a bunch of fields
> and each field has a corresponding database column. now a user sends me a
> query which contains some fields and I want to replace them with columns.
> Is there any inbuilt way in calcite to do this rather than me implementing
> a visitor? If so, any example?
>
> I already did it with a SqlShuttle visitor and it works but want to know if
> there is an easier and out of the box way to do this?
>
>
> Thanks.
>
>
> On Tue, Jan 5, 2021 at 6:43 PM JiaTao Tao <ta...@gmail.com> wrote:
>
> > Hi
> > Try this:  SqlParser.create(expr).parseExpression();
> >
> > Regards!
> >
> > Aron Tao
> >
> >
> > kant kodali <ka...@gmail.com> 于2021年1月5日周二 上午9:34写道:
> >
> > > Hi All,
> > >
> > > is there a way to parse only the where clause using calcite (any sample
> > > code)? meaning,  for example, assume the input is something like
> > >
> > > view1.col1 = value1 or view2.col2 = value2 and view3.col3 = value3
> > >
> > > I want to parse this expression and replace view1.col1 with a,
> view2.col2
> > > with b and view3.col3 = c such that the output should be
> > >
> > > a = value1 or b = value2 and c = value3.
> > >
> > > This is just an example but I can get more complex boolean expressions
> as
> > > input.
> > >
> > > Thanks!
> > >
> >
>

Re: How to rewrite a query using calcite. Any Example?

Posted by kant kodali <ka...@gmail.com>.
Hi,

Thanks, that works. Got another question.

Lets say, I want to expose a model to users that contains a bunch of fields
and each field has a corresponding database column. now a user sends me a
query which contains some fields and I want to replace them with columns.
Is there any inbuilt way in calcite to do this rather than me implementing
a visitor? If so, any example?

I already did it with a SqlShuttle visitor and it works but want to know if
there is an easier and out of the box way to do this?


Thanks.


On Tue, Jan 5, 2021 at 6:43 PM JiaTao Tao <ta...@gmail.com> wrote:

> Hi
> Try this:  SqlParser.create(expr).parseExpression();
>
> Regards!
>
> Aron Tao
>
>
> kant kodali <ka...@gmail.com> 于2021年1月5日周二 上午9:34写道:
>
> > Hi All,
> >
> > is there a way to parse only the where clause using calcite (any sample
> > code)? meaning,  for example, assume the input is something like
> >
> > view1.col1 = value1 or view2.col2 = value2 and view3.col3 = value3
> >
> > I want to parse this expression and replace view1.col1 with a, view2.col2
> > with b and view3.col3 = c such that the output should be
> >
> > a = value1 or b = value2 and c = value3.
> >
> > This is just an example but I can get more complex boolean expressions as
> > input.
> >
> > Thanks!
> >
>

Re: How to rewrite a query using calcite. Any Example?

Posted by JiaTao Tao <ta...@gmail.com>.
Hi
Try this:  SqlParser.create(expr).parseExpression();

Regards!

Aron Tao


kant kodali <ka...@gmail.com> 于2021年1月5日周二 上午9:34写道:

> Hi All,
>
> is there a way to parse only the where clause using calcite (any sample
> code)? meaning,  for example, assume the input is something like
>
> view1.col1 = value1 or view2.col2 = value2 and view3.col3 = value3
>
> I want to parse this expression and replace view1.col1 with a, view2.col2
> with b and view3.col3 = c such that the output should be
>
> a = value1 or b = value2 and c = value3.
>
> This is just an example but I can get more complex boolean expressions as
> input.
>
> Thanks!
>