Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
liurenjie1024 committed Mar 2, 2022
1 parent aebf1f4 commit 7b394de
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public ConfigEntry<?> getConfigEntry(String stringKey) {
var configEntry = confRegistry.get(stringKey);
if (configEntry == null) {
throw new PgException(
PgErrorCode.CONFIG_FILE_ERROR,
"Config entry '%s' not found!",
stringKey);
PgErrorCode.CONFIG_FILE_ERROR, "Config entry '%s' not found!", stringKey);
} else {
return configEntry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ public enum CatalogMode {
.withDoc("Meta service node address")
.withConverter(STRING_PARSER)
.build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.util.Util;

/**
* Utilities functions for Planner
*/
/** Utilities functions for Planner */
public class PlannerUtils {
public static boolean isSingleMode(ExecutionContext context) {
return context.getConf().get(CLUSTER_MODE) == LeaderServerConfigurations.ClusterMode.Single;
Expand Down Expand Up @@ -75,9 +73,7 @@ public static RexNode toCnf(RexBuilder rexBuilder, int maxCnfNodeCount, RexNode
return new CnfHelper(rexBuilder, maxCnfNodeCnt).toCnf(rex);
}

/**
* Get the number of RexCall in the given node.
*/
/** Get the number of RexCall in the given node. */
private static int getNumberOfRexCall(RexNode rex) {
final int[] numberOfNodes = new int[] {0};
rex.accept(
Expand All @@ -91,9 +87,7 @@ public Void visitCall(RexCall call) {
return numberOfNodes[0];
}

/**
* Helps [[toCnf]]
*/
/** Helps [[toCnf]] */
private static class CnfHelper {
private final RexBuilder rexBuilder;
private final int maxNodeCount;
Expand All @@ -117,64 +111,69 @@ public RexNode toCnf(RexNode rex) {

private RexNode toCnf2(RexNode rex) {
switch (rex.getKind()) {
case AND: {
List<RexNode> cnfOperands = Lists.newArrayList();
var operands = RexUtil.flattenAnd(((RexCall) rex).getOperands());
for (RexNode node : operands) {
var cnf = toCnf2(node);
if (cnf.getKind() == SqlKind.AND) {
cnfOperands.addAll(((RexCall) cnf).getOperands());
} else {
cnfOperands.add(cnf);
case AND:
{
List<RexNode> cnfOperands = Lists.newArrayList();
var operands = RexUtil.flattenAnd(((RexCall) rex).getOperands());
for (RexNode node : operands) {
var cnf = toCnf2(node);
if (cnf.getKind() == SqlKind.AND) {
cnfOperands.addAll(((RexCall) cnf).getOperands());
} else {
cnfOperands.add(cnf);
}
}
var node = and(cnfOperands);
checkCnfRexCallCount(node);
return node;
}
var node = and(cnfOperands);
checkCnfRexCallCount(node);
return node;
}
case OR: {
var operands = RexUtil.flattenOr(((RexCall) rex).getOperands());
var head = operands.get(0);
var headCnf = toCnf2(head);
var headCnfs = RelOptUtil.conjunctions(headCnf);
var tail = or(Util.skip(operands));
var tailCnf = toCnf2(tail);
var tailCnfs = RelOptUtil.conjunctions(tailCnf);
List<RexNode> list = Lists.newArrayList();
for (var h : headCnfs) {
for (var t : tailCnfs) {
list.add(or(ImmutableList.of(h, t)));
case OR:
{
var operands = RexUtil.flattenOr(((RexCall) rex).getOperands());
var head = operands.get(0);
var headCnf = toCnf2(head);
var headCnfs = RelOptUtil.conjunctions(headCnf);
var tail = or(Util.skip(operands));
var tailCnf = toCnf2(tail);
var tailCnfs = RelOptUtil.conjunctions(tailCnf);
List<RexNode> list = Lists.newArrayList();
for (var h : headCnfs) {
for (var t : tailCnfs) {
list.add(or(ImmutableList.of(h, t)));
}
}
var node = and(list);
checkCnfRexCallCount(node);
return node;
}
var node = and(list);
checkCnfRexCallCount(node);
return node;
}
case NOT: {
var arg = ((RexCall) rex).getOperands().get(0);
switch (arg.getKind()) {
case NOT:
return toCnf2(((RexCall) arg).getOperands().get(0));
case OR: {
var operands = ((RexCall) arg).getOperands();
return toCnf2(
and(
RexUtil.flattenOr(operands).stream()
.map(this::addNot)
.collect(Collectors.toList())));
}
case AND: {
var operands = ((RexCall) arg).getOperands();
return toCnf2(
or(
RexUtil.flattenAnd(operands).stream()
.map(this::addNot)
.collect(Collectors.toList())));
case NOT:
{
var arg = ((RexCall) rex).getOperands().get(0);
switch (arg.getKind()) {
case NOT:
return toCnf2(((RexCall) arg).getOperands().get(0));
case OR:
{
var operands = ((RexCall) arg).getOperands();
return toCnf2(
and(
RexUtil.flattenOr(operands).stream()
.map(this::addNot)
.collect(Collectors.toList())));
}
case AND:
{
var operands = ((RexCall) arg).getOperands();
return toCnf2(
or(
RexUtil.flattenAnd(operands).stream()
.map(this::addNot)
.collect(Collectors.toList())));
}
default:
return rex;
}
default:
return rex;
}
}
default:
return rex;
}
Expand Down Expand Up @@ -348,9 +347,10 @@ public RexNode mergeSameExpr(RexNode expr) {
// merges same expressions in disjunctions
// e.g. (a > b OR c < 10) OR a > b -> a > b OR c < 10 OR false
sameExprMap.clear();
var newDisjunctions = RelOptUtil.disjunctions(newExpr2).stream()
.map(ex -> mergeSameExpr(ex, rexBuilder.makeLiteral(false)))
.toArray(RexNode[]::new);
var newDisjunctions =
RelOptUtil.disjunctions(newExpr2).stream()
.map(ex -> mergeSameExpr(ex, rexBuilder.makeLiteral(false)))
.toArray(RexNode[]::new);

RexNode newExpr3;
if (newDisjunctions.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.risingwave.planner.rel.serialization.ExplainWriter;
import com.risingwave.planner.rules.physical.BatchRuleSets;
import com.risingwave.planner.sql.SqlConverter;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelNode;
Expand All @@ -51,7 +50,10 @@ public BatchPlan plan(SqlNode ast, ExecutionContext context) {
return planDistributed(ast, context);
}

public RelNode plan(SqlNode ast, ExecutionContext context, Function<RelCollation, OptimizerProgram> optimizerProgramProvider) {
public RelNode plan(
SqlNode ast,
ExecutionContext context,
Function<RelCollation, OptimizerProgram> optimizerProgramProvider) {
SqlConverter sqlConverter = SqlConverter.builder(context).build();
RelRoot rawRoot = sqlConverter.toRel(ast);
OptimizerProgram optimizerProgram = optimizerProgramProvider.apply(rawRoot.collation);
Expand All @@ -71,20 +73,23 @@ public RelNode plan(SqlNode ast, ExecutionContext context, Function<RelCollation
}

public RelNode planLogical(SqlNode ast, ExecutionContext context) {
RelNode result = plan(ast, context, relCollation -> buildOptimizerProgram(LOGICAL_CBO, relCollation));
RelNode result =
plan(ast, context, relCollation -> buildOptimizerProgram(LOGICAL_CBO, relCollation));
log.info("Create logical plan:\n {}", ExplainWriter.explainPlan(result));
return result;
}

public BatchPlan planPhysical(SqlNode ast, ExecutionContext context) {
RelNode result = plan(ast, context, relCollation -> buildOptimizerProgram(PHYSICAL, relCollation));
RelNode result =
plan(ast, context, relCollation -> buildOptimizerProgram(PHYSICAL, relCollation));
RisingWaveBatchPhyRel root = (RisingWaveBatchPhyRel) result;
log.debug("Create physical plan:\n {}", ExplainWriter.explainPlan(root));
return new BatchPlan(root);
}

public BatchPlan planDistributed(SqlNode ast, ExecutionContext context) {
RelNode result = plan(ast, context, relCollation -> buildOptimizerProgram(DISTRIBUTED, relCollation));
RelNode result =
plan(ast, context, relCollation -> buildOptimizerProgram(DISTRIBUTED, relCollation));
RisingWaveBatchPhyRel root = (RisingWaveBatchPhyRel) result;
log.debug("Create distributed plan:\n {}", ExplainWriter.explainPlan(root));
return new BatchPlan(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.tools.RuleSet;

/** A wrapper against calcite's HepOptimizerProgram. */
public class HepOptimizerProgram implements OptimizerProgram {
private final HepProgram hepProgram;

Expand All @@ -33,6 +34,7 @@ public static Builder builder() {
return new Builder();
}

/** Program builder. */
public static class Builder {
private HepMatchOrder matchOrder = HepMatchOrder.ARBITRARY;
private int matchLimit = Integer.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ public SqlConverter build() {
RisingWaveConvertletTable sqlRexConvertletTable = new RisingWaveConvertletTable();

initAll();
this.config = this.config.addRelBuilderConfigTransform(c -> c.withSimplify(false))
.withExpand(context.getSessionConfiguration().get(OPTIMIZER_ENABLE_CALCITE_SUBQUERY_EXPAND));
this.config =
this.config
.addRelBuilderConfigTransform(c -> c.withSimplify(false))
.withExpand(
context.getSessionConfiguration().get(OPTIMIZER_ENABLE_CALCITE_SUBQUERY_EXPAND));

SqlToRelConverter sql2RelConverter =
new RisingWaveSqlToRelConverter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,9 @@ protected void runTestCase(PlannerTestCase testCase) {
String sql = testCase.getSql();

SqlNode ast = parseSql(sql);

verifyPhyPlan(testCase, ast);
verifyDistPlan(testCase, ast);
}

protected void runTestCaseWithProgram(PlannerTestCase testCase, OptimizerProgram program) {
String sql = testCase.getSql();

SqlNode ast = parseSql(sql);

verifyPlanWithProgram(testCase, ast, program);

}

protected void verifyPlanWithProgram(PlannerTestCase testCase, SqlNode ast, OptimizerProgram program) {
var root = batchPlanner.plan(ast, executionContext, relCollation -> program);
if (testCase.getPlan().isPresent()) {
String explainedPlan = ExplainWriter.explainPlan(root);
assertEquals(testCase.getPlan().get(), explainedPlan, "Plan not match!");
}
}

protected void verifyPhyPlan(PlannerTestCase testCase, SqlNode ast) {
BatchPlan phyPlan = batchPlanner.planPhysical(ast, executionContext);
ast = parseSql(sql);
BatchPlan distPlan = batchPlanner.planDistributed(ast, executionContext);

if (testCase.getPlan().isPresent()) {
String explainedPlan = ExplainWriter.explainPlan(phyPlan.getRoot());
Expand All @@ -75,6 +54,11 @@ protected void verifyPhyPlan(PlannerTestCase testCase, SqlNode ast) {
assertEquals(testCase.getPhyPlan().get(), explainedPlan, "Plan not match!");
}

if (testCase.getDistPlan().isPresent()) {
String explainedPlan = ExplainWriter.explainPlan(distPlan.getRoot());
assertEquals(testCase.getDistPlan().get(), explainedPlan, "Plan not match!");
}

if (testCase.getJson().isPresent()) {
// We still assume that the parsed json test case contains json value only.
String serializedJsonPlan = Messages.jsonFormat(phyPlan.getRoot().serialize());
Expand All @@ -83,13 +67,20 @@ protected void verifyPhyPlan(PlannerTestCase testCase, SqlNode ast) {
}
}

protected void verifyDistPlan(PlannerTestCase testCase, SqlNode ast) {
BatchPlan distPlan = batchPlanner.planDistributed(ast, executionContext);
protected void runTestCaseWithProgram(PlannerTestCase testCase, OptimizerProgram program) {
String sql = testCase.getSql();

SqlNode ast = parseSql(sql);

verifyPlanWithProgram(testCase, ast, program);
}

if (testCase.getDistPlan().isPresent()) {
String explainedPlan = ExplainWriter.explainPlan(distPlan.getRoot());
assertEquals(testCase.getDistPlan().get(), explainedPlan, "Plan not match!");
protected void verifyPlanWithProgram(
PlannerTestCase testCase, SqlNode ast, OptimizerProgram program) {
var root = batchPlanner.plan(ast, executionContext, relCollation -> program);
if (testCase.getPlan().isPresent()) {
String explainedPlan = ExplainWriter.explainPlan(root);
assertEquals(testCase.getPlan().get(), explainedPlan, "Plan not match!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class SimplifyFilterConditionRuleTest extends BatchPlanTestBase {
@BeforeAll
public void initAll() {
super.init();
executionContext.getSessionConfiguration().setByString(OPTIMIZER_ENABLE_CALCITE_SUBQUERY_EXPAND.getKey(), "false");
executionContext
.getSessionConfiguration()
.setByString(OPTIMIZER_ENABLE_CALCITE_SUBQUERY_EXPAND.getKey(), "false");
program = HepOptimizerProgram.builder().addRule(SimplifyFilterConditionRule.INSTANCE).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import akka.actor.typed.ActorSystem;
import akka.actor.typed.SpawnProtocol;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
Expand Down Expand Up @@ -57,9 +56,10 @@ CatalogService getCatalogService() {
@Singleton
@Provides
static Configuration getConfiguration() {
var testConfig = TestPlannerModule.class.getClassLoader()
.getResourceAsStream("config.properties");
return Configuration.load(testConfig, FrontendServerConfigurations.class, LeaderServerConfigurations.class);
var testConfig =
TestPlannerModule.class.getClassLoader().getResourceAsStream("config.properties");
return Configuration.load(
testConfig, FrontendServerConfigurations.class, LeaderServerConfigurations.class);
}

@Singleton
Expand Down

0 comments on commit 7b394de

Please sign in to comment.