use of org.apache.calcite.rel.core.Project in project hive by apache.
the class MaterializedViewRewritingRelVisitor method visit.
@Override
public void visit(RelNode node, int ordinal, RelNode parent) {
if (node instanceof Aggregate) {
this.containsAggregate = true;
// Aggregate mode - it should be followed by union
// that we need to analyze
RelNode input = node.getInput(0);
if (input instanceof Union) {
check((Union) input);
}
} else if (node instanceof Union) {
// Non aggregate mode - analyze union operator
check((Union) node);
} else if (node instanceof Project) {
// Project operator, we can continue
super.visit(node, ordinal, parent);
}
throw new ReturnedValue(false);
}
use of org.apache.calcite.rel.core.Project in project hive by apache.
the class MaterializedViewRewritingRelVisitor method check.
private void check(Union union) {
// We found the Union
if (union.getInputs().size() != 2) {
// Bail out
throw new ReturnedValue(false);
}
// First branch should have the query (with write ID filter conditions)
new RelVisitor() {
@Override
public void visit(RelNode node, int ordinal, RelNode parent) {
if (node instanceof TableScan || node instanceof Filter || node instanceof Project || node instanceof Join) {
// We can continue
super.visit(node, ordinal, parent);
} else if (node instanceof Aggregate && containsAggregate) {
Aggregate aggregate = (Aggregate) node;
for (int i = 0; i < aggregate.getAggCallList().size(); ++i) {
AggregateCall aggregateCall = aggregate.getAggCallList().get(i);
if (aggregateCall.getAggregation().getKind() == SqlKind.COUNT && aggregateCall.getArgList().size() == 0) {
countIndex = i + aggregate.getGroupCount();
break;
}
}
// We can continue
super.visit(node, ordinal, parent);
} else {
throw new ReturnedValue(false);
}
}
}.go(union.getInput(0));
// Second branch should only have the MV
new RelVisitor() {
@Override
public void visit(RelNode node, int ordinal, RelNode parent) {
if (node instanceof TableScan) {
// We can continue
// TODO: Need to check that this is the same MV that we are rebuilding
RelOptHiveTable hiveTable = (RelOptHiveTable) node.getTable();
if (!hiveTable.getHiveTableMD().isMaterializedView()) {
// If it is not a materialized view, we do not rewrite it
throw new ReturnedValue(false);
}
if (containsAggregate && !AcidUtils.isFullAcidTable(hiveTable.getHiveTableMD())) {
// we do not rewrite it (we need MERGE support)
throw new ReturnedValue(false);
}
} else if (node instanceof Project) {
// We can continue
super.visit(node, ordinal, parent);
} else {
throw new ReturnedValue(false);
}
}
}.go(union.getInput(1));
// We pass all the checks, we can rewrite
throw new ReturnedValue(true);
}
use of org.apache.calcite.rel.core.Project in project hive by apache.
the class FilterSelectivityEstimator method isPartitionPredicate.
private boolean isPartitionPredicate(RexNode expr, RelNode r) {
if (r instanceof Project) {
expr = RelOptUtil.pushFilterPastProject(expr, (Project) r);
return isPartitionPredicate(expr, ((Project) r).getInput());
} else if (r instanceof Filter) {
return isPartitionPredicate(expr, ((Filter) r).getInput());
} else if (r instanceof HiveTableScan) {
RelOptHiveTable table = (RelOptHiveTable) ((HiveTableScan) r).getTable();
ImmutableBitSet cols = RelOptUtil.InputFinder.bits(expr);
return table.containsPartitionColumnsOnly(cols);
}
return false;
}
use of org.apache.calcite.rel.core.Project in project hive by apache.
the class HiveRelDecorrelator method projectedLiteral.
/**
* Returns a literal output field, or null if it is not literal.
*/
private static RexLiteral projectedLiteral(RelNode rel, int i) {
if (rel instanceof Project) {
final Project project = (Project) rel;
final RexNode node = project.getProjects().get(i);
if (node instanceof RexLiteral) {
return (RexLiteral) node;
}
}
return null;
}
use of org.apache.calcite.rel.core.Project in project hive by apache.
the class HiveUnionSimpleSelectsToInlineTableRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
RexBuilder rexBuilder = call.builder().getRexBuilder();
final HiveUnion union = call.rel(0);
if (!union.all) {
return;
}
List<RelNode> inputs = new ArrayList<RelNode>();
List<Project> projects = new ArrayList<>();
List<HiveTableFunctionScan> inlineTables = new ArrayList<>();
for (RelNode input : union.getInputs()) {
input = HiveRelDecorrelator.stripHep(input);
if (isPlainProject(input)) {
projects.add((Project) input);
continue;
}
if (isInlineTableOperand(input)) {
inlineTables.add((HiveTableFunctionScan) input);
continue;
}
inputs.add(input);
}
if (projects.size() + inlineTables.size() <= 1) {
// nothing to do
return;
}
RowStorage newRows = new RowStorage();
for (HiveTableFunctionScan rel : inlineTables) {
// inline(array(row1,row2,...))
RexCall rex = (RexCall) ((RexCall) rel.getCall()).operands.get(0);
for (RexNode row : rex.operands) {
if (!(row.getType() instanceof RelRecordType)) {
return;
}
newRows.addRow(row);
}
}
for (Project proj : projects) {
RexNode row = rexBuilder.makeCall(SqlStdOperatorTable.ROW, proj.getProjects());
if (!(row.getType() instanceof RelRecordType)) {
return;
}
newRows.addRow(row);
}
if (newRows.keySet().size() + inputs.size() == union.getInputs().size()) {
// nothing to do
return;
}
if (dummyTable == null) {
LOG.warn("Unexpected; rule would match - but dummyTable is not available");
return;
}
for (RelRecordType type : newRows.keySet()) {
List<RexNode> rows = newRows.get(type);
RelDataType arrayType = rexBuilder.getTypeFactory().createArrayType(type, -1);
try {
SqlOperator inlineFn = SqlFunctionConverter.getCalciteFn("inline", Collections.singletonList(arrayType), type, true, false);
SqlOperator arrayFn = SqlFunctionConverter.getCalciteFn("array", Collections.nCopies(rows.size(), type), arrayType, true, false);
RexNode expr = rexBuilder.makeCall(arrayFn, rows);
expr = rexBuilder.makeCall(inlineFn, expr);
RelNode newInlineTable = buildTableFunctionScan(expr, union.getCluster());
inputs.add(newInlineTable);
} catch (CalciteSemanticException e) {
LOG.debug("Conversion failed with exception", e);
return;
}
}
if (inputs.size() > 1) {
HiveUnion newUnion = (HiveUnion) union.copy(union.getTraitSet(), inputs, true);
call.transformTo(newUnion);
} else {
call.transformTo(inputs.get(0));
}
}
Aggregations