use of org.apache.calcite.plan.RelTraitDef in project beam by apache.
the class ZetaSQLQueryPlanner method defaultConfig.
private static FrameworkConfig defaultConfig(JdbcConnection connection, Collection<RuleSet> ruleSets) {
final CalciteConnectionConfig config = connection.config();
final SqlParser.ConfigBuilder parserConfig = SqlParser.configBuilder().setQuotedCasing(config.quotedCasing()).setUnquotedCasing(config.unquotedCasing()).setQuoting(config.quoting()).setConformance(config.conformance()).setCaseSensitive(config.caseSensitive());
final SqlParserImplFactory parserFactory = config.parserFactory(SqlParserImplFactory.class, null);
if (parserFactory != null) {
parserConfig.setParserFactory(parserFactory);
}
final SchemaPlus schema = connection.getRootSchema();
final SchemaPlus defaultSchema = connection.getCurrentSchemaPlus();
final ImmutableList<RelTraitDef> traitDefs = ImmutableList.of(ConventionTraitDef.INSTANCE);
final CalciteCatalogReader catalogReader = new CalciteCatalogReader(CalciteSchema.from(schema), ImmutableList.of(defaultSchema.getName()), connection.getTypeFactory(), connection.config());
final SqlOperatorTable opTab0 = connection.config().fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
return Frameworks.newConfigBuilder().parserConfig(parserConfig.build()).defaultSchema(defaultSchema).traitDefs(traitDefs).ruleSets(ruleSets.toArray(new RuleSet[0])).costFactory(BeamCostModel.FACTORY).typeSystem(connection.getTypeFactory().getTypeSystem()).operatorTable(SqlOperatorTables.chain(opTab0, catalogReader)).build();
}
use of org.apache.calcite.plan.RelTraitDef 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);
}
}
use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.
the class RelOptRulesTest method testSortJoinTranspose4.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-931">[CALCITE-931]
* Wrong collation trait in SortJoinTransposeRule for right joins</a>.
*/
@Test
public void testSortJoinTranspose4() {
// Create a customized test with RelCollation trait in the test cluster.
Tester tester = new TesterImpl(getDiffRepos(), true, true, false, false, null, null) {
@Override
public RelOptPlanner createPlanner() {
return new MockRelOptPlanner(Contexts.empty()) {
@Override
public List<RelTraitDef> getRelTraitDefs() {
return ImmutableList.<RelTraitDef>of(RelCollationTraitDef.INSTANCE);
}
@Override
public RelTraitSet emptyTraitSet() {
return RelTraitSet.createEmpty().plus(RelCollationTraitDef.INSTANCE.getDefault());
}
};
}
};
final HepProgram preProgram = new HepProgramBuilder().addRuleInstance(SortProjectTransposeRule.INSTANCE).build();
final HepProgram program = new HepProgramBuilder().addRuleInstance(SortJoinTransposeRule.INSTANCE).build();
final String sql = "select * from sales.emp e right join (\n" + "select * from sales.dept d) using (deptno)\n" + "order by name";
checkPlanning(tester, preProgram, new HepPlanner(program), sql);
}
use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.
the class PlannerTest method testPlanWithExplicitTraitDefs.
/**
* Unit test that parses, validates, converts and plans. Planner is
* provided with a list of RelTraitDefs to register.
*/
@Test
public void testPlanWithExplicitTraitDefs() throws Exception {
RuleSet ruleSet = RuleSets.ofList(FilterMergeRule.INSTANCE, EnumerableRules.ENUMERABLE_FILTER_RULE, EnumerableRules.ENUMERABLE_PROJECT_RULE);
final List<RelTraitDef> traitDefs = new ArrayList<>();
traitDefs.add(ConventionTraitDef.INSTANCE);
traitDefs.add(RelCollationTraitDef.INSTANCE);
Planner planner = getPlanner(traitDefs, Programs.of(ruleSet));
SqlNode parse = planner.parse("select * from \"emps\"");
SqlNode validate = planner.validate(parse);
RelNode convert = planner.rel(validate).project();
RelTraitSet traitSet = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
RelNode transform = planner.transform(0, traitSet, convert);
assertThat(toString(transform), equalTo("EnumerableProject(empid=[$0], deptno=[$1], name=[$2], salary=[$3], commission=[$4])\n" + " EnumerableTableScan(table=[[hr, emps]])\n"));
}
use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.
the class PlannerTest method testOrderByNonSelectColumn.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-569">[CALCITE-569]
* ArrayIndexOutOfBoundsException when deducing collation</a>.
*/
@Test
public void testOrderByNonSelectColumn() throws Exception {
final SchemaPlus schema = Frameworks.createRootSchema(true).add("tpch", new ReflectiveSchema(new TpchSchema()));
String query = "select t.psPartkey from \n" + "(select ps.psPartkey from `tpch`.`partsupp` ps \n" + "order by ps.psPartkey, ps.psSupplyCost) t \n" + "order by t.psPartkey";
List<RelTraitDef> traitDefs = new ArrayList<>();
traitDefs.add(ConventionTraitDef.INSTANCE);
traitDefs.add(RelCollationTraitDef.INSTANCE);
final SqlParser.Config parserConfig = SqlParser.configBuilder().setLex(Lex.MYSQL).build();
FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(parserConfig).defaultSchema(schema).traitDefs(traitDefs).programs(Programs.ofRules(Programs.RULE_SET)).build();
String plan;
try (Planner p = Frameworks.getPlanner(config)) {
SqlNode n = p.parse(query);
n = p.validate(n);
RelNode r = p.rel(n).project();
plan = RelOptUtil.toString(r);
plan = Util.toLinux(plan);
}
assertThat(plan, equalTo("LogicalSort(sort0=[$0], dir0=[ASC])\n" + " LogicalProject(psPartkey=[$0])\n" + " LogicalProject(psPartkey=[$0])\n" + " LogicalSort(sort0=[$0], sort1=[$1], dir0=[ASC], dir1=[ASC])\n" + " LogicalProject(psPartkey=[$0], psSupplyCost=[$1])\n" + " EnumerableTableScan(table=[[tpch, partsupp]])\n"));
}
Aggregations