org.apache.calcite.rel.logical.LogicalFilter Java Examples

The following examples show how to use org.apache.calcite.rel.logical.LogicalFilter. 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: FlinkSemiAntiJoinFilterTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
	LogicalJoin join = call.rel(0);
	LogicalFilter filter = call.rel(1);

	RelNode newJoin = LogicalJoin.create(
			filter.getInput(),
			join.getRight(),
			join.getCondition(),
			join.getVariablesSet(),
			join.getJoinType());

	final RelFactories.FilterFactory factory =
			RelFactories.DEFAULT_FILTER_FACTORY;
	RelNode newFilter =
			factory.createFilter(newJoin, filter.getCondition());

	call.transformTo(newFilter);
}
 
Example #2
Source File: IncrementalUpdateUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(TableScan tableScan) {
  if (!(tableScan instanceof IncrementallyUpdateable)) {
    return tableScan;
  }

  final RelNode newScan = updateScan((IncrementallyUpdateable) tableScan);

  // build new filter to apply refresh condition.
  final RexBuilder rexBuilder = tableScan.getCluster().getRexBuilder();
  RelDataTypeField field = newScan.getRowType().getField(UPDATE_COLUMN, false, false);
  final RexNode inputRef = rexBuilder.makeInputRef(newScan, field.getIndex());
  final Optional<RexNode> literal = generateLiteral(rexBuilder, tableScan.getCluster().getTypeFactory(), field.getType().getSqlTypeName());
  if (literal.isPresent()) {
    RexNode condition = tableScan.getCluster().getRexBuilder().makeCall(SqlStdOperatorTable.GREATER_THAN, ImmutableList.of(inputRef, literal.get()));
    return LogicalFilter.create(newScan, condition);
  }
  return newScan;
}
 
Example #3
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalFilter filter) {
	final boolean hasSubQuery = RexUtil.SubQueryFinder.find(filter.getCondition()) != null;
	try {
		if (!corNodeStack.isEmpty()) {
			mapSubQueryNodeToCorSet.put(filter, corNodeStack.peek().getVariablesSet());
		}
		if (hasSubQuery) {
			corNodeStack.push(filter);
		}
		checkCorCondition(filter);
		filter.getCondition().accept(rexVisitor(filter));
		for (CorrelationId correlationId : filter.getVariablesSet()) {
			mapCorToCorRel.put(correlationId, filter);
		}
	} finally {
		if (hasSubQuery) {
			corNodeStack.pop();
		}
	}
	return super.visit(filter);
}
 
Example #4
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelNode toRel(ToRelContext context) {
  RelNode rel = LogicalTableScan.create(context.getCluster(), fromTable,
      context.getTableHints());
  final RexBuilder rexBuilder = context.getCluster().getRexBuilder();
  rel = LogicalFilter.create(
      rel, getConstraint(rexBuilder, rel.getRowType()));
  final List<RelDataTypeField> fieldList =
      rel.getRowType().getFieldList();
  final List<Pair<RexNode, String>> projects =
      new AbstractList<Pair<RexNode, String>>() {
        @Override public Pair<RexNode, String> get(int index) {
          return RexInputRef.of2(mapping.get(index), fieldList);
        }

        @Override public int size() {
          return mapping.size();
        }
      };
  return LogicalProject.create(rel,
      ImmutableList.of(),
      Pair.left(projects),
      Pair.right(projects));
}
 
Example #5
Source File: FilterMultiJoinMergeRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalFilter filter = call.rel(0);
  MultiJoin multiJoin = call.rel(1);

  // Create a new post-join filter condition
  // Conditions are nullable, so ImmutableList can't be used here
  List<RexNode> filters = Arrays.asList(
      filter.getCondition(),
      multiJoin.getPostJoinFilter());

  final RexBuilder rexBuilder = multiJoin.getCluster().getRexBuilder();
  MultiJoin newMultiJoin =
      new MultiJoin(
          multiJoin.getCluster(),
          multiJoin.getInputs(),
          multiJoin.getJoinFilter(),
          multiJoin.getRowType(),
          multiJoin.isFullOuterJoin(),
          multiJoin.getOuterJoinConditions(),
          multiJoin.getJoinTypes(),
          multiJoin.getProjFields(),
          multiJoin.getJoinFieldRefCountsMap(),
          RexUtil.composeConjunction(rexBuilder, filters, true));

  call.transformTo(newMultiJoin);
}
 
Example #6
Source File: FilterTranslator.java    From samza with Apache License 2.0 6 votes vote down vote up
@Override
public void init(Context context) {
  this.context = context;
  this.translatorContext = ((SamzaSqlApplicationContext) context.getApplicationTaskContext()).getTranslatorContexts().get(queryId);
  this.filter = (LogicalFilter) this.translatorContext.getRelNode(filterId);
  this.expr = this.translatorContext.getExpressionCompiler().compile(filter.getInputs(), Collections.singletonList(filter.getCondition()));
  ContainerContext containerContext = context.getContainerContext();
  metricsRegistry = containerContext.getContainerMetricsRegistry();
  processingTime = new SamzaHistogram(metricsRegistry, logicalOpId, TranslatorConstants.PROCESSING_TIME_NAME);
  inputEvents = metricsRegistry.newCounter(logicalOpId, TranslatorConstants.INPUT_EVENTS_NAME);
  inputEvents.clear();
  filteredOutEvents = metricsRegistry.newCounter(logicalOpId, TranslatorConstants.FILTERED_EVENTS_NAME);
  filteredOutEvents.clear();
  outputEvents = metricsRegistry.newCounter(logicalOpId, TranslatorConstants.OUTPUT_EVENTS_NAME);
  outputEvents.clear();
}
 
Example #7
Source File: SemiJoinFilterTransposeRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  SemiJoin semiJoin = call.rel(0);
  LogicalFilter filter = call.rel(1);

  RelNode newSemiJoin =
      SemiJoin.create(filter.getInput(),
          semiJoin.getRight(),
          semiJoin.getCondition(),
          semiJoin.getLeftKeys(),
          semiJoin.getRightKeys());

  final RelFactories.FilterFactory factory =
      RelFactories.DEFAULT_FILTER_FACTORY;
  RelNode newFilter =
      factory.createFilter(newSemiJoin, filter.getCondition());

  call.transformTo(newFilter);
}
 
Example #8
Source File: IncrementalUpdateUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(final LogicalFilter filter) {
  final RelBuilder relBuilder = newCalciteRelBuilderWithoutContext(filter.getCluster());
  RelNode input = filter.getInput().accept(this);
  relBuilder.push(input);

  RexNode newCondition = filter.getCondition().accept(new RexShuttle() {
    @Override
    public RexNode visitInputRef(RexInputRef inputRef) {
      return relBuilder.field(filter.getRowType().getFieldNames().get(inputRef.getIndex()));
    }
  });

  relBuilder.filter(newCondition);
  return relBuilder.build();
}
 
Example #9
Source File: JoinTranslator.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to check if the join condition can be evaluated by the remote table.
 * It does follow single path  using the index ref path checking if it is a simple reference all the way to table scan.
 * In case any RexCall is encountered will stop an return null as a marker otherwise will return Column Name.
 *
 * @param inputRexIndex rex ref index
 * @param relNode current Rel Node
 * @return false if any Relational Expression is encountered on the path, true if is simple ref to __key__ column.
 */
private static boolean isValidRemoteJoinRef(int inputRexIndex, RelNode relNode) {
  if (relNode instanceof TableScan) {
    return relNode.getRowType().getFieldList().get(inputRexIndex).getName().equals(SamzaSqlRelMessage.KEY_NAME);
  }
  // has to be a single rel kind filter/project/table scan
  Preconditions.checkState(relNode.getInputs().size() == 1,
      "Has to be single input RelNode and got " + relNode.getDigest());
  if (relNode instanceof LogicalFilter) {
    return isValidRemoteJoinRef(inputRexIndex, relNode.getInput(0));
  }
  RexNode inputRef = ((LogicalProject) relNode).getProjects().get(inputRexIndex);
  if (inputRef instanceof RexCall) {
    return false; // we can not push any expression as of now stop and return null.
  }
  return isValidRemoteJoinRef(((RexInputRef) inputRef).getIndex(), relNode.getInput(0));
}
 
Example #10
Source File: ReduceTrigFunctionsRule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  LogicalFilter filter = (LogicalFilter) call.rels[0];
  RexNode newCondition = RexRewriter.rewrite(filter.getCondition(), getRules(filter.getCluster().getRexBuilder()));
  if (newCondition != filter.getCondition()) {
    call.transformTo(LogicalFilter.create(filter.getInput(), newCondition));
  }
}
 
Example #11
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(final LogicalFilter filter) {
	try {
		stack.push(filter);
		filter.getCondition().accept(handleSubQuery(filter));
	} finally {
		stack.pop();
	}
	return super.visit(filter);
}
 
Example #12
Source File: StreamRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Filter filter = call.rel(1);
  final LogicalDelta newDelta = LogicalDelta.create(filter.getInput());
  final LogicalFilter newFilter =
      LogicalFilter.create(newDelta, filter.getCondition());
  call.transformTo(newFilter);
}
 
Example #13
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testBrokenCustomProviderWithMetadataQuery() {
  final List<String> buf = new ArrayList<>();
  ColTypeImpl.THREAD_LIST.set(buf);

  final String sql = "select deptno, count(*) from emp where deptno > 10 "
      + "group by deptno having count(*) = 0";
  final RelRoot root = tester
      .withClusterFactory(cluster -> {
        cluster.setMetadataProvider(
            ChainedRelMetadataProvider.of(
                ImmutableList.of(BrokenColTypeImpl.SOURCE,
                    cluster.getMetadataProvider())));
        cluster.setMetadataQuerySupplier(MyRelMetadataQuery::new);
        return cluster;
      })
      .convertSqlToRel(sql);

  final RelNode rel = root.rel;
  assertThat(rel, instanceOf(LogicalFilter.class));
  assertThat(rel.getCluster().getMetadataQuery(), instanceOf(MyRelMetadataQuery.class));
  final MyRelMetadataQuery mq = (MyRelMetadataQuery) rel.getCluster().getMetadataQuery();

  try {
    assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
    fail("expected error");
  } catch (IllegalArgumentException e) {
    final String value = "No handler for method [public abstract java.lang.String "
        + "org.apache.calcite.test.RelMetadataTest$ColType.getColType(int)] "
        + "applied to argument of type [interface org.apache.calcite.rel.RelNode]; "
        + "we recommend you create a catch-all (RelNode) handler";
    assertThat(e.getMessage(), is(value));
  }
}
 
Example #14
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RexNode splitCorrelatedFilterCondition(
    LogicalFilter filter,
    List<RexNode> joinKeys,
    List<RexNode> correlatedJoinKeys,
    boolean extractCorrelatedFieldAccess) {
  return splitCorrelatedFilterCondition(
      (Filter) filter,
      joinKeys,
      correlatedJoinKeys,
      extractCorrelatedFieldAccess);
}
 
Example #15
Source File: GeodeRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalFilter filter = call.rel(0);
  GeodeTableScan scan = call.rel(1);
  if (filter.getTraitSet().contains(Convention.NONE)) {
    final RelNode converted = convert(filter, scan);
    call.transformTo(converted);
  }
}
 
Example #16
Source File: StreamRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Filter filter = call.rel(1);
  final LogicalDelta newDelta = LogicalDelta.create(filter.getInput());
  final LogicalFilter newFilter =
      LogicalFilter.create(newDelta, filter.getCondition());
  call.transformTo(newFilter);
}
 
Example #17
Source File: SplunkPushDownRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected void transformToFarragoUdxRel(
      RelOptRuleCall call,
      SplunkTableScan splunkRel,
      LogicalFilter filter,
      LogicalProject topProj,
      LogicalProject bottomProj) {
    assert false;
/*
    RelNode rel =
        new EnumerableTableScan(
            udxRel.getCluster(),
            udxRel.getTable(),
            udxRel.getRowType(),
            udxRel.getServerMofId());

    rel = RelOptUtil.createCastRel(rel, udxRel.getRowType(), true);

    rel = addProjectionRule(bottomProj, rel);

    if (filter != null) {
      rel =
          new LogicalFilter(filter.getCluster(), rel, filter.getCondition());
    }

    rel = addProjectionRule(topProj, rel);

    call.transformTo(rel);
*/
  }
 
Example #18
Source File: PreProcessRel.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalFilter filter) {
  final RexNode condition = filter.getCondition().accept(unwrappingExpressionVisitor);
  filter = filter.copy(
      filter.getTraitSet(),
      filter.getInput(),
      condition);
  return visitChild(filter, 0, filter.getInput());
}
 
Example #19
Source File: GremlinRules.java    From sql-gremlin with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
    final LogicalFilter filter = (LogicalFilter) rel;
    final RelTraitSet traitSet = filter.getTraitSet().replace(out);
    return new GremlinFilter(
            rel.getCluster(),
            traitSet,
            convert(filter.getInput(), out),
            filter.getCondition());
}
 
Example #20
Source File: JoinTranslator.java    From samza with Apache License 2.0 5 votes vote down vote up
private SqlIOConfig resolveSQlIOForTable(RelNode relNode, TranslatorContext context) {
  // Let's recursively get to the TableScan node to identify IO for the table.
  if (relNode instanceof LogicalProject) {
    return resolveSQlIOForTable(((LogicalProject) relNode).getInput(), context);
  }

  if (relNode instanceof LogicalFilter) {
    return resolveSQlIOForTable(((LogicalFilter) relNode).getInput(), context);
  }

  // We return null for table IO as the table seems to be involved in another join. The output of stream-table join
  // is considered a stream. Hence, we return null for the table.
  if (relNode instanceof LogicalJoin && relNode.getInputs().size() > 1) {
    return null;
  }

  if (!(relNode instanceof TableScan)) {
    throw new SamzaException(String.format("Unsupported query. relNode %s is not of type TableScan.",
        relNode.toString()));
  }

  String sourceName = SqlIOConfig.getSourceFromSourceParts(relNode.getTable().getQualifiedName());
  SqlIOConfig sourceConfig =
      context.getExecutionContext().getSamzaSqlApplicationConfig().getInputSystemStreamConfigBySource().get(sourceName);
  if (sourceConfig == null) {
    throw new SamzaException("Unsupported source found in join statement: " + sourceName);
  }
  return sourceConfig;
}
 
Example #21
Source File: FilterTableFunctionTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FilterTableFunctionTransposeRule.
 */
public FilterTableFunctionTransposeRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(LogicalFilter.class,
          operand(LogicalTableFunctionScan.class, any())),
      relBuilderFactory, null);
}
 
Example #22
Source File: FilterTranslator.java    From samza with Apache License 2.0 5 votes vote down vote up
void translate(final LogicalFilter filter, final String logicalOpId, final TranslatorContext context) {
  MessageStream<SamzaSqlRelMessage> inputStream = context.getMessageStream(filter.getInput().getId());
  final int filterId = filter.getId();

  MessageStream<SamzaSqlRelMessage> outputStream = inputStream.filter(new FilterTranslatorFunction(filterId, queryId, logicalOpId));

  context.registerMessageStream(filterId, outputStream);
  context.registerRelNode(filterId, filter);
}
 
Example #23
Source File: Bindables.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a BindableFilterRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public BindableFilterRule(RelBuilderFactory relBuilderFactory) {
  super(LogicalFilter.class,
      (Predicate<LogicalFilter>) RelOptUtil::notContainsWindowedAgg,
      Convention.NONE, BindableConvention.INSTANCE, relBuilderFactory,
      "BindableFilterRule");
}
 
Example #24
Source File: ElasticsearchFilterRule.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode relNode) {
    final LogicalFilter filter = (LogicalFilter) relNode;
    final RelTraitSet traitSet = filter.getTraitSet().replace(getOutTrait());
    return new ElasticsearchFilter(relNode.getCluster(), traitSet,
            convert(filter.getInput(), getOutTrait()),
            filter.getCondition());
}
 
Example #25
Source File: ElasticsearchFilterRule.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode relNode) {
    final LogicalFilter filter = (LogicalFilter) relNode;
    final RelTraitSet traitSet = filter.getTraitSet().replace(getOutTrait());
    return new ElasticsearchFilter(relNode.getCluster(), traitSet,
            convert(filter.getInput(), getOutTrait()),
            filter.getCondition());
}
 
Example #26
Source File: CassandraRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(LogicalFilter filter, CassandraTableScan scan) {
  final RelTraitSet traitSet = filter.getTraitSet().replace(CassandraRel.CONVENTION);
  final Pair<List<String>, List<String>> keyFields = scan.cassandraTable.getKeyFields();
  return new CassandraFilter(
      filter.getCluster(),
      traitSet,
      convert(filter.getInput(), CassandraRel.CONVENTION),
      filter.getCondition(),
      keyFields.left,
      keyFields.right,
      scan.cassandraTable.getClusteringOrder());
}
 
Example #27
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * check whether a node has some input which have correlation condition.
 * e.g. SELECT * FROM l WHERE EXISTS (SELECT * FROM r LEFT JOIN (SELECT * FROM t WHERE t.j=l.b) t1 ON r.f=t1.k)
 * the above sql can not be converted to semi-join plan,
 * because the right input of Left-Join has the correlation condition(t.j=l.b).
 */
private void checkCorConditionOfInput(final RelNode input) {
	final RelShuttleImpl shuttle = new RelShuttleImpl() {
		final RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
			@Override
			public Void visitCorrelVariable(RexCorrelVariable correlVariable) {
				hasUnsupportedCorCondition = true;
				return super.visitCorrelVariable(correlVariable);
			}
		};

		@Override
		public RelNode visit(LogicalFilter filter) {
			filter.getCondition().accept(visitor);
			return super.visit(filter);
		}

		@Override
		public RelNode visit(LogicalProject project) {
			for (RexNode rex : project.getProjects()) {
				rex.accept(visitor);
			}
			return super.visit(project);
		}

		@Override
		public RelNode visit(LogicalJoin join) {
			join.getCondition().accept(visitor);
			return super.visit(join);
		}
	};
	input.accept(shuttle);
}
 
Example #28
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(final LogicalFilter filter) {
	try {
		stack.push(filter);
		filter.getCondition().accept(handleSubQuery(filter));
	} finally {
		stack.pop();
	}
	return super.visit(filter);
}
 
Example #29
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RelNode visit(final LogicalFilter filter) {
  try {
    stack.push(filter);
    filter.getCondition().accept(rexVisitor(filter));
  } finally {
    stack.pop();
  }
  return super.visit(filter);
}
 
Example #30
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * check whether a node has some input which have correlation condition.
 * e.g. SELECT * FROM l WHERE EXISTS (SELECT * FROM r LEFT JOIN (SELECT * FROM t WHERE t.j=l.b) t1 ON r.f=t1.k)
 * the above sql can not be converted to semi-join plan,
 * because the right input of Left-Join has the correlation condition(t.j=l.b).
 */
private void checkCorConditionOfInput(final RelNode input) {
	final RelShuttleImpl shuttle = new RelShuttleImpl() {
		final RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
			@Override
			public Void visitCorrelVariable(RexCorrelVariable correlVariable) {
				hasUnsupportedCorCondition = true;
				return super.visitCorrelVariable(correlVariable);
			}
		};

		@Override
		public RelNode visit(LogicalFilter filter) {
			filter.getCondition().accept(visitor);
			return super.visit(filter);
		}

		@Override
		public RelNode visit(LogicalProject project) {
			for (RexNode rex : project.getProjects()) {
				rex.accept(visitor);
			}
			return super.visit(project);
		}

		@Override
		public RelNode visit(LogicalJoin join) {
			join.getCondition().accept(visitor);
			return super.visit(join);
		}
	};
	input.accept(shuttle);
}