use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project beam by apache.
the class BeamAggregationRel method beamComputeSelfCost.
@Override
public BeamCostModel beamComputeSelfCost(RelOptPlanner planner, BeamRelMetadataQuery mq) {
NodeStats inputStat = BeamSqlRelUtils.getNodeStats(this.input, mq);
inputStat = computeWindowingCostEffect(inputStat);
// Aggregates with more aggregate functions cost a bit more
float multiplier = 1f + (float) aggCalls.size() * 0.125f;
for (AggregateCall aggCall : aggCalls) {
if (aggCall.getAggregation().getName().equals("SUM")) {
// Pretend that SUM costs a little bit more than $SUM0,
// to make things deterministic.
multiplier += 0.0125f;
}
}
return BeamCostModel.FACTORY.makeCost(inputStat.getRowCount() * multiplier, inputStat.getRate() * multiplier);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner 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);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project samza by apache.
the class QueryPlanner method plan.
public RelRoot plan(String query) {
try (Planner planner = getPlanner()) {
SqlNode sql = planner.parse(query);
SqlNode validatedSql = planner.validate(sql);
RelRoot relRoot = planner.rel(validatedSql);
LOG.info("query plan without optimization:\n" + RelOptUtil.toString(relRoot.rel, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
if (!isQueryPlanOptimizerEnabled) {
LOG.info("Skipping query optimization as it is disabled.");
return relRoot;
}
return optimize(planner, relRoot);
} catch (Exception e) {
String errorMsg = SamzaSqlValidator.formatErrorString(query, e);
LOG.error(errorMsg, e);
throw new SamzaException(errorMsg, e);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner 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.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project calcite by apache.
the class LexCaseSensitiveTest method runProjectQueryWithLex.
private static void runProjectQueryWithLex(Lex lex, String sql) throws SqlParseException, ValidationException, RelConversionException {
Config javaLex = SqlParser.configBuilder().setLex(lex).build();
Planner planner = getPlanner(null, javaLex, Programs.ofRules(Programs.RULE_SET));
SqlNode parse = planner.parse(sql);
SqlNode validate = planner.validate(parse);
RelNode convert = planner.rel(validate).rel;
RelTraitSet traitSet = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
RelNode transform = planner.transform(0, traitSet, convert);
assertThat(transform, instanceOf(EnumerableProject.class));
List<String> fieldNames = transform.getRowType().getFieldNames();
assertThat(fieldNames.size(), is(2));
if (lex.caseSensitive) {
assertThat(fieldNames.get(0), is("EMPID"));
assertThat(fieldNames.get(1), is("empid"));
} else {
assertThat(fieldNames.get(0) + "-" + fieldNames.get(1), anyOf(is("EMPID-empid0"), is("EMPID0-empid")));
}
}
Aggregations