org.apache.calcite.rex.RexSubQuery Java Examples

The following examples show how to use org.apache.calcite.rex.RexSubQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RexSubQueryUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode visitSubQuery(RexSubQuery subQuery) {
  RelNode transformed;
  try {
    transformed = PrelTransformer.transform(config, PlannerType.HEP_AC, PlannerPhase.JDBC_PUSHDOWN, subQuery.rel, traitSet, false);

    // We may need to run the planner again on the sub-queries in the sub-tree this produced.
    final RelsWithRexSubQueryTransformer nestedSubqueryTransformer = new RelsWithRexSubQueryTransformer(config);
    transformed = transformed.accept(nestedSubqueryTransformer);
    if (!(transformed instanceof JdbcCrel) || nestedSubqueryTransformer.failed()) {
      failed = true;
      return subQuery;
    }
  } catch (Throwable t) {
    failed = true;
    return subQuery;
  }
  return subQuery.clone(((JdbcCrel) transformed).getInput());
}
 
Example #2
Source File: SubQueryRemoveRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Rewrites an EXISTS RexSubQuery into a {@link Join}.
 *
 * @param e            EXISTS sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param logic        Logic for evaluating
 * @param builder      Builder
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteExists(RexSubQuery e, Set<CorrelationId> variablesSet, RelOptUtil.Logic logic,
        RelBuilder builder) {
    builder.push(e.getRel());

    builder.project(builder.alias(builder.literal(true), "i"));
    switch (logic) {
    case TRUE:
        // Handles queries with single EXISTS in filter condition:
        // select e.deptno from emp as e
        // where exists (select deptno from emp)
        builder.aggregate(builder.groupKey(0));
        builder.as("dt");
        builder.join(JoinRelType.INNER, builder.literal(true), variablesSet);
        return builder.literal(true);
    default:
        builder.distinct();
    }

    builder.as("dt");

    builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);

    return builder.isNotNull(Util.last(builder.fields()));
}
 
Example #3
Source File: SubQueryRemoveRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected RexNode apply(RexSubQuery e, Set<CorrelationId> variablesSet,
    RelOptUtil.Logic logic,
    RelBuilder builder, int inputCount, int offset) {
  switch (e.getKind()) {
  case SCALAR_QUERY:
    return rewriteScalarQuery(e, variablesSet, builder, inputCount, offset);
  case SOME:
    return rewriteSome(e, variablesSet, builder);
  case IN:
    return rewriteIn(e, variablesSet, logic, builder, offset);
  case EXISTS:
    return rewriteExists(e, variablesSet, logic, builder);
  default:
    throw new AssertionError(e.getKind());
  }
}
 
Example #4
Source File: SubQueryRemoveRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Rewrites an EXISTS RexSubQuery into a {@link Join}.
 *
 * @param e            EXISTS sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param logic        Logic for evaluating
 * @param builder      Builder
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteExists(RexSubQuery e, Set<CorrelationId> variablesSet,
    RelOptUtil.Logic logic, RelBuilder builder) {
  builder.push(e.rel);

  builder.project(builder.alias(builder.literal(true), "i"));
  switch (logic) {
  case TRUE:
    // Handles queries with single EXISTS in filter condition:
    // select e.deptno from emp as e
    // where exists (select deptno from emp)
    builder.aggregate(builder.groupKey(0));
    builder.as("dt");
    builder.join(JoinRelType.INNER, builder.literal(true), variablesSet);
    return builder.literal(true);
  default:
    builder.distinct();
  }

  builder.as("dt");

  builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);

  return builder.isNotNull(Util.last(builder.fields()));
}
 
Example #5
Source File: SubQueryRemoveRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected RexNode apply(RexSubQuery e, Set<CorrelationId> variablesSet, RelOptUtil.Logic logic, RelBuilder builder,
        int inputCount, int offset) {
    switch (e.getKind()) {
    case SCALAR_QUERY:
        return rewriteScalarQuery(e, variablesSet, builder, inputCount, offset);
    case SOME:
        return rewriteSome(e, builder);
    case IN:
        return rewriteIn(e, variablesSet, logic, builder, offset);
    case EXISTS:
        return rewriteExists(e, variablesSet, logic, builder);
    default:
        throw new AssertionError(e.getKind());
    }
}
 
Example #6
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> handleSubQuery(final RelNode rel) {
	return new RexVisitorImpl<Void>(true) {

		@Override
		public Void visitSubQuery(RexSubQuery subQuery) {
			RelNode newRel = subQuery.rel;
			if (subQuery.getKind() == SqlKind.IN) {
				newRel = addProjectionForIn(subQuery.rel);
			}
			final Frame frame = decorrelator.getInvoke(newRel);
			if (frame != null && frame.c != null) {

				Frame target = frame;
				if (subQuery.getKind() == SqlKind.EXISTS) {
					target = addProjectionForExists(frame);
				}

				final DecorrelateRexShuttle shuttle = new DecorrelateRexShuttle(
						rel.getRowType(),
						target.r.getRowType(),
						rel.getVariablesSet());

				final RexNode newCondition = target.c.accept(shuttle);
				Pair<RelNode, RexNode> newNodeAndCondition = new Pair<>(target.r, newCondition);
				subQueryMap.put(subQuery, newNodeAndCondition);
			}
			return null;
		}
	};
}
 
Example #7
Source File: RexSubQueryUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitSubQuery(RexSubQuery subQuery) {
  RexSubQueryUtils.RexSubQueryPushdownChecker checker = new RexSubQueryUtils.RexSubQueryPushdownChecker(pluginId);
  checker.visit(subQuery.rel);

  if (!checker.canPushdownRexSubQuery()) {
    canPushdownRexSubQuery = false;
  }

  // Begin validating against the plugin ID identified by the pushdown checker.
  if (checker.getPluginId() != null) {
    pluginId = checker.getPluginId();
  }
  return super.visitSubQuery(subQuery);
}
 
Example #8
Source File: SubQueryRemoveRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrites a scalar sub-query into an
 * {@link org.apache.calcite.rel.core.Aggregate}.
 *
 * @param e            IN sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param builder      Builder
 * @param offset       Offset to shift {@link RexInputRef}
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteScalarQuery(RexSubQuery e, Set<CorrelationId> variablesSet,
    RelBuilder builder, int inputCount, int offset) {
  builder.push(e.rel);
  final RelMetadataQuery mq = e.rel.getCluster().getMetadataQuery();
  final Boolean unique = mq.areColumnsUnique(builder.peek(),
      ImmutableBitSet.of());
  if (unique == null || !unique) {
    builder.aggregate(builder.groupKey(),
        builder.aggregateCall(SqlStdOperatorTable.SINGLE_VALUE,
            builder.field(0)));
  }
  builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);
  return field(builder, inputCount, offset);
}
 
Example #9
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> handleSubQuery(final RelNode rel) {
	return new RexVisitorImpl<Void>(true) {

		@Override
		public Void visitSubQuery(RexSubQuery subQuery) {
			RelNode newRel = subQuery.rel;
			if (subQuery.getKind() == SqlKind.IN) {
				newRel = addProjectionForIn(subQuery.rel);
			}
			final Frame frame = decorrelator.getInvoke(newRel);
			if (frame != null && frame.c != null) {

				Frame target = frame;
				if (subQuery.getKind() == SqlKind.EXISTS) {
					target = addProjectionForExists(frame);
				}

				final DecorrelateRexShuttle shuttle = new DecorrelateRexShuttle(
						rel.getRowType(),
						target.r.getRowType(),
						rel.getVariablesSet());

				final RexNode newCondition = target.c.accept(shuttle);
				Pair<RelNode, RexNode> newNodeAndCondition = new Pair<>(target.r, newCondition);
				subQueryMap.put(subQuery, newNodeAndCondition);
			}
			return null;
		}
	};
}
 
Example #10
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
  return new RexVisitorImpl<Void>(true) {
    @Override public Void visitFieldAccess(RexFieldAccess fieldAccess) {
      final RexNode ref = fieldAccess.getReferenceExpr();
      if (ref instanceof RexCorrelVariable) {
        final RexCorrelVariable var = (RexCorrelVariable) ref;
        if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
          // for cases where different Rel nodes are referring to
          // same correlation var (e.g. in case of NOT IN)
          // avoid generating another correlation var
          // and record the 'rel' is using the same correlation
          mapRefRelToCorRef.put(rel,
              mapFieldAccessToCorVar.get(fieldAccess));
        } else {
          final CorRef correlation =
              new CorRef(var.id, fieldAccess.getField().getIndex(),
                  corrIdGenerator++);
          mapFieldAccessToCorVar.put(fieldAccess, correlation);
          mapRefRelToCorRef.put(rel, correlation);
        }
      }
      return super.visitFieldAccess(fieldAccess);
    }

    @Override public Void visitSubQuery(RexSubQuery subQuery) {
      subQuery.rel.accept(CorelMapBuilder.this);
      return super.visitSubQuery(subQuery);
    }
  };
}
 
Example #11
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
  return new RexVisitorImpl<Void>(true) {
    @Override public Void visitFieldAccess(RexFieldAccess fieldAccess) {
      final RexNode ref = fieldAccess.getReferenceExpr();
      if (ref instanceof RexCorrelVariable) {
        final RexCorrelVariable var = (RexCorrelVariable) ref;
        if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
          // for cases where different Rel nodes are referring to
          // same correlation var (e.g. in case of NOT IN)
          // avoid generating another correlation var
          // and record the 'rel' is using the same correlation
          mapRefRelToCorRef.put(rel,
              mapFieldAccessToCorVar.get(fieldAccess));
        } else {
          final CorRef correlation =
              new CorRef(var.id, fieldAccess.getField().getIndex(),
                  corrIdGenerator++);
          mapFieldAccessToCorVar.put(fieldAccess, correlation);
          mapRefRelToCorRef.put(rel, correlation);
        }
      }
      return super.visitFieldAccess(fieldAccess);
    }

    @Override public Void visitSubQuery(RexSubQuery subQuery) {
      subQuery.rel.accept(CorelMapBuilder.this);
      return super.visitSubQuery(subQuery);
    }
  };
}
 
Example #12
Source File: CorrelationReferenceFinder.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitSubQuery(RexSubQuery subQuery) {
  final RelNode r = subQuery.rel.accept(finder); // look inside sub-queries
  if (r != subQuery.rel) {
    subQuery = subQuery.clone(r);
  }
  return super.visitSubQuery(subQuery);
}
 
Example #13
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitSubQuery(RexSubQuery subQuery) {
    if (relShuttle != null) {
        subQuery.getRel().accept(relShuttle); // look inside sub-queries
    }
    return super.visitSubQuery(subQuery);
}
 
Example #14
Source File: DeduplicateCorrelateVariables.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitSubQuery(RexSubQuery subQuery) {
    if (shuttle != null) {
        RelNode r = subQuery.getRel().accept(shuttle); // look inside sub-queries
        if (r != subQuery.getRel()) {
            subQuery = subQuery.clone(r);
        }
    }
    return super.visitSubQuery(subQuery);
}
 
Example #15
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
    return new RexVisitorImpl<Void>(true) {
        @Override
        public Void visitFieldAccess(RexFieldAccess fieldAccess) {
            final RexNode ref = fieldAccess.getReferenceExpr();
            if (ref instanceof RexCorrelVariable) {
                final RexCorrelVariable var = (RexCorrelVariable) ref;
                if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
                    // for cases where different Rel nodes are referring to
                    // same correlation var (e.g. in case of NOT IN)
                    // avoid generating another correlation var
                    // and record the 'rel' is using the same correlation
                    mapRefRelToCorRef.put(rel, mapFieldAccessToCorVar.get(fieldAccess));
                } else {
                    final CorRef correlation = new CorRef(var.getCorrelationId(),
                            fieldAccess.getField().getIndex(), corrIdGenerator++);
                    mapFieldAccessToCorVar.put(fieldAccess, correlation);
                    mapRefRelToCorRef.put(rel, correlation);
                }
            }
            return super.visitFieldAccess(fieldAccess);
        }

        @Override
        public Void visitSubQuery(RexSubQuery subQuery) {
            subQuery.getRel().accept(CorelMapBuilder.this);
            return super.visitSubQuery(subQuery);
        }
    };
}
 
Example #16
Source File: SubQueryRemoveRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrites a scalar sub-query into an
 * {@link org.apache.calcite.rel.core.Aggregate}.
 *
 * @param e            IN sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param builder      Builder
 * @param offset       Offset to shift {@link RexInputRef}
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteScalarQuery(RexSubQuery e, Set<CorrelationId> variablesSet, RelBuilder builder,
        int inputCount, int offset) {
    builder.push(e.getRel());
    final RelMetadataQuery mq = e.getRel().getCluster().getMetadataQuery();
    final Boolean unique = mq.areColumnsUnique(builder.peek(), ImmutableBitSet.of());
    if (unique == null || !unique) {
        builder.aggregate(builder.groupKey(),
                builder.aggregateCall(SqlStdOperatorTable.SINGLE_VALUE, builder.field(0)));
    }
    builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);
    return field(builder, inputCount, offset);
}
 
Example #17
Source File: CorrelationReferenceFinder.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitSubQuery(RexSubQuery subQuery) {
    final RelNode r = subQuery.getRel().accept(finder); // look inside sub-queries
    if (r != subQuery.getRel()) {
        subQuery = subQuery.clone(r);
    }
    return super.visitSubQuery(subQuery);
}
 
Example #18
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
  return new RexVisitorImpl<Void>(true) {
    @Override public Void visitFieldAccess(RexFieldAccess fieldAccess) {
      final RexNode ref = fieldAccess.getReferenceExpr();
      if (ref instanceof RexCorrelVariable) {
        final RexCorrelVariable var = (RexCorrelVariable) ref;
        if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
          // for cases where different Rel nodes are referring to
          // same correlation var (e.g. in case of NOT IN)
          // avoid generating another correlation var
          // and record the 'rel' is using the same correlation
          mapRefRelToCorRef.put(rel,
              mapFieldAccessToCorVar.get(fieldAccess));
        } else {
          final CorRef correlation =
              new CorRef(var.id, fieldAccess.getField().getIndex(),
                  corrIdGenerator++);
          mapFieldAccessToCorVar.put(fieldAccess, correlation);
          mapRefRelToCorRef.put(rel, correlation);
        }
      }
      return super.visitFieldAccess(fieldAccess);
    }

    @Override public Void visitSubQuery(RexSubQuery subQuery) {
      subQuery.rel.accept(CorelMapBuilder.this);
      return super.visitSubQuery(subQuery);
    }
  };
}
 
Example #19
Source File: DeduplicateCorrelateVariables.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitSubQuery(RexSubQuery subQuery) {
  if (shuttle != null) {
    RelNode r = subQuery.rel.accept(shuttle); // look inside sub-queries
    if (r != subQuery.rel) {
      subQuery = subQuery.clone(r);
    }
  }
  return super.visitSubQuery(subQuery);
}
 
Example #20
Source File: ORCFindRelevantFilters.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RexNode visitSubQuery(RexSubQuery subQuery) {
  return null;
}
 
Example #21
Source File: ORCSearchArgumentGenerator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitSubQuery(RexSubQuery subQuery) {
  throw new IllegalArgumentException("this shouldn't be part of the input expression: " + subQuery);
}
 
Example #22
Source File: RelOptUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public RexNode visitSubQuery(RexSubQuery subQuery) {
  if (relShuttle != null) {
    subQuery.rel.accept(relShuttle); // look inside sub-queries
  }
  return super.visitSubQuery(subQuery);
}
 
Example #23
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
	return new RexVisitorImpl<Void>(true) {
		@Override
		public Void visitSubQuery(RexSubQuery subQuery) {
			hasAggregateNode = false; // reset to default value
			hasOverNode = false; // reset to default value
			subQuery.rel.accept(CorelMapBuilder.this);
			return super.visitSubQuery(subQuery);
		}

		@Override
		public Void visitFieldAccess(RexFieldAccess fieldAccess) {
			final RexNode ref = fieldAccess.getReferenceExpr();
			if (ref instanceof RexCorrelVariable) {
				final RexCorrelVariable var = (RexCorrelVariable) ref;
				// check the scope of correlation id
				// we do not support nested correlation variables in SubQuery, such as:
				// select * from t1 where exists (select * from t2 where t1.a = t2.c and
				// t2.d in (select t3.d from t3 where t1.b = t3.e)
				if (!hasUnsupportedCorCondition) {
					hasUnsupportedCorCondition = !mapSubQueryNodeToCorSet.containsKey(rel);
				}
				if (!hasNestedCorScope && mapSubQueryNodeToCorSet.containsKey(rel)) {
					hasNestedCorScope = !mapSubQueryNodeToCorSet.get(rel).contains(var.id);
				}

				if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
					// for cases where different Rel nodes are referring to
					// same correlation var (e.g. in case of NOT IN)
					// avoid generating another correlation var
					// and record the 'rel' is using the same correlation
					mapRefRelToCorRef.put(rel, mapFieldAccessToCorVar.get(fieldAccess));
				} else {
					final CorRef correlation = new CorRef(
							var.id,
							fieldAccess.getField().getIndex(),
							corrIdGenerator++);
					mapFieldAccessToCorVar.put(fieldAccess, correlation);
					mapRefRelToCorRef.put(rel, correlation);
				}
			}
			return super.visitFieldAccess(fieldAccess);
		}
	};
}
 
Example #24
Source File: ReduceExpressionsRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public Void visitSubQuery(RexSubQuery subQuery) {
  analyzeCall(subQuery, Constancy.REDUCIBLE_CONSTANT);
  return null;
}
 
Example #25
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
private Result(Map<RexSubQuery, Pair<RelNode, RexNode>> subQueryMap) {
	this.subQueryMap = com.google.common.collect.ImmutableMap.copyOf(subQueryMap);
}
 
Example #26
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
public Pair<RelNode, RexNode> getSubQueryEquivalent(RexSubQuery subQuery) {
	return subQueryMap.get(subQuery);
}
 
Example #27
Source File: ReduceExpressionsRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public Void visitSubQuery(RexSubQuery subQuery) {
  analyzeCall(subQuery, Constancy.REDUCIBLE_CONSTANT);
  return null;
}
 
Example #28
Source File: SubQueryRemoveRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
ReplaceSubQueryShuttle(RexSubQuery subQuery, RexNode replacement) {
  this.subQuery = subQuery;
  this.replacement = replacement;
}
 
Example #29
Source File: SubQueryRemoveRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public RexNode visitSubQuery(RexSubQuery subQuery) {
  return subQuery.equals(this.subQuery) ? replacement : subQuery;
}
 
Example #30
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public Result visitSubQuery(RexSubQuery subQuery) {
  throw new RuntimeException("cannot translate expression " + subQuery);
}