You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by 沈志宏 <bl...@cnic.cn> on 2016/09/09 09:21:56 UTC

is there any tool class to generate RexNodes from a SQL command?

hi, dear all

is there any tool class to generate RexNodes from a SQL command?
for example,

List<RexNode> RexNodeUtils.parse(schema, "select * from persons where name like 'm%' and age>10")

i am writing a solr adapter and i really really need it to help write java tests for my query translator.

best regards




Re: is there any tool class to generate RexNodes from a SQL command?

Posted by "jordan.halterman@gmail.com" <jo...@gmail.com>.
I think there's a reset method IIRC that can be used to reset the state of the planner.

> On Sep 14, 2016, at 7:59 PM, bluejoe <bl...@cnic.cn> wrote:
> 
> By the way, I took an insight into the PlanerImpl.validate():
> 
> public SqlNode validate(SqlNode sqlNode) throws ValidationException {
>    ensure(State.STATE_3_PARSED);
>    this.validator =
>        new CalciteSqlValidator(
>            operatorTable, createCatalogReader(), typeFactory);
>    this.validator.setIdentifierExpansion(true);
>    try {
>      validatedSqlNode = validator.validate(sqlNode);
>    } catch (RuntimeException e) {
>      throw new ValidationException(e);
>    }
>    state = State.STATE_4_VALIDATED;
>    return validatedSqlNode;
>  }
> 
> 
> I noticed the state is often changed in such steps, so, if this cause a PlanerImpl instance is always bound to one SqlNode and cannot be reused to parse/validate another SqlNodes?
> 
> ———————————————
> Zhihong SHEN, Ph. D., Senior Engineer
> Big Data Application Service Technology Laboratory,
> Computer Network Information Center, Chinese Academy of Sciences
> office phone:+86-10-58812516
> mobile:+86-13671116520
> 
> 
> 
> 
> 
> 
> 
> 
>> On 9/15/16, 1:07 AM, "jordan.halterman@gmail.com" <dev-return-3808-bluejoe=cnic.cn@calcite.apache.org on behalf of jordan.halterman@gmail.com> wrote:
>> 
>> You have to run planner.validate after parse, otherwise the state in PlannerImpl will be incorrect. You can also go into the PlannerImpl and steal some code if you need to circumvent those states, but I agree this is probably the easiest way to go about it. The alternative is just creating a parser and SqlToRelConverter to do the parsing and conversion yourself. PlannerImpl takes care of a lot of other things as well like flattening nested types, view expansion, etc IIRC.
>> 
>>> On Sep 14, 2016, at 2:29 AM, bluejoe <bl...@cnic.cn> wrote:
>>> 
>>> Hi, Hyde
>>> 
>>> I wrote code like this:
>>> 
>>> FrameworkConfig config = Frameworks.newConfigBuilder().build();
>>> Planner planner = Frameworks.getPlanner(config);
>>>       SqlNode node = planner.parse(sql);
>>>       RelRoot relRoot = planner.rel(node);
>>>       RelNode project = relRoot.project();
>>>       RexNode condition = ((Filter) ((Project) project).getInput()).getCondition();
>>> 
>>> 
>>> However, an exception was thrown while running 'RelRoot relRoot = planner.rel(node);’:
>>> 
>>> java.lang.IllegalArgumentException: cannot move from STATE_3_PARSED to STATE_4_VALIDATED
>>>   at org.apache.calcite.prepare.PlannerImpl$State.from(PlannerImpl.java:318)
>>>   at org.apache.calcite.prepare.PlannerImpl.ensure(PlannerImpl.java:108)
>>>   at org.apache.calcite.prepare.PlannerImpl.rel(PlannerImpl.java:200)
>>> 
>>> How to avoid the error?
>>> 
>>> thanks~
>>> 
>>> 
>>> ———————————————
>>> Zhihong SHEN, Ph. D., Senior Engineer
>>> Big Data Application Service Technology Laboratory,
>>> Computer Network Information Center, Chinese Academy of Sciences
>>> office phone:+86-10-58812516
>>> mobile:+86-13671116520
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 在 16/9/10 上午1:25,“Julian Hyde”<dev-return-3781-bluejoe=cnic.cn@calcite.apache.org 代表 jhyde@apache.org> 写入:
>>> 
>>>> If you can parse & convert to get a RelNode, then the RelNode will probably be a Project with a Filter underneath. So you can write
>>>> 
>>>> RelNode r;
>>>> RexNode condition = ((Filter) ((Project) r).getInput()).getCondition;
>>>> 
>>>> Now, how to parse a SQL statement to a RelNode? I think I’d use
>>>> 
>>>> String sql = …;
>>>> FrameworkConfig config = ...;
>>>> Planner planner = Frameworks.getPlanner(config);
>>>> SqlNode node = planner.parse(sql);
>>>> RelRoot relRoot = planner.rel(node);
>>>> RelNode r = relRoot.project();
>>>> 
>>>> but I’d like to hear what others consider to be the most painless way to use Calcite’s parser.
>>>> 
>>>> Julian
>>>> 
>>>>> On Sep 9, 2016, at 2:21 AM, 沈志宏 <bl...@cnic.cn> wrote:
>>>>> 
>>>>> hi, dear all
>>>>> 
>>>>> is there any tool class to generate RexNodes from a SQL command?
>>>>> for example,
>>>>> 
>>>>> List<RexNode> RexNodeUtils.parse(schema, "select * from persons where name like 'm%' and age>10")
>>>>> 
>>>>> i am writing a solr adapter and i really really need it to help write java tests for my query translator.
>>>>> 
>>>>> best regards
> 
> 

Re: is there any tool class to generate RexNodes from a SQL command?

Posted by bluejoe <bl...@cnic.cn>.
By the way, I took an insight into the PlanerImpl.validate():

public SqlNode validate(SqlNode sqlNode) throws ValidationException {
    ensure(State.STATE_3_PARSED);
    this.validator =
        new CalciteSqlValidator(
            operatorTable, createCatalogReader(), typeFactory);
    this.validator.setIdentifierExpansion(true);
    try {
      validatedSqlNode = validator.validate(sqlNode);
    } catch (RuntimeException e) {
      throw new ValidationException(e);
    }
    state = State.STATE_4_VALIDATED;
    return validatedSqlNode;
  }


I noticed the state is often changed in such steps, so, if this cause a PlanerImpl instance is always bound to one SqlNode and cannot be reused to parse/validate another SqlNodes?
 
———————————————
Zhihong SHEN, Ph. D., Senior Engineer
Big Data Application Service Technology Laboratory,
Computer Network Information Center, Chinese Academy of Sciences
office phone:+86-10-58812516
mobile:+86-13671116520








On 9/15/16, 1:07 AM, "jordan.halterman@gmail.com" <dev-return-3808-bluejoe=cnic.cn@calcite.apache.org on behalf of jordan.halterman@gmail.com> wrote:

>You have to run planner.validate after parse, otherwise the state in PlannerImpl will be incorrect. You can also go into the PlannerImpl and steal some code if you need to circumvent those states, but I agree this is probably the easiest way to go about it. The alternative is just creating a parser and SqlToRelConverter to do the parsing and conversion yourself. PlannerImpl takes care of a lot of other things as well like flattening nested types, view expansion, etc IIRC.
>
>> On Sep 14, 2016, at 2:29 AM, bluejoe <bl...@cnic.cn> wrote:
>> 
>> Hi, Hyde
>> 
>> I wrote code like this:
>> 
>> FrameworkConfig config = Frameworks.newConfigBuilder().build();
>> Planner planner = Frameworks.getPlanner(config);
>>        SqlNode node = planner.parse(sql);
>>        RelRoot relRoot = planner.rel(node);
>>        RelNode project = relRoot.project();
>>        RexNode condition = ((Filter) ((Project) project).getInput()).getCondition();
>> 
>> 
>> However, an exception was thrown while running 'RelRoot relRoot = planner.rel(node);’:
>> 
>> java.lang.IllegalArgumentException: cannot move from STATE_3_PARSED to STATE_4_VALIDATED
>>    at org.apache.calcite.prepare.PlannerImpl$State.from(PlannerImpl.java:318)
>>    at org.apache.calcite.prepare.PlannerImpl.ensure(PlannerImpl.java:108)
>>    at org.apache.calcite.prepare.PlannerImpl.rel(PlannerImpl.java:200)
>> 
>> How to avoid the error?
>> 
>> thanks~
>> 
>> 
>> ———————————————
>> Zhihong SHEN, Ph. D., Senior Engineer
>> Big Data Application Service Technology Laboratory,
>> Computer Network Information Center, Chinese Academy of Sciences
>> office phone:+86-10-58812516
>> mobile:+86-13671116520
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 在 16/9/10 上午1:25,“Julian Hyde”<dev-return-3781-bluejoe=cnic.cn@calcite.apache.org 代表 jhyde@apache.org> 写入:
>> 
>>> If you can parse & convert to get a RelNode, then the RelNode will probably be a Project with a Filter underneath. So you can write
>>> 
>>> RelNode r;
>>> RexNode condition = ((Filter) ((Project) r).getInput()).getCondition;
>>> 
>>> Now, how to parse a SQL statement to a RelNode? I think I’d use
>>> 
>>> String sql = …;
>>> FrameworkConfig config = ...;
>>> Planner planner = Frameworks.getPlanner(config);
>>> SqlNode node = planner.parse(sql);
>>> RelRoot relRoot = planner.rel(node);
>>> RelNode r = relRoot.project();
>>> 
>>> but I’d like to hear what others consider to be the most painless way to use Calcite’s parser.
>>> 
>>> Julian
>>> 
>>>> On Sep 9, 2016, at 2:21 AM, 沈志宏 <bl...@cnic.cn> wrote:
>>>> 
>>>> hi, dear all
>>>> 
>>>> is there any tool class to generate RexNodes from a SQL command?
>>>> for example,
>>>> 
>>>> List<RexNode> RexNodeUtils.parse(schema, "select * from persons where name like 'm%' and age>10")
>>>> 
>>>> i am writing a solr adapter and i really really need it to help write java tests for my query translator.
>>>> 
>>>> best regards
>> 
>> 



Re: is there any tool class to generate RexNodes from a SQL command?

Posted by "jordan.halterman@gmail.com" <jo...@gmail.com>.
You have to run planner.validate after parse, otherwise the state in PlannerImpl will be incorrect. You can also go into the PlannerImpl and steal some code if you need to circumvent those states, but I agree this is probably the easiest way to go about it. The alternative is just creating a parser and SqlToRelConverter to do the parsing and conversion yourself. PlannerImpl takes care of a lot of other things as well like flattening nested types, view expansion, etc IIRC.

> On Sep 14, 2016, at 2:29 AM, bluejoe <bl...@cnic.cn> wrote:
> 
> Hi, Hyde
> 
> I wrote code like this:
> 
> FrameworkConfig config = Frameworks.newConfigBuilder().build();
> Planner planner = Frameworks.getPlanner(config);
>        SqlNode node = planner.parse(sql);
>        RelRoot relRoot = planner.rel(node);
>        RelNode project = relRoot.project();
>        RexNode condition = ((Filter) ((Project) project).getInput()).getCondition();
> 
> 
> However, an exception was thrown while running 'RelRoot relRoot = planner.rel(node);’:
> 
> java.lang.IllegalArgumentException: cannot move from STATE_3_PARSED to STATE_4_VALIDATED
>    at org.apache.calcite.prepare.PlannerImpl$State.from(PlannerImpl.java:318)
>    at org.apache.calcite.prepare.PlannerImpl.ensure(PlannerImpl.java:108)
>    at org.apache.calcite.prepare.PlannerImpl.rel(PlannerImpl.java:200)
> 
> How to avoid the error?
> 
> thanks~
> 
> 
> ———————————————
> Zhihong SHEN, Ph. D., Senior Engineer
> Big Data Application Service Technology Laboratory,
> Computer Network Information Center, Chinese Academy of Sciences
> office phone:+86-10-58812516
> mobile:+86-13671116520
> 
> 
> 
> 
> 
> 
> 
> 
> 在 16/9/10 上午1:25,“Julian Hyde”<dev-return-3781-bluejoe=cnic.cn@calcite.apache.org 代表 jhyde@apache.org> 写入:
> 
>> If you can parse & convert to get a RelNode, then the RelNode will probably be a Project with a Filter underneath. So you can write
>> 
>> RelNode r;
>> RexNode condition = ((Filter) ((Project) r).getInput()).getCondition;
>> 
>> Now, how to parse a SQL statement to a RelNode? I think I’d use
>> 
>> String sql = …;
>> FrameworkConfig config = ...;
>> Planner planner = Frameworks.getPlanner(config);
>> SqlNode node = planner.parse(sql);
>> RelRoot relRoot = planner.rel(node);
>> RelNode r = relRoot.project();
>> 
>> but I’d like to hear what others consider to be the most painless way to use Calcite’s parser.
>> 
>> Julian
>> 
>>> On Sep 9, 2016, at 2:21 AM, 沈志宏 <bl...@cnic.cn> wrote:
>>> 
>>> hi, dear all
>>> 
>>> is there any tool class to generate RexNodes from a SQL command?
>>> for example,
>>> 
>>> List<RexNode> RexNodeUtils.parse(schema, "select * from persons where name like 'm%' and age>10")
>>> 
>>> i am writing a solr adapter and i really really need it to help write java tests for my query translator.
>>> 
>>> best regards
> 
> 

Re: is there any tool class to generate RexNodes from a SQL command?

Posted by bluejoe <bl...@cnic.cn>.
Hi, Hyde

I wrote code like this:

FrameworkConfig config = Frameworks.newConfigBuilder().build();
Planner planner = Frameworks.getPlanner(config);
		SqlNode node = planner.parse(sql);
		RelRoot relRoot = planner.rel(node);
		RelNode project = relRoot.project();
		RexNode condition = ((Filter) ((Project) project).getInput()).getCondition();


However, an exception was thrown while running 'RelRoot relRoot = planner.rel(node);’:

java.lang.IllegalArgumentException: cannot move from STATE_3_PARSED to STATE_4_VALIDATED
	at org.apache.calcite.prepare.PlannerImpl$State.from(PlannerImpl.java:318)
	at org.apache.calcite.prepare.PlannerImpl.ensure(PlannerImpl.java:108)
	at org.apache.calcite.prepare.PlannerImpl.rel(PlannerImpl.java:200)

How to avoid the error?

thanks~


———————————————
Zhihong SHEN, Ph. D., Senior Engineer
Big Data Application Service Technology Laboratory,
Computer Network Information Center, Chinese Academy of Sciences
office phone:+86-10-58812516
mobile:+86-13671116520








在 16/9/10 上午1:25,“Julian Hyde”<dev-return-3781-bluejoe=cnic.cn@calcite.apache.org 代表 jhyde@apache.org> 写入:

>If you can parse & convert to get a RelNode, then the RelNode will probably be a Project with a Filter underneath. So you can write
>
>  RelNode r;
>  RexNode condition = ((Filter) ((Project) r).getInput()).getCondition;
>
>Now, how to parse a SQL statement to a RelNode? I think I’d use
>
>  String sql = …;
>  FrameworkConfig config = ...;
>  Planner planner = Frameworks.getPlanner(config);
>  SqlNode node = planner.parse(sql);
>  RelRoot relRoot = planner.rel(node);
>  RelNode r = relRoot.project();
>
>but I’d like to hear what others consider to be the most painless way to use Calcite’s parser.
>
>Julian
>
>> On Sep 9, 2016, at 2:21 AM, 沈志宏 <bl...@cnic.cn> wrote:
>> 
>> hi, dear all
>> 
>> is there any tool class to generate RexNodes from a SQL command?
>> for example,
>> 
>> List<RexNode> RexNodeUtils.parse(schema, "select * from persons where name like 'm%' and age>10")
>> 
>> i am writing a solr adapter and i really really need it to help write java tests for my query translator.
>> 
>> best regards
>> 
>> 
>> 
>



Re: is there any tool class to generate RexNodes from a SQL command?

Posted by Julian Hyde <jh...@apache.org>.
If you can parse & convert to get a RelNode, then the RelNode will probably be a Project with a Filter underneath. So you can write

  RelNode r;
  RexNode condition = ((Filter) ((Project) r).getInput()).getCondition;

Now, how to parse a SQL statement to a RelNode? I think I’d use

  String sql = …;
  FrameworkConfig config = ...;
  Planner planner = Frameworks.getPlanner(config);
  SqlNode node = planner.parse(sql);
  RelRoot relRoot = planner.rel(node);
  RelNode r = relRoot.project();

but I’d like to hear what others consider to be the most painless way to use Calcite’s parser.

Julian

> On Sep 9, 2016, at 2:21 AM, 沈志宏 <bl...@cnic.cn> wrote:
> 
> hi, dear all
> 
> is there any tool class to generate RexNodes from a SQL command?
> for example,
> 
> List<RexNode> RexNodeUtils.parse(schema, "select * from persons where name like 'm%' and age>10")
> 
> i am writing a solr adapter and i really really need it to help write java tests for my query translator.
> 
> best regards
> 
> 
>