OmniSciDB  a5dc49c757
 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 
46 public class tester {
47  final static org.slf4j.Logger HEAVYDBLOGGER = LoggerFactory.getLogger(tester.class);
48 
49  public static void main(String[] args) {
50  final SqlStdOperatorTable stdOpTab = SqlStdOperatorTable.instance();
51 
52  HeavyDBUser mdu = new HeavyDBUser("admin", "passwd", "omnisci", -1, null);
53  HeavyDBSchema dbSchema =
54  new HeavyDBSchema("<<PATH_TO_DATA_DIR>>", null, -1, mdu, null, null);
55  final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
56  final FrameworkConfig config =
57  Frameworks.newConfigBuilder()
58  .defaultSchema(rootSchema.add("omnisci", dbSchema))
59  .operatorTable(stdOpTab)
60  .parserConfig(SqlParser.configBuilder()
61  .setConformance(SqlConformanceEnum.LENIENT)
62  .setUnquotedCasing(Casing.UNCHANGED)
63  .setCaseSensitive(false)
64  .build())
65  .build();
66 
67  Planner p = Frameworks.getPlanner(config);
68 
69  SqlNode parseR = null;
70  try {
71  parseR = p.parse("<<QUERY>>");
72  } catch (SqlParseException ex) {
73  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
74  }
75 
76  SqlNode validateR = null;
77  try {
78  p.validate(parseR);
79  } catch (ValidationException ex) {
80  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
81  }
82  RelRoot relR = null;
83  try {
84  relR = p.rel(validateR);
85  } catch (RelConversionException ex) {
86  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
87  }
88  HEAVYDBLOGGER.error("Result was " + relR);
89  HEAVYDBLOGGER.error("Result project() " + relR.project());
90  HEAVYDBLOGGER.error("Result project() " + RelOptUtil.toString(relR.project()));
91  HEAVYDBLOGGER.error("Json Version \n" + HeavyDBSerializer.toString(relR.project()));
92 
93  // now do with HeavyDB parser
94  Supplier<HeavyDBSqlOperatorTable> operatorTable =
95  new Supplier<HeavyDBSqlOperatorTable>() {
96  @Override
97  public HeavyDBSqlOperatorTable get() {
98  return new HeavyDBSqlOperatorTable(SqlStdOperatorTable.instance());
99  }
100  };
101  HeavyDBParser mp = new HeavyDBParser("<<PATH_TO_DATA_DIR>>", operatorTable, -1, null);
102  mp.setUser(mdu);
103 
104  try {
106  HEAVYDBLOGGER.error("HeavyDBParser result: \n" + mp.processSql("<<QUERY>>", mdpo));
107  } catch (SqlParseException ex) {
108  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
109  } catch (ValidationException ex) {
110  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
111  } catch (RelConversionException ex) {
112  Logger.getLogger(tester.class.getName()).log(Level.SEVERE, null, ex);
113  }
114  }
115 
116  // /** User-defined aggregate function. */
117  // public static class MyCountAggFunction extends SqlAggFunction {
118  // public MyCountAggFunction() {
119  // super("MY_COUNT", null, SqlKind.OTHER_FUNCTION, ReturnTypes.BIGINT, null,
120  // OperandTypes.ANY, SqlFunctionCategory.NUMERIC, false, false);
121  // }
122  //
123  // @SuppressWarnings("deprecation")
124  // public List<RelDataType> getParameterTypes(RelDataTypeFactory typeFactory) {
125  // return ImmutableList.of(typeFactory.createSqlType(SqlTypeName.ANY));
126  // }
127  //
128  // @SuppressWarnings("deprecation")
129  // public RelDataType getReturnType(RelDataTypeFactory typeFactory) {
130  // return typeFactory.createSqlType(SqlTypeName.BIGINT);
131  // }
132  //
133  // public RelDataType deriveType(SqlValidator validator,
134  // SqlValidatorScope scope, SqlCall call) {
135  // // Check for COUNT(*) function. If it is we don't
136  // // want to try and derive the "*"
137  // if (call.isCountStar()) {
138  // return validator.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
139  // }
140  // return super.deriveType(validator, scope, call);
141  // }
142  // }
143 }
static final org.slf4j.Logger HEAVYDBLOGGER
Definition: tester.java:47
static void main(String[] args)
Definition: tester.java:49