use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class SamzaSqlApplicationConfig method populateSystemStreamsAndGetRelRoots.
public static Collection<RelRoot> populateSystemStreamsAndGetRelRoots(List<String> dslStmts, Config config, List<String> inputSystemStreams, List<String> outputSystemStreams) {
// TODO: Get the converter factory based on the file type. Create abstraction around this.
DslConverterFactory dslConverterFactory = new SamzaSqlDslConverterFactory();
DslConverter dslConverter = dslConverterFactory.create(config);
Collection<RelRoot> relRoots = dslConverter.convertDsl(String.join("\n", dslStmts));
// RelRoot does not have sink node for Samza SQL dsl, so we can not traverse the relRoot tree to get
// "outputSystemStreams"
// FIXME: the snippet below does not work for Samza SQL dsl but is required for other dsls. Future fix could be
// for samza sql to build TableModify for sink and stick it to the relRoot, so we could get output stream out of it.
// for (RelRoot relRoot : relRoots) {
// SamzaSqlApplicationConfig.populateSystemStreams(relRoot.project(), inputSystemStreams, outputSystemStreams);
// }
// The below code is specific to Samza SQL dsl and should be removed once Samza SQL includes sink as part of
// relRoot and the above code in uncommented.
List<String> sqlStmts = SamzaSqlDslConverter.fetchSqlFromConfig(config);
List<SamzaSqlQueryParser.QueryInfo> queryInfo = SamzaSqlDslConverter.fetchQueryInfo(sqlStmts);
inputSystemStreams.addAll(queryInfo.stream().map(SamzaSqlQueryParser.QueryInfo::getSources).flatMap(Collection::stream).collect(Collectors.toList()));
outputSystemStreams.addAll(queryInfo.stream().map(SamzaSqlQueryParser.QueryInfo::getSink).collect(Collectors.toList()));
return relRoots;
}
use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class QueryPlanner method plan.
public RelRoot plan(String query) {
try (Planner planner = getPlanner()) {
SqlNode sql = planner.parse(query);
SqlNode validatedSql = planner.validate(sql);
RelRoot relRoot = planner.rel(validatedSql);
LOG.info("query plan without optimization:\n" + RelOptUtil.toString(relRoot.rel, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
if (!isQueryPlanOptimizerEnabled) {
LOG.info("Skipping query optimization as it is disabled.");
return relRoot;
}
return optimize(planner, relRoot);
} catch (Exception e) {
String errorMsg = SamzaSqlValidator.formatErrorString(query, e);
LOG.error(errorMsg, e);
throw new SamzaException(errorMsg, e);
}
}
use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class SamzaSqlDslConverter method convertDsl.
@Override
public Collection<RelRoot> convertDsl(String dsl) {
// TODO: Introduce an API to parse a dsl string and return one or more sql statements
List<String> sqlStmts = fetchSqlFromConfig(config);
List<RelRoot> relRoots = new LinkedList<>();
for (String sql : sqlStmts) {
QueryPlanner planner = getQueryPlanner(getSqlConfig(Collections.singletonList(sql), config));
// we always pass only select query to the planner for samza sql. The reason is that samza sql supports
// schema evolution where source and destination could up to an extent have independent schema evolution while
// calcite expects strict comformance of the destination schema with that of the fields in the select query.
SamzaSqlQueryParser.QueryInfo qinfo = SamzaSqlQueryParser.parseQuery(sql);
RelRoot relRoot = planner.plan(qinfo.getSelectQuery());
relRoots.add(relRoot);
}
return relRoots;
}
use of org.apache.calcite.rel.RelRoot in project calcite by apache.
the class CalcitePrepareImpl method convert_.
private ParseResult convert_(Context context, String sql, boolean analyze, boolean fail, CalciteCatalogReader catalogReader, SqlValidator validator, SqlNode sqlNode1) {
final JavaTypeFactory typeFactory = context.getTypeFactory();
final Convention resultConvention = enableBindable ? BindableConvention.INSTANCE : EnumerableConvention.INSTANCE;
final HepPlanner planner = new HepPlanner(new HepProgramBuilder().build());
planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
final SqlToRelConverter.ConfigBuilder configBuilder = SqlToRelConverter.configBuilder().withTrimUnusedFields(true);
if (analyze) {
configBuilder.withConvertTableAccess(false);
}
final CalcitePreparingStmt preparingStmt = new CalcitePreparingStmt(this, context, catalogReader, typeFactory, context.getRootSchema(), null, planner, resultConvention, createConvertletTable());
final SqlToRelConverter converter = preparingStmt.getSqlToRelConverter(validator, catalogReader, configBuilder.build());
final RelRoot root = converter.convertQuery(sqlNode1, false, true);
if (analyze) {
return analyze_(validator, sql, sqlNode1, root, fail);
}
return new ConvertResult(this, validator, sql, sqlNode1, validator.getValidatedNodeType(sqlNode1), root);
}
use of org.apache.calcite.rel.RelRoot in project calcite by apache.
the class RelMetadataTest method testBrokenCustomProvider.
@Test
public void testBrokenCustomProvider() {
final List<String> buf = Lists.newArrayList();
ColTypeImpl.THREAD_LIST.set(buf);
final String sql = "select deptno, count(*) from emp where deptno > 10 " + "group by deptno having count(*) = 0";
final RelRoot root = tester.withClusterFactory(new Function<RelOptCluster, RelOptCluster>() {
public RelOptCluster apply(RelOptCluster cluster) {
cluster.setMetadataProvider(ChainedRelMetadataProvider.of(ImmutableList.of(BrokenColTypeImpl.SOURCE, cluster.getMetadataProvider())));
return cluster;
}
}).convertSqlToRel(sql);
final RelNode rel = root.rel;
assertThat(rel, instanceOf(LogicalFilter.class));
final MyRelMetadataQuery mq = new MyRelMetadataQuery();
try {
assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
fail("expected error");
} catch (IllegalArgumentException e) {
final String value = "No handler for method [public abstract java.lang.String " + "org.apache.calcite.test.RelMetadataTest$ColType.getColType(int)] " + "applied to argument of type [interface org.apache.calcite.rel.RelNode]; " + "we recommend you create a catch-all (RelNode) handler";
assertThat(e.getMessage(), is(value));
}
}
Aggregations