Search in sources :

Example 6 with RelRoot

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());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SamzaSqlDslConverter(org.apache.samza.sql.dsl.SamzaSqlDslConverter) SqlSchema(org.apache.samza.sql.schema.SqlSchema) SamzaSqlDslConverterFactory(org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory) RelRoot(org.apache.calcite.rel.RelRoot)

Example 7 with RelRoot

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);
    }
}
Also used : RelRoot(org.apache.calcite.rel.RelRoot) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException)

Example 8 with RelRoot

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);
        }
    });
}
Also used : TaskContext(org.apache.samza.context.TaskContext) HashMap(java.util.HashMap) RelRoot(org.apache.calcite.rel.RelRoot) QueryPlanner(org.apache.samza.sql.planner.QueryPlanner) ApplicationContainerContext(org.apache.samza.context.ApplicationContainerContext) ContainerContext(org.apache.samza.context.ContainerContext) ApplicationContainerContext(org.apache.samza.context.ApplicationContainerContext) SamzaSqlApplicationContext(org.apache.samza.sql.runner.SamzaSqlApplicationContext) ExternalContext(org.apache.samza.context.ExternalContext) SamzaSqlExecutionContext(org.apache.samza.sql.data.SamzaSqlExecutionContext) JobContext(org.apache.samza.context.JobContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with RelRoot

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));
    }
}
Also used : SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) RelRoot(org.apache.calcite.rel.RelRoot) SamzaSqlQueryParser(org.apache.samza.sql.util.SamzaSqlQueryParser) SamzaException(org.apache.samza.SamzaException)

Example 10 with RelRoot

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());
}
Also used : DslConverter(org.apache.samza.sql.interfaces.DslConverter) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) SamzaSqlDslConverterFactory(org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory) RelRoot(org.apache.calcite.rel.RelRoot) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test)

Aggregations

RelRoot (org.apache.calcite.rel.RelRoot)35 RelNode (org.apache.calcite.rel.RelNode)11 SqlNode (org.apache.calcite.sql.SqlNode)10 SamzaSqlDslConverterFactory (org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory)8 Test (org.junit.Test)8 DslConverter (org.apache.samza.sql.interfaces.DslConverter)7 SamzaSqlApplicationConfig (org.apache.samza.sql.runner.SamzaSqlApplicationConfig)7 SqlToRelConverter (org.apache.calcite.sql2rel.SqlToRelConverter)6 Config (org.apache.samza.config.Config)6 MapConfig (org.apache.samza.config.MapConfig)6 SamzaSqlTestConfig (org.apache.samza.sql.util.SamzaSqlTestConfig)6 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)5 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)4 BeamRelNode (org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode)3 RelTraitSet (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet)3 RelRoot (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)3 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)3 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)3