Search in sources :

Example 1 with ElasticsearchProject

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);
}
Also used : ElasticsearchSort(org.apache.calcite.adapter.elasticsearch.ElasticsearchSort) RelNode(org.apache.calcite.rel.RelNode) ElasticsearchProject(org.apache.calcite.adapter.elasticsearch.ElasticsearchProject) ElasticsearchAggregate(org.apache.calcite.adapter.elasticsearch.ElasticsearchAggregate) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RexNode(org.apache.calcite.rex.RexNode) ElasticsearchFilter(org.apache.calcite.adapter.elasticsearch.ElasticsearchFilter)

Example 2 with ElasticsearchProject

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());
    }
}
Also used : DrillRelOptUtil(org.apache.drill.exec.planner.common.DrillRelOptUtil) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) Project(org.apache.calcite.rel.core.Project) ElasticsearchProject(org.apache.calcite.adapter.elasticsearch.ElasticsearchProject) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

ElasticsearchProject (org.apache.calcite.adapter.elasticsearch.ElasticsearchProject)2 RexNode (org.apache.calcite.rex.RexNode)2 ArrayList (java.util.ArrayList)1 ElasticsearchAggregate (org.apache.calcite.adapter.elasticsearch.ElasticsearchAggregate)1 ElasticsearchFilter (org.apache.calcite.adapter.elasticsearch.ElasticsearchFilter)1 ElasticsearchSort (org.apache.calcite.adapter.elasticsearch.ElasticsearchSort)1 RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 RelNode (org.apache.calcite.rel.RelNode)1 Project (org.apache.calcite.rel.core.Project)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RelRecordType (org.apache.calcite.rel.type.RelRecordType)1 RexInputRef (org.apache.calcite.rex.RexInputRef)1 DrillRelOptUtil (org.apache.drill.exec.planner.common.DrillRelOptUtil)1