Search in sources :

Example 1 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.

the class SqlDdlNodes method populate.

/**
 * 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(context.getRootSchema().plus()).build();
    final Planner planner = Frameworks.getPlanner(config);
    try {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        final SqlPrettyWriter w = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, false, pw);
        pw.print("INSERT INTO ");
        name.unparse(w, 0, 0);
        pw.print(" ");
        query.unparse(w, 0, 0);
        pw.flush();
        final String sql = sw.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);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SQLException(java.sql.SQLException) RelRoot(org.apache.calcite.rel.RelRoot) PreparedStatement(java.sql.PreparedStatement) RelConversionException(org.apache.calcite.tools.RelConversionException) StringWriter(java.io.StringWriter) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) PrintWriter(java.io.PrintWriter) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.

the class Prepare method prepareSql.

public PreparedResult prepareSql(SqlNode sqlQuery, SqlNode sqlNodeOriginal, Class runtimeContextClass, SqlValidator validator, boolean needsValidation) {
    init(runtimeContextClass);
    final SqlToRelConverter.ConfigBuilder builder = SqlToRelConverter.configBuilder().withTrimUnusedFields(true).withExpand(THREAD_EXPAND.get()).withExplain(sqlQuery.getKind() == SqlKind.EXPLAIN);
    final SqlToRelConverter sqlToRelConverter = getSqlToRelConverter(validator, catalogReader, builder.build());
    SqlExplain sqlExplain = null;
    if (sqlQuery.getKind() == SqlKind.EXPLAIN) {
        // dig out the underlying SQL statement
        sqlExplain = (SqlExplain) sqlQuery;
        sqlQuery = sqlExplain.getExplicandum();
        sqlToRelConverter.setDynamicParamCountInExplain(sqlExplain.getDynamicParamCount());
    }
    RelRoot root = sqlToRelConverter.convertQuery(sqlQuery, needsValidation, true);
    Hook.CONVERTED.run(root.rel);
    if (timingTracer != null) {
        timingTracer.traceTime("end sql2rel");
    }
    final RelDataType resultType = validator.getValidatedNodeType(sqlQuery);
    fieldOrigins = validator.getFieldOrigins(sqlQuery);
    assert fieldOrigins.size() == resultType.getFieldCount();
    parameterRowType = validator.getParameterRowType(sqlQuery);
    // storage and decorrelation
    if (sqlExplain != null) {
        SqlExplain.Depth explainDepth = sqlExplain.getDepth();
        SqlExplainFormat format = sqlExplain.getFormat();
        SqlExplainLevel detailLevel = sqlExplain.getDetailLevel();
        switch(explainDepth) {
            case TYPE:
                return createPreparedExplanation(resultType, parameterRowType, null, format, detailLevel);
            case LOGICAL:
                return createPreparedExplanation(null, parameterRowType, root, format, detailLevel);
            default:
        }
    }
    // Structured type flattening, view expansion, and plugging in physical
    // storage.
    root = root.withRel(flattenTypes(root.rel, true));
    if (this.context.config().forceDecorrelate()) {
        // Sub-query decorrelation.
        root = root.withRel(decorrelate(sqlToRelConverter, sqlQuery, root.rel));
    }
    // Trim unused fields.
    root = trimUnusedFields(root);
    Hook.TRIMMED.run(root.rel);
    // Display physical plan after decorrelation.
    if (sqlExplain != null) {
        switch(sqlExplain.getDepth()) {
            case PHYSICAL:
            default:
                root = optimize(root, getMaterializations(), getLattices());
                return createPreparedExplanation(null, parameterRowType, root, sqlExplain.getFormat(), sqlExplain.getDetailLevel());
        }
    }
    root = optimize(root, getMaterializations(), getLattices());
    if (timingTracer != null) {
        timingTracer.traceTime("end optimization");
    }
    // use original kind.
    if (!root.kind.belongsTo(SqlKind.DML)) {
        root = root.withKind(sqlNodeOriginal.getKind());
    }
    return implement(root);
}
Also used : SqlExplainFormat(org.apache.calcite.sql.SqlExplainFormat) SqlExplainLevel(org.apache.calcite.sql.SqlExplainLevel) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) SqlExplain(org.apache.calcite.sql.SqlExplain) RelRoot(org.apache.calcite.rel.RelRoot) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 3 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.

the class RelOptRulesTest method transitiveInference.

private void transitiveInference(RelOptRule... extraRules) throws Exception {
    final DiffRepository diffRepos = getDiffRepos();
    final String sql = diffRepos.expand(null, "${sql}");
    final HepProgram program = new HepProgramBuilder().addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).build();
    final HepPlanner planner = new HepPlanner(program);
    final RelRoot root = tester.convertSqlToRel(sql);
    final RelNode relInitial = root.rel;
    assertTrue(relInitial != null);
    List<RelMetadataProvider> list = Lists.newArrayList();
    list.add(DefaultRelMetadataProvider.INSTANCE);
    planner.registerMetadataProviders(list);
    RelMetadataProvider plannerChain = ChainedRelMetadataProvider.of(list);
    relInitial.getCluster().setMetadataProvider(new CachingRelMetadataProvider(plannerChain, planner));
    planner.setRoot(relInitial);
    RelNode relBefore = planner.findBestExp();
    String planBefore = NL + RelOptUtil.toString(relBefore);
    diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
    HepProgram program2 = new HepProgramBuilder().addMatchOrder(HepMatchOrder.BOTTOM_UP).addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).addRuleInstance(JoinPushTransitivePredicatesRule.INSTANCE).addRuleCollection(Arrays.asList(extraRules)).build();
    final HepPlanner planner2 = new HepPlanner(program2);
    planner.registerMetadataProviders(list);
    planner2.setRoot(relBefore);
    RelNode relAfter = planner2.findBestExp();
    String planAfter = NL + RelOptUtil.toString(relAfter);
    diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) RelNode(org.apache.calcite.rel.RelNode) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) RelRoot(org.apache.calcite.rel.RelRoot) HepPlanner(org.apache.calcite.plan.hep.HepPlanner)

Example 4 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.

the class RelMetadataTest method convertSql.

// ~ Methods ----------------------------------------------------------------
// ----------------------------------------------------------------------
// Tests for getPercentageOriginalRows
// ----------------------------------------------------------------------
private RelNode convertSql(String sql) {
    final RelRoot root = tester.convertSqlToRel(sql);
    root.rel.getCluster().setMetadataProvider(DefaultRelMetadataProvider.INSTANCE);
    return root.rel;
}
Also used : RelRoot(org.apache.calcite.rel.RelRoot)

Example 5 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project drill by apache.

the class SqlConverter method toRel.

public RelRoot toRel(final SqlNode validatedNode) {
    initCluster(initPlanner());
    DrillViewExpander viewExpander = new DrillViewExpander(this);
    final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(viewExpander, validator, catalog, cluster, DrillConvertletTable.INSTANCE, sqlToRelConverterConfig);
    boolean topLevelQuery = !isInnerQuery || isExpandedView;
    RelRoot rel = sqlToRelConverter.convertQuery(validatedNode, false, topLevelQuery);
    // add another project to remove them.
    if (topLevelQuery && rel.rel.getRowType().getFieldCount() - rel.fields.size() > 0) {
        RexBuilder builder = rel.rel.getCluster().getRexBuilder();
        RelNode relNode = rel.rel;
        List<RexNode> expressions = rel.fields.stream().map(f -> builder.makeInputRef(relNode, f.left)).collect(Collectors.toList());
        RelNode project = LogicalProject.create(rel.rel, expressions, rel.validatedRowType);
        rel = RelRoot.of(project, rel.validatedRowType, rel.kind);
    }
    return rel.withRel(sqlToRelConverter.flattenTypes(rel.rel, true));
}
Also used : Arrays(java.util.Arrays) UserException(org.apache.drill.common.exceptions.UserException) DrillSqlParseException(org.apache.drill.exec.planner.sql.parser.impl.DrillSqlParseException) LoggerFactory(org.slf4j.LoggerFactory) RelRoot(org.apache.calcite.rel.RelRoot) DrillParserConfig(org.apache.drill.exec.planner.sql.DrillParserConfig) DrillRelDataTypeSystem(org.apache.drill.exec.planner.types.DrillRelDataTypeSystem) FunctionImplementationRegistry(org.apache.drill.exec.expr.fn.FunctionImplementationRegistry) Utilities(org.apache.drill.exec.util.Utilities) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode) DynamicSchema(org.apache.calcite.jdbc.DynamicSchema) ConventionTraitDef(org.apache.calcite.plan.ConventionTraitDef) RelOptCluster(org.apache.calcite.plan.RelOptCluster) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) QueryContext(org.apache.drill.exec.ops.QueryContext) SchemaUtilites(org.apache.drill.exec.planner.sql.SchemaUtilites) RelOptCostFactory(org.apache.calcite.plan.RelOptCostFactory) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Logger(org.slf4j.Logger) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexBuilder(org.apache.calcite.rex.RexBuilder) DrillDistributionTraitDef(org.apache.drill.exec.planner.physical.DrillDistributionTraitDef) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) DrillConstExecutor(org.apache.drill.exec.planner.logical.DrillConstExecutor) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RelNode(org.apache.calcite.rel.RelNode) Collectors(java.util.stream.Collectors) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) List(java.util.List) DrillConfig(org.apache.drill.common.config.DrillConfig) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) DrillConvertletTable(org.apache.drill.exec.planner.sql.DrillConvertletTable) SqlParser(org.apache.calcite.sql.parser.SqlParser) DrillCostBase(org.apache.drill.exec.planner.cost.DrillCostBase) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) ExecConstants(org.apache.drill.exec.ExecConstants) DrillRelFactories(org.apache.drill.exec.planner.logical.DrillRelFactories) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Hook(org.apache.calcite.runtime.Hook) UserSession(org.apache.drill.exec.rpc.user.UserSession) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RelRoot(org.apache.calcite.rel.RelRoot) RexNode(org.apache.calcite.rex.RexNode)

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 List (java.util.List)3 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