use of org.apache.calcite.adapter.elasticsearch.ElasticsearchProject in project drill by apache.
the class ElasticPlanTransformer method visit.
@Override
public RelNode visit(RelNode other) {
// with actual table fields
if (other instanceof ElasticsearchProject) {
ElasticsearchProject project = (ElasticsearchProject) other;
RelNode input = project.getInput().accept(this);
List<RexNode> convertedExpressions = project.getProjects();
// project closest to the scan should be rewritten only
if (!this.hasProject) {
ElasticExpressionMapper expressionMapper = new ElasticExpressionMapper(project.getCluster().getRexBuilder(), project.getInput().getRowType(), mapField);
convertedExpressions = convertedExpressions.stream().map(expression -> expression.accept(expressionMapper)).collect(Collectors.toList());
RelRecordType relDataType = getRelRecordType(other.getRowType());
this.hasProject = true;
return CalciteUtils.createProject(project.getTraitSet(), input, convertedExpressions, relDataType);
} else {
return input;
}
} else if (other instanceof ElasticsearchFilter) {
ElasticsearchFilter filter = (ElasticsearchFilter) other;
RexNode convertedCondition = filter.getCondition().accept(new ElasticExpressionMapper(other.getCluster().getRexBuilder(), filter.getInput().getRowType(), mapField));
return filter.copy(other.getTraitSet(), filter.getInput().accept(this), convertedCondition);
} else if (other instanceof ElasticsearchSort) {
ElasticsearchSort sort = (ElasticsearchSort) other;
RelNode input = getMappedInput(sort.getInput());
return sort.copy(other.getTraitSet(), input, sort.getCollation(), sort.offset, sort.fetch);
} else if (other instanceof ElasticsearchAggregate) {
ElasticsearchAggregate aggregate = (ElasticsearchAggregate) other;
RelNode input = getMappedInput(aggregate.getInput());
return aggregate.copy(other.getTraitSet(), input, aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList());
}
return super.visit(other);
}
use of org.apache.calcite.adapter.elasticsearch.ElasticsearchProject in project drill by apache.
the class ElasticsearchProjectRule method convert.
@Override
public RelNode convert(RelNode relNode) {
Project project = (Project) relNode;
NodeTypeFinder projectFinder = new NodeTypeFinder(ElasticsearchProject.class);
project.getInput().accept(projectFinder);
if (projectFinder.containsNode) {
// Calcite adapter allows only a single Elasticsearch project per tree
return null;
}
RelTraitSet traitSet = project.getTraitSet().replace(out);
List<RexNode> innerProjections = new ArrayList<>();
RelDataType rowType = project.getInput().getRowType();
// check for literals only without input exprs
DrillRelOptUtil.InputRefVisitor collectRefs = new DrillRelOptUtil.InputRefVisitor();
project.getChildExps().forEach(exp -> exp.accept(collectRefs));
if (!collectRefs.getInputRefs().isEmpty()) {
for (RelDataTypeField relDataTypeField : rowType.getFieldList()) {
innerProjections.add(project.getCluster().getRexBuilder().makeInputRef(project.getInput(), relDataTypeField.getIndex()));
}
}
boolean allExprsInputRefs = project.getChildExps().stream().allMatch(rexNode -> rexNode instanceof RexInputRef);
if (collectRefs.getInputRefs().isEmpty() || allExprsInputRefs) {
return CalciteUtils.createProject(traitSet, convert(project.getInput(), out), project.getProjects(), project.getRowType());
} else {
Project elasticsearchProject = CalciteUtils.createProject(traitSet, convert(project.getInput(), out), innerProjections, project.getInput().getRowType());
return project.copy(project.getTraitSet(), elasticsearchProject, project.getProjects(), project.getRowType());
}
}
Aggregations