Java Code Examples for org.apache.calcite.tools.Planner#rel()
The following examples show how to use
org.apache.calcite.tools.Planner#rel() .
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: SQLExecEnvironment.java From attic-apex-malhar with Apache License 2.0 | 6 votes |
/** * This is the main method takes SQL statement as input and contructs a DAG using contructs registered with this * {@link SQLExecEnvironment}. * * @param sql SQL statement that should be converted to a DAG. */ public void executeSQL(DAG dag, String sql) { FrameworkConfig config = buildFrameWorkConfig(); Planner planner = Frameworks.getPlanner(config); try { logger.info("Parsing SQL statement: {}", sql); SqlNode parsedTree = planner.parse(sql); SqlNode validatedTree = planner.validate(parsedTree); RelNode relationalTree = planner.rel(validatedTree).rel; logger.info("RelNode relationalTree generate from SQL statement is:\n {}", Util.toLinux(RelOptUtil.toString(relationalTree))); RelNodeVisitor visitor = new RelNodeVisitor(dag, typeFactory); visitor.traverse(relationalTree); } catch (Exception e) { throw Throwables.propagate(e); } finally { planner.close(); } }
Example 2
Source File: SortRemoveRuleTest.java From calcite with Apache License 2.0 | 6 votes |
/** * The default schema that is used in these tests provides tables sorted on the primary key. Due * to this scan operators always come with a {@link org.apache.calcite.rel.RelCollation} trait. */ private RelNode transform(String sql, RuleSet prepareRules) throws Exception { final SchemaPlus rootSchema = Frameworks.createRootSchema(true); final SchemaPlus defSchema = rootSchema.add("hr", new HrClusteredSchema()); final FrameworkConfig config = Frameworks.newConfigBuilder() .parserConfig(SqlParser.Config.DEFAULT) .defaultSchema(defSchema) .traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE) .programs( Programs.of(prepareRules), Programs.ofRules(SortRemoveRule.INSTANCE)) .build(); Planner planner = Frameworks.getPlanner(config); SqlNode parse = planner.parse(sql); SqlNode validate = planner.validate(parse); RelRoot planRoot = planner.rel(validate); RelNode planBefore = planRoot.rel; RelTraitSet desiredTraits = planBefore.getTraitSet() .replace(EnumerableConvention.INSTANCE); RelNode planAfter = planner.transform(0, desiredTraits, planBefore); return planner.transform(1, desiredTraits, planAfter); }
Example 3
Source File: LexCaseSensitiveTest.java From calcite with Apache License 2.0 | 6 votes |
private static void runProjectQueryWithLex(Lex lex, String sql) throws SqlParseException, ValidationException, RelConversionException { Config javaLex = SqlParser.configBuilder().setLex(lex).build(); Planner planner = getPlanner(null, javaLex, Programs.ofRules(Programs.RULE_SET)); SqlNode parse = planner.parse(sql); SqlNode validate = planner.validate(parse); RelNode convert = planner.rel(validate).rel; RelTraitSet traitSet = convert.getTraitSet().replace(EnumerableConvention.INSTANCE); RelNode transform = planner.transform(0, traitSet, convert); assertThat(transform, instanceOf(EnumerableProject.class)); List<String> fieldNames = transform.getRowType().getFieldNames(); assertThat(fieldNames.size(), is(2)); if (lex.caseSensitive) { assertThat(fieldNames.get(0), is("EMPID")); assertThat(fieldNames.get(1), is("empid")); } else { assertThat(fieldNames.get(0) + "-" + fieldNames.get(1), anyOf(is("EMPID-empid0"), is("EMPID0-empid"))); } }
Example 4
Source File: LexEscapeTest.java From calcite with Apache License 2.0 | 6 votes |
private static void runProjectQueryWithLex(Lex lex, String sql) throws SqlParseException, ValidationException, RelConversionException { Config javaLex = SqlParser.configBuilder().setLex(lex).build(); Planner planner = getPlanner(null, javaLex, Programs.ofRules(Programs.RULE_SET)); SqlNode parse = planner.parse(sql); SqlNode validate = planner.validate(parse); RelNode convert = planner.rel(validate).rel; assertThat(convert, instanceOf(LogicalProject.class)); List<RelDataTypeField> fields = convert.getRowType().getFieldList(); // Get field type from sql text and validate we parsed it after validation. assertThat(fields.size(), is(4)); assertThat(fields.get(0).getType().getSqlTypeName(), is(SqlTypeName.VARCHAR)); assertThat(fields.get(1).getType().getSqlTypeName(), is(SqlTypeName.TIME)); assertThat(fields.get(2).getType().getSqlTypeName(), is(SqlTypeName.INTEGER)); assertThat(fields.get(3).getType().getSqlTypeName(), is(SqlTypeName.TIMESTAMP)); }
Example 5
Source File: TpcdsLatticeSuggesterTest.java From calcite with Apache License 2.0 | 5 votes |
List<Lattice> addQuery(String q) throws SqlParseException, ValidationException, RelConversionException { final Planner planner = new PlannerImpl(config); final SqlNode node = planner.parse(q); final SqlNode node2 = planner.validate(node); final RelRoot root = planner.rel(node2); return suggester.addQuery(root.project()); }
Example 6
Source File: ServerDdlExecutor.java From calcite with Apache License 2.0 | 5 votes |
/** Populates the table called {@code name} by executing {@code query}. */ static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) { // Generate, prepare and execute an "INSERT INTO table query" statement. // (It's a bit inefficient that we convert from SqlNode to SQL and back // again.) final FrameworkConfig config = Frameworks.newConfigBuilder() .defaultSchema(context.getRootSchema().plus()) .build(); final Planner planner = Frameworks.getPlanner(config); try { final StringBuilder buf = new StringBuilder(); final SqlWriterConfig writerConfig = SqlPrettyWriter.config().withAlwaysUseParentheses(false); final SqlPrettyWriter w = new SqlPrettyWriter(writerConfig, buf); buf.append("INSERT INTO "); name.unparse(w, 0, 0); buf.append(' '); query.unparse(w, 0, 0); final String sql = buf.toString(); final SqlNode query1 = planner.parse(sql); final SqlNode query2 = planner.validate(query1); final RelRoot r = planner.rel(query2); final PreparedStatement prepare = context.getRelRunner().prepare(r.rel); int rowCount = prepare.executeUpdate(); Util.discard(rowCount); prepare.close(); } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) { throw new RuntimeException(e); } }
Example 7
Source File: LatticeSuggesterTest.java From calcite with Apache License 2.0 | 5 votes |
List<Lattice> addQuery(String q) throws SqlParseException, ValidationException, RelConversionException { final Planner planner = new PlannerImpl(config); final SqlNode node = planner.parse(q); final SqlNode node2 = planner.validate(node); final RelRoot root = planner.rel(node2); return s.addQuery(root.project()); }
Example 8
Source File: ToLogicalConverterTest.java From calcite with Apache License 2.0 | 5 votes |
private static RelNode rel(String sql) { final Planner planner = Frameworks.getPlanner(frameworkConfig()); try { SqlNode parse = planner.parse(sql); SqlNode validate = planner.validate(parse); return planner.rel(validate).rel; } catch (Exception e) { throw TestUtil.rethrow(e); } }
Example 9
Source File: ExtensionDdlExecutor.java From calcite with Apache License 2.0 | 5 votes |
/** Populates the table called {@code name} by executing {@code query}. */ protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) { // Generate, prepare and execute an "INSERT INTO table query" statement. // (It's a bit inefficient that we convert from SqlNode to SQL and back // again.) final FrameworkConfig config = Frameworks.newConfigBuilder() .defaultSchema( Objects.requireNonNull( Schemas.subSchema(context.getRootSchema(), context.getDefaultSchemaPath())).plus()) .build(); final Planner planner = Frameworks.getPlanner(config); try { final StringBuilder buf = new StringBuilder(); final SqlPrettyWriter w = new SqlPrettyWriter( SqlPrettyWriter.config() .withDialect(CalciteSqlDialect.DEFAULT) .withAlwaysUseParentheses(false), buf); buf.append("INSERT INTO "); name.unparse(w, 0, 0); buf.append(" "); query.unparse(w, 0, 0); final String sql = buf.toString(); final SqlNode query1 = planner.parse(sql); final SqlNode query2 = planner.validate(query1); final RelRoot r = planner.rel(query2); final PreparedStatement prepare = context.getRelRunner().prepare(r.rel); int rowCount = prepare.executeUpdate(); Util.discard(rowCount); prepare.close(); } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) { throw new RuntimeException(e); } }
Example 10
Source File: RexSqlStandardConvertletTableTest.java From calcite with Apache License 2.0 | 5 votes |
private RelNode convertSqlToRel(String sql, boolean simplifyRex) { final FrameworkConfig config = Frameworks.newConfigBuilder() .defaultSchema(CalciteSchema.createRootSchema(false).plus()) .parserConfig(SqlParser.configBuilder().build()) .build(); final Planner planner = Frameworks.getPlanner(config); try (Closer closer = new Closer()) { closer.add(Hook.REL_BUILDER_SIMPLIFY.addThread(Hook.propertyJ(simplifyRex))); final SqlNode parsed = planner.parse(sql); final SqlNode validated = planner.validate(parsed); return planner.rel(validated).rel; } catch (SqlParseException | RelConversionException | ValidationException e) { throw TestUtil.rethrow(e); } }
Example 11
Source File: QueryPlanner.java From samza with Apache License 2.0 | 4 votes |
public RelRoot plan(String query) { try { Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); SchemaPlus rootSchema = calciteConnection.getRootSchema(); registerSourceSchemas(rootSchema); List<SamzaSqlScalarFunctionImpl> samzaSqlFunctions = udfMetadata.stream() .map(x -> new SamzaSqlScalarFunctionImpl(x)) .collect(Collectors.toList()); final List<RelTraitDef> traitDefs = new ArrayList<>(); traitDefs.add(ConventionTraitDef.INSTANCE); traitDefs.add(RelCollationTraitDef.INSTANCE); List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>(); sqlOperatorTables.add(new SamzaSqlOperatorTable()); sqlOperatorTables.add(new SamzaSqlUdfOperatorTable(samzaSqlFunctions)); // Using lenient so that !=,%,- are allowed. FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder() .parserConfig(SqlParser.configBuilder() .setLex(Lex.JAVA) .setConformance(SqlConformanceEnum.LENIENT) .setCaseSensitive(false) // Make Udfs case insensitive .build()) .defaultSchema(rootSchema) .operatorTable(new ChainedSqlOperatorTable(sqlOperatorTables)) .sqlToRelConverterConfig(SqlToRelConverter.Config.DEFAULT) .traitDefs(traitDefs) .context(Contexts.EMPTY_CONTEXT) .costFactory(null) .build(); Planner planner = Frameworks.getPlanner(frameworkConfig); SqlNode sql = planner.parse(query); SqlNode validatedSql = planner.validate(sql); RelRoot relRoot = planner.rel(validatedSql); LOG.info("query plan:\n" + RelOptUtil.toString(relRoot.rel, SqlExplainLevel.ALL_ATTRIBUTES)); return relRoot; } catch (Exception e) { String errorMsg = SamzaSqlValidator.formatErrorString(query, e); LOG.error(errorMsg, e); throw new SamzaException(errorMsg, e); } }