OmniSciDB  8fa3bf436f
 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 
19 import com.google.common.collect.ImmutableSet;
23 
24 import org.apache.calcite.config.CalciteConnectionConfig;
25 import org.apache.calcite.config.CalciteConnectionConfigImpl;
26 import org.apache.calcite.config.CalciteConnectionProperty;
27 import org.apache.calcite.jdbc.CalciteSchema;
28 import org.apache.calcite.linq4j.function.Functions;
29 import org.apache.calcite.plan.Context;
30 import org.apache.calcite.plan.RelOptCluster;
31 import org.apache.calcite.plan.RelOptCostImpl;
32 import org.apache.calcite.plan.RelOptRule;
33 import org.apache.calcite.plan.hep.HepPlanner;
34 import org.apache.calcite.plan.hep.HepProgram;
35 import org.apache.calcite.plan.hep.HepProgramBuilder;
36 import org.apache.calcite.plan.volcano.VolcanoPlanner;
37 import org.apache.calcite.rel.RelNode;
38 import org.apache.calcite.rel.RelRoot;
39 import org.apache.calcite.rel.core.RelFactories;
41 import org.apache.calcite.rel.metadata.DefaultRelMetadataProvider;
42 import org.apache.calcite.rel.rules.CoreRules;
44 import org.apache.calcite.rel.rules.FilterJoinRule;
49 import org.apache.calcite.rex.RexBuilder;
50 import org.apache.calcite.schema.SchemaPlus;
51 import org.apache.calcite.sql.SqlKind;
52 import org.apache.calcite.sql.SqlNode;
53 import org.apache.calcite.sql.advise.SqlAdvisor;
54 import org.apache.calcite.sql.parser.SqlParseException;
55 import org.apache.calcite.sql.validate.SqlConformanceEnum;
56 import org.apache.calcite.sql.validate.SqlMoniker;
57 import org.apache.calcite.sql.validate.SqlValidator;
58 import org.apache.calcite.tools.FrameworkConfig;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61 
62 import java.io.IOException;
63 import java.lang.reflect.InvocationTargetException;
64 import java.lang.reflect.Method;
65 import java.util.ArrayList;
66 import java.util.List;
67 import java.util.Properties;
68 
74 public class MapDPlanner extends PlannerImpl {
75  FrameworkConfig config;
76  private List<MapDParserOptions.FilterPushDownInfo> filterPushDownInfo =
77  new ArrayList<>();
78  private Restriction restriction = null;
79  final static Logger MAPDLOGGER = LoggerFactory.getLogger(MapDPlanner.class);
80 
81  public MapDPlanner(FrameworkConfig config) {
82  super(config);
83  this.config = config;
84  }
85 
86  private static SchemaPlus rootSchema(SchemaPlus schema) {
87  for (;;) {
88  if (schema.getParentSchema() == null) {
89  return schema;
90  }
91  schema = schema.getParentSchema();
92  }
93  }
94 
95  public static class CompletionResult {
96  public List<SqlMoniker> hints;
97  public String replaced;
98 
99  CompletionResult(final List<SqlMoniker> hints, final String replaced) {
100  this.hints = hints;
101  this.replaced = replaced;
102  }
103  }
104 
105  private CalciteCatalogReader createCatalogReader() {
106  final SchemaPlus rootSchema = rootSchema(config.getDefaultSchema());
107  final Context context = config.getContext();
108  final CalciteConnectionConfig connectionConfig;
109 
110  if (context != null) {
111  connectionConfig = context.unwrap(CalciteConnectionConfig.class);
112  } else {
113  Properties properties = new Properties();
114  properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
115  String.valueOf(config.getParserConfig().caseSensitive()));
116  connectionConfig = new CalciteConnectionConfigImpl(properties);
117  }
118 
119  return new CalciteCatalogReader(CalciteSchema.from(rootSchema),
120  CalciteSchema.from(config.getDefaultSchema()).path(null),
121  getTypeFactory(),
122  connectionConfig);
123  }
124 
125  public void advanceToValidate() {
126  try {
127  String dummySql = "SELECT 1";
128  super.parse(dummySql);
129  } catch (SqlParseException e) {
130  throw new RuntimeException(e);
131  }
132  }
133 
134  public void ready() {
135  // need to call ready on the super class, but that method is marked private
136  // circumventing via reflection for now
137  try {
138  Method readyMethod = getClass().getSuperclass().getDeclaredMethod("ready");
139  readyMethod.setAccessible(true);
140  readyMethod.invoke(this);
141  } catch (InvocationTargetException e) {
142  if (e.getCause() instanceof RuntimeException) {
143  throw(RuntimeException) e.getCause();
144  } else {
145  throw new RuntimeException(e.getCause());
146  }
147  } catch (Exception e) {
148  throw new RuntimeException(e);
149  }
150  }
151 
153  final String sql, final int cursor, final List<String> visibleTables) {
154  ready();
155 
156  SqlValidator.Config validatorConfig = SqlValidator.Config.DEFAULT;
157  validatorConfig = validatorConfig.withSqlConformance(SqlConformanceEnum.LENIENT);
158 
159  MapDSqlAdvisorValidator advisor_validator = new MapDSqlAdvisorValidator(visibleTables,
160  config.getOperatorTable(),
162  getTypeFactory(),
163  validatorConfig);
164  SqlAdvisor advisor = new MapDSqlAdvisor(advisor_validator, config.getParserConfig());
165  String[] replaced = new String[1];
166  int adjusted_cursor = cursor < 0 ? sql.length() : cursor;
167  java.util.List<SqlMoniker> hints =
168  advisor.getCompletionHints(sql, adjusted_cursor, replaced);
169  return new CompletionResult(hints, replaced[0]);
170  }
171 
172  public static HepPlanner getHepPlanner(HepProgram hepProgram, boolean noDag) {
173  if (noDag) {
174  return new HepPlanner(
175  hepProgram, null, true, Functions.ignore2(), RelOptCostImpl.FACTORY);
176  } else {
177  return new HepPlanner(hepProgram);
178  }
179  }
180 
181  @Override
182  public RelRoot rel(SqlNode sql) {
183  RelRoot root = super.rel(sql);
184  if (restriction != null) {
185  root = applyInjectFilterRule(root, restriction);
186  }
187  root = applyQueryOptimizationRules(root);
188  root = applyFilterPushdown(root);
189  return root;
190  }
191 
192  private RelRoot applyInjectFilterRule(RelRoot root, Restriction restriction) {
193  // TODO consider doing these rules in one preplan pass
194 
195  final InjectFilterRule injectFilterRule =
196  InjectFilterRule.Config.DEFAULT.toRule(restriction);
197 
198  final HepProgram program =
199  HepProgram.builder().addRuleInstance(injectFilterRule).build();
200  HepPlanner prePlanner = MapDPlanner.getHepPlanner(program, false);
201  prePlanner.setRoot(root.rel);
202  final RelNode rootRelNode = prePlanner.findBestExp();
203  return root.withRel(rootRelNode);
204  }
205 
206  private RelRoot applyFilterPushdown(RelRoot root) {
207  if (filterPushDownInfo.isEmpty()) {
208  return root;
209  }
210  final DynamicFilterJoinRule dynamicFilterJoinRule = new DynamicFilterJoinRule(true,
211  RelFactories.LOGICAL_BUILDER,
212  FilterJoinRule.TRUE_PREDICATE,
214  final HepProgram program =
215  HepProgram.builder().addRuleInstance(dynamicFilterJoinRule).build();
216  HepPlanner prePlanner = MapDPlanner.getHepPlanner(program, false);
217  prePlanner.setRoot(root.rel);
218  final RelNode rootRelNode = prePlanner.findBestExp();
219  filterPushDownInfo.clear();
220  return root.withRel(rootRelNode);
221  }
222 
223  private RelRoot applyQueryOptimizationRules(RelRoot root) {
224  QueryOptimizationRules outerJoinOptRule =
225  new OuterJoinOptViaNullRejectionRule(RelFactories.LOGICAL_BUILDER);
226 
227  HepProgram program = HepProgram.builder().addRuleInstance(outerJoinOptRule).build();
228  HepPlanner prePlanner = MapDPlanner.getHepPlanner(program, true);
229  prePlanner.setRoot(root.rel);
230  final RelNode rootRelNode = prePlanner.findBestExp();
231  return root.withRel(rootRelNode);
232  }
233 
234  private RelRoot applyOptimizationsRules(RelRoot root, ImmutableSet<RelOptRule> rules) {
235  HepProgramBuilder programBuilder = new HepProgramBuilder();
236  for (RelOptRule rule : rules) {
237  programBuilder.addRuleInstance(rule);
238  }
239  HepPlanner hepPlanner = MapDPlanner.getHepPlanner(programBuilder.build(), false);
240  hepPlanner.setRoot(root.rel);
241  return root.withRel(hepPlanner.findBestExp());
242  }
243 
244  public RelRoot optimizeRaQuery(String query, MapDSchema schema) throws IOException {
245  ready();
246  RexBuilder builder = new RexBuilder(getTypeFactory());
247  RelOptCluster cluster = RelOptCluster.create(new VolcanoPlanner(), builder);
248  CalciteCatalogReader catalogReader = createCatalogReader();
249  MapDRelJsonReader reader = new MapDRelJsonReader(cluster, catalogReader, schema);
250 
251  RelRoot relR = RelRoot.of(reader.read(query), SqlKind.SELECT);
252 
253  if (restriction != null) {
254  relR = applyInjectFilterRule(relR, restriction);
255  }
256 
257  relR = applyQueryOptimizationRules(relR);
258  relR = applyFilterPushdown(relR);
259  relR = applyOptimizationsRules(relR,
260  ImmutableSet.of(CoreRules.JOIN_PROJECT_BOTH_TRANSPOSE_INCLUDE_OUTER,
261  CoreRules.FILTER_REDUCE_EXPRESSIONS,
263  CoreRules.PROJECT_FILTER_TRANSPOSE));
264  relR = applyOptimizationsRules(relR, ImmutableSet.of(CoreRules.PROJECT_MERGE));
265  relR = applyOptimizationsRules(relR,
266  ImmutableSet.of(
267  CoreRules.FILTER_PROJECT_TRANSPOSE, CoreRules.PROJECT_REMOVE));
268  return RelRoot.of(relR.project(), relR.kind);
269  }
270 
272  final List<MapDParserOptions.FilterPushDownInfo> filterPushDownInfo) {
273  this.filterPushDownInfo = filterPushDownInfo;
274  }
275 
277  this.restriction = restriction;
278  }
279 }
280 
281 // End MapDPlanner.java
RelRoot applyQueryOptimizationRules(RelRoot root)
RelRoot applyOptimizationsRules(RelRoot root, ImmutableSet< RelOptRule > rules)
tuple root
Definition: setup.in.py:14
static SchemaPlus rootSchema(SchemaPlus schema)
CalciteCatalogReader createCatalogReader()
RelRoot optimizeRaQuery(String query, MapDSchema schema)
static HepPlanner getHepPlanner(HepProgram hepProgram, boolean noDag)
void setRestriction(Restriction restriction)
List< MapDParserOptions.FilterPushDownInfo > filterPushDownInfo
CompletionResult(final List< SqlMoniker > hints, final String replaced)
void setFilterPushDownInfo(final List< MapDParserOptions.FilterPushDownInfo > filterPushDownInfo)
#define DEFAULT
CompletionResult getCompletionHints(final String sql, final int cursor, final List< String > visibleTables)
RelRoot applyInjectFilterRule(RelRoot root, Restriction restriction)
MapDPlanner(FrameworkConfig config)
RelRoot applyFilterPushdown(RelRoot root)