Java Code Examples for org.apache.calcite.rel.RelRoot#withRel()
The following examples show how to use
org.apache.calcite.rel.RelRoot#withRel() .
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: ViewTable.java From Bats with Apache License 2.0 | 6 votes |
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 2
Source File: TableEnv.java From marble with Apache License 2.0 | 6 votes |
protected RelRoot getSqlPlanRel(String sql) throws Throwable { try (Planner planner = Frameworks.getPlanner(frameworkConfig)) { RelRoot root; final SqlNode parsedSqlNode = planner.parse(sql); final Pair<SqlNode, RelDataType> validatedSqlNodeAndType = planner .validateAndGetType( parsedSqlNode); root = planner.rel(validatedSqlNodeAndType.getKey()); final Program program = createProgram(); //getDesiredTraits final RelTraitSet desiredTraits = root.rel.getTraitSet() .replace(EnumerableConvention.INSTANCE) .replace(root.collation) .simplify(); RelNode logicalRelNode = root.rel; final RelNode optimizedRelNode = program.run( root.rel.getCluster().getPlanner(), logicalRelNode, desiredTraits, Collections.emptyList(), Collections.emptyList()); root = root.withRel(optimizedRelNode); return root; } }
Example 3
Source File: ViewTable.java From calcite with Apache License 2.0 | 6 votes |
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 4
Source File: PlannerImpl.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
@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 5
Source File: PlannerImpl.java From calcite with Apache License 2.0 | 5 votes |
@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 6
Source File: Prepare.java From calcite with Apache License 2.0 | 5 votes |
/** * 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 7
Source File: CalcitePrepareImpl.java From calcite with Apache License 2.0 | 4 votes |
private PreparedResult prepare_(Supplier<RelNode> fn, RelDataType resultType) { Class runtimeContextClass = Object.class; init(runtimeContextClass); final RelNode rel = fn.get(); final RelDataType rowType = rel.getRowType(); final List<Pair<Integer, String>> fields = Pair.zip(ImmutableIntList.identity(rowType.getFieldCount()), rowType.getFieldNames()); final RelCollation collation = rel instanceof Sort ? ((Sort) rel).collation : RelCollations.EMPTY; RelRoot root = new RelRoot(rel, resultType, SqlKind.SELECT, fields, collation, new ArrayList<>()); if (timingTracer != null) { timingTracer.traceTime("end sql2rel"); } final RelDataType jdbcType = makeStruct(rexBuilder.getTypeFactory(), resultType); fieldOrigins = Collections.nCopies(jdbcType.getFieldCount(), null); parameterRowType = rexBuilder.getTypeFactory().builder().build(); // Structured type flattening, view expansion, and plugging in // physical storage. root = root.withRel(flattenTypes(root.rel, true)); // Trim unused fields. root = trimUnusedFields(root); final List<Materialization> materializations = ImmutableList.of(); final List<CalciteSchema.LatticeEntry> lattices = ImmutableList.of(); root = optimize(root, materializations, lattices); if (timingTracer != null) { timingTracer.traceTime("end optimization"); } return implement(root); }