com.qubole.quark.planner.RuleSets.java Source code

Java tutorial

Introduction

Here is the source code for com.qubole.quark.planner.RuleSets.java

Source

/*
 * Copyright (c) 2015. Qubole Inc
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.qubole.quark.planner;

import org.apache.calcite.adapter.enumerable.EnumerableRules;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.rel.rules.AggregateExpandDistinctAggregatesRule;
import org.apache.calcite.rel.rules.AggregateReduceFunctionsRule;
import org.apache.calcite.rel.rules.FilterAggregateTransposeRule;
import org.apache.calcite.rel.rules.FilterJoinRule;
import org.apache.calcite.rel.rules.FilterProjectTransposeRule;
import org.apache.calcite.rel.rules.FilterTableScanRule;
import org.apache.calcite.rel.rules.JoinAssociateRule;
import org.apache.calcite.rel.rules.JoinCommuteRule;
import org.apache.calcite.rel.rules.JoinPushThroughJoinRule;
import org.apache.calcite.rel.rules.ProjectFilterTransposeRule;
import org.apache.calcite.rel.rules.ProjectMergeRule;
import org.apache.calcite.rel.rules.SortProjectTransposeRule;
import org.apache.calcite.rel.rules.TableScanRule;
import org.apache.calcite.tools.RuleSet;
import org.apache.calcite.util.Util;

import com.google.common.collect.ImmutableList;

import java.util.Iterator;
import java.util.List;

/**
 * Created by amognm on 11/18/15.
 *
 * Utility function for Planners RuleSet
 */
public class RuleSets {
    private RuleSets() {
    }

    public static final boolean COMMUTE = Util.getBooleanProperty("calcite.enable.join.commute");

    private static final ImmutableList<RelOptRule> DEFAULT_RULES = ImmutableList.of(AggStarRule.INSTANCE,
            AggStarRule.INSTANCE2, FilterAggStarRule.INSTANCE, FilterAggStarRule.INSTANCE2, TableScanRule.INSTANCE,
            COMMUTE ? JoinAssociateRule.INSTANCE : ProjectMergeRule.INSTANCE, FilterTableScanRule.INSTANCE,
            ProjectFilterTransposeRule.INSTANCE, FilterProjectTransposeRule.INSTANCE, FilterJoinRule.FILTER_ON_JOIN,
            AggregateExpandDistinctAggregatesRule.INSTANCE, AggregateReduceFunctionsRule.INSTANCE,
            FilterAggregateTransposeRule.INSTANCE, JoinCommuteRule.INSTANCE, JoinPushThroughJoinRule.RIGHT,
            JoinPushThroughJoinRule.LEFT, SortProjectTransposeRule.INSTANCE);

    private static final List<RelOptRule> ENUMERABLE_RULES = ImmutableList.of(EnumerableRules.ENUMERABLE_JOIN_RULE,
            EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE, EnumerableRules.ENUMERABLE_SEMI_JOIN_RULE,
            EnumerableRules.ENUMERABLE_CORRELATE_RULE, EnumerableRules.ENUMERABLE_PROJECT_RULE,
            EnumerableRules.ENUMERABLE_FILTER_RULE, EnumerableRules.ENUMERABLE_AGGREGATE_RULE,
            EnumerableRules.ENUMERABLE_SORT_RULE, EnumerableRules.ENUMERABLE_LIMIT_RULE,
            EnumerableRules.ENUMERABLE_COLLECT_RULE, EnumerableRules.ENUMERABLE_UNCOLLECT_RULE,
            EnumerableRules.ENUMERABLE_UNION_RULE, EnumerableRules.ENUMERABLE_INTERSECT_RULE,
            EnumerableRules.ENUMERABLE_MINUS_RULE, EnumerableRules.ENUMERABLE_TABLE_MODIFICATION_RULE,
            EnumerableRules.ENUMERABLE_VALUES_RULE, EnumerableRules.ENUMERABLE_WINDOW_RULE,
            EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE, EnumerableRules.ENUMERABLE_TABLE_FUNCTION_SCAN_RULE);

    /**
     * Gets the default set of rules for planning process
     */
    public static RuleSet getDefaultRuleSet() {
        ImmutableList.Builder<RelOptRule> builder = ImmutableList.builder();
        builder.addAll(DEFAULT_RULES).addAll(ENUMERABLE_RULES);
        return new QuarkRuleSet(builder.build());
    }

    /**
     * Quark's Rule Set which will be used for planning process
     */
    private static class QuarkRuleSet implements RuleSet {
        final ImmutableList<RelOptRule> rules;

        QuarkRuleSet(ImmutableList<RelOptRule> rules) {
            this.rules = rules;
        }

        @Override
        public Iterator<RelOptRule> iterator() {
            return rules.iterator();
        }
    }
}