Commit 1682697f authored by Stanley Clark's avatar Stanley Clark
Browse files

Multiple planner executions = happiness.

parent ad47a565
......@@ -55,7 +55,7 @@ public class TestSuite {
* @param relOptRules The rules to use
* @return A new middleware instance
*/
public static Middleware getMiddleware(Environment environment, List<RelOptRule> relOptRules) {
public static Middleware getMiddleware(Environment environment, List<List<RelOptRule>> relOptRules) {
Configuration configuration = new Configuration(relOptRules);
configuration.setEnvironment(environment);
Policy policy = Policy.create(configuration);
......
......@@ -18,7 +18,7 @@ public class ExhaustiveEnumeration implements IQueryRun {
private int permutationNum;
ExhaustiveEnumeration() {
mw = TestSuite.getMiddleware(new Environment(1), Rules.secRaRewriteRules);
mw = TestSuite.getMiddleware(new Environment(1), List.of(Rules.secRaPass1, Rules.secRaPass2));
}
/**
......
......@@ -11,7 +11,7 @@ import utils.EnvironmentVariables;
import utils.InfoLogger;
public class Pipeline implements IQueryRun {
private List<RelOptRule> rules;
private List<List<RelOptRule>> rules;
private String sql;
private int caseNum;
private int userId;
......@@ -45,8 +45,8 @@ public class Pipeline implements IQueryRun {
InfoLogger.addLevel();
this.userId = userId;
verifyQuery(testSuite);
runVariant(testSuite, Rules.defaultRewriteRules, 1);
runVariant(testSuite, Rules.secRaRewriteRules, 2);
runVariant(testSuite, List.of(Rules.defaultRewriteRules), 1);
runVariant(testSuite, List.of(Rules.secRaPass1, Rules.secRaPass2), 2);
InfoLogger.removeLevel();
}
}
......@@ -72,7 +72,7 @@ public class Pipeline implements IQueryRun {
* @param rules List of rules to use
* @param caseNum The case number for logging
*/
public void runVariant(TestSuite testSuite, List<RelOptRule> rules, int caseNum) {
public void runVariant(TestSuite testSuite, List<List<RelOptRule>> rules, int caseNum) {
InfoLogger.info("Case " + caseNum);
InfoLogger.addLevel();
this.caseNum = caseNum;
......@@ -85,7 +85,7 @@ public class Pipeline implements IQueryRun {
InfoLogger.info("Determining actual number results for query...");
testSuite.newRun(0);
Environment env = new Environment(userId);
Middleware mw = TestSuite.getMiddleware(env, Rules.enumerableConversionRules).reset(sql);
Middleware mw = TestSuite.getMiddleware(env, List.of(Rules.enumerableConversionRules)).reset(sql);
String sql = mw.buildRaTree().makeApriori().plan().genSqlString();
testSuite.timeDb("EXPLAIN ANALYZE " + sql);
testSuite.recordResultsForRun();
......
......@@ -2,12 +2,14 @@ package app.pipeline;
import app.IQueryRun;
import app.TestSuite;
import java.util.List;
import saqp.Environment;
import saqp.Middleware;
import saqp.Rules;
import utils.EnvironmentVariables;
public class Planning implements IQueryRun {
private String sql ="";
/**
* Run planning tests.
......@@ -25,10 +27,16 @@ public class Planning implements IQueryRun {
* @param testSuite TestSuite class instance
*/
public void testQuery(String sql, TestSuite testSuite) {
this.sql = sql;
testSuite.runN();
}
public void runN(TestSuite testSuite) {
Middleware mw;
int userId = EnvironmentVariables.getInstance().saqpUserIds[0];
mw = TestSuite.getMiddleware(new Environment(userId), Rules.defaultRewriteRules).reset(sql);
mw = TestSuite.getMiddleware(new Environment(userId), List.of(Rules.defaultRewriteRules)).reset(sql);
testSuite.newRun(0);
testSuite.time("ra", mw::buildRaTree);
testSuite.time("apriori", mw::makeApriori);
......@@ -36,7 +44,7 @@ public class Planning implements IQueryRun {
testSuite.recordResultsForRun();
mw.printRaTree();
mw = TestSuite.getMiddleware(new Environment(userId), Rules.secRaRewriteRules).reset(sql);
mw = TestSuite.getMiddleware(new Environment(userId), List.of(Rules.secRaPass1, Rules.secRaPass2)).reset(sql);
testSuite.newRun(1);
testSuite.time("ra", mw::buildRaTree);
testSuite.time("apriori", mw::makeApriori);
......@@ -45,10 +53,6 @@ public class Planning implements IQueryRun {
mw.printRaTree();
}
public void runN(TestSuite testSuite) {
}
public void addRunNProps(TestSuite testSuite) {
}
......
package saqp;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.calcite.config.Lex;
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.plan.RelOptRule;
......@@ -27,6 +29,7 @@ public class Configuration {
private final FrameworkConfig frameworkConfig;
private final Planner planner;
private final SqlValidator sqlValidator;
private final List<Integer> ruleSetIds;
private Environment environment;
......@@ -35,16 +38,17 @@ public class Configuration {
*
* @param rewriteRules The list of rewrite rules to use
*/
public Configuration(List<RelOptRule> rewriteRules) {
public Configuration(List<List<RelOptRule>> rewriteRules) {
tpcSchema = Schema.getSchema();
frameworkConfig = Frameworks.newConfigBuilder()
.defaultSchema(tpcSchema)
.parserConfig(SqlParser.configBuilder().setLex(Lex.MYSQL_ANSI).build())
.ruleSets(RuleSets.ofList(rewriteRules))
.ruleSets(rewriteRules.stream().map(RuleSets::ofList).collect(Collectors.toList()))
.build();
planner = Frameworks.getPlanner(frameworkConfig);
sqlValidator = setValidator();
environment = null;
ruleSetIds = IntStream.range(0, rewriteRules.size()).boxed().collect(Collectors.toList());
}
public SchemaPlus getTpcSchema() {
......@@ -88,6 +92,10 @@ public class Configuration {
return Frameworks.getPlanner(frameworkConfig);
}
public List<Integer> getRuleSetIds() {
return ruleSetIds;
}
public Environment getEnvironment() {
return environment;
}
......
......@@ -23,7 +23,6 @@ import saqp.policy.Policy;
import saqp.visitors.APrioriRelVisitor;
import saqp.visitors.PlanExpander;
import saqp.visitors.SecRelToSqlConverter;
import utils.EnvironmentVariables;
import utils.InfoLogger;
import utils.ProcessExecutor;
......@@ -106,7 +105,9 @@ public class Middleware {
InfoLogger.info("Planning...");
RelTraitSet outputTraits = raParseTree.getTraitSet().replace(EnumerableConvention.INSTANCE);
try {
raParseTree = configuration.getPlanner().transform(0, outputTraits, raParseTree);
for(int ruleSetId : configuration.getRuleSetIds()) {
raParseTree = configuration.getPlanner().transform(ruleSetId, outputTraits, raParseTree);
}
} catch (RelConversionException e) {
e.printStackTrace();
}
......
......@@ -11,14 +11,14 @@ import org.apache.calcite.rel.rules.JoinPushThroughJoinRule;
import org.apache.calcite.rel.rules.ProjectFilterTransposeRule;
import org.apache.calcite.rel.rules.ProjectJoinTransposeRule;
import org.apache.calcite.rel.rules.ProjectMergeRule;
import saqp.secrules.EQ10RowProjectTransposeRule;
import saqp.secrules.EQ11RowCommutativeRule;
import saqp.secrules.EQ12RemoveCellRule;
import saqp.secrules.EQ1CellRowTransposeRule;
import saqp.secrules.EQ2CellCommutativeRule;
import saqp.secrules.EQ5CellProjectTransposeRule;
import saqp.secrules.EQ5ProjectCellTransposeRule;
import saqp.secrules.EQ6FilterCellTransposeRule;
import saqp.secrules.EQ7JoinCellRule;
import saqp.secrules.EQ7JoinCellRuleRight;
import saqp.secrules.EQ8FilterRowTransposeRule;
import saqp.secrules.EQ8RowFilterTransposeRule;
......@@ -62,7 +62,7 @@ public class Rules {
JoinPushThroughJoinRule.RIGHT, // Swap a relation in right join tree with left
JoinPushThroughJoinRule.LEFT, // Swap a relation in left join tree with right
ProjectMergeRule.INSTANCE, // Merge projects into one
ProjectJoinTransposeRule.INSTANCE
ProjectJoinTransposeRule.INSTANCE // Make columns joins operate over smaller
)
);
......@@ -73,37 +73,27 @@ public class Rules {
* to activating extra default planning rules.
* TODO: numbers should correspond to paper
*/
public static final List<RelOptRule> secRaRewriteRules = combine(
public static final List<RelOptRule> secRaPass1 = combine(
defaultRewriteRules,
List.of(
EQ1CellRowTransposeRule.INSTANCE, // Push cell operator below row
// EQ1RowCellTransposeRule.INSTANCE,
EQ2CellCommutativeRule.INSTANCE, // Swap cell operators
// EQ3CellMergeRule.INSTANCE, // Merge cell operators
// EQ3CellSplitRule.INSTANCE,
// EQ4JoinRowRule.INSTANCE,
// EQ4RowJoinRule.INSTANCE,
// EQ5CellProjectTransposeRule.INSTANCE,
EQ5CellProjectTransposeRule.INSTANCE,
EQ5ProjectCellTransposeRule.INSTANCE, // Project first before doing cell filtering
// EQ6CellFilterTransposeRule.INSTANCE,
EQ6FilterCellTransposeRule.INSTANCE, // Filter first before doing cell
// EQ7CellJoinRule.INSTANCE,
// EQ7JoinCellRule.INSTANCE, // Apply cell security after joins
EQ8RowFilterTransposeRule.INSTANCE, // Swap row and filters among themselves
EQ8FilterRowTransposeRule.INSTANCE, // Swap row and filters among themselves
// EQ9RowMergeRule.INSTANCE, // Merge row operators when possible
// EQ9RowSplitRule.INSTANCE,
EQ10RowProjectTransposeRule.INSTANCE, // Pull up projections
// EQ10ProjectRowTransposeRule.INSTANCE,
EQ11RowCommutativeRule.INSTANCE, // Swap rows amongst themselves
EQ12RemoveCellRule.INSTANCE // Remove cell security if possible
)
);
public static final List<RelOptRule> secRaRewriteRulesPlus = combine(
secRaRewriteRules,
public static final List<RelOptRule> secRaPass2 = combine(
enumerableConversionRules,
List.of(
EQ7JoinCellRule.INSTANCE
EQ6FilterCellTransposeRule.INSTANCE,
EQ7JoinCellRule.INSTANCE,
EQ7JoinCellRuleRight.INSTANCE,
EQ12RemoveCellRule.INSTANCE
)
);
......
......@@ -3,9 +3,9 @@ package saqp.secrules;
import java.util.List;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.core.Project;
import saqp.secform.CellSecForms;
import saqp.secra.LogicalCellRel;
import saqp.secra.CellRel;
/**
* If the target attribute of a cell rel is projected away, remove the cell rel.
......@@ -14,24 +14,21 @@ public class EQ12RemoveCellRule extends SaqpRule {
public static EQ12RemoveCellRule INSTANCE = new EQ12RemoveCellRule();
EQ12RemoveCellRule() {
super(LogicalProject.class, LogicalCellRel.class);
super(Project.class, CellRel.class);
}
@Override
void perform(RelOptRuleCall call) {
LogicalProject project = call.rel(0);
LogicalCellRel cell = call.rel(1);
Project project = call.rel(0);
CellRel cell = call.rel(1);
RelNode stripped = cell.getInput();
// This is the line - ensuring these are classed as equivalent
RelNode child = call.getPlanner().register(stripped, cell);
call.transformTo(project.copy(project.getTraitSet(), List.of(child)));
call.transformTo(project.copy(project.getTraitSet(), List.of(stripped)));
}
@Override
boolean checkCondition(RelOptRuleCall call) {
LogicalProject project = call.rel(0);
LogicalCellRel cell = call.rel(1);
Project project = call.rel(0);
CellRel cell = call.rel(1);
String cellTargetAttribute = ((CellSecForms) cell.getSecForm()).getTargetAttribute();
return !project.getRowType().getFieldNames().contains(cellTargetAttribute);
......
......@@ -3,15 +3,13 @@ package saqp.secrules;
import org.apache.calcite.plan.RelOptRuleCall;
import saqp.secform.CellSecForms;
import saqp.secra.CellRel;
import saqp.secra.LogicalCellRel;
import saqp.secra.LogicalRowRel;
import saqp.secra.RowRel;
public class EQ1CellRowTransposeRule extends SwapRule {
public static EQ1CellRowTransposeRule INSTANCE = new EQ1CellRowTransposeRule();
public EQ1CellRowTransposeRule() {
super(LogicalCellRel.class, LogicalRowRel.class);
super(CellRel.class, RowRel.class);
}
@Override
......
......@@ -3,13 +3,12 @@ package saqp.secrules;
import org.apache.calcite.plan.RelOptRuleCall;
import saqp.secform.CellSecForms;
import saqp.secra.CellRel;
import saqp.secra.LogicalCellRel;
public class EQ2CellCommutativeRule extends SwapRule {
public static EQ2CellCommutativeRule INSTANCE = new EQ2CellCommutativeRule();
public EQ2CellCommutativeRule() {
super(LogicalCellRel.class, LogicalCellRel.class);
super(CellRel.class, CellRel.class);
}
@Override
......
......@@ -2,18 +2,16 @@ package saqp.secrules;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.logical.LogicalFilter;
import org.apache.calcite.rex.RexNode;
import saqp.secform.CellSecForms;
import saqp.secra.CellRel;
import saqp.secra.LogicalCellRel;
import saqp.visitors.FilterConditionVisitor;
public class EQ6FilterCellTransposeRule extends SwapRule {
public static EQ6FilterCellTransposeRule INSTANCE = new EQ6FilterCellTransposeRule();
public EQ6FilterCellTransposeRule() {
super(LogicalFilter.class, LogicalCellRel.class);
super(Filter.class, CellRel.class);
}
@Override
......
package saqp.secrules;
import saqp.secra.LogicalCellRel;
import saqp.secra.CellRel;
public class EQ7JoinCellRule extends PushUpThroughJoinRule {
public static EQ7JoinCellRule INSTANCE = new EQ7JoinCellRule();
public EQ7JoinCellRule() {
super(LogicalCellRel.class);
super(CellRel.class);
}
}
package saqp.secrules;
import saqp.secra.CellRel;
public class EQ7JoinCellRuleRight extends PushUpThroughJoinRuleRight {
public static EQ7JoinCellRuleRight INSTANCE = new EQ7JoinCellRuleRight();
public EQ7JoinCellRuleRight() {
super(CellRel.class);
}
}
package saqp.secrules;
import org.apache.calcite.rel.logical.LogicalFilter;
import saqp.secra.LogicalRowRel;
import org.apache.calcite.rel.core.Filter;
import saqp.secra.RowRel;
public class EQ8FilterRowTransposeRule extends SwapRule {
public static EQ8FilterRowTransposeRule INSTANCE = new EQ8FilterRowTransposeRule();
public EQ8FilterRowTransposeRule() {
super(LogicalFilter.class, LogicalRowRel.class);
super(Filter.class, RowRel.class);
}
}
package saqp.secrules;
import org.apache.calcite.rel.logical.LogicalFilter;
import saqp.secra.LogicalRowRel;
import org.apache.calcite.rel.core.Filter;
import saqp.secra.RowRel;
public class EQ8RowFilterTransposeRule extends SwapRule {
public static EQ8RowFilterTransposeRule INSTANCE = new EQ8RowFilterTransposeRule();
public EQ8RowFilterTransposeRule() {
super(LogicalRowRel.class, LogicalFilter.class);
super(RowRel.class, Filter.class);
}
}
......@@ -12,7 +12,7 @@ import saqp.secra.SecRel;
*/
public class PushUpThroughJoinRule extends SaqpRule {
public PushUpThroughJoinRule(Class<? extends SecRel> secOp) {
super(operand(LogicalJoin.class, operand(secOp, any())));
super(operand(Join.class, operand(secOp, any())));
}
@Override
......@@ -28,6 +28,6 @@ public class PushUpThroughJoinRule extends SaqpRule {
@Override
boolean checkCondition(RelOptRuleCall call) {
Join join = call.rel(0);
return join.getConvention() == join.getRight().getConvention();
return join.getConvention() == join.getLeft().getConvention();
}
}
package saqp.secrules;
import java.util.List;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Join;
import saqp.secra.SecRel;
/**
* Push a sec-RA operator up through a join.
*/
public class PushUpThroughJoinRuleRight extends SaqpRule {
public PushUpThroughJoinRuleRight(Class<? extends SecRel> secOp) {
super(operand(Join.class, operand(RelNode.class, any()), operand(secOp, any())));
}
@Override
void perform(RelOptRuleCall call) {
Join join = call.rel(0);
SecRel right = call.rel(2);
RelNode left = call.rel(1);
RelNode newJoin = join.copy(join.getTraitSet(), List.of(left, right.getInput()));
RelNode newParent = right.copy(right.getTraitSet(), List.of(newJoin));
call.transformTo(newParent);
}
@Override
boolean checkCondition(RelOptRuleCall call) {
Join join = call.rel(0);
return join.getConvention() == join.getRight().getConvention();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment