Search in sources :

Example 41 with RelRoot

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

the class TestPhysicalPlan method parse.

private static RelNode parse(String sql) throws SqlParseException {
    SqlNode sqlNode = parser.parse(sql);
    sqlNode = parser.validate(sqlNode);
    RelRoot relRoot = parser.convert(sqlNode);
    RelNode relNode = parser.optimize(relRoot.rel);
    log.info("relNode = {}", relNode);
    return relNode;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) AssertRelNode(io.dingodb.calcite.assertion.AssertRelNode) RelRoot(org.apache.calcite.rel.RelRoot) SqlNode(org.apache.calcite.sql.SqlNode)

Example 42 with RelRoot

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

the class TrainDBQueryEngine method processQuery.

@Override
public VerdictSingleResult processQuery(String query) throws Exception {
    SqlParser.Config parserConf = SqlParser.config().withParserFactory(TrainDBCalciteSQLParserImpl.FACTORY).withUnquotedCasing(Casing.TO_LOWER);
    FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schemaManager.getCurrentSchema()).parserConfig(parserConf).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(query);
    TableNameQualifier.toFullyQualifiedName(schemaManager, conn.getDefaultSchema(), parse);
    LOG.debug("Parsed query: " + parse.toString());
    SqlNode validate = planner.validate(parse);
    RelRoot relRoot = planner.rel(validate);
    LOG.debug(RelOptUtil.dumpPlan("Generated plan: ", relRoot.rel, SqlExplainFormat.TEXT, SqlExplainLevel.ALL_ATTRIBUTES));
    SqlDialect.DatabaseProduct dp = SqlDialect.DatabaseProduct.POSTGRESQL;
    String queryString = validate.toSqlString(dp.getDialect()).getSql();
    LOG.debug("query string: " + queryString);
    try {
        Connection internalConn = DriverManager.getConnection("jdbc:traindb-calcite:");
        PreparedStatement stmt = internalConn.prepareStatement(queryString);
        ResultSet rs = stmt.executeQuery();
        return new VerdictSingleResultFromDbmsQueryResult(new JdbcQueryResult(rs));
    } catch (SQLException e) {
        LOG.debug(ExceptionUtils.getStackTrace(e));
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) SqlParser(org.apache.calcite.sql.parser.SqlParser) Connection(java.sql.Connection) CachedDbmsConnection(org.verdictdb.connection.CachedDbmsConnection) JdbcConnection(org.verdictdb.connection.JdbcConnection) DbmsConnection(org.verdictdb.connection.DbmsConnection) JdbcQueryResult(org.verdictdb.connection.JdbcQueryResult) RelRoot(org.apache.calcite.rel.RelRoot) PreparedStatement(java.sql.PreparedStatement) VerdictSingleResultFromDbmsQueryResult(org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult) ResultSet(java.sql.ResultSet) SqlDialect(org.apache.calcite.sql.SqlDialect) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 43 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot 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 44 with RelRoot

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

the class QueryConverter method convert.

public QueryConvertResult convert(SqlNode node) {
    SqlToRelConverter converter = new HazelcastSqlToRelConverter(viewExpander, validator, catalogReader, cluster, StandardConvertletTable.INSTANCE, CONFIG);
    // 1. Perform initial conversion.
    RelRoot root = converter.convertQuery(node, false, true);
    // 2. Remove subquery expressions, converting them to Correlate nodes.
    RelNode relNoSubqueries = rewriteSubqueries(root.project());
    // 3. Perform decorrelation, i.e. rewrite a nested loop where the right side depends on the value of the left side,
    // to a variation of joins, semijoins and aggregations, which could be executed much more efficiently.
    // See "Unnesting Arbitrary Queries", Thomas Neumann and Alfons Kemper.
    RelNode result = converter.decorrelate(node, relNoSubqueries);
    // The bug is likely in decorrelation which produces LogicalAggregate with 0 output columns.
    if (!hasNestedExists(root.rel)) {
        result = converter.trimUnusedFields(true, result);
    }
    // 5. Collect original field names.
    return new QueryConvertResult(result, Pair.right(root.fields));
}
Also used : SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) HazelcastSqlToRelConverter(com.hazelcast.jet.sql.impl.HazelcastSqlToRelConverter) RelNode(org.apache.calcite.rel.RelNode) RelRoot(org.apache.calcite.rel.RelRoot) HazelcastSqlToRelConverter(com.hazelcast.jet.sql.impl.HazelcastSqlToRelConverter)

Example 45 with RelRoot

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

the class HazelcastViewExpander method expandView.

@Override
public RelRoot expandView(RelDataType rowType, String queryString, List<String> schemaPath, @Nullable List<String> viewPath) {
    if (expansionStack.contains(viewPath)) {
        throw QueryException.error("Cycle detected in view references");
    }
    expansionStack.push(viewPath);
    SqlNode sqlNode = parser.parse(queryString).getNode();
    final RelRoot root = sqlToRelConverter.convertQuery(sqlNode, true, true);
    expansionStack.pop();
    final RelRoot root2 = root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
    final RelBuilder relBuilder = QueryConverter.CONFIG.getRelBuilderFactory().create(relOptCluster, null);
    return root2.withRel(RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelRoot(org.apache.calcite.rel.RelRoot) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

RelRoot (org.apache.calcite.rel.RelRoot)77 SqlNode (org.apache.calcite.sql.SqlNode)30 RelNode (org.apache.calcite.rel.RelNode)22 Test (org.junit.jupiter.api.Test)18 NlsString (org.apache.calcite.util.NlsString)17 DingoTableScan (io.dingodb.calcite.rel.DingoTableScan)13 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)11 SqlToRelConverter (org.apache.calcite.sql2rel.SqlToRelConverter)11 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)9 SamzaSqlDslConverterFactory (org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory)8 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)7 RelDataType (org.apache.calcite.rel.type.RelDataType)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)7 DslConverter (org.apache.samza.sql.interfaces.DslConverter)7 Test (org.junit.Test)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 SqlParser (org.apache.calcite.sql.parser.SqlParser)6 Planner (org.apache.calcite.tools.Planner)6 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)5