org.apache.calcite.rel.core.TableScan Java Examples

The following examples show how to use org.apache.calcite.rel.core.TableScan. 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: Lattice.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static boolean populate(List<RelNode> nodes, List<int[][]> tempLinks,
    RelNode rel) {
  if (nodes.isEmpty() && rel instanceof LogicalProject) {
    return populate(nodes, tempLinks, ((LogicalProject) rel).getInput());
  }
  if (rel instanceof TableScan) {
    nodes.add(rel);
    return true;
  }
  if (rel instanceof LogicalJoin) {
    LogicalJoin join = (LogicalJoin) rel;
    if (join.getJoinType() != JoinRelType.INNER) {
      throw new RuntimeException("only inner join allowed, but got "
          + join.getJoinType());
    }
    populate(nodes, tempLinks, join.getLeft());
    populate(nodes, tempLinks, join.getRight());
    for (RexNode rex : RelOptUtil.conjunctions(join.getCondition())) {
      tempLinks.add(grab(nodes, rex));
    }
    return true;
  }
  throw new RuntimeException("Invalid node type "
      + rel.getClass().getSimpleName() + " in lattice query");
}
 
Example #2
Source File: Lattice.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static boolean populate(List<RelNode> nodes, List<int[][]> tempLinks,
    RelNode rel) {
  if (nodes.isEmpty() && rel instanceof LogicalProject) {
    return populate(nodes, tempLinks, ((LogicalProject) rel).getInput());
  }
  if (rel instanceof TableScan) {
    nodes.add(rel);
    return true;
  }
  if (rel instanceof LogicalJoin) {
    LogicalJoin join = (LogicalJoin) rel;
    if (join.getJoinType().isOuterJoin()) {
      throw new RuntimeException("only non nulls-generating join allowed, but got "
          + join.getJoinType());
    }
    populate(nodes, tempLinks, join.getLeft());
    populate(nodes, tempLinks, join.getRight());
    for (RexNode rex : RelOptUtil.conjunctions(join.getCondition())) {
      tempLinks.add(grab(nodes, rex));
    }
    return true;
  }
  throw new RuntimeException("Invalid node type "
      + rel.getClass().getSimpleName() + " in lattice query");
}
 
Example #3
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 #4
Source File: ViewTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelRoot expandView(RelOptTable.ToRelContext context,
    RelDataType rowType, String queryString) {
  try {
    final RelRoot root =
        context.expandView(rowType, queryString, schemaPath, viewPath);
    final RelNode rel = RelOptUtil.createCastRel(root.rel, rowType, true);
    // Expand any views
    final RelNode rel2 = rel.accept(
        new RelShuttleImpl() {
          @Override public RelNode visit(TableScan scan) {
            final RelOptTable table = scan.getTable();
            final TranslatableTable translatableTable =
                table.unwrap(TranslatableTable.class);
            if (translatableTable != null) {
              return translatableTable.toRel(context, table);
            }
            return super.visit(scan);
          }
        });
    return root.withRel(rel2);
  } catch (Exception e) {
    throw new RuntimeException("Error while parsing view definition: "
        + queryString, e);
  }
}
 
Example #5
Source File: GroupByPartitionRule.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public GroupByPartitionRule() {
    super(operandJ(Aggregate.class, null, GroupByPartitionRule::test,
            operandJ(Union.class, null, r -> !r.isDistinct(),
                    operandJ(TableScan.class, null, (Predicate<TableScan>) input -> {
                        if (input != null) {
                            RelOptTable table = input.getTable();
                            if (table != null) {
                                MycatPhysicalTable table1 = table.unwrap(MycatPhysicalTable.class);
                                MycatLogicTable logicTable = table1.getLogicTable();
                                TableHandler tableHandler = logicTable.getTable();
                                if (tableHandler instanceof ShardingTable) {
                                    ShardingTable handler = (ShardingTable) tableHandler;
                                    return logicTable.getDataNodes().size() > 1;
                                }
                                return false;
                            }
                        }
                        return false;
                    }, none()))));
}
 
Example #6
Source File: TableScanNode.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static TableScanNode createFilterable(Compiler compiler,
    TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects,
    FilterableTable filterableTable) {
  final DataContext root = compiler.getDataContext();
  final List<RexNode> mutableFilters = Lists.newArrayList(filters);
  final Enumerable<Object[]> enumerable =
      filterableTable.scan(root, mutableFilters);
  for (RexNode filter : mutableFilters) {
    if (!filters.contains(filter)) {
      throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
    }
  }
  final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable);
  return createEnumerable(compiler, rel, rowEnumerable, null,
      mutableFilters, projects);
}
 
Example #7
Source File: JoinTranslator.java    From samza with Apache License 2.0 6 votes vote down vote up
private JoinInputNode.InputType getInputType(RelNode relNode, TranslatorContext context) {

    // NOTE: Any intermediate form of a join is always a stream. Eg: For the second level join of
    // stream-table-table join, the left side of the join is join output, which we always
    // assume to be a stream. The intermediate stream won't be an instance of TableScan.
    // The join key(s) for the table could be an udf in which case the relNode would be LogicalProject.

    if (relNode instanceof TableScan || relNode instanceof LogicalProject) {
      SqlIOConfig sourceTableConfig = resolveSQlIOForTable(relNode, context);
      if (sourceTableConfig == null || !sourceTableConfig.getTableDescriptor().isPresent()) {
        return JoinInputNode.InputType.STREAM;
      } else if (sourceTableConfig.getTableDescriptor().get() instanceof RemoteTableDescriptor ||
          sourceTableConfig.getTableDescriptor().get() instanceof CachingTableDescriptor) {
        return JoinInputNode.InputType.REMOTE_TABLE;
      } else {
        return JoinInputNode.InputType.LOCAL_TABLE;
      }
    } else {
      return JoinInputNode.InputType.STREAM;
    }
  }
 
Example #8
Source File: RelMdExpressionLineage.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Expression lineage from {@link TableScan}.
 *
 * <p>We extract the fields referenced by the expression and we express them
 * using {@link RexTableInputRef}.
 */
public Set<RexNode> getExpressionLineage(TableScan rel,
    RelMetadataQuery mq, RexNode outputExpression) {
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();

  // Extract input fields referenced by expression
  final ImmutableBitSet inputFieldsUsed = extractInputRefs(outputExpression);

  // Infer column origin expressions for given references
  final Map<RexInputRef, Set<RexNode>> mapping = new LinkedHashMap<>();
  for (int idx : inputFieldsUsed) {
    final RexNode inputRef = RexTableInputRef.of(
        RelTableRef.of(rel.getTable(), 0),
        RexInputRef.of(idx, rel.getRowType().getFieldList()));
    final RexInputRef ref = RexInputRef.of(idx, rel.getRowType().getFieldList());
    mapping.put(ref, ImmutableSet.of(inputRef));
  }

  // Return result
  return createAllPossibleExpressions(rexBuilder, outputExpression, mapping);
}
 
Example #9
Source File: QueryMetadata.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static List<List<String>> getScans(RelNode logicalPlan) {
  final ImmutableList.Builder<List<String>> builder = ImmutableList.builder();
  logicalPlan.accept(new StatelessRelShuttleImpl() {
    @Override
    public RelNode visit(final TableScan scan) {
      builder.add(scan.getTable().getQualifiedName());
      return super.visit(scan);
    }

    @Override
    public RelNode visit(RelNode other) {
      if (other instanceof ContainerRel) {
        ContainerRel containerRel = (ContainerRel)other;
        containerRel.getSubTree().accept(this);
      }
      return super.visit(other);
    }
  });
  return builder.build();
}
 
Example #10
Source File: PruneScanRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
  Aggregate aggregate = call.rel(0);
  TableScan scan = call.rel(1);

  if (!isQualifiedFilePruning(scan)
      || scan.getRowType().getFieldCount() != aggregate.getRowType().getFieldCount()) {
    return false;
  }

  List<String> fieldNames = scan.getRowType().getFieldNames();
  // Check if select contains partition columns (dir0, dir1, dir2,..., dirN) only
  for (String field : fieldNames) {
    if (!dirPattern.matcher(field).matches()) {
      return false;
    }
  }

  return scan.isDistinct() || aggregate.getGroupCount() > 0;
}
 
Example #11
Source File: ParquetPartitionDescriptor.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public TableScan createTableScan(List<PartitionLocation> newPartitionLocation,
                                 Path cacheFileRoot,
                                 boolean wasAllPartitionsPruned,
                                 MetadataContext metaContext) throws Exception {
  List<Path> newFiles = new ArrayList<>();
  for (final PartitionLocation location : newPartitionLocation) {
    newFiles.add(location.getEntirePartitionLocation());
  }

  final GroupScan newGroupScan = createNewGroupScan(newFiles, cacheFileRoot, wasAllPartitionsPruned, metaContext);

  if (newGroupScan == null) {
    logger.warn("Unable to create new group scan, returning original table scan.");
    return scanRel;
  }

  return new DrillScanRel(scanRel.getCluster(),
      scanRel.getTraitSet().plus(DrillRel.DRILL_LOGICAL),
      scanRel.getTable(),
      newGroupScan,
      scanRel.getRowType(),
      scanRel.getColumns(),
      true /*filter pushdown*/);
}
 
Example #12
Source File: RelMdColumnUniqueness.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Boolean areColumnsUnique(RelSubset rel, RelMetadataQuery mq,
    ImmutableBitSet columns, boolean ignoreNulls) {
  int nullCount = 0;
  for (RelNode rel2 : rel.getRels()) {
    if (rel2 instanceof Aggregate
        || rel2 instanceof Filter
        || rel2 instanceof Values
        || rel2 instanceof TableScan
        || simplyProjects(rel2, columns)) {
      try {
        final Boolean unique = mq.areColumnsUnique(rel2, columns, ignoreNulls);
        if (unique != null) {
          if (unique) {
            return true;
          }
        } else {
          ++nullCount;
        }
      } catch (CyclicMetadataException e) {
        // Ignore this relational expression; there will be non-cyclic ones
        // in this set.
      }
    }
  }
  return nullCount == 0 ? false : null;
}
 
Example #13
Source File: FileSystemPartitionDescriptor.java    From Bats with Apache License 2.0 6 votes vote down vote up
public FileSystemPartitionDescriptor(PlannerSettings settings, TableScan scanRel) {
  Preconditions.checkArgument(scanRel instanceof DrillScanRel || scanRel instanceof EnumerableTableScan);
  this.partitionLabel = settings.getFsPartitionColumnLabel();
  this.partitionLabelLength = partitionLabel.length();
  this.scanRel = scanRel;
  DrillTable unwrap;
  unwrap = scanRel.getTable().unwrap(DrillTable.class);
  if (unwrap == null) {
    unwrap = scanRel.getTable().unwrap(DrillTranslatableTable.class).getDrillTable();
  }

  table = unwrap;

  for(int i =0; i < 10; i++){
    partitions.put(partitionLabel + i, i);
  }
}
 
Example #14
Source File: PruneScanRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static boolean isQualifiedDirPruning(final TableScan scan) {
  if (scan instanceof EnumerableTableScan) {
    final Object selection = DrillRelOptUtil.getDrillTable(scan).getSelection();
    if (selection instanceof FormatSelection
        && ((FormatSelection)selection).supportDirPruning()) {
      return true;  // Do directory-based pruning in Calcite logical
    } else {
      return false; // Do not do directory-based pruning in Calcite logical
    }
  } else if (scan instanceof DrillScanRel) {
    final GroupScan groupScan = ((DrillScanRel) scan).getGroupScan();
    // this rule is applicable only for dfs based partition pruning in Drill Logical
    return groupScan instanceof FileGroupScan && groupScan.supportsPartitionFilterPushdown() && !((DrillScanRel)scan).partitionFilterPushdown();
  }
  return false;
}
 
Example #15
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 #16
Source File: SubstitutionUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether {@code table} uses one or more of the tables in
 * {@code usedTables}.
 */
public static boolean usesTable(final Set<List<String>> tables, final RelNode rel) {
  final Pointer<Boolean> used = new Pointer<>(false);
  rel.accept(new RoutingShuttle() {
    @Override
    public RelNode visit(TableScan scan) {
      if (tables.contains(scan.getTable().getQualifiedName())) {
        used.value = true;
      }
      return scan;
    }
    @Override
    public RelNode visit(RelNode other) {
      if (used.value) {
        return other;
      }
      return super.visit(other);
    }
  });
  return used.value;
}
 
Example #17
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountFilter() {
  final String sql = "select * from emp where ename='Mathilda'";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 1);
  expected.put(Project.class, 1);
  expected.put(Filter.class, 1);
  checkNodeTypeCount(sql, expected);
}
 
Example #18
Source File: ApexRelNode.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public RelInfo visit(RelContext context, RelNode node, List<RelInfo> inputStreams)
{
  TableScan scan = (TableScan)node;
  ApexSQLTable table = scan.getTable().unwrap(ApexSQLTable.class);
  Endpoint endpoint = table.getEndpoint();
  return endpoint.populateInputDAG(context.dag, context.typeFactory);
}
 
Example #19
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountJoinFiniteEmpty() {
  final String sql = "select * from (select * from emp limit 7) as emp\n"
      + "inner join (select * from dept limit 0) as dept\n"
      + "on emp.deptno = dept.deptno";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 2);
  expected.put(Join.class, 1);
  expected.put(Project.class, 3);
  expected.put(Sort.class, 2);
  checkNodeTypeCount(sql, expected);
}
 
Example #20
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountEmp() {
  final String sql = "select * from emp";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 1);
  expected.put(Project.class, 1);
  checkNodeTypeCount(sql, expected);
}
 
Example #21
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountRightJoinEmptyFinite() {
  final String sql = "select * from (select * from emp limit 0) as emp\n"
      + "right join (select * from dept limit 4) as dept\n"
      + "on emp.deptno = dept.deptno";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 2);
  expected.put(Join.class, 1);
  expected.put(Project.class, 3);
  expected.put(Sort.class, 2);
  checkNodeTypeCount(sql, expected);
}
 
Example #22
Source File: DrillRelMdRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Double getRowCount(RelNode rel, RelMetadataQuery mq) {
  if (rel instanceof TableScan) {
    return getRowCountInternal((TableScan)rel, mq);
  }
  return super.getRowCount(rel, mq);
}
 
Example #23
Source File: RelOptMaterialization.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static ProjectFilterTable of3(RexNode condition,
    Mappings.TargetMapping mapping, RelNode node) {
  if (node instanceof TableScan) {
    return new ProjectFilterTable(condition, mapping,
        (TableScan) node);
  } else {
    return null;
  }
}
 
Example #24
Source File: IncrementalUpdateUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static String findRefreshField(RelNode plan, final ReflectionSettings reflectionSettings) {
  final Pointer<String> refreshField = new Pointer<>();
  plan.accept(new StatelessRelShuttleImpl() {
    @Override
    public RelNode visit(TableScan tableScan) {
      List<String> tablePath = tableScan.getTable().getQualifiedName();
      final AccelerationSettings settings = reflectionSettings.getReflectionSettings(new NamespaceKey(tablePath));
      refreshField.value = settings.getRefreshField();
      return tableScan;
    }
  });
  return refreshField.value;
}
 
Example #25
Source File: IncrementalUpdateUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(TableScan tableScan) {
  List<String> tablePath = tableScan.getTable().getQualifiedName();
  final AccelerationSettings settings = reflectionSettings.getReflectionSettings(new NamespaceKey(tablePath));
  isIncremental  = settings.getMethod() == RefreshMethod.INCREMENTAL;
  return tableScan;
}
 
Example #26
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of all tables used by this expression or its children
 */
public static List<RelOptTable> findAllTables(RelNode rel) {
    final Multimap<Class<? extends RelNode>, RelNode> nodes = RelMetadataQuery.instance().getNodeTypes(rel);
    final List<RelOptTable> usedTables = new ArrayList<>();
    for (Entry<Class<? extends RelNode>, Collection<RelNode>> e : nodes.asMap().entrySet()) {
        if (TableScan.class.isAssignableFrom(e.getKey())) {
            for (RelNode node : e.getValue()) {
                usedTables.add(node.getTable());
            }
        }
    }
    return usedTables;
}
 
Example #27
Source File: PlanCompiler.java    From streamline with Apache License 2.0 5 votes vote down vote up
private void doChainOperators(PrintWriter pw, RelNode node, Set<TableScan> tables, String parentCtx) {
  pw.print(
          String.format("    ChannelContext CTX_%d = Channels.chain(%2$s, %3$s);\n",
                        node.getId(), parentCtx, RelNodeCompiler.getStageName(node)));
  String currentCtx = String.format("CTX_%d", node.getId());
  if (node instanceof TableScan) {
    tables.add((TableScan) node);
  }
  for (RelNode i : node.getInputs()) {
    doChainOperators(pw, i, tables, currentCtx);
  }
}
 
Example #28
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountJoinEmptyEmpty() {
  final String sql = "select * from (select * from emp limit 0) as emp\n"
      + "inner join (select * from dept limit 0) as dept\n"
      + "on emp.deptno = dept.deptno";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 2);
  expected.put(Join.class, 1);
  expected.put(Project.class, 3);
  expected.put(Sort.class, 2);
  checkNodeTypeCount(sql, expected);
}
 
Example #29
Source File: DrillPushProjectIntoScanRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
protected ScanPrel createScan(TableScan scan, ProjectPushInfo projectPushInfo) {
  ScanPrel drillScan = (ScanPrel) scan;

  return new ScanPrel(drillScan.getCluster(),
      drillScan.getTraitSet().plus(Prel.DRILL_PHYSICAL),
      drillScan.getGroupScan().clone(projectPushInfo.getFields()),
      projectPushInfo.createNewRowType(drillScan.getCluster().getTypeFactory()),
      drillScan.getTable());
}
 
Example #30
Source File: PushDownLogicTableRule.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param call todo result set with order,backend
 */
@Override
public void onMatch(RelOptRuleCall call) {
    TableScan judgeObject = (TableScan) call.rels[0];
    if (canHandle(judgeObject.getTable())) {
        RelNode value = toPhyTable(call.builder(), judgeObject);
        if (value != null) {
            call.transformTo(value);
        }
    }

}