Search in sources :

Example 56 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project ignite-3 by apache.

the class TableFunctionScanConverterRule method convert.

/**
 * {@inheritDoc}
 */
@Override
protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalTableFunctionScan rel) {
    assert nullOrEmpty(rel.getInputs());
    RelTraitSet traitSet = rel.getTraitSet().replace(IgniteConvention.INSTANCE).replace(RewindabilityTrait.REWINDABLE).replace(IgniteDistributions.broadcast());
    Set<CorrelationId> corrIds = RexUtils.extractCorrelationIds(rel.getCall());
    if (!corrIds.isEmpty()) {
        traitSet = traitSet.replace(CorrelationTrait.correlations(corrIds));
    }
    return new IgniteTableFunctionScan(rel.getCluster(), traitSet, rel.getCall(), rel.getRowType());
}
Also used : RelTraitSet(org.apache.calcite.plan.RelTraitSet) CorrelationId(org.apache.calcite.rel.core.CorrelationId) IgniteTableFunctionScan(org.apache.ignite.internal.sql.engine.rel.IgniteTableFunctionScan)

Example 57 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project drill by apache.

the class ComplexUnnestVisitor method visit.

public RelNode visit(Uncollect uncollect) {
    RelBuilder builder = DrillRelFactories.LOGICAL_BUILDER.create(uncollect.getCluster(), null);
    RexBuilder rexBuilder = builder.getRexBuilder();
    assert uncollect.getInput() instanceof Project : "Uncollect should have Project input";
    Project project = (Project) uncollect.getInput();
    // If project below uncollect contains only field references, no need to rewrite it
    List<RexNode> projectChildExps = project.getProjects();
    assert projectChildExps.size() == 1 : "Uncollect does not support multiple expressions";
    RexNode projectExpr = projectChildExps.iterator().next();
    if (projectExpr.getKind() == SqlKind.FIELD_ACCESS) {
        return uncollect;
    }
    // Collects CorrelationId instances used in current rel node
    RelOptUtil.VariableUsedVisitor variableUsedVisitor = new RelOptUtil.VariableUsedVisitor(null);
    project.accept(variableUsedVisitor);
    assert variableUsedVisitor.variables.size() == 1 : "Uncollect supports only single correlated reference";
    CorrelationId oldCorrId = variableUsedVisitor.variables.iterator().next();
    RelNode left = leftInputs.get(oldCorrId);
    // Creates new project to be placed on top of the left input of correlate
    List<RexNode> leftProjExprs = new ArrayList<>();
    List<String> fieldNames = new ArrayList<>();
    for (RelDataTypeField field : left.getRowType().getFieldList()) {
        leftProjExprs.add(rexBuilder.makeInputRef(left, field.getIndex()));
        fieldNames.add(field.getName());
    }
    fieldNames.add(COMPLEX_FIELD_NAME);
    builder.push(left);
    // Adds complex expression with replaced correlation
    // to the projected list from the left
    leftProjExprs.add(new RexFieldAccessReplacer(builder).apply(projectExpr));
    RelNode leftProject = builder.project(leftProjExprs, fieldNames).build();
    leftInputs.put(oldCorrId, leftProject);
    builder.push(project.getInput());
    CorrelationId newCorrId = uncollect.getCluster().createCorrel();
    // stores new CorrelationId to be used during the creation of new Correlate
    updatedCorrelationIds.put(oldCorrId, newCorrId);
    RexNode rexCorrel = rexBuilder.makeCorrel(leftProject.getRowType(), newCorrId);
    // constructs Project below Uncollect with updated RexCorrelVariable
    builder.project(ImmutableList.of(rexBuilder.makeFieldAccess(rexCorrel, leftProjExprs.size() - 1)), ImmutableList.of(COMPLEX_FIELD_NAME));
    return uncollect.copy(uncollect.getTraitSet(), builder.build());
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelOptUtil(org.apache.calcite.plan.RelOptUtil) ArrayList(java.util.ArrayList) Project(org.apache.calcite.rel.core.Project) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.calcite.rex.RexNode)

Example 58 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project drill by apache.

the class ComplexUnnestVisitor method visit.

@Override
public RelNode visit(LogicalCorrelate correlate) {
    RelNode left = correlate.getLeft().accept(this);
    leftInputs.put(correlate.getCorrelationId(), left);
    RelNode right = correlate.getRight().accept(this);
    // after rewriting right input, no need to create Correlate with new CorrelationId
    if (correlate.getRight() == right || left == leftInputs.get(correlate.getCorrelationId())) {
        if (correlate.getLeft() == left) {
            return correlate;
        }
        // changed only inputs, but CorrelationId left the same
        return correlate.copy(correlate.getTraitSet(), Arrays.asList(left, right));
    }
    Correlate newCorrelate = correlate.copy(correlate.getTraitSet(), leftInputs.get(correlate.getCorrelationId()), right, updatedCorrelationIds.get(correlate.getCorrelationId()), ImmutableBitSet.of(left.getRowType().getFieldCount()), correlate.getJoinType());
    RelBuilder builder = DrillRelFactories.LOGICAL_BUILDER.create(correlate.getCluster(), null);
    builder.push(newCorrelate);
    List<RexNode> topProjectExpressions = left.getRowType().getFieldList().stream().map(field -> builder.getRexBuilder().makeInputRef(newCorrelate, field.getIndex())).collect(Collectors.toList());
    // Accommodate the new $COMPLEX_FIELD_NAME column.
    int rightStartIndex = left.getRowType().getFieldList().size() + 1;
    switch(correlate.getJoinType()) {
        case LEFT:
        case INNER:
            // adds field from the right input of correlate to the top project
            topProjectExpressions.addAll(right.getRowType().getFieldList().stream().map(field -> builder.getRexBuilder().makeInputRef(newCorrelate, field.getIndex() + rightStartIndex)).collect(Collectors.toList()));
        // fall through
        case ANTI:
        case SEMI:
            builder.project(topProjectExpressions, correlate.getRowType().getFieldNames());
    }
    return builder.build();
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Project(org.apache.calcite.rel.core.Project) Uncollect(org.apache.calcite.rel.core.Uncollect) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) Arrays(java.util.Arrays) SqlKind(org.apache.calcite.sql.SqlKind) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) Correlate(org.apache.calcite.rel.core.Correlate) RexBuilder(org.apache.calcite.rex.RexBuilder) HashMap(java.util.HashMap) RelNode(org.apache.calcite.rel.RelNode) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Collectors(java.util.stream.Collectors) ImmutableList(org.apache.drill.shaded.guava.com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.calcite.rex.RexNode) RelBuilder(org.apache.calcite.tools.RelBuilder) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Map(java.util.Map) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) DrillRelFactories(org.apache.drill.exec.planner.logical.DrillRelFactories) RexShuttle(org.apache.calcite.rex.RexShuttle) Correlate(org.apache.calcite.rel.core.Correlate) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Example 59 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project drill by apache.

the class LateralUnnestRowIDVisitor method visitLateral.

@Override
public Prel visitLateral(LateralJoinPrel prel, Boolean isRightOfLateral) throws RuntimeException {
    List<RelNode> children = Lists.newArrayList();
    children.add(((Prel) prel.getInput(0)).accept(this, isRightOfLateral));
    children.add(((Prel) prel.getInput(1)).accept(this, true));
    if (!isRightOfLateral) {
        return (Prel) prel.copy(prel.getTraitSet(), children);
    } else {
        // Adjust the column numbering due to an additional column "$drill_implicit_field$" is added to the inputs.
        Map<Integer, Integer> requiredColsMap = new HashMap<>();
        for (Integer corrColIndex : prel.getRequiredColumns()) {
            requiredColsMap.put(corrColIndex, corrColIndex + 1);
        }
        ImmutableBitSet requiredColumns = prel.getRequiredColumns().shift(1);
        CorrelationId corrId = prel.getCluster().createCorrel();
        RexCorrelVariable updatedCorrel = (RexCorrelVariable) prel.getCluster().getRexBuilder().makeCorrel(children.get(0).getRowType(), corrId);
        RelNode rightChild = children.get(1).accept(new CorrelateVarReplacer(new ProjectCorrelateTransposeRule.RexFieldAccessReplacer(prel.getCorrelationId(), updatedCorrel, prel.getCluster().getRexBuilder(), requiredColsMap)));
        return (Prel) prel.copy(prel.getTraitSet(), children.get(0), rightChild, corrId, requiredColumns, prel.getJoinType());
    }
}
Also used : RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HashMap(java.util.HashMap) CorrelationId(org.apache.calcite.rel.core.CorrelationId) LateralJoinPrel(org.apache.drill.exec.planner.physical.LateralJoinPrel) Prel(org.apache.drill.exec.planner.physical.Prel) UnnestPrel(org.apache.drill.exec.planner.physical.UnnestPrel)

Example 60 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project flink-mirror by flink-ci.

the class RexNodeJsonSerdeTest method testRexNodeSerde.

// --------------------------------------------------------------------------------------------
// Test data
// --------------------------------------------------------------------------------------------
@SuppressWarnings("UnstableApiUsage")
private static Stream<RexNode> testRexNodeSerde() {
    final RexBuilder rexBuilder = new RexBuilder(FACTORY);
    final RelDataType inputType = FACTORY.createStructType(StructKind.PEEK_FIELDS_NO_EXPAND, Arrays.asList(FACTORY.createSqlType(SqlTypeName.INTEGER), FACTORY.createSqlType(SqlTypeName.BIGINT), FACTORY.createStructType(StructKind.PEEK_FIELDS_NO_EXPAND, Arrays.asList(FACTORY.createSqlType(SqlTypeName.VARCHAR), FACTORY.createSqlType(SqlTypeName.VARCHAR)), Arrays.asList("n1", "n2"))), Arrays.asList("f1", "f2", "f3"));
    return Stream.of(rexBuilder.makeNullLiteral(FACTORY.createSqlType(SqlTypeName.VARCHAR)), rexBuilder.makeLiteral(true), rexBuilder.makeExactLiteral(new BigDecimal(Byte.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.TINYINT)), rexBuilder.makeExactLiteral(new BigDecimal(Short.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.SMALLINT)), rexBuilder.makeExactLiteral(new BigDecimal(Integer.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeExactLiteral(new BigDecimal(Long.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.BIGINT)), rexBuilder.makeExactLiteral(BigDecimal.valueOf(Double.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.DOUBLE)), rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Float.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.FLOAT)), rexBuilder.makeExactLiteral(new BigDecimal("23.1234567890123456789012345678")), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(100), new SqlIntervalQualifier(TimeUnit.YEAR, 4, TimeUnit.YEAR, RelDataType.PRECISION_NOT_SPECIFIED, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.YEAR, 2, TimeUnit.MONTH, RelDataType.PRECISION_NOT_SPECIFIED, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.DAY, 2, TimeUnit.SECOND, 6, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.SECOND, 2, TimeUnit.SECOND, 6, SqlParserPos.ZERO)), rexBuilder.makeDateLiteral(DateString.fromDaysSinceEpoch(10)), rexBuilder.makeDateLiteral(new DateString("2000-12-12")), rexBuilder.makeTimeLiteral(TimeString.fromMillisOfDay(1234), 3), rexBuilder.makeTimeLiteral(TimeString.fromMillisOfDay(123456), 6), rexBuilder.makeTimeLiteral(new TimeString("01:01:01.000000001"), 9), rexBuilder.makeTimestampLiteral(TimestampString.fromMillisSinceEpoch(1234), 3), rexBuilder.makeTimestampLiteral(TimestampString.fromMillisSinceEpoch(123456789), 9), rexBuilder.makeTimestampLiteral(new TimestampString("0001-01-01 01:01:01.000000001"), 9), rexBuilder.makeTimestampLiteral(new TimestampString("2000-12-12 12:30:57.1234"), 4), rexBuilder.makeBinaryLiteral(ByteString.EMPTY), rexBuilder.makeBinaryLiteral(ByteString.ofBase64("SGVsbG8gV29ybGQh")), rexBuilder.makeLiteral(""), rexBuilder.makeLiteral("abc"), rexBuilder.makeFlag(SqlTrimFunction.Flag.BOTH), rexBuilder.makeFlag(TimeUnitRange.DAY), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, ImmutableRangeSet.of(Range.closed(BigDecimal.valueOf(1), BigDecimal.valueOf(10)))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, ImmutableRangeSet.of(Range.range(BigDecimal.valueOf(1), BoundType.OPEN, BigDecimal.valueOf(10), BoundType.CLOSED))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, TreeRangeSet.create(Arrays.asList(Range.closed(BigDecimal.valueOf(1), BigDecimal.valueOf(1)), Range.closed(BigDecimal.valueOf(3), BigDecimal.valueOf(3)), Range.closed(BigDecimal.valueOf(6), BigDecimal.valueOf(6))))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.BIGINT), 0), rexBuilder.makeCorrel(inputType, new CorrelationId("$cor1")), rexBuilder.makeFieldAccess(rexBuilder.makeCorrel(inputType, new CorrelationId("$cor2")), "f2", true), // cast($1 as smallint)
    rexBuilder.makeCast(FACTORY.createSqlType(SqlTypeName.SMALLINT), rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1)), // $1 in (1, 3, 5)
    rexBuilder.makeIn(rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1), Arrays.asList(rexBuilder.makeExactLiteral(new BigDecimal(1)), rexBuilder.makeExactLiteral(new BigDecimal(3)), rexBuilder.makeExactLiteral(new BigDecimal(5)))), // null or $1 is null
    rexBuilder.makeCall(SqlStdOperatorTable.OR, rexBuilder.makeNullLiteral(FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1))), // $1 >= 10
    rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1), rexBuilder.makeExactLiteral(new BigDecimal(10))), // hash_code($1)
    rexBuilder.makeCall(FlinkSqlOperatorTable.HASH_CODE, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1)), rexBuilder.makePatternFieldRef("test", FACTORY.createSqlType(SqlTypeName.INTEGER), 0));
}
Also used : TimeString(org.apache.calcite.util.TimeString) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) DateString(org.apache.calcite.util.DateString) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) TimestampString(org.apache.calcite.util.TimestampString) CorrelationId(org.apache.calcite.rel.core.CorrelationId) BigDecimal(java.math.BigDecimal)

Aggregations

CorrelationId (org.apache.calcite.rel.core.CorrelationId)74 RelNode (org.apache.calcite.rel.RelNode)39 RexNode (org.apache.calcite.rex.RexNode)33 RelDataType (org.apache.calcite.rel.type.RelDataType)25 ArrayList (java.util.ArrayList)22 RelOptCluster (org.apache.calcite.plan.RelOptCluster)19 RexBuilder (org.apache.calcite.rex.RexBuilder)18 RelBuilder (org.apache.calcite.tools.RelBuilder)17 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)15 ImmutableList (com.google.common.collect.ImmutableList)12 RelTraitSet (org.apache.calcite.plan.RelTraitSet)12 RexCorrelVariable (org.apache.calcite.rex.RexCorrelVariable)11 RexShuttle (org.apache.calcite.rex.RexShuttle)11 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 List (java.util.List)9 RelOptUtil (org.apache.calcite.plan.RelOptUtil)9 JoinRelType (org.apache.calcite.rel.core.JoinRelType)9 ByteString (org.apache.calcite.avatica.util.ByteString)7 SqlOperator (org.apache.calcite.sql.SqlOperator)7