Search in sources :

Example 1 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project calcite by apache.

the class JavaTypeFactoryExtImpl method createPdxType2.

// Experimental flattering the nested structures.
public RelDataType createPdxType2(PdxInstance pdxInstance) {
    final List<RelDataTypeField> list = new ArrayList<>();
    recursiveCreatePdxType(pdxInstance, list, "");
    return canonize(new RelRecordType(list));
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Example 2 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project drill by axbaretto.

the class RewriteProjectToFlatten method visitProject.

@Override
public Prel visitProject(ProjectPrel node, Object unused) throws RelConversionException {
    ProjectPrel project = node;
    List<RexNode> exprList = new ArrayList<>();
    boolean rewrite = false;
    List<RelDataTypeField> relDataTypes = new ArrayList<>();
    int i = 0;
    RexNode flatttenExpr = null;
    for (RexNode rex : project.getChildExps()) {
        RexNode newExpr = rex;
        if (rex instanceof RexCall) {
            RexCall function = (RexCall) rex;
            String functionName = function.getOperator().getName();
            if (functionName.equalsIgnoreCase("flatten")) {
                rewrite = true;
                if (function.getOperands().size() != 1) {
                    throw new RelConversionException("Flatten expression expects a single input.");
                }
                newExpr = function.getOperands().get(0);
                RexBuilder builder = new RexBuilder(factory);
                flatttenExpr = builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), i);
            }
        }
        relDataTypes.add(project.getRowType().getFieldList().get(i));
        i++;
        exprList.add(newExpr);
    }
    if (rewrite == true) {
        // TODO - figure out what is the right setting for the traits
        Prel newChild = ((Prel) project.getInput(0)).accept(this, null);
        ProjectPrel newProject = new ProjectPrel(node.getCluster(), project.getTraitSet(), newChild, exprList, new RelRecordType(relDataTypes));
        FlattenPrel flatten = new FlattenPrel(project.getCluster(), project.getTraitSet(), newProject, flatttenExpr);
        return flatten;
    }
    Prel child = ((Prel) project.getInput()).accept(this, null);
    return (Prel) project.copy(project.getTraitSet(), child, exprList, new RelRecordType(relDataTypes));
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) ArrayList(java.util.ArrayList) RelDataTypeDrillImpl(org.apache.drill.exec.planner.types.RelDataTypeDrillImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RelConversionException(org.apache.calcite.tools.RelConversionException) Prel(org.apache.drill.exec.planner.physical.Prel) ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexCall(org.apache.calcite.rex.RexCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataTypeHolder(org.apache.drill.exec.planner.types.RelDataTypeHolder) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project drill by apache.

the class WindowPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final DrillWindowRel window = call.rel(0);
    RelNode input = call.rel(1);
    // TODO: Order window based on existing partition by
    // input.getTraitSet().subsumes()
    boolean partitionby = false;
    boolean addMerge = false;
    // The start index of the constant fields of DrillWindowRel
    final int startConstantsIndex = window.getInput().getRowType().getFieldCount();
    int constantShiftIndex = 0;
    for (final Ord<Window.Group> w : Ord.zip(window.groups)) {
        Window.Group windowBase = w.getValue();
        RelTraitSet traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL);
        // For empty Over-Clause
        if (windowBase.keys.isEmpty() && windowBase.orderKeys.getFieldCollations().isEmpty()) {
            DrillDistributionTrait distEmptyKeys = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.SINGLETON);
            traits = traits.plus(distEmptyKeys);
        } else if (windowBase.keys.size() > 0) {
            DrillDistributionTrait distOnAllKeys = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionFields(windowBase)));
            partitionby = true;
            traits = traits.plus(distOnAllKeys);
        } else if (windowBase.orderKeys.getFieldCollations().size() > 0) {
            // if only the order-by clause is specified, there is a single partition
            // consisting of all the rows, so we do a distributed sort followed by a
            // single merge as the input of the window operator
            DrillDistributionTrait distKeys = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionFieldsFromCollation(windowBase)));
            traits = traits.plus(distKeys);
            if (!isSingleMode(call)) {
                addMerge = true;
            }
        }
        // Add collation trait if either partition-by or order-by is specified.
        if (partitionby || windowBase.orderKeys.getFieldCollations().size() > 0) {
            RelCollation collation = getCollation(windowBase);
            traits = traits.plus(collation);
        }
        RelNode convertedInput = convert(input, traits);
        if (addMerge) {
            traits = traits.plus(DrillDistributionTrait.SINGLETON);
            convertedInput = new SingleMergeExchangePrel(window.getCluster(), traits, convertedInput, windowBase.collation());
        }
        List<RelDataTypeField> newRowFields = Lists.newArrayList();
        newRowFields.addAll(convertedInput.getRowType().getFieldList());
        Iterable<RelDataTypeField> newWindowFields = Iterables.filter(window.getRowType().getFieldList(), new Predicate<RelDataTypeField>() {

            @Override
            public boolean apply(RelDataTypeField relDataTypeField) {
                return relDataTypeField.getName().startsWith("w" + w.i + "$");
            }
        });
        for (RelDataTypeField newField : newWindowFields) {
            newRowFields.add(newField);
        }
        RelDataType rowType = new RelRecordType(newRowFields);
        List<Window.RexWinAggCall> newWinAggCalls = Lists.newArrayList();
        for (Ord<Window.RexWinAggCall> aggOrd : Ord.zip(windowBase.aggCalls)) {
            Window.RexWinAggCall aggCall = aggOrd.getValue();
            // If the argument points at the constant and
            // additional fields have been generated by the Window below,
            // the index of constants will be shifted
            final List<RexNode> newOperandsOfWindowFunction = Lists.newArrayList();
            for (RexNode operand : aggCall.getOperands()) {
                if (operand instanceof RexInputRef) {
                    final RexInputRef rexInputRef = (RexInputRef) operand;
                    final int refIndex = rexInputRef.getIndex();
                    // Check if this RexInputRef points at the constants
                    if (rexInputRef.getIndex() >= startConstantsIndex) {
                        operand = new RexInputRef(refIndex + constantShiftIndex, window.constants.get(refIndex - startConstantsIndex).getType());
                    }
                }
                newOperandsOfWindowFunction.add(operand);
            }
            aggCall = new Window.RexWinAggCall((SqlAggFunction) aggCall.getOperator(), aggCall.getType(), newOperandsOfWindowFunction, aggCall.ordinal, aggCall.distinct);
            newWinAggCalls.add(new Window.RexWinAggCall((SqlAggFunction) aggCall.getOperator(), aggCall.getType(), aggCall.getOperands(), aggOrd.i, aggCall.distinct));
        }
        windowBase = new Window.Group(windowBase.keys, windowBase.isRows, windowBase.lowerBound, windowBase.upperBound, windowBase.orderKeys, newWinAggCalls);
        input = new WindowPrel(window.getCluster(), window.getTraitSet().merge(traits), convertedInput, window.getConstants(), rowType, windowBase);
        constantShiftIndex += windowBase.aggCalls.size();
    }
    call.transformTo(input);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillWindowRel(org.apache.drill.exec.planner.logical.DrillWindowRel) Window(org.apache.calcite.rel.core.Window) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RelCollation(org.apache.calcite.rel.RelCollation) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType 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 5 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project samza by apache.

the class TestAvroRelConversion method testSimpleSchemaConversion.

@Test
public void testSimpleSchemaConversion() {
    String streamName = "stream";
    SqlSchema sqlSchema = simpleRecordSchemaProvider.getSqlSchema();
    RelDataType dataType = relSchemaConverter.convertToRelSchema(sqlSchema);
    junit.framework.Assert.assertTrue(dataType instanceof RelRecordType);
    RelRecordType recordType = (RelRecordType) dataType;
    junit.framework.Assert.assertEquals(recordType.getFieldCount(), SimpleRecord.SCHEMA$.getFields().size());
    junit.framework.Assert.assertTrue(recordType.getField("id", true, false).getType().getSqlTypeName() == SqlTypeName.INTEGER);
    junit.framework.Assert.assertTrue(recordType.getField("name", true, false).getType().getSqlTypeName() == SqlTypeName.VARCHAR);
    LOG.info("Relational schema " + dataType);
}
Also used : SqlSchema(org.apache.samza.sql.schema.SqlSchema) RelDataType(org.apache.calcite.rel.type.RelDataType) ByteString(org.apache.calcite.avatica.util.ByteString) RelRecordType(org.apache.calcite.rel.type.RelRecordType) Test(org.junit.Test)

Aggregations

RelRecordType (org.apache.calcite.rel.type.RelRecordType)26 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)20 ArrayList (java.util.ArrayList)13 RexNode (org.apache.calcite.rex.RexNode)13 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)12 RelDataType (org.apache.calcite.rel.type.RelDataType)10 RelNode (org.apache.calcite.rel.RelNode)8 RexBuilder (org.apache.calcite.rex.RexBuilder)4 RexInputRef (org.apache.calcite.rex.RexInputRef)4 Prel (org.apache.drill.exec.planner.physical.Prel)4 ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)4 RelDataTypeDrillImpl (org.apache.drill.exec.planner.types.RelDataTypeDrillImpl)4 RelDataTypeHolder (org.apache.drill.exec.planner.types.RelDataTypeHolder)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 Type (java.lang.reflect.Type)2 LinkedHashSet (java.util.LinkedHashSet)2 RelDataTypeField (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField)2 RelRecordType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelRecordType)2 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)2 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)2