org.apache.calcite.config.CalciteConnectionConfigImpl Java Examples

The following examples show how to use org.apache.calcite.config.CalciteConnectionConfigImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractMaterializedViewTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelNode toRel(RelOptCluster cluster, SchemaPlus rootSchema,
    SchemaPlus defaultSchema, String sql) throws SqlParseException {
  final SqlParser parser = SqlParser.create(sql, SqlParser.Config.DEFAULT);
  final SqlNode parsed = parser.parseStmt();

  final CalciteCatalogReader catalogReader = new CalciteCatalogReader(
      CalciteSchema.from(rootSchema),
      CalciteSchema.from(defaultSchema).path(null),
      new JavaTypeFactoryImpl(), new CalciteConnectionConfigImpl(new Properties()));

  final SqlValidator validator = new ValidatorForTest(SqlStdOperatorTable.instance(),
      catalogReader, new JavaTypeFactoryImpl(), SqlConformanceEnum.DEFAULT);
  final SqlNode validated = validator.validate(parsed);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
      .withTrimUnusedFields(true)
      .withExpand(true)
      .withDecorrelationEnabled(true)
      .build();
  final SqlToRelConverter converter = new SqlToRelConverter(
      (rowType, queryString, schemaPath, viewPath) -> {
        throw new UnsupportedOperationException("cannot expand view");
      }, validator, catalogReader, cluster, StandardConvertletTable.INSTANCE, config);
  return converter.convertQuery(validated, false, true).rel;
}
 
Example #2
Source File: SqlToRelConverterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2323">[CALCITE-2323]
 * Validator should allow alternative nullCollations for ORDER BY in
 * OVER</a>. */
@Test void testUserDefinedOrderByOver() {
  String sql = "select deptno,\n"
      + "  rank() over(partition by empno order by deptno)\n"
      + "from emp\n"
      + "order by row_number() over(partition by empno order by deptno)";
  Properties properties = new Properties();
  properties.setProperty(
      CalciteConnectionProperty.DEFAULT_NULL_COLLATION.camelName(),
      NullCollation.LOW.name());
  CalciteConnectionConfigImpl connectionConfig =
      new CalciteConnectionConfigImpl(properties);
  TesterImpl tester = new TesterImpl(getDiffRepos(), false, false, true, false, true,
      null, null, SqlToRelConverter.Config.DEFAULT,
      SqlConformanceEnum.DEFAULT, Contexts.of(connectionConfig));
  sql(sql).with(tester).ok();
}
 
Example #3
Source File: QuarkConnectionImpl.java    From quark with Apache License 2.0 6 votes vote down vote up
protected QuarkConnectionImpl(QuarkDriver driver, AvaticaFactory factory, String url,
                              Properties info, CalciteRootSchema rootSchema,
                              JavaTypeFactory typeFactory) throws SQLException {
  super(driver, factory, url, info);

  CalciteConnectionConfig cfg = new CalciteConnectionConfigImpl(info);

  if (typeFactory != null) {
    this.typeFactory = typeFactory;
  } else {
    final RelDataTypeSystem typeSystem =
        cfg.typeSystem(RelDataTypeSystem.class, RelDataTypeSystem.DEFAULT);
    this.typeFactory = new JavaTypeFactoryImpl(typeSystem);
  }

  this.properties.put(InternalProperty.CASE_SENSITIVE, cfg.caseSensitive());
  this.properties.put(InternalProperty.UNQUOTED_CASING, cfg.unquotedCasing());
  this.properties.put(InternalProperty.QUOTED_CASING, cfg.quotedCasing());
  this.properties.put(InternalProperty.QUOTING, cfg.quoting());
}
 
Example #4
Source File: SqlExprToRexConverterImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a catalog reader that contains a single {@link Table} with temporary table name
 * and specified {@code rowType}.
 *
 * @param rowType     table row type
 * @return the {@link CalciteCatalogReader} instance
 */
private static CalciteCatalogReader createSingleTableCatalogReader(
		boolean lenientCaseSensitivity,
		FrameworkConfig config,
		FlinkTypeFactory typeFactory,
		RelDataType rowType) {

	// connection properties
	boolean caseSensitive = !lenientCaseSensitivity && config.getParserConfig().caseSensitive();
	Properties properties = new Properties();
	properties.put(
		CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
		String.valueOf(caseSensitive));
	CalciteConnectionConfig connectionConfig = new CalciteConnectionConfigImpl(properties);

	// prepare root schema
	final RowTypeSpecifiedTable table = new RowTypeSpecifiedTable(rowType);
	final Map<String, Table> tableMap = Collections.singletonMap(TEMPORARY_TABLE_NAME, table);
	CalciteSchema schema = CalciteSchemaBuilder.asRootSchema(new TableSpecifiedSchema(tableMap));

	return new FlinkCalciteCatalogReader(
		schema,
		new ArrayList<>(new ArrayList<>()),
		typeFactory,
		connectionConfig);
}
 
Example #5
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 6 votes vote down vote up
static CalciteCatalogReader createCalciteCatalogReader(RelDataTypeFactory typeFactory) {
    CalciteSchema rootSchema = CalciteSchema.createRootSchema(true);
    SchemaPlus schemaPlus = rootSchema.plus();

    String schemaName = "my_schema";
    List<String> defaultSchema = new ArrayList<>();
    defaultSchema.add(schemaName);

    CalciteSchema subSchema = CalciteSchema.createRootSchema(true);
    SchemaPlus subSchemaPlus = subSchema.plus();
    subSchemaPlus.add("test", createTable());
    schemaPlus.add(schemaName, subSchemaPlus);

    CalciteSchema subSchema2 = CalciteSchema.createRootSchema(true);
    SchemaPlus subSchemaPlus2 = subSchema2.plus();
    schemaPlus.add("my_schema2", subSchemaPlus2);
    subSchemaPlus2.add("test2", createTable());

    CalciteConnectionConfig config = new CalciteConnectionConfigImpl(new Properties());
    return new CalciteCatalogReader(rootSchema, defaultSchema, typeFactory, config);
}
 
Example #6
Source File: TpcdsLatticeSuggesterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
Tester tpcds() {
  final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
  final double scaleFactor = 0.01d;
  final SchemaPlus schema =
      rootSchema.add("tpcds", new TpcdsSchema(scaleFactor));
  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .parserConfig(SqlParser.Config.DEFAULT)
      .context(
          Contexts.of(
              new CalciteConnectionConfigImpl(new Properties())
                  .set(CalciteConnectionProperty.CONFORMANCE,
                      SqlConformanceEnum.LENIENT.name())))
      .defaultSchema(schema)
      .build();
  return withConfig(config);
}
 
Example #7
Source File: FlinkCalciteCatalogReaderTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
	rootSchemaPlus = CalciteSchema.createRootSchema(true, false).plus();
	Properties prop = new Properties();
	prop.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), "false");
	CalciteConnectionConfigImpl calciteConnConfig = new CalciteConnectionConfigImpl(prop);
	catalogReader = new FlinkCalciteCatalogReader(
		CalciteSchema.from(rootSchemaPlus),
		Collections.emptyList(),
		typeFactory,
		calciteConnConfig);
}
 
Example #8
Source File: CalciteAssert.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a {@link FrameworkConfig} that does not decorrelate. */
private FrameworkConfig forceDecorrelate(FrameworkConfig config) {
  return Frameworks.newConfigBuilder(config)
      .context(
          Contexts.of(new CalciteConnectionConfigImpl(new Properties())
              .set(CalciteConnectionProperty.FORCE_DECORRELATE,
                  Boolean.toString(false))))
      .build();
}
 
Example #9
Source File: Schemas.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static CalciteConnectionConfig mutate(CalciteConnectionConfig config,
    ImmutableMap<CalciteConnectionProperty, String> propValues) {
  for (Map.Entry<CalciteConnectionProperty, String> e
      : propValues.entrySet()) {
    config =
        ((CalciteConnectionConfigImpl) config).set(e.getKey(), e.getValue());
  }
  return config;
}
 
Example #10
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a catalog reader that contains a single {@link Table} with temporary table name
 * and specified {@code rowType}.
 *
 * <p>Make this method public so that other systems can also use it.
 *
 * @param caseSensitive whether to match case sensitively
 * @param tableName     table name to register with
 * @param typeFactory   type factory
 * @param rowType       table row type
 * @return the {@link CalciteCatalogReader} instance
 */
public static CalciteCatalogReader createSingleTableCatalogReader(
    boolean caseSensitive,
    String tableName,
    RelDataTypeFactory typeFactory,
    RelDataType rowType) {
  // connection properties
  Properties properties = new Properties();
  properties.put(
      CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
      String.valueOf(caseSensitive));
  CalciteConnectionConfig connectionConfig = new CalciteConnectionConfigImpl(properties);

  // prepare root schema
  final ExplicitRowTypeTable table = new ExplicitRowTypeTable(rowType);
  final Map<String, Table> tableMap = Collections.singletonMap(tableName, table);
  CalciteSchema schema = CalciteSchema.createRootSchema(
      false,
      false,
      "",
      new ExplicitTableSchema(tableMap));

  return new CalciteCatalogReader(
      schema,
      new ArrayList<>(new ArrayList<>()),
      typeFactory,
      connectionConfig);
}
 
Example #11
Source File: ContextSqlValidator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static CalciteCatalogReader getCatalogReader(
    CalcitePrepare.Context context, boolean mutable) {
  return new CalciteCatalogReader(
      mutable ? context.getMutableRootSchema() : context.getRootSchema(),
      ImmutableList.of(),
      context.getTypeFactory(),
      new CalciteConnectionConfigImpl(new Properties()));
}
 
Example #12
Source File: TableEnv.java    From marble with Apache License 2.0 5 votes vote down vote up
private CalciteConnectionConfig createDefaultConnectionConfig(
    SqlParser.Config sqlParserConfig) {
  Properties prop = new Properties();
  prop.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
      String.valueOf(sqlParserConfig.caseSensitive()));
  return new CalciteConnectionConfigImpl(prop);
}
 
Example #13
Source File: HiveTableEnv.java    From marble with Apache License 2.0 5 votes vote down vote up
public static TableEnv getTableEnv() {
    TableConfig tableConfig = new TableConfig();
    tableConfig.setSqlOperatorTable(
        ChainedSqlOperatorTable.of(HiveSqlOperatorTable.instance(),
            SqlStdOperatorTable.instance()));
    tableConfig.setSqlParserConfig(SqlParser
        .configBuilder()
        .setLex(Lex.JAVA).setCaseSensitive(false).setConformance(
            SqlConformanceEnum.HIVE)
        .setParserFactory(HiveSqlParserImpl.FACTORY)
        .build());
//    tableConfig.setRelDataTypeSystem(new HiveTypeSystemImpl());
    Properties prop = new Properties();
    prop.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
        String.valueOf(tableConfig.getSqlParserConfig().caseSensitive()));
    tableConfig.setCalciteConnectionConfig(
        new CalciteConnectionConfigImpl(prop));
    tableConfig.setConvertletTable(new HiveConvertletTable());
    RexExecutor rexExecutor = new HiveRexExecutorImpl(
        Schemas.createDataContext(null, null));
    tableConfig.setRexExecutor(rexExecutor);
    TableEnv tableEnv = new HiveTableEnv(tableConfig);
    //add table functions
    tableEnv.addFunction("", "explode",
        "org.apache.calcite.adapter.hive.udtf.UDTFExplode", "eval");
    return tableEnv;
  }
 
Example #14
Source File: FlinkCalciteCatalogReaderTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
	rootSchemaPlus = CalciteSchema.createRootSchema(true, false).plus();
	Properties prop = new Properties();
	prop.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), "false");
	CalciteConnectionConfigImpl calciteConnConfig = new CalciteConnectionConfigImpl(prop);
	catalogReader = new FlinkCalciteCatalogReader(
		CalciteSchema.from(rootSchemaPlus),
		Collections.emptyList(),
		typeFactory,
		calciteConnConfig);
}
 
Example #15
Source File: SqlConverter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates {@link CalciteConnectionConfigImpl} instance with specified caseSensitive property.
 *
 * @param caseSensitive is case sensitive.
 * @return {@link CalciteConnectionConfigImpl} instance
 */
private static CalciteConnectionConfigImpl getConnectionConfig(boolean caseSensitive) {
  Properties properties = new Properties();
  properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
      String.valueOf(caseSensitive));
  return new CalciteConnectionConfigImpl(properties);
}
 
Example #16
Source File: QuarkConnectionImpl.java    From quark with Apache License 2.0 4 votes vote down vote up
public CalciteConnectionConfig config() {
  return new CalciteConnectionConfigImpl(info);
}
 
Example #17
Source File: CalciteConnectionImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CalciteConnectionConfig config() {
  return new CalciteConnectionConfigImpl(info);
}
 
Example #18
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 4 votes vote down vote up
private PlannerResult runPlanner(String defaultTableSpace, String query) throws RelConversionException,
        SqlParseException, ValidationException, MetadataStorageManagerException, StatementExecutionException {
    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());
    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;
                }
            })
            // define the rules you want to apply
            .programs(Programs.ofRules(Programs.RULE_SET))
            .build();
    Planner planner = Frameworks.getPlanner(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(ReduceExpressionsRule.FILTER_INSTANCE);
        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);
    }
}
 
Example #19
Source File: FrameworksTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Unit test for {@link CalciteConnectionConfigImpl#set}
 * and {@link CalciteConnectionConfigImpl#isSet}. */
@Test void testConnectionConfig() {
  final CalciteConnectionProperty forceDecorrelate =
      CalciteConnectionProperty.FORCE_DECORRELATE;
  final CalciteConnectionProperty lenientOperatorLookup =
      CalciteConnectionProperty.LENIENT_OPERATOR_LOOKUP;
  final CalciteConnectionProperty caseSensitive =
      CalciteConnectionProperty.CASE_SENSITIVE;
  final CalciteConnectionProperty model = CalciteConnectionProperty.MODEL;

  final Properties p = new Properties();
  p.setProperty(forceDecorrelate.camelName(),
      Boolean.toString(false));
  p.setProperty(lenientOperatorLookup.camelName(),
      Boolean.toString(false));

  final CalciteConnectionConfigImpl c = new CalciteConnectionConfigImpl(p);

  assertThat(c.lenientOperatorLookup(), is(false));
  assertThat(c.isSet(lenientOperatorLookup), is(true));
  assertThat(c.caseSensitive(), is(true));
  assertThat(c.isSet(caseSensitive), is(false));
  assertThat(c.forceDecorrelate(), is(false));
  assertThat(c.isSet(forceDecorrelate), is(true));
  assertThat(c.model(), nullValue());
  assertThat(c.isSet(model), is(false));

  final CalciteConnectionConfigImpl c2 = c
      .set(lenientOperatorLookup, Boolean.toString(true))
      .set(caseSensitive, Boolean.toString(true));

  assertThat(c2.lenientOperatorLookup(), is(true));
  assertThat(c2.isSet(lenientOperatorLookup), is(true));
  assertThat("same value as for c", c2.caseSensitive(), is(true));
  assertThat("set to the default value", c2.isSet(caseSensitive), is(true));
  assertThat(c2.forceDecorrelate(), is(false));
  assertThat(c2.isSet(forceDecorrelate), is(true));
  assertThat(c2.model(), nullValue());
  assertThat(c2.isSet(model), is(false));
  assertThat("retrieves default because not set", c2.schema(), nullValue());

  // Create a config similar to c2 but starting from an empty Properties.
  final CalciteConnectionConfigImpl c3 =
      new CalciteConnectionConfigImpl(new Properties());
  final CalciteConnectionConfigImpl c4 = c3
      .set(lenientOperatorLookup, Boolean.toString(true))
      .set(caseSensitive, Boolean.toString(true));
  assertThat(c4.lenientOperatorLookup(), is(true));
  assertThat(c4.isSet(lenientOperatorLookup), is(true));
  assertThat(c4.caseSensitive(), is(true));
  assertThat("set to the default value", c4.isSet(caseSensitive), is(true));
  assertThat("different from c2", c4.forceDecorrelate(), is(true));
  assertThat("different from c2", c4.isSet(forceDecorrelate), is(false));
  assertThat(c4.model(), nullValue());
  assertThat(c4.isSet(model), is(false));
  assertThat("retrieves default because not set", c4.schema(), nullValue());

  // Call 'unset' on a few properties.
  final CalciteConnectionConfigImpl c5 = c2.unset(lenientOperatorLookup);
  assertThat(c5.isSet(lenientOperatorLookup), is(false));
  assertThat(c5.lenientOperatorLookup(), is(false));
  assertThat(c5.isSet(caseSensitive), is(true));
  assertThat(c5.caseSensitive(), is(true));

  // Call 'set' on properties that have already been set.
  final CalciteConnectionConfigImpl c6 = c5
      .set(lenientOperatorLookup, Boolean.toString(false))
      .set(forceDecorrelate, Boolean.toString(true));
  assertThat(c6.isSet(lenientOperatorLookup), is(true));
  assertThat(c6.lenientOperatorLookup(), is(false));
  assertThat(c6.isSet(caseSensitive), is(true));
  assertThat(c6.caseSensitive(), is(true));
  assertThat(c6.isSet(forceDecorrelate), is(true));
  assertThat(c6.forceDecorrelate(), is(true));
}