Search in sources :

Example 16 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project beam by apache.

the class BeamAggregationRel method beamComputeSelfCost.

@Override
public BeamCostModel beamComputeSelfCost(RelOptPlanner planner, BeamRelMetadataQuery mq) {
    NodeStats inputStat = BeamSqlRelUtils.getNodeStats(this.input, mq);
    inputStat = computeWindowingCostEffect(inputStat);
    // Aggregates with more aggregate functions cost a bit more
    float multiplier = 1f + (float) aggCalls.size() * 0.125f;
    for (AggregateCall aggCall : aggCalls) {
        if (aggCall.getAggregation().getName().equals("SUM")) {
            // Pretend that SUM costs a little bit more than $SUM0,
            // to make things deterministic.
            multiplier += 0.0125f;
        }
    }
    return BeamCostModel.FACTORY.makeCost(inputStat.getRowCount() * multiplier, inputStat.getRate() * multiplier);
}
Also used : AggregateCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall) NodeStats(org.apache.beam.sdk.extensions.sql.impl.planner.NodeStats)

Example 17 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project beam by apache.

the class ZetaSQLPlannerImpl method rel.

public RelRoot rel(String sql, QueryParameters params) {
    RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(typeFactory));
    AnalyzerOptions options = SqlAnalyzer.getAnalyzerOptions(params, defaultTimezone);
    BeamZetaSqlCatalog catalog = BeamZetaSqlCatalog.create(defaultSchemaPlus, (JavaTypeFactory) cluster.getTypeFactory(), options);
    // Set up table providers that need to be pre-registered
    SqlAnalyzer analyzer = new SqlAnalyzer();
    List<List<String>> tables = analyzer.extractTableNames(sql, options);
    TableResolution.registerTables(this.defaultSchemaPlus, tables);
    QueryTrait trait = new QueryTrait();
    catalog.addTables(tables, trait);
    ResolvedQueryStmt statement = analyzer.analyzeQuery(sql, options, catalog);
    ExpressionConverter expressionConverter = new ExpressionConverter(cluster, params, catalog.getUserFunctionDefinitions());
    ConversionContext context = ConversionContext.of(config, expressionConverter, cluster, trait);
    RelNode convertedNode = QueryStatementConverter.convertRootQuery(context, statement);
    return RelRoot.of(convertedNode, SqlKind.ALL);
}
Also used : RelOptCluster(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptCluster) ConversionContext(org.apache.beam.sdk.extensions.sql.zetasql.translation.ConversionContext) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) RexBuilder(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder) ExpressionConverter(org.apache.beam.sdk.extensions.sql.zetasql.translation.ExpressionConverter) List(java.util.List) ImmutableList(org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList) ResolvedQueryStmt(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedQueryStmt) AnalyzerOptions(com.google.zetasql.AnalyzerOptions)

Example 18 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner 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);
    }
}
Also used : Planner(org.apache.calcite.tools.Planner) RelRoot(org.apache.calcite.rel.RelRoot) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 19 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project samza by apache.

the class QueryPlanner method getPlanner.

private Planner getPlanner() {
    Planner planner = null;
    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(SamzaSqlScalarFunctionImpl::new).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));
        // TODO: Introduce a pluggable rule factory.
        List<RelOptRule> rules = ImmutableList.of(FilterProjectTransposeRule.INSTANCE, ProjectMergeRule.INSTANCE, new SamzaSqlFilterRemoteJoinRule.SamzaSqlFilterIntoRemoteJoinRule(true, RelFactories.LOGICAL_BUILDER, systemStreamConfigBySource));
        // Using lenient so that !=,%,- are allowed.
        FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).setConformance(SqlConformanceEnum.LENIENT).setCaseSensitive(// Make Udfs case insensitive
        false).build()).defaultSchema(rootSchema).operatorTable(new ChainedSqlOperatorTable(sqlOperatorTables)).sqlToRelConverterConfig(SqlToRelConverter.Config.DEFAULT).traitDefs(traitDefs).programs(Programs.hep(rules, true, DefaultRelMetadataProvider.INSTANCE)).build();
        planner = Frameworks.getPlanner(frameworkConfig);
        return planner;
    } catch (Exception e) {
        String errorMsg = "Failed to create planner.";
        LOG.error(errorMsg, e);
        if (planner != null) {
            planner.close();
        }
        throw new SamzaException(errorMsg, e);
    }
}
Also used : Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException) RelOptRule(org.apache.calcite.plan.RelOptRule) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) RelTraitDef(org.apache.calcite.plan.RelTraitDef) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 20 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project calcite by apache.

the class LexCaseSensitiveTest method runProjectQueryWithLex.

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 = planner.getEmptyTraitSet().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")));
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) Config(org.apache.calcite.sql.parser.SqlParser.Config) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) Planner(org.apache.calcite.tools.Planner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) EnumerableProject(org.apache.calcite.adapter.enumerable.EnumerableProject) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

Planner (org.apache.calcite.tools.Planner)17 SqlNode (org.apache.calcite.sql.SqlNode)15 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)15 RelNode (org.apache.calcite.rel.RelNode)10 SchemaPlus (org.apache.calcite.schema.SchemaPlus)7 ArrayList (java.util.ArrayList)6 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)5 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)5 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)4 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)4 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)4 StreamableTable (org.apache.calcite.schema.StreamableTable)4 Table (org.apache.calcite.schema.Table)4 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)4 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 SqlSelect (org.apache.calcite.sql.SqlSelect)2 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)2