OmniSciDB  17c254d2f8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MapDPlanner.java
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to you under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.calcite.prepare;
18 
20 
21 import org.apache.calcite.config.CalciteConnectionConfig;
22 import org.apache.calcite.config.CalciteConnectionConfigImpl;
23 import org.apache.calcite.config.CalciteConnectionProperty;
24 import org.apache.calcite.jdbc.CalciteSchema;
25 import org.apache.calcite.linq4j.function.Functions;
26 import org.apache.calcite.plan.Context;
27 import org.apache.calcite.plan.RelOptCostImpl;
28 import org.apache.calcite.plan.hep.HepPlanner;
29 import org.apache.calcite.plan.hep.HepProgram;
30 import org.apache.calcite.rel.RelNode;
31 import org.apache.calcite.rel.RelRoot;
32 import org.apache.calcite.rel.core.RelFactories;
34 import org.apache.calcite.rel.rules.FilterJoinRule;
37 import org.apache.calcite.schema.SchemaPlus;
38 import org.apache.calcite.sql.SqlNode;
39 import org.apache.calcite.sql.advise.SqlAdvisor;
40 import org.apache.calcite.sql.validate.SqlConformanceEnum;
41 import org.apache.calcite.sql.validate.SqlMoniker;
42 import org.apache.calcite.tools.FrameworkConfig;
43 import org.apache.calcite.tools.RelConversionException;
44 
45 import java.lang.reflect.InvocationTargetException;
46 import java.lang.reflect.Method;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Properties;
50 
57 public class MapDPlanner extends PlannerImpl {
58  FrameworkConfig config;
59  private List<MapDParserOptions.FilterPushDownInfo> filterPushDownInfo =
60  new ArrayList<>();
61 
62  public MapDPlanner(FrameworkConfig config) {
63  super(config);
64  this.config = config;
65  }
66 
67  private static SchemaPlus rootSchema(SchemaPlus schema) {
68  for (;;) {
69  if (schema.getParentSchema() == null) {
70  return schema;
71  }
72  schema = schema.getParentSchema();
73  }
74  }
75 
76  public static class CompletionResult {
77  public List<SqlMoniker> hints;
78  public String replaced;
79 
80  CompletionResult(final List<SqlMoniker> hints, final String replaced) {
81  this.hints = hints;
82  this.replaced = replaced;
83  }
84  }
85 
86  private CalciteCatalogReader createCatalogReader() {
87  final SchemaPlus rootSchema = rootSchema(config.getDefaultSchema());
88  final Context context = config.getContext();
89  final CalciteConnectionConfig connectionConfig;
90 
91  if (context != null) {
92  connectionConfig = context.unwrap(CalciteConnectionConfig.class);
93  } else {
94  Properties properties = new Properties();
95  properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
96  String.valueOf(config.getParserConfig().caseSensitive()));
97  connectionConfig = new CalciteConnectionConfigImpl(properties);
98  }
99 
100  return new CalciteCatalogReader(CalciteSchema.from(rootSchema),
101  CalciteSchema.from(config.getDefaultSchema()).path(null),
102  getTypeFactory(),
103  connectionConfig);
104  }
105 
106  public void ready() {
107  // need to call ready on the super class, but that method is marked private
108  // circumventing via reflection for now
109  try {
110  Method readyMethod = getClass().getSuperclass().getDeclaredMethod("ready");
111  readyMethod.setAccessible(true);
112  readyMethod.invoke(this);
113  } catch (InvocationTargetException e) {
114  if (e.getCause() instanceof RuntimeException) {
115  throw(RuntimeException) e.getCause();
116  } else {
117  throw new RuntimeException(e.getCause());
118  }
119  } catch (Exception e) {
120  throw new RuntimeException(e);
121  }
122  }
123 
125  final String sql, final int cursor, final List<String> visibleTables) {
126  ready();
127  MapDSqlAdvisorValidator advisor_validator = new MapDSqlAdvisorValidator(visibleTables,
128  config.getOperatorTable(),
130  getTypeFactory(),
131  SqlConformanceEnum.LENIENT);
132  SqlAdvisor advisor = new MapDSqlAdvisor(advisor_validator);
133  String[] replaced = new String[1];
134  int adjusted_cursor = cursor < 0 ? sql.length() : cursor;
135  java.util.List<SqlMoniker> hints =
136  advisor.getCompletionHints(sql, adjusted_cursor, replaced);
137  return new CompletionResult(hints, replaced[0]);
138  }
139 
140  @Override
141  public RelRoot rel(SqlNode sql) throws RelConversionException {
142  RelRoot root = super.rel(sql);
143  root = applyQueryOptimizationRules(root);
144  root = applyFilterPushdown(root);
145  return root;
146  }
147 
148  private RelRoot applyFilterPushdown(RelRoot root) {
149  if (filterPushDownInfo.isEmpty()) {
150  return root;
151  }
152  final DynamicFilterJoinRule dynamicFilterJoinRule = new DynamicFilterJoinRule(true,
153  RelFactories.LOGICAL_BUILDER,
154  FilterJoinRule.TRUE_PREDICATE,
156  final HepProgram program =
157  HepProgram.builder().addRuleInstance(dynamicFilterJoinRule).build();
158  HepPlanner prePlanner = new HepPlanner(program);
159  prePlanner.setRoot(root.rel);
160  final RelNode rootRelNode = prePlanner.findBestExp();
161  filterPushDownInfo.clear();
162  return root.withRel(rootRelNode);
163  }
164 
165  private RelRoot applyQueryOptimizationRules(RelRoot root) {
166  QueryOptimizationRules outerJoinOptRule =
167  new OuterJoinOptViaNullRejectionRule(RelFactories.LOGICAL_BUILDER);
168 
169  HepProgram opt_program =
170  HepProgram.builder().addRuleInstance(outerJoinOptRule).build();
171  HepPlanner prePlanner = new HepPlanner(
172  opt_program, null, true, Functions.ignore2(), RelOptCostImpl.FACTORY);
173  prePlanner.setRoot(root.rel);
174  final RelNode rootRelNode = prePlanner.findBestExp();
175  return root.withRel(rootRelNode);
176  }
177 
179  final List<MapDParserOptions.FilterPushDownInfo> filterPushDownInfo) {
180  this.filterPushDownInfo = filterPushDownInfo;
181  }
182 }
183 
184 // End MapDPlanner.java
RelRoot applyQueryOptimizationRules(RelRoot root)
static SchemaPlus rootSchema(SchemaPlus schema)
CalciteCatalogReader createCatalogReader()
List< MapDParserOptions.FilterPushDownInfo > filterPushDownInfo
CompletionResult(final List< SqlMoniker > hints, final String replaced)
void setFilterPushDownInfo(final List< MapDParserOptions.FilterPushDownInfo > filterPushDownInfo)
CompletionResult getCompletionHints(final String sql, final int cursor, final List< String > visibleTables)
MapDPlanner(FrameworkConfig config)
RelRoot applyFilterPushdown(RelRoot root)