17 package org.apache.calcite.prepare;
19 import com.google.common.collect.ImmutableSet;
25 import org.apache.calcite.config.CalciteConnectionConfig;
26 import org.apache.calcite.config.CalciteConnectionConfigImpl;
27 import org.apache.calcite.config.CalciteConnectionProperty;
28 import org.apache.calcite.jdbc.CalciteSchema;
29 import org.apache.calcite.linq4j.function.Functions;
30 import org.apache.calcite.plan.Context;
31 import org.apache.calcite.plan.RelOptCluster;
32 import org.apache.calcite.plan.RelOptCostImpl;
33 import org.apache.calcite.plan.RelOptRule;
34 import org.apache.calcite.plan.hep.HepPlanner;
35 import org.apache.calcite.plan.hep.HepProgram;
36 import org.apache.calcite.plan.hep.HepProgramBuilder;
37 import org.apache.calcite.plan.volcano.VolcanoPlanner;
38 import org.apache.calcite.rel.RelNode;
39 import org.apache.calcite.rel.RelRoot;
40 import org.apache.calcite.rel.core.RelFactories;
42 import org.apache.calcite.rel.metadata.DefaultRelMetadataProvider;
43 import org.apache.calcite.rel.rules.*;
44 import org.apache.calcite.rex.RexBuilder;
45 import org.apache.calcite.schema.SchemaPlus;
46 import org.apache.calcite.sql.SqlKind;
47 import org.apache.calcite.sql.SqlNode;
48 import org.apache.calcite.sql.advise.SqlAdvisor;
49 import org.apache.calcite.sql.parser.SqlParseException;
50 import org.apache.calcite.sql.validate.SqlConformanceEnum;
51 import org.apache.calcite.sql.validate.SqlMoniker;
52 import org.apache.calcite.sql.validate.SqlValidator;
53 import org.apache.calcite.tools.FrameworkConfig;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 import java.io.IOException;
58 import java.lang.reflect.InvocationTargetException;
59 import java.lang.reflect.Method;
60 import java.util.ArrayList;
61 import java.util.List;
62 import java.util.Properties;
74 final static Logger
HEAVYDBLOGGER = LoggerFactory.getLogger(HeavyDBPlanner.class);
81 private static SchemaPlus
rootSchema(SchemaPlus schema) {
83 if (schema.getParentSchema() == null) {
86 schema = schema.getParentSchema();
102 final Context context = config.getContext();
103 final CalciteConnectionConfig connectionConfig;
105 if (context != null) {
106 connectionConfig = context.unwrap(CalciteConnectionConfig.class);
108 Properties properties =
new Properties();
109 properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
110 String.valueOf(
config.getParserConfig().caseSensitive()));
111 connectionConfig =
new CalciteConnectionConfigImpl(properties);
114 return new CalciteCatalogReader(CalciteSchema.from(rootSchema),
115 CalciteSchema.from(config.getDefaultSchema()).path(null),
122 String dummySql =
"SELECT 1";
123 super.parse(dummySql);
124 }
catch (SqlParseException e) {
125 throw new RuntimeException(e);
133 Method readyMethod = getClass().getSuperclass().getDeclaredMethod(
"ready");
134 readyMethod.setAccessible(
true);
135 readyMethod.invoke(
this);
136 }
catch (InvocationTargetException e) {
137 if (e.getCause() instanceof RuntimeException) {
138 throw(RuntimeException) e.getCause();
140 throw new RuntimeException(e.getCause());
142 }
catch (Exception e) {
143 throw new RuntimeException(e);
148 final String sql,
final int cursor,
final List<String> visibleTables) {
151 SqlValidator.Config validatorConfig = SqlValidator.Config.DEFAULT;
152 validatorConfig = validatorConfig.withSqlConformance(SqlConformanceEnum.LENIENT);
156 config.getOperatorTable(),
162 String[] replaced =
new String[1];
163 int adjusted_cursor = cursor < 0 ? sql.length() : cursor;
164 java.util.List<SqlMoniker> hints =
165 advisor.getCompletionHints(sql, adjusted_cursor, replaced);
170 HepProgram hepProgram,
boolean doNotEliminateSharedNodesInQueryPlanDag) {
171 if (doNotEliminateSharedNodesInQueryPlanDag) {
172 return new HepPlanner(
173 hepProgram, null,
true, Functions.ignore2(), RelOptCostImpl.FACTORY);
175 return new HepPlanner(hepProgram);
180 public RelRoot
rel(SqlNode sql) {
181 return super.rel(sql);
185 return super.rel(sqlNode);
189 RelNode rootNode,
boolean viewOptimizationEnabled,
boolean foundView) {
190 HepProgramBuilder firstOptPhaseProgram = HepProgram.builder();
191 firstOptPhaseProgram.addRuleInstance(CoreRules.AGGREGATE_MERGE)
193 new OuterJoinOptViaNullRejectionRule(RelFactories.LOGICAL_BUILDER))
194 .addRuleInstance(CoreRules.AGGREGATE_UNION_TRANSPOSE);
195 if (!viewOptimizationEnabled) {
196 firstOptPhaseProgram.addRuleInstance(CoreRules.FILTER_PROJECT_TRANSPOSE)
199 .addRuleInstance(CoreRules.FILTER_PROJECT_TRANSPOSE);
202 firstOptPhaseProgram.addRuleInstance(
203 CoreRules.JOIN_PROJECT_BOTH_TRANSPOSE_INCLUDE_OUTER);
204 firstOptPhaseProgram.addRuleInstance(CoreRules.FILTER_MERGE);
206 firstOptPhaseProgram.addRuleInstance(CoreRules.FILTER_PROJECT_TRANSPOSE);
207 firstOptPhaseProgram.addRuleInstance(
208 FilterTableFunctionMultiInputTransposeRule.Config.DEFAULT.toRule());
209 firstOptPhaseProgram.addRuleInstance(CoreRules.FILTER_PROJECT_TRANSPOSE);
211 firstOptPhaseProgram.addRuleInstance(CoreRules.PROJECT_MERGE);
215 HepProgram firstOptPhase = firstOptPhaseProgram.build();
216 HepPlanner firstPlanner = HeavyDBPlanner.getHepPlanner(firstOptPhase,
true);
217 firstPlanner.setRoot(rootNode);
218 final RelNode firstOptimizedPlanRoot = firstPlanner.findBestExp();
220 boolean hasRLSFilter = null !=
restrictions && !restrictions.isEmpty();
221 boolean needsSecondOptPhase = hasRLSFilter || !filterPushDownInfo.isEmpty();
222 if (needsSecondOptPhase) {
223 HepProgramBuilder secondOptPhaseProgram = HepProgram.builder();
225 final InjectFilterRule injectFilterRule =
227 secondOptPhaseProgram.addRuleInstance(injectFilterRule);
230 final DynamicFilterJoinRule dynamicFilterJoinRule =
231 new DynamicFilterJoinRule(
true,
232 RelFactories.LOGICAL_BUILDER,
233 FilterJoinRule.TRUE_PREDICATE,
235 secondOptPhaseProgram.addRuleInstance(dynamicFilterJoinRule);
238 HepProgram secondOptPhase = secondOptPhaseProgram.build();
239 HepPlanner secondPlanner = HeavyDBPlanner.getHepPlanner(secondOptPhase,
true);
240 secondPlanner.setRoot(firstOptimizedPlanRoot);
241 final RelNode secondOptimizedPlanRoot = secondPlanner.findBestExp();
243 filterPushDownInfo.clear();
245 return secondOptimizedPlanRoot;
247 return firstOptimizedPlanRoot;
254 final InjectFilterRule injectFilterRule =
257 final HepProgram program =
258 HepProgram.builder().addRuleInstance(injectFilterRule).build();
259 HepPlanner prePlanner = HeavyDBPlanner.getHepPlanner(program,
false);
260 prePlanner.setRoot(root.rel);
261 final RelNode rootRelNode = prePlanner.findBestExp();
262 return root.withRel(rootRelNode);
269 final DynamicFilterJoinRule dynamicFilterJoinRule =
new DynamicFilterJoinRule(
true,
270 RelFactories.LOGICAL_BUILDER,
271 FilterJoinRule.TRUE_PREDICATE,
273 final HepProgram program =
274 HepProgram.builder().addRuleInstance(dynamicFilterJoinRule).build();
275 HepPlanner prePlanner = HeavyDBPlanner.getHepPlanner(program,
false);
276 prePlanner.setRoot(root.rel);
277 final RelNode rootRelNode = prePlanner.findBestExp();
278 filterPushDownInfo.clear();
279 return root.withRel(rootRelNode);
283 HepProgramBuilder programBuilder =
new HepProgramBuilder();
284 for (RelOptRule rule : rules) {
285 programBuilder.addRuleInstance(rule);
287 HepPlanner hepPlanner = HeavyDBPlanner.getHepPlanner(programBuilder.build(),
false);
288 hepPlanner.setRoot(root.rel);
289 return root.withRel(hepPlanner.findBestExp());
295 RexBuilder builder =
new RexBuilder(getTypeFactory());
296 RelOptCluster cluster = RelOptCluster.create(
new VolcanoPlanner(), builder);
301 RelRoot relR = RelRoot.of(reader.read(query), SqlKind.SELECT);
306 QueryOptimizationRules outerJoinOptRule =
307 new OuterJoinOptViaNullRejectionRule(RelFactories.LOGICAL_BUILDER);
309 relR, ImmutableSet.of(CoreRules.AGGREGATE_MERGE, outerJoinOptRule));
312 ImmutableSet.of(CoreRules.JOIN_PROJECT_BOTH_TRANSPOSE_INCLUDE_OUTER,
313 CoreRules.FILTER_REDUCE_EXPRESSIONS,
315 CoreRules.PROJECT_FILTER_TRANSPOSE));
319 CoreRules.FILTER_PROJECT_TRANSPOSE, CoreRules.PROJECT_REMOVE));
320 return RelRoot.of(relR.project(), relR.kind);
void setFilterPushDownInfo(final List< HeavyDBParserOptions.FilterPushDownInfo > filterPushDownInfo)
List< HeavyDBParserOptions.FilterPushDownInfo > filterPushDownInfo
RelRoot getRelRoot(SqlNode sqlNode)
RelRoot applyOptimizationsRules(RelRoot root, ImmutableSet< RelOptRule > rules)
RelRoot applyFilterPushdown(RelRoot root)
RelRoot applyInjectFilterRule(RelRoot root, List< Restriction > restrictions)
List< Restriction > restrictions
RelNode optimizeRATree(RelNode rootNode, boolean viewOptimizationEnabled, boolean foundView)
static final Logger HEAVYDBLOGGER
static SchemaPlus rootSchema(SchemaPlus schema)
CalciteCatalogReader createCatalogReader()
static HepPlanner getHepPlanner(HepProgram hepProgram, boolean doNotEliminateSharedNodesInQueryPlanDag)
void setRestrictions(List< Restriction > restrictions)
static final ProjectProjectRemoveRule INSTANCE
RelRoot buildRATreeAndPerformQueryOptimization(String query, HeavyDBSchema schema)
CompletionResult getCompletionHints(final String sql, final int cursor, final List< String > visibleTables)
HeavyDBPlanner(FrameworkConfig config)
CompletionResult(final List< SqlMoniker > hints, final String replaced)