use of org.apache.calcite.rel.core.CorrelationId in project Mycat2 by MyCATApache.
the class MycatRelBuilder method filter.
//
@Override
public RelBuilder filter(Iterable<CorrelationId> variablesSet, Iterable<? extends RexNode> predicates) {
ImmutableList<CorrelationId> correlationIds = ImmutableList.copyOf(variablesSet);
if (correlationIds.isEmpty()) {
RelNode peek = peek();
if (peek instanceof Filter) {
Filter filter = (Filter) build();
ImmutableList.Builder<RexNode> builder = ImmutableList.builder();
ImmutableList<RexNode> rexNodes = builder.add(filter.getCondition()).addAll(predicates).build();
push(filter.copy(filter.getTraitSet(), filter.getInput(), RexUtil.composeConjunction(getRexBuilder(), rexNodes)));
return this;
}
}
return super.filter(variablesSet, predicates);
}
use of org.apache.calcite.rel.core.CorrelationId in project flink by splunk.
the class RexNodeJsonDeserializer method deserializeCorrelVariable.
private static RexNode deserializeCorrelVariable(JsonNode jsonNode, SerdeContext serdeContext) {
final String correl = jsonNode.required(FIELD_NAME_CORREL).asText();
final JsonNode logicalTypeNode = jsonNode.required(FIELD_NAME_TYPE);
final RelDataType fieldType = RelDataTypeJsonDeserializer.deserialize(logicalTypeNode, serdeContext);
return serdeContext.getRexBuilder().makeCorrel(fieldType, new CorrelationId(correl));
}
use of org.apache.calcite.rel.core.CorrelationId in project flink by splunk.
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));
}
use of org.apache.calcite.rel.core.CorrelationId in project ignite by apache.
the class IgnitePlanner method replaceCorrelatesCollisions.
/**
* When rewriting sub-queries to {@code LogicalCorrelate} instances, correlate nodes with the same correlation ids
* can be created (if there was more then one sub-query using the same correlate table). It's not a problem, when
* rows are processed one by one (like in the enumerable convension), but Ignite execution nodes process batches
* of rows, and execution nodes in some cases can get unexpected values for correlated variables.
*
* This method replaces collisions by variables in correlates. For the left hand of LogicalCorrelate duplicated
* correlated variable and it's usages replaced with the new one. For example:
*
* LogicalCorrelate(correlation=[$cor0]) LogicalCorrelate(correlation=[$cor0])
* LogicalCorrelate(correlation=[$cor0]) transforms to LogicalCorrelate(correlation=[$cor1])
* ... condition=[=($cor0.A, $0)] ... ... condition=[=($cor1.A, $0)] ...
* ... condition=[=($cor0.A, $0)] ... ... condition=[=($cor0.A, $0)] ...
*
* For the right hand of LogicalCorrelate duplicated LogicalCorrelate is just replaced with regular join.
* For example:
*
* LogicalCorrelate(correlation=[$cor0]) LogicalCorrelate(correlation=[$cor0])
* ... transforms to ...
* LogicalCorrelate(correlation=[$cor0]) LogicalJoin(condition=true)
*
* @param rel Relational expression tree.
* @return Relational expression without collisions in correlates.
*/
public RelNode replaceCorrelatesCollisions(RelNode rel) {
RelShuttle relShuttle = new RelHomogeneousShuttle() {
/**
* Set of used correlates.
*/
private final Set<CorrelationId> usedSet = new HashSet<>();
/**
* Map to find correlates, that should be replaced (in the left hand of correlate).
*/
private final Map<CorrelationId, CorrelationId> replaceMap = new HashMap<>();
/**
* Multiset to find correlates, that should be removed (in the right hand of correlate).
*/
private final Map<CorrelationId, Integer> removeMap = new HashMap<>();
/**
*/
private final RexShuttle rexShuttle = new RexShuttle() {
@Override
public RexNode visitCorrelVariable(RexCorrelVariable variable) {
CorrelationId newCorId = replaceMap.get(variable.id);
if (newCorId != null)
return cluster().getRexBuilder().makeCorrel(variable.getType(), newCorId);
else
return variable;
}
};
/**
* {@inheritDoc}
*/
@Override
public RelNode visit(LogicalCorrelate correlate) {
CorrelationId corId = correlate.getCorrelationId();
if (usedSet.contains(corId)) {
if (removeMap.containsKey(corId)) {
// We are in the right hand of correlate by corId: replace correlate with join.
RelNode join = LogicalJoin.create(correlate.getLeft(), correlate.getRight(), Collections.emptyList(), cluster().getRexBuilder().makeLiteral(true), Collections.emptySet(), correlate.getJoinType());
return super.visit(join);
} else {
// We are in the right hand of correlate by corId: replace correlate variable.
CorrelationId newCorId = cluster().createCorrel();
CorrelationId oldCorId = replaceMap.put(corId, newCorId);
try {
correlate = correlate.copy(correlate.getTraitSet(), correlate.getLeft(), correlate.getRight(), newCorId, correlate.getRequiredColumns(), correlate.getJoinType());
return visitLeftAndRightCorrelateHands(correlate, corId);
} finally {
if (oldCorId == null)
replaceMap.remove(corId);
else
replaceMap.put(corId, oldCorId);
}
}
} else {
usedSet.add(corId);
return visitLeftAndRightCorrelateHands(correlate, corId);
}
}
/**
*/
private RelNode visitLeftAndRightCorrelateHands(LogicalCorrelate correlate, CorrelationId corId) {
RelNode node = correlate;
node = visitChild(node, 0, correlate.getLeft());
removeMap.compute(corId, (k, v) -> v == null ? 1 : v + 1);
try {
node = visitChild(node, 1, correlate.getRight());
} finally {
removeMap.compute(corId, (k, v) -> v == 1 ? null : v - 1);
}
return node;
}
/**
* {@inheritDoc}
*/
@Override
public RelNode visit(RelNode other) {
RelNode next = super.visit(other);
return replaceMap.isEmpty() ? next : next.accept(rexShuttle);
}
};
return relShuttle.visit(rel);
}
use of org.apache.calcite.rel.core.CorrelationId in project ignite by apache.
the class IgniteCorrelatedNestedLoopJoin method passThroughCorrelation.
/**
* {@inheritDoc}
*/
@Override
public Pair<RelTraitSet, List<RelTraitSet>> passThroughCorrelation(RelTraitSet nodeTraits, List<RelTraitSet> inTraits) {
CorrelationTrait nodeCorr = TraitUtils.correlation(nodeTraits);
Set<CorrelationId> selfCorrIds = U.newHashSet(variablesSet.size() + nodeCorr.correlationIds().size());
selfCorrIds.addAll(variablesSet);
selfCorrIds.addAll(nodeCorr.correlationIds());
return Pair.of(nodeTraits, ImmutableList.of(inTraits.get(0).replace(nodeCorr), inTraits.get(1).replace(CorrelationTrait.correlations(selfCorrIds))));
}
Aggregations