org.apache.calcite.plan.RelOptSchema Java Examples

The following examples show how to use org.apache.calcite.plan.RelOptSchema. 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: PlanningConfigurationBuilder.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a configured {@link FlinkRelBuilder} for a planning session.
 *
 * @param currentCatalog the current default catalog to look for first during planning.
 * @param currentDatabase the current default database to look for first during planning.
 * @return configured rel builder
 */
public FlinkRelBuilder createRelBuilder(String currentCatalog, String currentDatabase) {
	RelOptCluster cluster = FlinkRelOptClusterFactory.create(
		planner,
		new RexBuilder(typeFactory));
	RelOptSchema relOptSchema = createCatalogReader(false, currentCatalog, currentDatabase);
	Context chain = Contexts.chain(
		context,
		// We need to overwrite the default scan factory, which does not
		// expand views. The expandingScanFactory uses the FlinkPlanner to translate a view
		// into a rel tree, before applying any subsequent rules.
		Contexts.of(RelFactories.expandingScanFactory(
			createFlinkPlanner(currentCatalog, currentDatabase),
			RelFactories.DEFAULT_TABLE_SCAN_FACTORY))
	);

	return new FlinkRelBuilder(chain, cluster, relOptSchema, expressionBridge);
}
 
Example #2
Source File: RelBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected RelBuilder(Context context, RelOptCluster cluster,
    RelOptSchema relOptSchema) {
  this.cluster = cluster;
  this.relOptSchema = relOptSchema;
  if (context == null) {
    context = Contexts.EMPTY_CONTEXT;
  }
  this.config = getConfig(context);
  this.viewExpander = getViewExpander(cluster, context);
  this.struct =
      Objects.requireNonNull(RelFactories.Struct.fromContext(context));
  final RexExecutor executor =
      Util.first(context.unwrap(RexExecutor.class),
          Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR));
  final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
  this.simplifier =
      new RexSimplify(cluster.getRexBuilder(), predicates, executor);
}
 
Example #3
Source File: JdbcTableUtils.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
static RelNode toRel(RelOptCluster cluster, RelOptSchema relOptSchema, JdbcTable table, List<String> qualifiedName) {
	RelOptTable.ToRelContext toRelContext = new RelOptTable.ToRelContext() {
		@Override
		public RelOptCluster getCluster() {
			return cluster;
		}

		@Override
		public RelRoot expandView(RelDataType rowType, String queryString, List<String> schemaPath, List<String> viewPath) {
			throw new UnsupportedOperationException();
		}
	};

	return table.toRel(
			toRelContext,
			relOptSchema.getTableForMember(qualifiedName)
	);
}
 
Example #4
Source File: FlinkCalciteCatalogReader.java    From flink with Apache License 2.0 6 votes vote down vote up
private static FlinkPreparingTableBase convertCatalogTable(
		RelOptSchema relOptSchema,
		List<String> names,
		RelDataType rowType,
		CatalogTable catalogTable,
		CatalogSchemaTable schemaTable) {
	if (isLegacySourceOptions(catalogTable, schemaTable)) {
		return new LegacyCatalogSourceTable<>(
			relOptSchema,
			names,
			rowType,
			schemaTable,
			catalogTable);
	} else {
		return new CatalogSourceTable<>(
			relOptSchema,
			names,
			rowType,
			schemaTable,
			catalogTable);
	}
}
 
Example #5
Source File: RelBuilder.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/** Creates a {@link RelBuilderFactory}, a partially-created RelBuilder.
 * Just add a {@link RelOptCluster} and a {@link RelOptSchema} */
public static RelBuilderFactory proto(final Context context) {
  return new RelBuilderFactory() {
    @Override
    public RelBuilder create(RelOptCluster cluster, RelOptSchema schema) {
      return new RelBuilder(context, cluster, schema);
    }
  };
}
 
Example #6
Source File: FlinkPreparingTableBase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link org.apache.calcite.prepare.Prepare.AbstractPreparingTable} instance.
 *
 * @param relOptSchema The RelOptSchema that this table comes from
 * @param rowType      The table row type
 * @param names        The table qualified name
 * @param statistic    The table statistics
 */
public FlinkPreparingTableBase(
		@Nullable RelOptSchema relOptSchema,
		RelDataType rowType,
		Iterable<String> names,
		FlinkStatistic statistic) {
	this.relOptSchema = relOptSchema;
	this.rowType = Objects.requireNonNull(rowType);
	this.names = Objects.requireNonNull(ImmutableList.copyOf(names));
	this.statistic = Objects.requireNonNull(statistic);
}
 
Example #7
Source File: SqlCatalogViewTable.java    From flink with Apache License 2.0 5 votes vote down vote up
public SqlCatalogViewTable(
		@Nullable RelOptSchema relOptSchema,
		RelDataType rowType,
		Iterable<String> names,
		FlinkStatistic statistic,
		CatalogView view,
		List<String> viewPath) {
	super(relOptSchema, rowType, names, statistic);
	this.view = view;
	this.viewPath = viewPath;
}
 
Example #8
Source File: QueryOperationCatalogViewTable.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Creates a QueryOperationCatalogViewTable. */
private QueryOperationCatalogViewTable(
		RelOptSchema relOptSchema,
		List<String> names,
		RelDataType rowType,
		QueryOperationCatalogView catalogView) {
	super(relOptSchema, rowType, names, FlinkStatistic.UNKNOWN());
	this.catalogView = catalogView;
}
 
Example #9
Source File: ExpandingPreparingTable.java    From flink with Apache License 2.0 5 votes vote down vote up
protected ExpandingPreparingTable(
	@Nullable RelOptSchema relOptSchema,
	RelDataType rowType,
	Iterable<String> names,
	FlinkStatistic statistic) {
	super(relOptSchema, rowType, names, statistic);
}
 
Example #10
Source File: FlinkCalciteCatalogReader.java    From flink with Apache License 2.0 5 votes vote down vote up
private static FlinkPreparingTableBase convertQueryOperationView(
		RelOptSchema relOptSchema,
		List<String> names,
		RelDataType rowType,
		QueryOperationCatalogView view) {
	return QueryOperationCatalogViewTable.create(relOptSchema, names, rowType, view);
}
 
Example #11
Source File: FlinkCalciteCatalogReader.java    From flink with Apache License 2.0 5 votes vote down vote up
private static FlinkPreparingTableBase convertCatalogView(
		RelOptSchema relOptSchema,
		List<String> names,
		RelDataType rowType,
		FlinkStatistic statistic,
		CatalogView view) {
	return new SqlCatalogViewTable(relOptSchema, rowType, names, statistic, view, names.subList(0, 2));
}
 
Example #12
Source File: FlinkCalciteCatalogReader.java    From flink with Apache License 2.0 5 votes vote down vote up
private static FlinkPreparingTableBase convertSourceTable(
		RelOptSchema relOptSchema,
		RelDataType rowType,
		ObjectIdentifier tableIdentifier,
		ConnectorCatalogTable<?, ?> table,
		FlinkStatistic statistic,
		boolean isStreamingMode) {
	TableSource<?> tableSource = table.getTableSource().get();
	if (!(tableSource instanceof StreamTableSource ||
		tableSource instanceof LookupableTableSource)) {
		throw new ValidationException(
			"Only StreamTableSource and LookupableTableSource can be used in Blink planner.");
	}
	if (!isStreamingMode && tableSource instanceof StreamTableSource &&
		!((StreamTableSource<?>) tableSource).isBounded()) {
		throw new ValidationException("Only bounded StreamTableSource can be used in batch mode.");
	}

	return new LegacyTableSourceTable<>(
		relOptSchema,
		tableIdentifier,
		rowType,
		statistic,
		tableSource,
		isStreamingMode,
		table);
}
 
Example #13
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelOptTableImpl(
    RelOptSchema schema,
    RelDataType rowType,
    List<String> names,
    Table table,
    Function<Class, Expression> expressionFunction,
    Double rowCount) {
  this.schema = schema;
  this.rowType = Objects.requireNonNull(rowType);
  this.names = ImmutableList.copyOf(names);
  this.table = table; // may be null
  this.expressionFunction = expressionFunction; // may be null
  this.rowCount = rowCount; // may be null
}
 
Example #14
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelOptTableImpl create(
    RelOptSchema schema,
    RelDataType rowType,
    List<String> names,
    Expression expression) {
  return new RelOptTableImpl(schema, rowType, names, null,
      c -> expression, null);
}
 
Example #15
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelOptTableImpl create(
    RelOptSchema schema,
    RelDataType rowType,
    List<String> names,
    Table table,
    Expression expression) {
  return new RelOptTableImpl(schema, rowType, names, table,
      c -> expression, table.getStatistic().getRowCount());
}
 
Example #16
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType,
    Table table, Path path) {
  final SchemaPlus schemaPlus = MySchemaPlus.create(path);
  return new RelOptTableImpl(schema, rowType, Pair.left(path), table,
      getClassExpressionFunction(schemaPlus, Util.last(path).left, table),
      table.getStatistic().getRowCount());
}
 
Example #17
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema,
    RelDataType rowType, Table table, ImmutableList<String> names) {
  assert table instanceof TranslatableTable
      || table instanceof ScannableTable
      || table instanceof ModifiableTable;
  return new RelOptTableImpl(schema, rowType, names, table, null, null);
}
 
Example #18
Source File: VolcanoPlanner.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void registerSchema(RelOptSchema schema) {
  if (registeredSchemas.add(schema)) {
    try {
      schema.registerRules(this);
    } catch (Exception e) {
      throw new AssertionError("While registering schema " + schema, e);
    }
  }
}
 
Example #19
Source File: SqlToRelConverterExtendedTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static void foo(RelNode rel) {
  // Convert rel tree to JSON.
  final RelJsonWriter writer = new RelJsonWriter();
  rel.explain(writer);
  final String json = writer.asString();

  // Find the schema. If there are no tables in the plan, we won't need one.
  final RelOptSchema[] schemas = {null};
  rel.accept(new RelShuttleImpl() {
    @Override public RelNode visit(TableScan scan) {
      schemas[0] = scan.getTable().getRelOptSchema();
      return super.visit(scan);
    }
  });

  // Convert JSON back to rel tree.
  Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> {
    final RelJsonReader reader = new RelJsonReader(
        cluster,
        schemas[0], rootSchema);
    try {
      RelNode x = reader.read(json);
    } catch (IOException e) {
      throw TestUtil.rethrow(e);
    }
    return null;
  });
}
 
Example #20
Source File: JournalledJdbcTableTest.java    From calcite-sql-rewriter with Apache License 2.0 5 votes vote down vote up
@Before
@SuppressWarnings("ResultOfMethodCallIgnored") // Mockito syntax
public void setupMocks() {
	relBuilder = Mockito.mock(JdbcRelBuilder.class);
	journalTable = Mockito.mock(JdbcTable.class);
	relOptTable = Mockito.mock(RelOptTable.class);
	context = Mockito.mock(RelOptTable.ToRelContext.class);

	JdbcRelBuilderFactory relBuilderFactory = Mockito.mock(JdbcRelBuilderFactory.class);
	RelOptCluster relOptCluster = Mockito.mock(RelOptCluster.class);
	JournalledJdbcSchema schema = Mockito.mock(JournalledJdbcSchema.class);
	RelOptSchema relOptSchema = Mockito.mock(RelOptSchema.class);
	RexInputRef versionField = Mockito.mock(RexInputRef.class);
	RexInputRef subsequentVersionField = Mockito.mock(RexInputRef.class);

	Mockito.doReturn(Schema.TableType.TABLE).when(journalTable).getJdbcTableType();
	table = new JournalledJdbcTable("theView", schema, journalTable, new String[] {"key1", "key2"});
	table.relBuilderFactory = relBuilderFactory;

	Mockito.doReturn("myV").when(schema).getVersionField();
	Mockito.doReturn("mySV").when(schema).getSubsequentVersionField();
	Mockito.doReturn(relOptCluster).when(context).getCluster();
	Mockito.doReturn(relBuilder).when(relBuilderFactory).create(Mockito.same(relOptCluster), Mockito.same(relOptSchema));
	Mockito.doReturn(Mockito.mock(RelOptPlanner.class)).when(relOptCluster).getPlanner();
	Mockito.doReturn(relOptSchema).when(relOptTable).getRelOptSchema();
	Mockito.doReturn(ImmutableList.of("theSchema", "theView")).when(relOptTable).getQualifiedName();
	Mockito.doReturn(new SqlIdentifier(
			ImmutableList.of("wrongSchema", "theJournal"),
			null,
			new SqlParserPos(0, 0),
			null
	)).when(journalTable).tableName();
	Mockito.doReturn(versionField).when(relBuilder).field(Mockito.eq("myV"));
	Mockito.doReturn(subsequentVersionField).when(relBuilder).field(Mockito.eq("mySV"));
}
 
Example #21
Source File: RelOptTableImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RelOptTableImpl(RelOptSchema schema, RelDataType rowType, List<String> names, Table table,
        Double rowCount) {
    this.schema = schema;
    this.rowType = Objects.requireNonNull(rowType);
    this.names = ImmutableList.copyOf(names);
    this.table = table; // may be null
    this.rowCount = rowCount; // may be null
}
 
Example #22
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void registerSchema(RelOptSchema schema) {
  if (registeredSchemas.add(schema)) {
    try {
      schema.registerRules(this);
    } catch (Exception e) {
      throw new AssertionError("While registering schema " + schema, e);
    }
  }
}
 
Example #23
Source File: DrillRelBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a {@link RelBuilderFactory}, a partially-created DrillRelBuilder.
 * Just add a {@link RelOptCluster} and a {@link RelOptSchema} */
public static RelBuilderFactory proto(final Context context) {
  return new RelBuilderFactory() {
    public RelBuilder create(RelOptCluster cluster, RelOptSchema schema) {
      return new DrillRelBuilder(context, cluster, schema);
    }
  };
}
 
Example #24
Source File: PlanningConfigurationBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a configured {@link FlinkRelBuilder} for a planning session.
 *
 * @param currentCatalog the current default catalog to look for first during planning.
 * @param currentDatabase the current default database to look for first during planning.
 * @return configured rel builder
 */
public FlinkRelBuilder createRelBuilder(String currentCatalog, String currentDatabase) {
	RelOptCluster cluster = FlinkRelOptClusterFactory.create(
		planner,
		new RexBuilder(typeFactory));
	RelOptSchema relOptSchema = createCatalogReader(false, currentCatalog, currentDatabase);

	return new FlinkRelBuilder(context, cluster, relOptSchema, expressionBridge);
}
 
Example #25
Source File: RelBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected RelBuilder(Context context, RelOptCluster cluster, RelOptSchema relOptSchema) {
    this.cluster = cluster;
    this.relOptSchema = relOptSchema;
    if (context == null) {
        context = Contexts.EMPTY_CONTEXT;
    }
    this.simplify = Hook.REL_BUILDER_SIMPLIFY.get(true);
    this.aggregateFactory = Util.first(context.unwrap(RelFactories.AggregateFactory.class),
            RelFactories.DEFAULT_AGGREGATE_FACTORY);
    this.filterFactory = Util.first(context.unwrap(RelFactories.FilterFactory.class),
            RelFactories.DEFAULT_FILTER_FACTORY);
    this.projectFactory = Util.first(context.unwrap(RelFactories.ProjectFactory.class),
            RelFactories.DEFAULT_PROJECT_FACTORY);
    this.sortFactory = Util.first(context.unwrap(RelFactories.SortFactory.class),
            RelFactories.DEFAULT_SORT_FACTORY);
    this.exchangeFactory = Util.first(context.unwrap(RelFactories.ExchangeFactory.class),
            RelFactories.DEFAULT_EXCHANGE_FACTORY);
    this.sortExchangeFactory = Util.first(context.unwrap(RelFactories.SortExchangeFactory.class),
            RelFactories.DEFAULT_SORT_EXCHANGE_FACTORY);
    this.setOpFactory = Util.first(context.unwrap(RelFactories.SetOpFactory.class),
            RelFactories.DEFAULT_SET_OP_FACTORY);
    this.joinFactory = Util.first(context.unwrap(RelFactories.JoinFactory.class),
            RelFactories.DEFAULT_JOIN_FACTORY);
    this.semiJoinFactory = Util.first(context.unwrap(RelFactories.SemiJoinFactory.class),
            RelFactories.DEFAULT_SEMI_JOIN_FACTORY);
    this.correlateFactory = Util.first(context.unwrap(RelFactories.CorrelateFactory.class),
            RelFactories.DEFAULT_CORRELATE_FACTORY);
    this.valuesFactory = Util.first(context.unwrap(RelFactories.ValuesFactory.class),
            RelFactories.DEFAULT_VALUES_FACTORY);
    this.scanFactory = Util.first(context.unwrap(RelFactories.TableScanFactory.class),
            RelFactories.DEFAULT_TABLE_SCAN_FACTORY);
    this.snapshotFactory = Util.first(context.unwrap(RelFactories.SnapshotFactory.class),
            RelFactories.DEFAULT_SNAPSHOT_FACTORY);
    this.matchFactory = Util.first(context.unwrap(RelFactories.MatchFactory.class),
            RelFactories.DEFAULT_MATCH_FACTORY);
    final RexExecutor executor = Util.first(context.unwrap(RexExecutor.class),
            Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR));
    final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
    this.simplifier = new RexSimplify(cluster.getRexBuilder(), predicates, executor);
}
 
Example #26
Source File: RelBuilder.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelOptSchema getRelOptSchema() {
  return relOptSchema;
}
 
Example #27
Source File: RelOptTableImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType,
        final CalciteSchema.TableEntry tableEntry, Double rowCount) {
    final Table table = tableEntry.getTable();
    return new RelOptTableImpl(schema, rowType, tableEntry.path(), table, rowCount);
}
 
Example #28
Source File: RelOptTableImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType, Table table, Path path) {
    return new RelOptTableImpl(schema, rowType, Pair.left(path), table, table.getStatistic().getRowCount());
}
 
Example #29
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType,
    final CalciteSchema.TableEntry tableEntry, Double rowCount) {
  final Table table = tableEntry.getTable();
  return new RelOptTableImpl(schema, rowType, tableEntry.path(),
      table, getClassExpressionFunction(tableEntry, table), rowCount);
}
 
Example #30
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelOptSchema getRelOptSchema() {
  return schema;
}