OmniSciDB  04ee39c94c
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.plan.Context;
26 import org.apache.calcite.plan.hep.HepPlanner;
27 import org.apache.calcite.plan.hep.HepProgram;
28 import org.apache.calcite.rel.RelNode;
29 import org.apache.calcite.rel.RelRoot;
30 import org.apache.calcite.rel.core.RelFactories;
32 import org.apache.calcite.rel.rules.FilterJoinRule;
33 import org.apache.calcite.schema.SchemaPlus;
34 import org.apache.calcite.sql.SqlNode;
35 import org.apache.calcite.sql.advise.SqlAdvisor;
36 import org.apache.calcite.sql.validate.SqlConformanceEnum;
37 import org.apache.calcite.sql.validate.SqlMoniker;
38 import org.apache.calcite.tools.FrameworkConfig;
39 import org.apache.calcite.tools.RelConversionException;
40 
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Properties;
46 
53 public class MapDPlanner extends PlannerImpl {
54  FrameworkConfig config;
56  new ArrayList<>();
57 
58  public MapDPlanner(FrameworkConfig config) {
59  super(config);
60  this.config = config;
61  }
62 
63  private static SchemaPlus rootSchema(SchemaPlus schema) {
64  for (;;) {
65  if (schema.getParentSchema() == null) {
66  return schema;
67  }
68  schema = schema.getParentSchema();
69  }
70  }
71 
72  public static class CompletionResult {
73  public List<SqlMoniker> hints;
74  public String replaced;
75 
76  CompletionResult(final List<SqlMoniker> hints, final String replaced) {
77  this.hints = hints;
78  this.replaced = replaced;
79  }
80  }
81 
82  private CalciteCatalogReader createCatalogReader() {
83  final SchemaPlus rootSchema = rootSchema(config.getDefaultSchema());
84  final Context context = config.getContext();
85  final CalciteConnectionConfig connectionConfig;
86 
87  if (context != null) {
88  connectionConfig = context.unwrap(CalciteConnectionConfig.class);
89  } else {
90  Properties properties = new Properties();
91  properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
92  String.valueOf(config.getParserConfig().caseSensitive()));
93  connectionConfig = new CalciteConnectionConfigImpl(properties);
94  }
95 
96  return new CalciteCatalogReader(CalciteSchema.from(rootSchema),
97  CalciteSchema.from(config.getDefaultSchema()).path(null),
98  getTypeFactory(),
99  connectionConfig);
100  }
101 
102  public void ready() {
103  // need to call ready on the super class, but that method is marked private
104  // circumventing via reflection for now
105  try {
106  Method readyMethod = getClass().getSuperclass().getDeclaredMethod("ready");
107  readyMethod.setAccessible(true);
108  readyMethod.invoke(this);
109  } catch (InvocationTargetException e) {
110  if (e.getCause() instanceof RuntimeException) {
111  throw(RuntimeException) e.getCause();
112  } else {
113  throw new RuntimeException(e.getCause());
114  }
115  } catch (Exception e) {
116  throw new RuntimeException(e);
117  }
118  }
119 
121  final String sql, final int cursor, final List<String> visibleTables) {
122  ready();
123  MapDSqlAdvisorValidator advisor_validator = new MapDSqlAdvisorValidator(visibleTables,
124  config.getOperatorTable(),
126  getTypeFactory(),
127  SqlConformanceEnum.LENIENT);
128  SqlAdvisor advisor = new MapDSqlAdvisor(advisor_validator);
129  String[] replaced = new String[1];
130  int adjusted_cursor = cursor < 0 ? sql.length() : cursor;
131  java.util.List<SqlMoniker> hints =
132  advisor.getCompletionHints(sql, adjusted_cursor, replaced);
133  return new CompletionResult(hints, replaced[0]);
134  }
135 
136  @Override
137  public RelRoot rel(SqlNode sql) throws RelConversionException {
138  RelRoot root = super.rel(sql);
139  root = applyFilterPushdown(root);
140  return root;
141  }
142 
143  private RelRoot applyFilterPushdown(RelRoot root) {
144  if (filterPushDownInfo.isEmpty()) {
145  return root;
146  }
147  final DynamicFilterJoinRule dynamicFilterJoinRule = new DynamicFilterJoinRule(true,
148  RelFactories.LOGICAL_BUILDER,
149  FilterJoinRule.TRUE_PREDICATE,
151  final HepProgram program =
152  HepProgram.builder().addRuleInstance(dynamicFilterJoinRule).build();
153  HepPlanner prePlanner = new HepPlanner(program);
154  prePlanner.setRoot(root.rel);
155  final RelNode rootRelNode = prePlanner.findBestExp();
156  filterPushDownInfo.clear();
157  return root.withRel(rootRelNode);
158  }
159 
161  final List<MapDParserOptions.FilterPushDownInfo> filterPushDownInfo) {
162  this.filterPushDownInfo = filterPushDownInfo;
163  }
164 }
165 
166 // End MapDPlanner.java
auto sql(const std::string &sql_stmts)
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)