use of org.apache.calcite.rel.logical.LogicalProject in project calcite by apache.
the class ProjectCalcMergeRule method onMatch.
// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
final LogicalProject project = call.rel(0);
final LogicalCalc calc = call.rel(1);
// Don't merge a project which contains windowed aggregates onto a
// calc. That would effectively be pushing a windowed aggregate down
// through a filter. Transform the project into an identical calc,
// which we'll have chance to merge later, after the over is
// expanded.
final RelOptCluster cluster = project.getCluster();
RexProgram program = RexProgram.create(calc.getRowType(), project.getProjects(), null, project.getRowType(), cluster.getRexBuilder());
if (RexOver.containsOver(program)) {
LogicalCalc projectAsCalc = LogicalCalc.create(calc, program);
call.transformTo(projectAsCalc);
return;
}
// Create a program containing the project node's expressions.
final RexBuilder rexBuilder = cluster.getRexBuilder();
final RexProgramBuilder progBuilder = new RexProgramBuilder(calc.getRowType(), rexBuilder);
for (Pair<RexNode, String> field : project.getNamedProjects()) {
progBuilder.addProject(field.left, field.right);
}
RexProgram topProgram = progBuilder.getProgram();
RexProgram bottomProgram = calc.getProgram();
// Merge the programs together.
RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder);
final LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), mergedProgram);
call.transformTo(newCalc);
}
use of org.apache.calcite.rel.logical.LogicalProject in project calcite by apache.
the class ProjectMultiJoinMergeRule method onMatch.
// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
LogicalProject project = call.rel(0);
MultiJoin multiJoin = call.rel(1);
// if all inputs have their projFields set, then projection information
// has already been pushed into each input
boolean allSet = true;
for (int i = 0; i < multiJoin.getInputs().size(); i++) {
if (multiJoin.getProjFields().get(i) == null) {
allSet = false;
break;
}
}
if (allSet) {
return;
}
// create a new MultiJoin that reflects the columns in the projection
// above the MultiJoin
final RelBuilder relBuilder = call.builder();
MultiJoin newMultiJoin = RelOptUtil.projectMultiJoin(multiJoin, project);
relBuilder.push(newMultiJoin).project(project.getProjects(), project.getRowType().getFieldNames());
call.transformTo(relBuilder.build());
}
use of org.apache.calcite.rel.logical.LogicalProject in project calcite by apache.
the class ProjectToCalcRule method onMatch.
// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
final LogicalProject project = call.rel(0);
final RelNode input = project.getInput();
final RexProgram program = RexProgram.create(input.getRowType(), project.getProjects(), null, project.getRowType(), project.getCluster().getRexBuilder());
final LogicalCalc calc = LogicalCalc.create(input, program);
call.transformTo(calc);
}
use of org.apache.calcite.rel.logical.LogicalProject in project calcite by apache.
the class RexTransformerTest method testSplitJoinCondition.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-833">[CALCITE-833]
* RelOptUtil.splitJoinCondition attempts to split a Join-Condition which
* has a remaining condition</a>.
*/
@Test
public void testSplitJoinCondition() {
final String sql = "select * \n" + "from emp a \n" + "INNER JOIN dept b \n" + "ON CAST(a.empno AS int) <> b.deptno";
final RelNode relNode = toRel(sql);
final LogicalProject project = (LogicalProject) relNode;
final LogicalJoin join = (LogicalJoin) project.getInput(0);
final List<RexNode> leftJoinKeys = new ArrayList<>();
final List<RexNode> rightJoinKeys = new ArrayList<>();
final ArrayList<RelDataTypeField> sysFieldList = new ArrayList<>();
final RexNode remaining = RelOptUtil.splitJoinCondition(sysFieldList, join.getInputs().get(0), join.getInputs().get(1), join.getCondition(), leftJoinKeys, rightJoinKeys, null, null);
assertThat(remaining.toString(), is("<>(CAST($0):INTEGER NOT NULL, $9)"));
assertThat(leftJoinKeys.isEmpty(), is(true));
assertThat(rightJoinKeys.isEmpty(), is(true));
}
use of org.apache.calcite.rel.logical.LogicalProject in project calcite by apache.
the class Lattice method populate.
private static boolean populate(List<RelNode> nodes, List<int[][]> tempLinks, RelNode rel) {
if (nodes.isEmpty() && rel instanceof LogicalProject) {
return populate(nodes, tempLinks, ((LogicalProject) rel).getInput());
}
if (rel instanceof TableScan) {
nodes.add(rel);
return true;
}
if (rel instanceof LogicalJoin) {
LogicalJoin join = (LogicalJoin) rel;
if (join.getJoinType() != JoinRelType.INNER) {
throw new RuntimeException("only inner join allowed, but got " + join.getJoinType());
}
populate(nodes, tempLinks, join.getLeft());
populate(nodes, tempLinks, join.getRight());
for (RexNode rex : RelOptUtil.conjunctions(join.getCondition())) {
tempLinks.add(grab(nodes, rex));
}
return true;
}
throw new RuntimeException("Invalid node type " + rel.getClass().getSimpleName() + " in lattice query");
}
Aggregations