Search in sources :

Example 61 with RelRoot

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

the class PlannerImpl method expandView.

@Override
public RelRoot expandView(RelDataType rowType, String queryString, List<String> schemaPath, @Nullable List<String> viewPath) {
    RelOptPlanner planner = this.planner;
    if (planner == null) {
        ready();
        planner = requireNonNull(this.planner, "planner");
    }
    SqlParser parser = SqlParser.create(queryString, parserConfig);
    SqlNode sqlNode;
    try {
        sqlNode = parser.parseQuery();
    } catch (SqlParseException e) {
        throw new RuntimeException("parse failed", e);
    }
    final CalciteCatalogReader catalogReader = createCatalogReader().withSchemaPath(schemaPath);
    final SqlValidator validator = createSqlValidator(catalogReader);
    final RexBuilder rexBuilder = createRexBuilder();
    final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    final SqlToRelConverter.Config config = sqlToRelConverterConfig.withTrimUnusedFields(false);
    final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(this, validator, catalogReader, cluster, convertletTable, config);
    final RelRoot root = sqlToRelConverter.convertQuery(sqlNode, true, false);
    final RelRoot root2 = root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
    final RelBuilder relBuilder = config.getRelBuilderFactory().create(cluster, null);
    return root2.withRel(RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) RelBuilder(org.apache.calcite.tools.RelBuilder) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlParser(org.apache.calcite.sql.parser.SqlParser) RelRoot(org.apache.calcite.rel.RelRoot) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) RexBuilder(org.apache.calcite.rex.RexBuilder) SqlNode(org.apache.calcite.sql.SqlNode)

Example 62 with RelRoot

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

the class SqlToRelConverterTest method testLarge.

@Test
void testLarge() {
    // Size factor used to be 400, but lambdas use a lot of stack
    final int x = 300;
    final SqlToRelFixture fixture = fixture();
    SqlValidatorTest.checkLarge(x, input -> {
        final RelRoot root = fixture.withSql(input).toRoot();
        final String s = RelOptUtil.toString(root.project());
        assertThat(s, notNullValue());
    });
}
Also used : RelRoot(org.apache.calcite.rel.RelRoot) Test(org.junit.jupiter.api.Test)

Example 63 with RelRoot

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

the class PlannerTest method testConvertWithoutValidateFails.

/**
 * Tests that planner throws an error if you pass to
 * {@link Planner#rel(org.apache.calcite.sql.SqlNode)}
 * a {@link org.apache.calcite.sql.SqlNode} that has been parsed but not
 * validated.
 */
@Test
void testConvertWithoutValidateFails() throws Exception {
    Planner planner = getPlanner(null);
    SqlNode parse = planner.parse("select * from \"emps\"");
    try {
        RelRoot rel = planner.rel(parse);
        fail("expected error, got " + rel);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("cannot move from STATE_3_PARSED to STATE_4_VALIDATED"));
    }
}
Also used : RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelRoot(org.apache.calcite.rel.RelRoot) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.jupiter.api.Test) RelBuilderTest(org.apache.calcite.test.RelBuilderTest)

Example 64 with RelRoot

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

the class ExtensionDdlExecutor 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(Objects.requireNonNull(Schemas.subSchema(context.getRootSchema(), context.getDefaultSchemaPath())).plus()).build();
    final Planner planner = Frameworks.getPlanner(config);
    try {
        final StringBuilder buf = new StringBuilder();
        final SqlPrettyWriter w = new SqlPrettyWriter(SqlPrettyWriter.config().withDialect(CalciteSqlDialect.DEFAULT).withAlwaysUseParentheses(false), buf);
        buf.append("INSERT INTO ");
        name.unparse(w, 0, 0);
        buf.append(" ");
        query.unparse(w, 0, 0);
        final String sql = buf.toString();
        final SqlNode query1 = planner.parse(sql);
        final SqlNode query2 = planner.validate(query1);
        final RelRoot r = planner.rel(query2);
        final PreparedStatement prepare = context.getRelRunner().prepareStatement(r.rel);
        int rowCount = prepare.executeUpdate();
        Util.discard(rowCount);
        prepare.close();
    } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
        throw Util.throwAsRuntime(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) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 65 with RelRoot

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

the class SortRemoveRuleTest method transform.

/**
 * The default schema that is used in these tests provides tables sorted on the primary key. Due
 * to this scan operators always come with a {@link org.apache.calcite.rel.RelCollation} trait.
 */
private RelNode transform(String sql, RuleSet prepareRules) throws Exception {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final SchemaPlus defSchema = rootSchema.add("hr", new HrClusteredSchema());
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(defSchema).traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE).programs(Programs.of(prepareRules), Programs.ofRules(CoreRules.SORT_REMOVE)).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelRoot planRoot = planner.rel(validate);
    RelNode planBefore = planRoot.rel;
    RelTraitSet desiredTraits = planBefore.getTraitSet().replace(EnumerableConvention.INSTANCE);
    RelNode planAfter = planner.transform(0, desiredTraits, planBefore);
    return planner.transform(1, desiredTraits, planAfter);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) SchemaPlus(org.apache.calcite.schema.SchemaPlus) HrClusteredSchema(org.apache.calcite.schemas.HrClusteredSchema) Planner(org.apache.calcite.tools.Planner) RelRoot(org.apache.calcite.rel.RelRoot) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) RelTraitSet(org.apache.calcite.plan.RelTraitSet) 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