org.apache.calcite.config.CalciteConnectionConfig Java Examples

The following examples show how to use org.apache.calcite.config.CalciteConnectionConfig. 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: 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 #2
Source File: TableEnv.java    From marble with Apache License 2.0 6 votes vote down vote up
private FrameworkConfig createFrameworkConfig(
    SqlParser.Config sqlParserConfig, SqlOperatorTable sqlOperatorTable,
    SqlRexConvertletTable convertletTable,
    CalciteConnectionConfig calciteConnectionConfig,
    RelDataTypeSystem relDataTypeSystem, RexExecutor rexExecutor) {
  return Frameworks
      .newConfigBuilder()
      .defaultSchema(rootSchema)
      .parserConfig(sqlParserConfig)
      .operatorTable(sqlOperatorTable)
      .convertletTable(convertletTable)
      .typeSystem(relDataTypeSystem)
      .executor(rexExecutor)
      .context(Contexts.of(calciteConnectionConfig))
      .build();
}
 
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: FlinkCalciteCatalogReader.java    From flink with Apache License 2.0 6 votes vote down vote up
public FlinkCalciteCatalogReader(
	CalciteSchema rootSchema,
	List<List<String>> defaultSchemas,
	RelDataTypeFactory typeFactory,
	CalciteConnectionConfig config) {

	super(
		rootSchema,
		SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
			Stream.concat(
				defaultSchemas.stream(),
				Stream.of(Collections.<String>emptyList())
			).collect(Collectors.toList()),
		typeFactory,
		config);
}
 
Example #5
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 #6
Source File: FlinkCalciteCatalogReader.java    From flink with Apache License 2.0 6 votes vote down vote up
public FlinkCalciteCatalogReader(
	CalciteSchema rootSchema,
	List<List<String>> defaultSchemas,
	RelDataTypeFactory typeFactory,
	CalciteConnectionConfig config) {

	super(
		rootSchema,
		SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
			Stream.concat(
				defaultSchemas.stream(),
				Stream.of(Collections.<String>emptyList())
			).collect(Collectors.toList()),
		typeFactory,
		config);
}
 
Example #7
Source File: DateRangeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Filter filter = call.rel(0);
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final String timeZone = filter.getCluster().getPlanner().getContext()
      .unwrap(CalciteConnectionConfig.class).timeZone();
  final RexNode condition =
      replaceTimeUnits(rexBuilder, filter.getCondition(), timeZone);
  if (condition.equals(filter.getCondition())) {
    return;
  }
  final RelBuilder relBuilder =
      relBuilderFactory.create(filter.getCluster(), null);
  relBuilder.push(filter.getInput())
      .filter(condition);
  call.transformTo(relBuilder.build());
}
 
Example #8
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void executeDdl(Context context, SqlNode node) {
  final CalciteConnectionConfig config = context.config();
  final SqlParserImplFactory parserFactory =
      config.parserFactory(SqlParserImplFactory.class, SqlParserImpl.FACTORY);
  final DdlExecutor ddlExecutor = parserFactory.getDdlExecutor();
  ddlExecutor.executeDdl(context, node);
}
 
Example #9
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final CalciteConnectionConfig config =
      planner.getContext().unwrap(CalciteConnectionConfig.class);
  if (config != null && config.forceDecorrelate()) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return RelDecorrelator.decorrelateQuery(rel, relBuilder);
  }
  return rel;
}
 
Example #10
Source File: CalciteCatalogReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
public CalciteCatalogReader(CalciteSchema rootSchema,
    List<String> defaultSchema, RelDataTypeFactory typeFactory, CalciteConnectionConfig config) {
  this(rootSchema, SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
      ImmutableList.of(Objects.requireNonNull(defaultSchema),
          ImmutableList.of()),
      typeFactory, config);
}
 
Example #11
Source File: CalciteCatalogReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected CalciteCatalogReader(CalciteSchema rootSchema,
    SqlNameMatcher nameMatcher, List<List<String>> schemaPaths,
    RelDataTypeFactory typeFactory, CalciteConnectionConfig config) {
  this.rootSchema = Objects.requireNonNull(rootSchema);
  this.nameMatcher = nameMatcher;
  this.schemaPaths =
      Util.immutableCopy(Util.isDistinct(schemaPaths)
          ? schemaPaths
          : new LinkedHashSet<>(schemaPaths));
  this.typeFactory = typeFactory;
  this.config = config;
}
 
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: CatalogReader.java    From flink with Apache License 2.0 5 votes vote down vote up
public CatalogReader(
		CalciteSchema rootSchema,
		List<List<String>> defaultSchema,
		RelDataTypeFactory typeFactory,
		CalciteConnectionConfig config) {
	super(rootSchema,
		SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
		Stream.concat(
			defaultSchema.stream(),
			Stream.of(Collections.<String>emptyList())
		).collect(Collectors.toList()),
		typeFactory,
		config);
}
 
Example #14
Source File: PlannerSettings.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T unwrap(Class<T> clazz) {
  if(clazz == PlannerSettings.class){
    return (T) this;
  } else if(clazz == CalciteConnectionConfig.class){
    return (T) CONFIG;
  } if (clazz == SabotConfig.class) {
    return (T) sabotConfig;
  } else if (CancelFlag.class.isAssignableFrom(clazz)) {
    return clazz.cast(cancelFlag);
  }
  return null;
}
 
Example #15
Source File: DateRangeRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
    final Filter filter = call.rel(0);
    final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
    final String timeZone = filter.getCluster().getPlanner().getContext().unwrap(CalciteConnectionConfig.class)
            .timeZone();
    final RexNode condition = replaceTimeUnits(rexBuilder, filter.getCondition(), timeZone);
    if (condition.equals(filter.getCondition())) {
        return;
    }
    final RelBuilder relBuilder = relBuilderFactory.create(filter.getCluster(), null);
    relBuilder.push(filter.getInput()).filter(condition);
    call.transformTo(relBuilder.build());
}
 
Example #16
Source File: RelToSqlConverterStructsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean rolledUpColumnValidInsideAgg(
    String column,
    SqlCall call,
    SqlNode parent,
    CalciteConnectionConfig config) {
  return false;
}
 
Example #17
Source File: SqlQueryParser.java    From quark with Apache License 2.0 5 votes vote down vote up
public SqlParser getSqlParser(String sql) {
  try {
    final CalciteConnectionConfig config = context.getCfg();
    return SqlParser.create(sql,
        SqlParser.configBuilder()
            .setQuotedCasing(config.quotedCasing())
            .setUnquotedCasing(config.unquotedCasing())
            .setQuoting(config.quoting())
            .build());
  } catch (Exception e) {
    return SqlParser.create(sql);
  }
}
 
Example #18
Source File: QueryContext.java    From quark with Apache License 2.0 5 votes vote down vote up
public CalcitePrepare.Context getPrepareContext() {
  return new CalcitePrepare.Context() {

    @Override
    public JavaTypeFactory getTypeFactory() {
      return typeFactory;
    }

    @Override
    public CalciteSchema getRootSchema() {
      return CalciteSchema.from(rootSchema);
    }

    @Override
    public List<String> getDefaultSchemaPath() {
      return defaultSchema;
    }

    @Override
    public CalciteConnectionConfig config() {
      return cfg;
    }

    @Override
    public CalcitePrepare.SparkHandler spark() {
      return CalcitePrepare.Dummy.getSparkHandler(false);
    }

    @Override
    public DataContext getDataContext() {
      return null;
    }
  };
}
 
Example #19
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 #20
Source File: CatalogReader.java    From flink with Apache License 2.0 5 votes vote down vote up
public CatalogReader(
		CalciteSchema rootSchema,
		List<List<String>> defaultSchema,
		RelDataTypeFactory typeFactory,
		CalciteConnectionConfig config) {
	super(rootSchema,
		SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
		Stream.concat(
			defaultSchema.stream(),
			Stream.of(Collections.<String>emptyList())
		).collect(Collectors.toList()),
		typeFactory,
		config);
}
 
Example #21
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final CalciteConnectionConfig config =
      planner.getContext().unwrap(CalciteConnectionConfig.class);
  if (config != null && config.forceDecorrelate()) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return RelDecorrelator.decorrelateQuery(rel, relBuilder);
  }
  return rel;
}
 
Example #22
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 #23
Source File: DruidTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean rolledUpColumnValidInsideAgg(String column, SqlCall call,
    SqlNode parent, CalciteConnectionConfig config) {
  assert isRolledUp(column);
  // Our rolled up columns are only allowed in COUNT(DISTINCT ...) aggregate functions.
  // We only allow this when approximate results are acceptable.
  return ((config != null
              && config.approximateDistinctCount()
              && isCountDistinct(call))
          || call.getOperator() == SqlStdOperatorTable.APPROX_COUNT_DISTINCT)
      && call.getOperandList().size() == 1 // for COUNT(a_1, a_2, ... a_n). n should be 1
      && isValidParentKind(parent);
}
 
Example #24
Source File: DruidQueryFilterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void testSetup() {
  druidQuery = Mockito.mock(DruidQuery.class);
  final CalciteConnectionConfig connectionConfigMock = Mockito
      .mock(CalciteConnectionConfig.class);
  Mockito.when(connectionConfigMock.timeZone()).thenReturn("UTC");
  Mockito.when(druidQuery.getConnectionConfig()).thenReturn(connectionConfigMock);
  Mockito.when(druidQuery.getDruidTable())
      .thenReturn(
          new DruidTable(Mockito.mock(DruidSchema.class), "dataSource", null,
              ImmutableSet.of(), "timestamp", null, null,
              null));
}
 
Example #25
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected CalciteCatalogReader(CalciteSchema rootSchema,
    SqlNameMatcher nameMatcher, List<List<String>> schemaPaths,
    RelDataTypeFactory typeFactory, CalciteConnectionConfig config) {
  this.rootSchema = Objects.requireNonNull(rootSchema);
  this.nameMatcher = nameMatcher;
  this.schemaPaths =
      Util.immutableCopy(Util.isDistinct(schemaPaths)
          ? schemaPaths
          : new LinkedHashSet<>(schemaPaths));
  this.typeFactory = typeFactory;
  this.config = config;
}
 
Example #26
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
public CalciteCatalogReader(CalciteSchema rootSchema,
    List<String> defaultSchema, RelDataTypeFactory typeFactory, CalciteConnectionConfig config) {
  this(rootSchema, SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
      ImmutableList.of(Objects.requireNonNull(defaultSchema),
          ImmutableList.of()),
      typeFactory, config);
}
 
Example #27
Source File: SqlValidatorCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns Config settings */
CalciteConnectionConfig getConfig();
 
Example #28
Source File: AbstractTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public boolean rolledUpColumnValidInsideAgg(String column,
    SqlCall call, SqlNode parent, CalciteConnectionConfig config) {
  return true;
}
 
Example #29
Source File: Schemas.java    From calcite with Apache License 2.0 4 votes vote down vote up
private static CalcitePrepare.Context makeContext(
    final CalciteConnectionConfig connectionConfig,
    final JavaTypeFactory typeFactory,
    final DataContext dataContext,
    final CalciteSchema schema,
    final List<String> schemaPath, final List<String> objectPath_) {
  final ImmutableList<String> objectPath =
      objectPath_ == null ? null : ImmutableList.copyOf(objectPath_);
  return new CalcitePrepare.Context() {
    public JavaTypeFactory getTypeFactory() {
      return typeFactory;
    }

    public CalciteSchema getRootSchema() {
      return schema.root();
    }

    public CalciteSchema getMutableRootSchema() {
      return getRootSchema();
    }

    public List<String> getDefaultSchemaPath() {
      // schemaPath is usually null. If specified, it overrides schema
      // as the context within which the SQL is validated.
      if (schemaPath == null) {
        return schema.path(null);
      }
      return schemaPath;
    }

    public List<String> getObjectPath() {
      return objectPath;
    }

    public CalciteConnectionConfig config() {
      return connectionConfig;
    }

    public DataContext getDataContext() {
      return dataContext;
    }

    public RelRunner getRelRunner() {
      throw new UnsupportedOperationException();
    }

    public CalcitePrepare.SparkHandler spark() {
      final boolean enable = config().spark();
      return CalcitePrepare.Dummy.getSparkHandler(enable);
    }
  };
}
 
Example #30
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public CalciteConnectionConfig getConfig() {
  return config;
}