Java Code Examples for org.apache.calcite.plan.RelOptTable

The following examples show how to use org.apache.calcite.plan.RelOptTable. These examples are extracted from open source projects. 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 Project: calcite   Source File: Bindables.java    License: 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 2
Source Project: Bats   Source File: StreamRules.java    License: 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 3
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: 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 4
Source Project: calcite   Source File: LoptOptimizeJoinRule.java    License: 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 5
Source Project: Mycat2   Source File: GroupByPartitionRule.java    License: 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 Project: Bats   Source File: DrillScanRel.java    License: 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 7
Source Project: dremio-oss   Source File: InfoSchemaScanPrel.java    License: 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 8
Source Project: calcite   Source File: StreamRules.java    License: 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 9
Source Project: dremio-oss   Source File: ScanRelBase.java    License: 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 10
Source Project: Bats   Source File: SqlValidatorUtil.java    License: 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 11
Source Project: Bats   Source File: SqlValidatorUtil.java    License: 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 12
Source Project: Bats   Source File: ModifiableViewTable.java    License: 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 13
Source Project: calcite   Source File: LoptOptimizeJoinRule.java    License: 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 14
Source Project: calcite   Source File: LatticeSpace.java    License: 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 15
Source Project: flink   Source File: QueryOperationCatalogViewTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable relOptTable) {
	FlinkRelBuilder relBuilder = FlinkRelBuilder.of(context.getCluster(), relOptTable.getRelOptSchema());

	RelNode relNode = relBuilder.tableOperation(catalogView.getQueryOperation()).build();
	return RelOptUtil.createCastRel(relNode, rowType.apply(relBuilder.getTypeFactory()), false);
}
 
Example 16
Source Project: dremio-oss   Source File: ParquetScanPrel.java    License: Apache License 2.0 5 votes vote down vote up
public ParquetScanPrel(RelOptCluster cluster, RelTraitSet traitSet, RelOptTable table, StoragePluginId pluginId,
                       TableMetadata dataset, List<SchemaPath> projectedColumns, double observedRowcountAdjustment,
                       ParquetScanFilter filter) {
  super(cluster, traitSet, table, pluginId, dataset, projectedColumns, observedRowcountAdjustment);
  this.filter = filter;
  this.globalDictionaryEncodedColumns = null;
  this.cachedRelDataType = null;
}
 
Example 17
Source Project: dremio-oss   Source File: HiveRulesFactory.java    License: Apache License 2.0 5 votes vote down vote up
public HiveScanPrel(RelOptCluster cluster, RelTraitSet traitSet, RelOptTable table, StoragePluginId pluginId,
                    TableMetadata dataset, List<SchemaPath> projectedColumns, double observedRowcountAdjustment,
                    ScanFilter filter, HiveReaderProto.ReaderType readerType) {
  super(cluster, traitSet, table, pluginId, dataset, projectedColumns, observedRowcountAdjustment);
  this.filter = filter;
  this.readerType = readerType;
}
 
Example 18
Source Project: Bats   Source File: MutableTableModify.java    License: Apache License 2.0 5 votes vote down vote up
private MutableTableModify(RelDataType rowType, MutableRel input,
    RelOptTable table, CatalogReader catalogReader,
    Operation operation, List<String> updateColumnList,
    List<RexNode> sourceExpressionList, boolean flattened) {
  super(MutableRelType.TABLE_MODIFY, rowType, input);
  this.table = table;
  this.catalogReader = catalogReader;
  this.operation = operation;
  this.updateColumnList = updateColumnList;
  this.sourceExpressionList = sourceExpressionList;
  this.flattened = flattened;
}
 
Example 19
Source Project: nifi   Source File: FlowFileTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RelNode toRel(final RelOptTable.ToRelContext context, final RelOptTable relOptTable) {
    // Request all fields.
    final int fieldCount = relOptTable.getRowType().getFieldCount();
    final int[] fields = new int[fieldCount];
    for (int i = 0; i < fieldCount; i++) {
        fields[i] = i;
    }

    return new FlowFileTableScan(context.getCluster(), relOptTable, this, fields);
}
 
Example 20
Source Project: Bats   Source File: LogicalTableModify.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalTableModify. */
public static LogicalTableModify create(RelOptTable table,
    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 21
Source Project: Bats   Source File: LogicalTableScan.java    License: 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 22
Source Project: Bats   Source File: RelColumnOrigin.java    License: 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 23
Source Project: Bats   Source File: RelMetadataQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Determines the origin of a {@link RelNode}, provided it maps to a single
 * table, optionally with filtering and projection.
 *
 * @param rel the RelNode
 *
 * @return the table, if the RelNode is a simple table; otherwise null
 */
public RelOptTable getTableOrigin(RelNode rel) {
  // Determine the simple origin of the first column in the
  // RelNode.  If it's simple, then that means that the underlying
  // table is also simple, even if the column itself is derived.
  if (rel.getRowType().getFieldCount() == 0) {
    return null;
  }
  final Set<RelColumnOrigin> colOrigins = getColumnOrigins(rel, 0);
  if (colOrigins == null || colOrigins.size() == 0) {
    return null;
  }
  return colOrigins.iterator().next().getOriginTable();
}
 
Example 24
Source Project: calcite   Source File: Frameworks.java    License: Apache License 2.0 5 votes vote down vote up
StdFrameworkConfig(Context context,
    SqlRexConvertletTable convertletTable,
    SqlOperatorTable operatorTable,
    ImmutableList<Program> programs,
    ImmutableList<RelTraitDef> traitDefs,
    SqlParser.Config parserConfig,
    SqlValidator.Config sqlValidatorConfig,
    SqlToRelConverter.Config sqlToRelConverterConfig,
    SchemaPlus defaultSchema,
    RelOptCostFactory costFactory,
    RelDataTypeSystem typeSystem,
    RexExecutor executor,
    boolean evolveLattice,
    SqlStatisticProvider statisticProvider,
    RelOptTable.ViewExpander viewExpander) {
  this.context = context;
  this.convertletTable = convertletTable;
  this.operatorTable = operatorTable;
  this.programs = programs;
  this.traitDefs = traitDefs;
  this.parserConfig = parserConfig;
  this.sqlValidatorConfig = sqlValidatorConfig;
  this.sqlToRelConverterConfig = sqlToRelConverterConfig;
  this.defaultSchema = defaultSchema;
  this.costFactory = costFactory;
  this.typeSystem = typeSystem;
  this.executor = executor;
  this.evolveLattice = evolveLattice;
  this.statisticProvider = statisticProvider;
  this.viewExpander = viewExpander;
}
 
Example 25
Source Project: Bats   Source File: EnumerableTableScan.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates an EnumerableTableScan. */
public static EnumerableTableScan create(RelOptCluster cluster,
    RelOptTable relOptTable) {
  final Table table = relOptTable.unwrap(Table.class);
  Class elementType = EnumerableTableScan.deduceElementType(table);
  final RelTraitSet traitSet =
      cluster.traitSetOf(EnumerableConvention.INSTANCE)
          .replaceIfs(RelCollationTraitDef.INSTANCE, () -> {
            if (table != null) {
              return table.getStatistic().getCollations();
            }
            return ImmutableList.of();
          });
  return new EnumerableTableScan(cluster, traitSet, relOptTable, elementType);
}
 
Example 26
Source Project: calcite   Source File: FilterTableScanRule.java    License: 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 27
Source Project: calcite   Source File: RelOptTableImpl.java    License: 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 28
Source Project: calcite   Source File: VirtualColumnsExpressionFactory.java    License: 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 29
Source Project: calcite   Source File: EnumerableTableSpool.java    License: 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 30
Source Project: Bats   Source File: NullInitializerExpressionFactory.java    License: 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;
  }
}