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);
}
}
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);
}
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);
}
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;
}
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));
}
Aggregations