OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tester.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.mapd.calcite.planner;
18 
25 
26 import org.apache.calcite.avatica.util.Casing;
27 import org.apache.calcite.plan.RelOptUtil;
28 import org.apache.calcite.rel.RelRoot;
29 import org.apache.calcite.schema.SchemaPlus;
30 import org.apache.calcite.sql.SqlNode;
31 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
32 import org.apache.calcite.sql.parser.SqlParseException;
33 import org.apache.calcite.sql.parser.SqlParser;
34 import org.apache.calcite.sql.validate.SqlConformanceEnum;
35 import org.apache.calcite.tools.FrameworkConfig;
36 import org.apache.calcite.tools.Frameworks;
37 import org.apache.calcite.tools.Planner;
38 import org.apache.calcite.tools.RelConversionException;
39 import org.apache.calcite.tools.ValidationException;
40 import org.slf4j.LoggerFactory;
41 
42 import java.util.function.Supplier;
43 import java.util.logging.Level;
44 import java.util.logging.Logger;
45 
50 public class tester {
51  final static org.slf4j.Logger HEAVYDBLOGGER = LoggerFactory.getLogger(tester.class);
52 
53  public static void main(String[] args) {
54  final SqlStdOperatorTable stdOpTab = SqlStdOperatorTable.instance();
55 
56  HeavyDBUser mdu = new HeavyDBUser("admin", "passwd", "omnisci", -1, null);
57  HeavyDBSchema dbSchema =
58  new HeavyDBSchema("<<PATH_TO_DATA_DIR>>", null, -1, mdu, null);
59  final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
60  final FrameworkConfig config =
61  Frameworks.newConfigBuilder()
62  .defaultSchema(rootSchema.add("omnisci", dbSchema))
63  .operatorTable(stdOpTab)
64  .parserConfig(SqlParser.configBuilder()
65  .setConformance(SqlConformanceEnum.LENIENT)
66  .setUnquotedCasing(Casing.UNCHANGED)
67  .setCaseSensitive(false)
68  .build())
69  .build();
70 
71  Planner p = Frameworks.getPlanner(config);
72 
73  SqlNode parseR = null;
74  try {
75  parseR = p.parse("<<QUERY>>");
76  } catch (SqlParseException ex) {
77  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
78  }
79 
80  SqlNode validateR = null;
81  try {
82  p.validate(parseR);
83  } catch (ValidationException ex) {
84  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
85  }
86  RelRoot relR = null;
87  try {
88  relR = p.rel(validateR);
89  } catch (RelConversionException ex) {
90  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
91  }
92  HEAVYDBLOGGER.error("Result was " + relR);
93  HEAVYDBLOGGER.error("Result project() " + relR.project());
94  HEAVYDBLOGGER.error("Result project() " + RelOptUtil.toString(relR.project()));
95  HEAVYDBLOGGER.error("Json Version \n" + HeavyDBSerializer.toString(relR.project()));
96 
97  // now do with HeavyDB parser
98  Supplier<HeavyDBSqlOperatorTable> operatorTable =
99  new Supplier<HeavyDBSqlOperatorTable>() {
100  @Override
101  public HeavyDBSqlOperatorTable get() {
102  return new HeavyDBSqlOperatorTable(SqlStdOperatorTable.instance());
103  }
104  };
105  HeavyDBParser mp = new HeavyDBParser("<<PATH_TO_DATA_DIR>>", operatorTable, -1, null);
106  mp.setUser(mdu);
107 
108  try {
110  HEAVYDBLOGGER.error("HeavyDBParser result: \n" + mp.processSql("<<QUERY>>", mdpo));
111  } catch (SqlParseException ex) {
112  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
113  } catch (ValidationException ex) {
114  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
115  } catch (RelConversionException ex) {
116  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
117  }
118  }
119 
120  // /** User-defined aggregate function. */
121  // public static class MyCountAggFunction extends SqlAggFunction {
122  // public MyCountAggFunction() {
123  // super("MY_COUNT", null, SqlKind.OTHER_FUNCTION, ReturnTypes.BIGINT, null,
124  // OperandTypes.ANY, SqlFunctionCategory.NUMERIC, false, false);
125  // }
126  //
127  // @SuppressWarnings("deprecation")
128  // public List<RelDataType> getParameterTypes(RelDataTypeFactory typeFactory) {
129  // return ImmutableList.of(typeFactory.createSqlType(SqlTypeName.ANY));
130  // }
131  //
132  // @SuppressWarnings("deprecation")
133  // public RelDataType getReturnType(RelDataTypeFactory typeFactory) {
134  // return typeFactory.createSqlType(SqlTypeName.BIGINT);
135  // }
136  //
137  // public RelDataType deriveType(SqlValidator validator,
138  // SqlValidatorScope scope, SqlCall call) {
139  // // Check for COUNT(*) function. If it is we don't
140  // // want to try and derive the "*"
141  // if (call.isCountStar()) {
142  // return validator.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
143  // }
144  // return super.deriveType(validator, scope, call);
145  // }
146  // }
147 }
static final org.slf4j.Logger HEAVYDBLOGGER
Definition: tester.java:51
static void main(String[] args)
Definition: tester.java:53