org.apache.calcite.plan.RelOptTable Java Examples

The following examples show how to use org.apache.calcite.plan.RelOptTable. 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: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public void validateUpdate(SqlUpdate call) {
	final SqlValidatorNamespace targetNamespace = getNamespace(call);
	validateNamespace(targetNamespace, unknownType);
	final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable(
		targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null);
	final SqlValidatorTable table = relOptTable == null
		? targetNamespace.getTable()
		: relOptTable.unwrap(SqlValidatorTable.class);

	final RelDataType targetRowType =
		createTargetRowType(
			table,
			call.getTargetColumnList(),
			true);

	final SqlSelect select = call.getSourceSelect();
	validateSelect(select, targetRowType);

	final RelDataType sourceRowType = getNamespace(call).getRowType();
	checkTypeAssignment(sourceRowType, targetRowType, call);

	checkConstraint(table, call, targetRowType);

	validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE);
}
 
Example #2
Source File: InfoSchemaScanPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public InfoSchemaScanPrel(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelOptTable table,
    TableMetadata dataset,
    SearchQuery query,
    List<SchemaPath> projectedColumns,
    double observedRowcountAdjustment
    ) {

  super(cluster, traitSet, table, dataset.getStoragePluginId(), dataset, projectedColumns, observedRowcountAdjustment);
  this.pluginId = dataset.getStoragePluginId();
  this.table = Preconditions.checkNotNull(
    InfoSchemaStoragePlugin.TABLE_MAP.get(dataset.getName().getName().toLowerCase()), "Unexpected system table.");
  this.query = query;
}
 
Example #3
Source File: DrillScanRel.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static List<SchemaPath> getProjectedColumns(final RelOptTable table, boolean isSelectStar) {
  List<String> columnNames = table.getRowType().getFieldNames();
  List<SchemaPath> projectedColumns = new ArrayList<SchemaPath>(columnNames.size());

  for (String columnName : columnNames) {
     projectedColumns.add(SchemaPath.getSimplePath(columnName));
  }

  // If the row-type doesn't contain the STAR keyword, then insert it
  // as we are dealing with a  SELECT_STAR query.
  if (isSelectStar && !Utilities.isStarQuery(projectedColumns)) {
    projectedColumns.add(SchemaPath.STAR_COLUMN);
  }

  return projectedColumns;
}
 
Example #4
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 #5
Source File: LoptOptimizeJoinRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves join factors that correspond to simple table references. A
 * simple table reference is a single table reference with no grouping or
 * aggregation.
 *
 * @param multiJoin join factors being optimized
 *
 * @return map consisting of the simple factors and the tables they
 * correspond
 */
private Map<Integer, RelOptTable> getSimpleFactors(RelMetadataQuery mq, LoptMultiJoin multiJoin) {
  final Map<Integer, RelOptTable> returnList = new HashMap<>();

  // Loop through all join factors and locate the ones where each
  // column referenced from the factor is not derived and originates
  // from the same underlying table.  Also, discard factors that
  // are null-generating or will be removed because of semijoins.
  if (multiJoin.getMultiJoinRel().isFullOuterJoin()) {
    return returnList;
  }
  for (int factIdx = 0; factIdx < multiJoin.getNumJoinFactors(); factIdx++) {
    if (multiJoin.isNullGenerating(factIdx)
        || (multiJoin.getJoinRemovalFactor(factIdx) != null)) {
      continue;
    }
    final RelNode rel = multiJoin.getJoinFactor(factIdx);
    final RelOptTable table = mq.getTableOrigin(rel);
    if (table != null) {
      returnList.put(factIdx, table);
    }
  }

  return returnList;
}
 
Example #6
Source File: LoptOptimizeJoinRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether a join is a removable self-join. It is if it's an
 * inner join between identical, simple factors and the equality portion of
 * the join condition consists of the same set of unique keys.
 *
 * @param joinRel the join
 *
 * @return true if the join is removable
 */
public static boolean isRemovableSelfJoin(Join joinRel) {
  final RelNode left = joinRel.getLeft();
  final RelNode right = joinRel.getRight();

  if (joinRel.getJoinType().isOuterJoin()) {
    return false;
  }

  // Make sure the join is between the same simple factor
  final RelMetadataQuery mq = joinRel.getCluster().getMetadataQuery();
  final RelOptTable leftTable = mq.getTableOrigin(left);
  if (leftTable == null) {
    return false;
  }
  final RelOptTable rightTable = mq.getTableOrigin(right);
  if (rightTable == null) {
    return false;
  }
  if (!leftTable.getQualifiedName().equals(rightTable.getQualifiedName())) {
    return false;
  }

  // Determine if the join keys are identical and unique
  return areSelfJoinKeysUnique(mq, left, right, joinRel.getCondition());
}
 
Example #7
Source File: StreamRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  final TableScan scan = call.rel(1);
  final RelOptCluster cluster = delta.getCluster();
  final RelOptTable relOptTable = scan.getTable();
  final StreamableTable streamableTable =
      relOptTable.unwrap(StreamableTable.class);
  if (streamableTable != null) {
    final Table table1 = streamableTable.stream();
    final RelOptTable relOptTable2 =
        RelOptTableImpl.create(relOptTable.getRelOptSchema(),
            relOptTable.getRowType(), table1,
            ImmutableList.<String>builder()
                .addAll(relOptTable.getQualifiedName())
                .add("(STREAM)").build());
    final LogicalTableScan newScan =
        LogicalTableScan.create(cluster, relOptTable2, scan.getHints());
    call.transformTo(newScan);
  }
}
 
Example #8
Source File: ScanRelBase.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public ScanRelBase(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelOptTable table,
    StoragePluginId pluginId,
    TableMetadata tableMetadata,
    List<SchemaPath> projectedColumns,
    double observedRowcountAdjustment) {
  super(cluster, traitSet, table);
  this.pluginId = Preconditions.checkNotNull(pluginId);
  this.tableMetadata = Preconditions.checkNotNull(tableMetadata);
  Preconditions.checkArgument(observedRowcountAdjustment >= 0 && observedRowcountAdjustment <= 1, "observedRowcountAdjustment cannot be set to " + observedRowcountAdjustment);
  this.observedRowcountAdjustment = observedRowcountAdjustment;
  this.projectedColumns = projectedColumns != null ? ImmutableList.copyOf(projectedColumns) : getAllColumns(table);
  setProjectedRowType(projectedColumns);
}
 
Example #9
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static RelOptTable getRelOptTable(
    TableNamespace tableNamespace,
    CatalogReader catalogReader,
    String datasetName,
    boolean[] usedDataset,
    List<RelDataTypeField> extendedFields) {
  final List<String> names = tableNamespace.getTable().getQualifiedName();
  RelOptTable table;
  if (datasetName != null
      && catalogReader instanceof RelOptSchemaWithSampling) {
    final RelOptSchemaWithSampling reader =
        (RelOptSchemaWithSampling) catalogReader;
    table = reader.getTableForMember(names, datasetName, usedDataset);
  } else {
    // Schema does not support substitution. Ignore the data set, if any.
    table = catalogReader.getTableForMember(names);
  }
  if (!extendedFields.isEmpty()) {
    table = table.extend(extendedFields);
  }
  return table;
}
 
Example #10
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve a target column name in the target table.
 *
 * @return the target field or null if the name cannot be resolved
 * @param rowType the target row type
 * @param id      the target column identifier
 * @param table   the target table or null if it is not a RelOptTable instance
 */
public static RelDataTypeField getTargetField(
    RelDataType rowType, RelDataTypeFactory typeFactory,
    SqlIdentifier id, SqlValidatorCatalogReader catalogReader,
    RelOptTable table) {
  final Table t = table == null ? null : table.unwrap(Table.class);
  if (!(t instanceof CustomColumnResolvingTable)) {
    final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
    return nameMatcher.field(rowType, id.getSimple());
  }

  final List<Pair<RelDataTypeField, List<String>>> entries =
      ((CustomColumnResolvingTable) t).resolveColumn(
          rowType, typeFactory, id.names);
  switch (entries.size()) {
  case 1:
    if (!entries.get(0).getValue().isEmpty()) {
      return null;
    }
    return entries.get(0).getKey();
  default:
    return null;
  }
}
 
Example #11
Source File: StreamRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  final TableScan scan = call.rel(1);
  final RelOptCluster cluster = delta.getCluster();
  final RelOptTable relOptTable = scan.getTable();
  final StreamableTable streamableTable =
      relOptTable.unwrap(StreamableTable.class);
  if (streamableTable != null) {
    final Table table1 = streamableTable.stream();
    final RelOptTable relOptTable2 =
        RelOptTableImpl.create(relOptTable.getRelOptSchema(),
            relOptTable.getRowType(), table1,
            ImmutableList.<String>builder()
                .addAll(relOptTable.getQualifiedName())
                .add("(STREAM)").build());
    final LogicalTableScan newScan =
        LogicalTableScan.create(cluster, relOptTable2);
    call.transformTo(newScan);
  }
}
 
Example #12
Source File: Bindables.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a BindableTableScan. */
public static BindableTableScan create(RelOptCluster cluster,
    RelOptTable relOptTable, List<RexNode> filters,
    List<Integer> projects) {
  final Table table = relOptTable.unwrap(Table.class);
  final RelTraitSet traitSet =
      cluster.traitSetOf(BindableConvention.INSTANCE)
          .replaceIfs(RelCollationTraitDef.INSTANCE, () -> {
            if (table != null) {
              return table.getStatistic().getCollations();
            }
            return ImmutableList.of();
          });
  return new BindableTableScan(cluster, traitSet, relOptTable,
      ImmutableList.copyOf(filters), ImmutableIntList.copyOf(projects));
}
 
Example #13
Source File: ModifiableViewTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public ColumnStrategy generationStrategy(RelOptTable table,
    int iColumn) {
  final ModifiableViewTable viewTable =
      table.unwrap(ModifiableViewTable.class);
  assert iColumn < viewTable.columnMapping.size();

  // Use the view constraint to generate the default value if the column is
  // constrained.
  final int mappedOrdinal = viewTable.columnMapping.get(iColumn);
  final RexNode viewConstraint = projectMap.get(mappedOrdinal);
  if (viewConstraint != null) {
    return ColumnStrategy.DEFAULT;
  }

  // Otherwise use the default value of the underlying table.
  final Table schemaTable = viewTable.getTable();
  if (schemaTable instanceof Wrapper) {
    final InitializerExpressionFactory initializerExpressionFactory =
        ((Wrapper) schemaTable).unwrap(InitializerExpressionFactory.class);
    if (initializerExpressionFactory != null) {
      return initializerExpressionFactory.generationStrategy(table,
          iColumn);
    }
  }
  return super.generationStrategy(table, iColumn);
}
 
Example #14
Source File: MongoTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode toRel(
    RelOptTable.ToRelContext context,
    RelOptTable relOptTable) {
  final RelOptCluster cluster = context.getCluster();
  return new MongoTableScan(cluster, cluster.traitSetOf(MongoRel.CONVENTION),
      relOptTable, this, null);
}
 
Example #15
Source File: LogicalTableModify.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a LogicalTableModify.
 *
 * <p>Use {@link #create} unless you know what you're doing.
 */
public LogicalTableModify(RelOptCluster cluster, RelTraitSet traitSet,
    RelOptTable table, Prepare.CatalogReader schema, RelNode input,
    Operation operation, List<String> updateColumnList,
    List<RexNode> sourceExpressionList, boolean flattened) {
  super(cluster, traitSet, table, schema, input, operation, updateColumnList,
      sourceExpressionList, flattened);
}
 
Example #16
Source File: FilterTableScanRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static boolean test(TableScan scan) {
  // We can only push filters into a FilterableTable or
  // ProjectableFilterableTable.
  final RelOptTable table = scan.getTable();
  return table.unwrap(FilterableTable.class) != null
      || table.unwrap(ProjectableFilterableTable.class) != null;
}
 
Example #17
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Converts the ordinal of a field into the ordinal of a stored field.
 * That is, it subtracts the number of virtual fields that come before it. */
public static int realOrdinal(final RelOptTable table, int i) {
  List<ColumnStrategy> strategies = table.getColumnStrategies();
  int n = 0;
  for (int j = 0; j < i; j++) {
    switch (strategies.get(j)) {
    case VIRTUAL:
      ++n;
    }
  }
  return i - n;
}
 
Example #18
Source File: RelColumnOrigin.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelColumnOrigin(
    RelOptTable originTable,
    int iOriginColumn,
    boolean isDerived) {
  this.originTable = originTable;
  this.iOriginColumn = iOriginColumn;
  this.isDerived = isDerived;
}
 
Example #19
Source File: ScanRelBase.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static ImmutableList<SchemaPath> getAllColumns(RelOptTable table){
  return FluentIterable.from(table.getRowType().getFieldNames()).transform(new Function<String, SchemaPath>(){

    @Override
    public SchemaPath apply(String input) {
      return SchemaPath.getSimplePath(input);
    }}).toList();
}
 
Example #20
Source File: LogicalTableScan.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalTableScan.
 *
 * @param cluster Cluster
 * @param relOptTable Table
 */
public static LogicalTableScan create(RelOptCluster cluster,
    final RelOptTable relOptTable) {
  final Table table = relOptTable.unwrap(Table.class);
  final RelTraitSet traitSet =
      cluster.traitSetOf(Convention.NONE)
          .replaceIfs(RelCollationTraitDef.INSTANCE, () -> {
            if (table != null) {
              return table.getStatistic().getCollations();
            }
            return ImmutableList.of();
          });
  return new LogicalTableScan(cluster, traitSet, relOptTable);
}
 
Example #21
Source File: VirtualColumnsExpressionFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode newColumnDefaultValue(
    RelOptTable table, int iColumn, InitializerContext context) {
  if (iColumn == 4) {
    final SqlNode node = context.parseExpression(SqlParser.Config.DEFAULT, "A + 1");
    // Actually we should validate the node with physical schema,
    // here full table schema(includes the virtual columns) also works
    // because the expression "A + 1" does not reference any virtual column.
    final SqlNode validated = context.validateExpression(table.getRowType(), node);
    return context.convertExpression(validated);
  } else {
    return super.newColumnDefaultValue(table, iColumn, context);
  }
}
 
Example #22
Source File: LogicalTableModify.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalTableModify. */
public static LogicalTableModify create(RelOptTable table,
    Prepare.CatalogReader schema, RelNode input,
    Operation operation, List<String> updateColumnList,
    List<RexNode> sourceExpressionList, boolean flattened) {
  final RelOptCluster cluster = input.getCluster();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE);
  return new LogicalTableModify(cluster, traitSet, table, schema, input,
      operation, updateColumnList, sourceExpressionList, flattened);
}
 
Example #23
Source File: EnumerableTableSpool.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates an EnumerableTableSpool. */
public static EnumerableTableSpool create(RelNode input, Type readType,
    Type writeType, RelOptTable table) {
  RelOptCluster cluster = input.getCluster();
  RelMetadataQuery mq = cluster.getMetadataQuery();
  RelTraitSet traitSet = cluster.traitSetOf(EnumerableConvention.INSTANCE)
      .replaceIfs(RelCollationTraitDef.INSTANCE,
          () -> mq.collations(input))
      .replaceIf(RelDistributionTraitDef.INSTANCE,
          () -> mq.distribution(input));
  return new EnumerableTableSpool(cluster, traitSet, input, readType, writeType, table);
}
 
Example #24
Source File: LatticeSpace.java    From calcite with Apache License 2.0 5 votes vote down vote up
LatticeTable register(RelOptTable t) {
  final LatticeTable table = tableMap.get(t.getQualifiedName());
  if (table != null) {
    return table;
  }
  final LatticeTable table2 = new LatticeTable(t);
  tableMap.put(t.getQualifiedName(), table2);
  g.addVertex(table2);
  return table2;
}
 
Example #25
Source File: NullInitializerExpressionFactory.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean isGeneratedAlways(RelOptTable table, int iColumn) {
  switch (generationStrategy(table, iColumn)) {
  case VIRTUAL:
  case STORED:
    return true;
  default:
    return false;
  }
}
 
Example #26
Source File: CsvTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected CsvTableScan(RelOptCluster cluster, RelOptTable table,
    CsvTranslatableTable csvTable, int[] fields) {
  super(cluster, cluster.traitSetOf(EnumerableConvention.INSTANCE), ImmutableList.of(), table);
  this.csvTable = csvTable;
  this.fields = fields;

  assert csvTable != null;
}
 
Example #27
Source File: DruidTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode toRel(RelOptTable.ToRelContext context,
    RelOptTable relOptTable) {
  final RelOptCluster cluster = context.getCluster();
  final TableScan scan = LogicalTableScan.create(cluster, relOptTable, ImmutableList.of());
  return DruidQuery.create(cluster,
      cluster.traitSetOf(BindableConvention.INSTANCE), relOptTable, this,
      ImmutableList.of(scan));
}
 
Example #28
Source File: ElasticsearchTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an ElasticsearchTableScan.
 *
 * @param cluster Cluster
 * @param traitSet Trait set
 * @param table Table
 * @param elasticsearchTable Elasticsearch table
 * @param projectRowType Fields and types to project; null to project raw row
 */
ElasticsearchTableScan(RelOptCluster cluster, RelTraitSet traitSet,
     RelOptTable table, ElasticsearchTable elasticsearchTable,
     RelDataType projectRowType) {
  super(cluster, traitSet, ImmutableList.of(), table);
  this.elasticsearchTable = Objects.requireNonNull(elasticsearchTable, "elasticsearchTable");
  this.projectRowType = projectRowType;

  assert getConvention() == ElasticsearchRel.CONVENTION;
}
 
Example #29
Source File: DrillScanRelBase.java    From Bats with Apache License 2.0 5 votes vote down vote up
public DrillScanRelBase(RelOptCluster cluster,
                        RelTraitSet traits,
                        RelOptTable table,
                        final List<SchemaPath> columns) {
  super(cluster, traits, table);
  this.drillTable = Utilities.getDrillTable(table);
  assert drillTable != null;
  try {
    this.groupScan = drillTable.getGroupScan().clone(columns);
  } catch (final IOException e) {
    throw new DrillRuntimeException("Failure creating scan.", e);
  }
}
 
Example #30
Source File: HBTQueryConvertor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public RelNode filterFromTable(FilterFromTableSchema input) {
    List<String> names = input.getNames();
    relBuilder.scan(names);
    TableScan tableScan = (TableScan) relBuilder.peek();
    RelOptTable table = tableScan.getTable();
    relBuilder.as(names.get(names.size() - 1));
    relBuilder.filter(toRex(input.getFilter()));
    Filter build = (Filter) relBuilder.build();
    Bindables.BindableTableScan bindableTableScan = Bindables.BindableTableScan.create(build.getCluster(), table, build.getChildExps(), TableScan.identity(table));
    relBuilder.clear();
    return PushDownLogicTableRule.BindableTableScan.toPhyTable(relBuilder, bindableTableScan);
}