use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class SamzaExecutor method generateResultSchema.
SqlSchema generateResultSchema(Config config) {
SamzaSqlDslConverter converter = (SamzaSqlDslConverter) new SamzaSqlDslConverterFactory().create(config);
RelRoot relRoot = converter.convertDsl("").iterator().next();
List<String> colNames = new ArrayList<>();
List<String> colTypeNames = new ArrayList<>();
for (RelDataTypeField dataTypeField : relRoot.validatedRowType.getFieldList()) {
colNames.add(dataTypeField.getName());
colTypeNames.add(dataTypeField.getType().toString());
}
// in QueryResult class and executeQuery().
return new SqlSchema(colNames, Collections.emptyList());
}
use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class QueryPlanner method optimize.
private RelRoot optimize(Planner planner, RelRoot relRoot) {
RelTraitSet relTraitSet = RelTraitSet.createEmpty();
try {
RelRoot optimizedRelRoot = RelRoot.of(planner.transform(0, relTraitSet, relRoot.project()), SqlKind.SELECT);
LOG.info("query plan with optimization:\n" + RelOptUtil.toString(optimizedRelRoot.rel, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
return optimizedRelRoot;
} catch (Exception e) {
String errorMsg = "Error while optimizing query plan:\n" + RelOptUtil.toString(relRoot.rel, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
LOG.error(errorMsg, e);
throw new SamzaException(errorMsg, e);
}
}
use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class QueryTranslator method translate.
/**
* For unit testing only
*/
@VisibleForTesting
void translate(SamzaSqlQueryParser.QueryInfo queryInfo, StreamApplicationDescriptor appDesc, int queryId) {
QueryPlanner planner = new QueryPlanner(sqlConfig.getRelSchemaProviders(), sqlConfig.getInputSystemStreamConfigBySource(), sqlConfig.getUdfMetadata(), sqlConfig.isQueryPlanOptimizerEnabled());
final RelRoot relRoot = planner.plan(queryInfo.getSelectQuery());
SamzaSqlExecutionContext executionContext = new SamzaSqlExecutionContext(sqlConfig);
TranslatorContext translatorContext = new TranslatorContext(appDesc, relRoot, executionContext);
translate(relRoot, sqlConfig.getOutputSystemStreams().get(queryId), translatorContext, queryId);
Map<Integer, TranslatorContext> translatorContexts = new HashMap<>();
translatorContexts.put(queryId, translatorContext.clone());
appDesc.withApplicationTaskContextFactory(new ApplicationTaskContextFactory<SamzaSqlApplicationContext>() {
@Override
public SamzaSqlApplicationContext create(ExternalContext externalContext, JobContext jobContext, ContainerContext containerContext, TaskContext taskContext, ApplicationContainerContext applicationContainerContext) {
return new SamzaSqlApplicationContext(translatorContexts);
}
});
}
use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class SamzaSqlValidator method validate.
/**
* Validate a list of sql statements
* @param sqlStmts list of sql statements
* @throws SamzaSqlValidatorException exception for sql validation
*/
public void validate(List<String> sqlStmts) throws SamzaSqlValidatorException {
for (String sql : sqlStmts) {
SamzaSqlApplicationConfig sqlConfig = SamzaSqlDslConverter.getSqlConfig(Collections.singletonList(sql), config);
QueryPlanner planner = SamzaSqlDslConverter.getQueryPlanner(sqlConfig);
// 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 conformance of the destination schema with that of the fields in the select query.
SamzaSqlQueryParser.QueryInfo qinfo = SamzaSqlQueryParser.parseQuery(sql);
RelRoot relRoot;
try {
relRoot = planner.plan(qinfo.getSelectQuery());
} catch (SamzaException e) {
throw new SamzaSqlValidatorException(String.format("Validation failed for sql stmt:\n%s\n with the following" + " error: \n%s\n", sql, e), e);
}
// Now that we have logical plan, validate different aspects.
String sink = qinfo.getSink();
validate(relRoot, sink, sqlConfig.getRelSchemaProviders().get(sink), sqlConfig.getSamzaRelConverters().get(sink));
}
}
use of org.apache.calcite.rel.RelRoot in project samza by apache.
the class TestQueryPlanner method testTranslate.
@Test
public void testTranslate() {
Map<String, String> config = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(10);
String sql = "Insert into testavro.outputTopic(id) select MyTest(id) from testavro.level1.level2.SIMPLE1 as s where s.id = 10";
config.put(SamzaSqlApplicationConfig.CFG_SQL_STMT, sql);
Config samzaConfig = SamzaSqlApplicationRunner.computeSamzaConfigs(true, new MapConfig(config));
DslConverter dslConverter = new SamzaSqlDslConverterFactory().create(samzaConfig);
Collection<RelRoot> relRoots = dslConverter.convertDsl(sql);
assertEquals(1, relRoots.size());
}
Aggregations