You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Kiril Menshikov <km...@gmail.com> on 2016/05/12 16:06:39 UTC

Make Schema from JDBC connection.

Hi,

I am trying to modify SQL query with Calcite (add select fields and where
statements). The schema are created from JDBC connection.

Executing simple SQL statement I get exception.

SqlParseException: Encounted ".timestamp" at line 1, column 10.

Can you help me and point me what is wrong? SQL are correct and executed
from sqlplus.

My Code:

              Class.*forName*("org.apache.calcite.jdbc.Driver");

              Connection connection = DriverManager.*getConnection*(
"jdbc:calcite:");

              CalciteConnection calciteConnection =connection
.unwrap(CalciteConnection.*class*);

              calciteConnection.setSchema("latency");

              SchemaPlus rootSchema = calciteConnection.getRootSchema();

              JdbcSchema defaultSchema = JdbcSchema.*create*(rootSchema,
"latency", dataSource, *null*,*null*);

              rootSchema.add("latency", defaultSchema);



              List<*RelTraitDef*> traitDefs = *new*ArrayList<*RelTraitDef*
>();

              SchemaPlus *defaultSchema1*;

              FrameworkConfig calciteFrameworkConfig = Frameworks.
*newConfigBuilder*()

                           .parserConfig(SqlParser.*configBuilder*
().setLex(Lex.*ORACLE*).build())

                            .defaultSchema(rootSchema.getSubSchema(
calciteConnection.getSchema())).traitDefs(traitDefs)

                           .context(Contexts.*EMPTY_CONTEXT*
).ruleSets(RuleSets.*ofList*()).costFactory(*null*)

                           .typeSystem(RelDataTypeSystem.*DEFAULT*).build();

              Planner planner1 = Frameworks.*getPlanner*(
calciteFrameworkConfig);

              SqlNode sqlNode1 = planner1.parse("select i1.timestamp
timestamp from latency i1");

              SqlNode validatedSqlNode1 =planner1.validate(sqlNode1);

              RelNode *logicalPlan* =planner1.rel(validatedSqlNode1
).project();


Thanks,
--Kiril

Re: Make Schema from JDBC connection.

Posted by Julian Hyde <jh...@apache.org>.
TIMESTAMP is a reserved word. Put double-quotes around the column name and you should be OK. In https://issues.apache.org/jira/browse/CALCITE-997 <https://issues.apache.org/jira/browse/CALCITE-997> someone was having a very similar problem with a column called VALUE.

Julian


> On May 12, 2016, at 9:06 AM, Kiril Menshikov <km...@gmail.com> wrote:
> 
> Hi,
> 
> I am trying to modify SQL query with Calcite (add select fields and where
> statements). The schema are created from JDBC connection.
> 
> Executing simple SQL statement I get exception.
> 
> SqlParseException: Encounted ".timestamp" at line 1, column 10.
> 
> Can you help me and point me what is wrong? SQL are correct and executed
> from sqlplus.
> 
> My Code:
> 
>              Class.*forName*("org.apache.calcite.jdbc.Driver");
> 
>              Connection connection = DriverManager.*getConnection*(
> "jdbc:calcite:");
> 
>              CalciteConnection calciteConnection =connection
> .unwrap(CalciteConnection.*class*);
> 
>              calciteConnection.setSchema("latency");
> 
>              SchemaPlus rootSchema = calciteConnection.getRootSchema();
> 
>              JdbcSchema defaultSchema = JdbcSchema.*create*(rootSchema,
> "latency", dataSource, *null*,*null*);
> 
>              rootSchema.add("latency", defaultSchema);
> 
> 
> 
>              List<*RelTraitDef*> traitDefs = *new*ArrayList<*RelTraitDef*
>> ();
> 
>              SchemaPlus *defaultSchema1*;
> 
>              FrameworkConfig calciteFrameworkConfig = Frameworks.
> *newConfigBuilder*()
> 
>                           .parserConfig(SqlParser.*configBuilder*
> ().setLex(Lex.*ORACLE*).build())
> 
>                            .defaultSchema(rootSchema.getSubSchema(
> calciteConnection.getSchema())).traitDefs(traitDefs)
> 
>                           .context(Contexts.*EMPTY_CONTEXT*
> ).ruleSets(RuleSets.*ofList*()).costFactory(*null*)
> 
>                           .typeSystem(RelDataTypeSystem.*DEFAULT*).build();
> 
>              Planner planner1 = Frameworks.*getPlanner*(
> calciteFrameworkConfig);
> 
>              SqlNode sqlNode1 = planner1.parse("select i1.timestamp
> timestamp from latency i1");
> 
>              SqlNode validatedSqlNode1 =planner1.validate(sqlNode1);
> 
>              RelNode *logicalPlan* =planner1.rel(validatedSqlNode1
> ).project();
> 
> 
> Thanks,
> --Kiril