Java Code Examples for org.apache.calcite.sql2rel.SqlToRelConverter#Config

The following examples show how to use org.apache.calcite.sql2rel.SqlToRelConverter#Config . 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: AbstractMaterializedViewTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelNode toRel(RelOptCluster cluster, SchemaPlus rootSchema,
    SchemaPlus defaultSchema, String sql) throws SqlParseException {
  final SqlParser parser = SqlParser.create(sql, SqlParser.Config.DEFAULT);
  final SqlNode parsed = parser.parseStmt();

  final CalciteCatalogReader catalogReader = new CalciteCatalogReader(
      CalciteSchema.from(rootSchema),
      CalciteSchema.from(defaultSchema).path(null),
      new JavaTypeFactoryImpl(), new CalciteConnectionConfigImpl(new Properties()));

  final SqlValidator validator = new ValidatorForTest(SqlStdOperatorTable.instance(),
      catalogReader, new JavaTypeFactoryImpl(), SqlConformanceEnum.DEFAULT);
  final SqlNode validated = validator.validate(parsed);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
      .withTrimUnusedFields(true)
      .withExpand(true)
      .withDecorrelationEnabled(true)
      .build();
  final SqlToRelConverter converter = new SqlToRelConverter(
      (rowType, queryString, schemaPath, viewPath) -> {
        throw new UnsupportedOperationException("cannot expand view");
      }, validator, catalogReader, cluster, StandardConvertletTable.INSTANCE, config);
  return converter.convertQuery(validated, false, true).rel;
}
 
Example 2
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected SqlToRelConverter createSqlToRelConverter(
    final SqlValidator validator,
    final Prepare.CatalogReader catalogReader,
    final RelDataTypeFactory typeFactory,
    final SqlToRelConverter.Config config) {
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  RelOptCluster cluster =
      RelOptCluster.create(getPlanner(), rexBuilder);
  if (clusterFactory != null) {
    cluster = clusterFactory.apply(cluster);
  }
  RelOptTable.ViewExpander viewExpander =
      new MockViewExpander(validator, catalogReader, cluster, config);
  return new SqlToRelConverter(viewExpander, validator, catalogReader, cluster,
      StandardConvertletTable.INSTANCE, config);
}
 
Example 3
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelRoot expandView(RelDataType rowType, String queryString,
    List<String> schemaPath, List<String> viewPath) {
  expansionDepth++;

  SqlParser parser = prepare.createParser(queryString);
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException("parse failed", e);
  }
  // View may have different schema path than current connection.
  final CatalogReader catalogReader =
      this.catalogReader.withSchemaPath(schemaPath);
  SqlValidator validator = createSqlValidator(catalogReader);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
          .withTrimUnusedFields(true).build();
  SqlToRelConverter sqlToRelConverter =
      getSqlToRelConverter(validator, catalogReader, config);
  RelRoot root =
      sqlToRelConverter.convertQuery(sqlNode, true, false);

  --expansionDepth;
  return root;
}
 
Example 4
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
public TesterImpl withConfig(SqlToRelConverter.Config config) {
  return this.config == config
      ? this
      : new TesterImpl(diffRepos, enableDecorrelate, enableTrim,
          enableExpand, enableLateDecorrelate, enableTypeCoercion, catalogReaderFactory,
          clusterFactory, config, conformance, context);
}
 
Example 5
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 5 votes vote down vote up
static RelNode testSqlToRelConverter(RelOptPlanner planner) throws Exception {
    RexBuilder rexBuilder = createRexBuilder();
    RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    RelOptTable.ViewExpander viewExpander = ViewExpanders.simpleContext(cluster);

    Pair<SqlNode, SqlValidator> pair = testSqlValidator();
    SqlNode sqlNode = pair.left;
    SqlValidator validator = pair.right;
    CatalogReader catalogReader = createCalciteCatalogReader();
    SqlRexConvertletTable convertletTable = StandardConvertletTable.INSTANCE;
    SqlToRelConverter.Config config = SqlToRelConverter.Config.DEFAULT;
    // 不转换成EnumerableTableScan,而是LogicalTableScan
    config = SqlToRelConverter.configBuilder().withConvertTableAccess(false).build();

    SqlToRelConverter converter = new SqlToRelConverter(viewExpander, validator, catalogReader, cluster,
            convertletTable, config);

    boolean needsValidation = false;
    boolean top = false;
    RelRoot root = converter.convertQuery(sqlNode, needsValidation, top);
    RelNode relNode = root.rel;

    String plan = RelOptUtil.toString(relNode);
    System.out.println("Logical Plan:");
    System.out.println("------------------------------------------------------------------");
    System.out.println(plan);
    System.out.println();

    // testPrograms(root.rel);

    return relNode;
}
 
Example 6
Source File: Frameworks.java    From calcite with 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 7
Source File: Prepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Walks over a tree of relational expressions, replacing each
 * {@link org.apache.calcite.rel.RelNode} with a 'slimmed down' relational
 * expression that projects
 * only the columns required by its consumer.
 *
 * @param root Root of relational expression tree
 * @return Trimmed relational expression
 */
protected RelRoot trimUnusedFields(RelRoot root) {
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
      .withTrimUnusedFields(shouldTrim(root.rel))
      .withExpand(THREAD_EXPAND.get())
      .build();
  final SqlToRelConverter converter =
      getSqlToRelConverter(getSqlValidator(), catalogReader, config);
  final boolean ordered = !root.collation.getFieldCollations().isEmpty();
  final boolean dml = SqlKind.DML.contains(root.kind);
  return root.withRel(converter.trimUnusedFields(dml || ordered, root.rel));
}
 
Example 8
Source File: PlannerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelRoot expandView(RelDataType rowType, String queryString,
    List<String> schemaPath, List<String> viewPath) {
  if (planner == null) {
    ready();
  }
  SqlParser parser = SqlParser.create(queryString, parserConfig);
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException("parse failed", e);
  }

  final CalciteCatalogReader catalogReader =
      createCatalogReader().withSchemaPath(schemaPath);
  final SqlValidator validator = createSqlValidator(catalogReader);

  final RexBuilder rexBuilder = createRexBuilder();
  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  final SqlToRelConverter.Config config = SqlToRelConverter
      .configBuilder()
      .withConfig(sqlToRelConverterConfig)
      .withTrimUnusedFields(false)
      .build();
  final SqlToRelConverter sqlToRelConverter =
      new SqlToRelConverter(this, validator,
          catalogReader, cluster, convertletTable, config);

  final RelRoot root =
      sqlToRelConverter.convertQuery(sqlNode, true, false);
  final RelRoot root2 =
      root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
  final RelBuilder relBuilder =
      config.getRelBuilderFactory().create(cluster, null);
  return root2.withRel(
      RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
 
Example 9
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override protected SqlToRelConverter getSqlToRelConverter(
    SqlValidator validator,
    CatalogReader catalogReader,
    SqlToRelConverter.Config config) {
  return new SqlToRelConverter(this, validator, catalogReader, cluster,
      convertletTable, config);
}
 
Example 10
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
MockViewExpander(
    SqlValidator validator,
    Prepare.CatalogReader catalogReader,
    RelOptCluster cluster,
    SqlToRelConverter.Config config) {
  this.validator = validator;
  this.catalogReader = catalogReader;
  this.cluster = cluster;
  this.config = config;
}
 
Example 11
Source File: PlannerContext.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link SqlToRelConverter} config.
 *
 * <p>`expand` is set as false, and each sub-query becomes a [[org.apache.calcite.rex.RexSubQuery]].
 */
private SqlToRelConverter.Config getSqlToRelConverterConfig(CalciteConfig calciteConfig) {
	return JavaScalaConversionUtil.<SqlToRelConverter.Config>toJava(calciteConfig.getSqlToRelConverterConfig()).orElseGet(
			() -> SqlToRelConverter.configBuilder()
					.withTrimUnusedFields(false)
					.withHintStrategyTable(FlinkHintStrategies.createHintStrategyTable())
					.withInSubQueryThreshold(Integer.MAX_VALUE)
					.withExpand(false)
					.withRelBuilderFactory(FlinkRelFactories.FLINK_REL_BUILDER())
					.build()
	);
}
 
Example 12
Source File: PlanningConfigurationBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link SqlToRelConverter} config.
 */
private SqlToRelConverter.Config getSqlToRelConverterConfig(
		CalciteConfig calciteConfig,
		ExpressionBridge<PlannerExpression> expressionBridge) {
	return JavaScalaConversionUtil.toJava(calciteConfig.sqlToRelConverterConfig()).orElseGet(
		() -> SqlToRelConverter.configBuilder()
			.withTrimUnusedFields(false)
			.withConvertTableAccess(true)
			.withInSubQueryThreshold(Integer.MAX_VALUE)
			.withRelBuilderFactory(new FlinkRelBuilderFactory(expressionBridge))
			.build()
	);
}
 
Example 13
Source File: SqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a rel root that defers materialization of scans via {@link com.dremio.exec.planner.logical.ConvertibleScan}
 *
 * Used for serialization.
 */
public RelRootPlus toConvertibleRelRoot(final SqlNode validatedNode, boolean expand) {

  final OptionManager o = settings.getOptions();
  final long inSubQueryThreshold =  o.getOption(ExecConstants.FAST_OR_ENABLE) ? o.getOption(ExecConstants.FAST_OR_MAX_THRESHOLD) : settings.getOptions().getOption(ExecConstants.PLANNER_IN_SUBQUERY_THRESHOLD);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
    .withInSubQueryThreshold((int) inSubQueryThreshold)
    .withTrimUnusedFields(true)
    .withConvertTableAccess(false)
    .withExpand(expand)
    .build();
  final ReflectionAllowedMonitoringConvertletTable convertletTable = new ReflectionAllowedMonitoringConvertletTable(new ConvertletTable(functionContext.getContextInformation()));
  final SqlToRelConverter sqlToRelConverter = new DremioSqlToRelConverter(this, validator, convertletTable, config);
  // Previously we had "top" = !innerQuery, but calcite only adds project if it is not a top query.
  final RelRoot rel = sqlToRelConverter.convertQuery(validatedNode, false /* needs validate */, false /* top */);
  final RelNode rel2 = sqlToRelConverter.flattenTypes(rel.rel, true);
  RelNode converted;
  final RelNode rel3 = expand ? rel2 : rel2.accept(new RelsWithRexSubQueryFlattener(sqlToRelConverter));
  if (settings.isRelPlanningEnabled()) {
    converted = rel3;
  } else {
    converted = DremioRelDecorrelator.decorrelateQuery(rel3, DremioRelFactories.CALCITE_LOGICAL_BUILDER.create(rel3.getCluster(), null), false, false);
  }

  if (logger.isDebugEnabled()) {
    logger.debug("ConvertQuery with expand = {}:\n{}", expand, RelOptUtil.toString(converted, SqlExplainLevel.ALL_ATTRIBUTES));
  }
  return RelRootPlus.of(converted, rel.validatedRowType, rel.kind, convertletTable.isReflectionDisallowed());
}
 
Example 14
Source File: PlannerContext.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link SqlToRelConverter} config.
 *
 * <p>`expand` is set as false, and each sub-query becomes a [[org.apache.calcite.rex.RexSubQuery]].
 */
private SqlToRelConverter.Config getSqlToRelConverterConfig(CalciteConfig calciteConfig) {
	return JavaScalaConversionUtil.toJava(calciteConfig.getSqlToRelConverterConfig()).orElseGet(
			() -> SqlToRelConverter.configBuilder()
					.withTrimUnusedFields(false)
					.withConvertTableAccess(false)
					.withInSubQueryThreshold(Integer.MAX_VALUE)
					.withExpand(false)
					.build()
	);
}
 
Example 15
Source File: PlanningConfigurationBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link SqlToRelConverter} config.
 */
private SqlToRelConverter.Config getSqlToRelConverterConfig(
		CalciteConfig calciteConfig,
		ExpressionBridge<PlannerExpression> expressionBridge) {
	return JavaScalaConversionUtil.toJava(calciteConfig.sqlToRelConverterConfig()).orElseGet(
		() -> SqlToRelConverter.configBuilder()
			.withTrimUnusedFields(false)
			.withConvertTableAccess(false)
			.withInSubQueryThreshold(Integer.MAX_VALUE)
			.withRelBuilderFactory(new FlinkRelBuilderFactory(expressionBridge))
			.build()
	);
}
 
Example 16
Source File: Prepare.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Protected method to allow subclasses to override construction of
 * SqlToRelConverter.
 */
protected abstract SqlToRelConverter getSqlToRelConverter(
    SqlValidator validator,
    CatalogReader catalogReader,
    SqlToRelConverter.Config config);
 
Example 17
Source File: MycatCalciteDataContext.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public SqlToRelConverter.Config getSqlToRelConverterConfig() {
    return MycatCalciteSupport.INSTANCE.config.getSqlToRelConverterConfig();
}
 
Example 18
Source File: Frameworks.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlToRelConverter.Config getSqlToRelConverterConfig() {
  return sqlToRelConverterConfig;
}
 
Example 19
Source File: FrameworkConfig.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * The configuration of {@link SqlToRelConverter}.
 */
SqlToRelConverter.Config getSqlToRelConverterConfig();
 
Example 20
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 2 votes vote down vote up
/** Returns a tester that optionally uses a
 * {@code SqlToRelConverter.Config}. */
Tester withConfig(SqlToRelConverter.Config config);