use of org.apache.calcite.config.CalciteConnectionConfigImpl in project calcite by apache.
the class RelOptRulesTest method testExtractYearToRange.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-434">[CALCITE-434]
* Converting predicates on date dimension columns into date ranges</a>,
* specifically a rule that converts {@code EXTRACT(YEAR FROM ...) = constant}
* to a range.
*/
@Test
public void testExtractYearToRange() {
final String sql = "select *\n" + "from sales.emp_b as e\n" + "where extract(year from birthdate) = 2014";
HepProgram program = new HepProgramBuilder().addRuleInstance(DateRangeRules.FILTER_INSTANCE).build();
final Context context = Contexts.of(new CalciteConnectionConfigImpl(new Properties()));
sql(sql).with(program).withContext(context).check();
}
use of org.apache.calcite.config.CalciteConnectionConfigImpl in project druid by druid-io.
the class DruidPlanner method getValidator.
/**
* Constructs an SQL validator, just like papa {@link #planner} uses.
*/
private SqlValidator getValidator() {
// this is sort of lame, planner won't cough up its validator, which is nice and seeded after validating a query,
// but it is private and has no accessors, so make another one so we can get the parameter types... but i suppose
// beats creating our own Prepare and Planner implementations
Preconditions.checkNotNull(planner.getTypeFactory());
final CalciteConnectionConfig connectionConfig;
if (frameworkConfig.getContext() != null) {
connectionConfig = frameworkConfig.getContext().unwrap(CalciteConnectionConfig.class);
} else {
Properties properties = new Properties();
properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), String.valueOf(PlannerFactory.PARSER_CONFIG.caseSensitive()));
connectionConfig = new CalciteConnectionConfigImpl(properties);
}
Prepare.CatalogReader catalogReader = new CalciteCatalogReader(CalciteSchema.from(frameworkConfig.getDefaultSchema().getParentSchema()), CalciteSchema.from(frameworkConfig.getDefaultSchema()).path(null), planner.getTypeFactory(), connectionConfig);
return SqlValidatorUtil.newValidator(frameworkConfig.getOperatorTable(), catalogReader, planner.getTypeFactory(), DruidConformance.instance());
}
use of org.apache.calcite.config.CalciteConnectionConfigImpl in project herddb by diennea.
the class CalcitePlanner method runPlanner.
private PlannerResult runPlanner(String defaultTableSpace, String query) throws RelConversionException, SqlParseException, ValidationException, MetadataStorageManagerException, StatementExecutionException {
try {
SchemaPlus subSchema = getSchemaForTableSpace(defaultTableSpace);
if (subSchema == null) {
clearCache();
throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
}
Properties props = new Properties();
props.put(CalciteConnectionProperty.TIME_ZONE.camelName(), TimeZone.getDefault().getID());
props.put(CalciteConnectionProperty.LOCALE.camelName(), Locale.ROOT.toString());
props.put(CalciteConnectionProperty.DEFAULT_NULL_COLLATION.camelName(), NullCollation.LAST.toString());
final CalciteConnectionConfigImpl calciteRuntimeContextConfig = new CalciteConnectionConfigImpl(props);
final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SQL_PARSER_CONFIG).defaultSchema(subSchema).traitDefs(TRAITS).context(new Context() {
@Override
public <C> C unwrap(Class<C> aClass) {
if (aClass == CalciteConnectionConfigImpl.class || aClass == CalciteConnectionConfig.class) {
return (C) calciteRuntimeContextConfig;
}
return null;
}
}).programs(Programs.ofRules(Programs.RULE_SET)).build();
Planner planner = new PlannerImpl(config);
if (LOG.isLoggable(Level.FINER)) {
LOG.log(Level.FINER, "Query: {0}", query);
}
try {
SqlNode n = planner.parse(query);
n = planner.validate(n);
RelNode logicalPlan = planner.rel(n).project();
if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[] { query, RelOptUtil.dumpPlan("-- Logical Plan", logicalPlan, SqlExplainFormat.TEXT, SqlExplainLevel.ALL_ATTRIBUTES) });
}
RelDataType originalRowType = logicalPlan.getRowType();
RelOptCluster cluster = logicalPlan.getCluster();
final RelOptPlanner optPlanner = cluster.getPlanner();
optPlanner.addRule(CoreRules.FILTER_REDUCE_EXPRESSIONS);
RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
final RelCollation collation = logicalPlan instanceof Sort ? ((Sort) logicalPlan).collation : null;
if (collation != null) {
desiredTraits = desiredTraits.replace(collation);
}
final RelNode newRoot = optPlanner.changeTraits(logicalPlan, desiredTraits);
optPlanner.setRoot(newRoot);
RelNode bestExp = optPlanner.findBestExp();
if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[] { query, RelOptUtil.dumpPlan("-- Best Plan", bestExp, SqlExplainFormat.TEXT, SqlExplainLevel.ALL_ATTRIBUTES) });
}
return new PlannerResult(bestExp, originalRowType, logicalPlan, n);
} catch (AssertionError err) {
throw new StatementExecutionException("Internal Calcite error " + err, err);
}
} catch (java.lang.LinkageError err) {
LOG.log(Level.SEVERE, "Error on Java Classpath", err);
throw new StatementExecutionException("Internal Calcite error " + err, err);
}
}
Aggregations