use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class CalciteMaterializer method populate.
/**
* Populates a materialization record, converting a table path
* (essentially a list of strings, like ["hr", "sales"]) into a table object
* that can be used in the planning process.
*/
void populate(Materialization materialization) {
SqlParser parser = SqlParser.create(materialization.sql);
SqlNode node;
try {
node = parser.parseStmt();
} catch (SqlParseException e) {
throw new RuntimeException("parse failed", e);
}
final SqlToRelConverter.Config config = SqlToRelConverter.config().withTrimUnusedFields(true);
SqlToRelConverter sqlToRelConverter2 = getSqlToRelConverter(getSqlValidator(), catalogReader, config);
RelRoot root = sqlToRelConverter2.convertQuery(node, true, true);
materialization.queryRel = trimUnusedFields(root).rel;
// Identify and substitute a StarTable in queryRel.
//
// It is possible that no StarTables match. That is OK, but the
// materialization patterns that are recognized will not be as rich.
//
// It is possible that more than one StarTable matches. TBD: should we
// take the best (whatever that means), or all of them?
useStar(schema, materialization);
List<String> tableName = materialization.materializedTable.path();
RelOptTable table = requireNonNull(this.catalogReader.getTable(tableName), () -> "table " + tableName + " is not found");
materialization.tableRel = sqlToRelConverter2.toRel(table, ImmutableList.of());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class PlannerImpl method rel.
@Override
public RelRoot rel(SqlNode sql) {
ensure(State.STATE_4_VALIDATED);
SqlNode validatedSqlNode = requireNonNull(this.validatedSqlNode, "validatedSqlNode is null. Need to call #validate() first");
final RexBuilder rexBuilder = createRexBuilder();
final RelOptCluster cluster = RelOptCluster.create(requireNonNull(planner, "planner"), rexBuilder);
final SqlToRelConverter.Config config = sqlToRelConverterConfig.withTrimUnusedFields(false);
final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(this, validator, createCatalogReader(), cluster, convertletTable, config);
RelRoot root = sqlToRelConverter.convertQuery(validatedSqlNode, false, true);
root = root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
final RelBuilder relBuilder = config.getRelBuilderFactory().create(cluster, null);
root = root.withRel(RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
state = State.STATE_5_CONVERTED;
return root;
}
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.Config config = SqlToRelConverter.config().withTrimUnusedFields(true).withExpand(castNonNull(THREAD_EXPAND.get())).withInSubQueryThreshold(castNonNull(THREAD_INSUBQUERY_THRESHOLD.get())).withExplain(sqlQuery.getKind() == SqlKind.EXPLAIN);
final Holder<SqlToRelConverter.Config> configHolder = Holder.of(config);
Hook.SQL2REL_CONVERTER_CONFIG_BUILDER.run(configHolder);
final SqlToRelConverter sqlToRelConverter = getSqlToRelConverter(validator, catalogReader, configHolder.get());
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);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class AbstractSqlTester method convertSqlToRel2.
@Override
public Pair<SqlValidator, RelRoot> convertSqlToRel2(SqlTestFactory factory, String sql, boolean decorrelate, boolean trim) {
requireNonNull(sql, "sql");
final SqlNode sqlQuery;
try {
sqlQuery = parseQuery(factory, sql);
} catch (RuntimeException | Error e) {
throw e;
} catch (Exception e) {
throw TestUtil.rethrow(e);
}
final SqlToRelConverter converter = factory.createSqlToRelConverter();
final SqlValidator validator = requireNonNull(converter.validator);
final SqlNode validatedQuery = validator.validate(sqlQuery);
RelRoot root = converter.convertQuery(validatedQuery, false, true);
requireNonNull(root, "root");
if (decorrelate || trim) {
root = root.withRel(converter.flattenTypes(root.rel, true));
}
if (decorrelate) {
root = root.withRel(converter.decorrelate(sqlQuery, root.rel));
}
if (trim) {
root = root.withRel(converter.trimUnusedFields(true, root.rel));
}
return Pair.of(validator, root);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class AbstractSqlTester method assertSqlConvertsTo.
private void assertSqlConvertsTo(SqlTestFactory factory, DiffRepository diffRepos, String sql, String plan, boolean trim, boolean decorrelate) {
String sql2 = diffRepos.expand("sql", sql);
final Pair<SqlValidator, RelRoot> pair = convertSqlToRel2(factory, sql2, decorrelate, trim);
final RelRoot root = requireNonNull(pair.right);
final SqlValidator validator = requireNonNull(pair.left);
RelNode rel = root.project();
assertNotNull(rel);
assertThat(rel, relIsValid());
if (trim) {
final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
final RelFieldTrimmer trimmer = createFieldTrimmer(validator, relBuilder);
rel = trimmer.trim(rel);
assertNotNull(rel);
assertThat(rel, relIsValid());
}
// NOTE jvs 28-Mar-2006: insert leading newline so
// that plans come out nicely stacked instead of first
// line immediately after CDATA start
String actual = NL + RelOptUtil.toString(rel);
diffRepos.assertEquals("plan", plan, actual);
}
Aggregations