You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Nirav Patel <np...@xactlycorp.com> on 2018/01/18 21:44:40 UTC
How to parse sql expression to obtain field names
Is it possible to just parse sql function expression which is not a
complete sql syntax.
e.g. I want to parse expression `SUM(t1.amount)/COUNT(t1)` instead of whole
sql like "select t1.name, SUM(t1.amount)/COUNT(t1) from mytable t1 group by
t1.name". I tried
val parser = SqlParser.create("SUM(t1.amount)/COUNT(t1)")
val sqlNode = parser.parseExpression()
but it fails with parse exception.
Thanks
--
[image: What's New with Xactly] <http://www.xactlycorp.com/email-click/>
<https://www.instagram.com/xactlycorp/>
<https://www.linkedin.com/company/xactly-corporation>
<https://twitter.com/Xactly> <https://www.facebook.com/XactlyCorp>
<http://www.youtube.com/xactlycorporation>
Re: How to parse sql expression to obtain field names
Posted by Nirav Patel <np...@xactlycorp.com>.
SqlShuttle helped to visit field names as well as change it according to
target dialect.
On Thu, Jan 18, 2018 at 4:01 PM, Nirav Patel <np...@xactlycorp.com> wrote:
> Sorry my original expression had an extra ")" in the end. Thanks for
> verifying.
>
> Followup question:
>
> How do I extract Field(Column) names from all such expressions? I tried
> implementing SqlVisitor. I can see SqlCall has fields operators and operand
> but when I fetch them I get List[SqlNode] can they be converted directly to
> String? or any getNames() method? Or do I need to do recursive traversal
> and each time I get sqlNode I provide new visitor until I hit
> SqlIdentifier? Can you provide some suggestion to effectively get all
> column names from expression.
>
> Thanks,
> Nirav
>
> On Thu, Jan 18, 2018 at 3:01 PM, Julian Hyde <jh...@apache.org> wrote:
>
>> It works for me.
>>
>> public static void main(String[] args) throws SqlParseException {
>> try {
>> SqlParser parser = SqlParser.create("SUM(t1.amount)/COUNT(t1)");
>> SqlNode sqlNode = parser.parseExpression();
>> } catch (Throwable e) {
>> e.printStackTrace();
>> }
>> }
>>
>>
>> > On Jan 18, 2018, at 1:44 PM, Nirav Patel <np...@xactlycorp.com> wrote:
>> >
>> > Is it possible to just parse sql function expression which is not a
>> > complete sql syntax.
>> > e.g. I want to parse expression `SUM(t1.amount)/COUNT(t1)` instead of
>> whole
>> > sql like "select t1.name, SUM(t1.amount)/COUNT(t1) from mytable t1
>> group by
>> > t1.name". I tried
>> >
>> > val parser = SqlParser.create("SUM(t1.amount)/COUNT(t1)")
>> > val sqlNode = parser.parseExpression()
>> >
>> > but it fails with parse exception.
>> >
>> > Thanks
>> >
>> > --
>> >
>> >
>> > [image: What's New with Xactly] <http://www.xactlycorp.com/email-click/
>> >
>> >
>> > <https://www.instagram.com/xactlycorp/>
>> > <https://www.linkedin.com/company/xactly-corporation>
>> > <https://twitter.com/Xactly> <https://www.facebook.com/XactlyCorp>
>> > <http://www.youtube.com/xactlycorporation>
>>
>>
>
--
[image: What's New with Xactly] <http://www.xactlycorp.com/email-click/>
<https://www.instagram.com/xactlycorp/>
<https://www.linkedin.com/company/xactly-corporation>
<https://twitter.com/Xactly> <https://www.facebook.com/XactlyCorp>
<http://www.youtube.com/xactlycorporation>
Re: How to parse sql expression to obtain field names
Posted by Nirav Patel <np...@xactlycorp.com>.
Sorry my original expression had an extra ")" in the end. Thanks for
verifying.
Followup question:
How do I extract Field(Column) names from all such expressions? I tried
implementing SqlVisitor. I can see SqlCall has fields operators and operand
but when I fetch them I get List[SqlNode] can they be converted directly to
String? or any getNames() method? Or do I need to do recursive traversal
and each time I get sqlNode I provide new visitor until I hit
SqlIdentifier? Can you provide some suggestion to effectively get all
column names from expression.
Thanks,
Nirav
On Thu, Jan 18, 2018 at 3:01 PM, Julian Hyde <jh...@apache.org> wrote:
> It works for me.
>
> public static void main(String[] args) throws SqlParseException {
> try {
> SqlParser parser = SqlParser.create("SUM(t1.amount)/COUNT(t1)");
> SqlNode sqlNode = parser.parseExpression();
> } catch (Throwable e) {
> e.printStackTrace();
> }
> }
>
>
> > On Jan 18, 2018, at 1:44 PM, Nirav Patel <np...@xactlycorp.com> wrote:
> >
> > Is it possible to just parse sql function expression which is not a
> > complete sql syntax.
> > e.g. I want to parse expression `SUM(t1.amount)/COUNT(t1)` instead of
> whole
> > sql like "select t1.name, SUM(t1.amount)/COUNT(t1) from mytable t1
> group by
> > t1.name". I tried
> >
> > val parser = SqlParser.create("SUM(t1.amount)/COUNT(t1)")
> > val sqlNode = parser.parseExpression()
> >
> > but it fails with parse exception.
> >
> > Thanks
> >
> > --
> >
> >
> > [image: What's New with Xactly] <http://www.xactlycorp.com/email-click/>
> >
> > <https://www.instagram.com/xactlycorp/>
> > <https://www.linkedin.com/company/xactly-corporation>
> > <https://twitter.com/Xactly> <https://www.facebook.com/XactlyCorp>
> > <http://www.youtube.com/xactlycorporation>
>
>
--
[image: What's New with Xactly] <http://www.xactlycorp.com/email-click/>
<https://www.instagram.com/xactlycorp/>
<https://www.linkedin.com/company/xactly-corporation>
<https://twitter.com/Xactly> <https://www.facebook.com/XactlyCorp>
<http://www.youtube.com/xactlycorporation>
Re: How to parse sql expression to obtain field names
Posted by Julian Hyde <jh...@apache.org>.
It works for me.
public static void main(String[] args) throws SqlParseException {
try {
SqlParser parser = SqlParser.create("SUM(t1.amount)/COUNT(t1)");
SqlNode sqlNode = parser.parseExpression();
} catch (Throwable e) {
e.printStackTrace();
}
}
> On Jan 18, 2018, at 1:44 PM, Nirav Patel <np...@xactlycorp.com> wrote:
>
> Is it possible to just parse sql function expression which is not a
> complete sql syntax.
> e.g. I want to parse expression `SUM(t1.amount)/COUNT(t1)` instead of whole
> sql like "select t1.name, SUM(t1.amount)/COUNT(t1) from mytable t1 group by
> t1.name". I tried
>
> val parser = SqlParser.create("SUM(t1.amount)/COUNT(t1)")
> val sqlNode = parser.parseExpression()
>
> but it fails with parse exception.
>
> Thanks
>
> --
>
>
> [image: What's New with Xactly] <http://www.xactlycorp.com/email-click/>
>
> <https://www.instagram.com/xactlycorp/>
> <https://www.linkedin.com/company/xactly-corporation>
> <https://twitter.com/Xactly> <https://www.facebook.com/XactlyCorp>
> <http://www.youtube.com/xactlycorporation>