public FrameworkConfig buildFrameWorkConfig() { if (hasUdf) { List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>(); sqlOperatorTables.add(SqlStdOperatorTable.instance()); sqlOperatorTables.add(new CalciteCatalogReader(CalciteSchema.from(schema), Collections.emptyList(), typeFactory, new CalciteConnectionConfigImpl(new Properties()))); return Frameworks.newConfigBuilder().defaultSchema(schema) .operatorTable(new ChainedSqlOperatorTable(sqlOperatorTables)).build(); } else { return Frameworks.newConfigBuilder().defaultSchema(schema).build(); } }
public static SchemaPlus createRootSchema( final DruidSchema druidSchema, final SystemSchema systemSchema, final AuthorizerMapper authorizerMapper ) { final SchemaPlus rootSchema = CalciteSchema.createRootSchema(false, false).plus(); rootSchema.add(DruidSchema.NAME, druidSchema); rootSchema.add(InformationSchema.NAME, new InformationSchema(rootSchema, authorizerMapper)); rootSchema.add(SystemSchema.NAME, systemSchema); return rootSchema; }
private Table findTable(CalciteSchema schema, String tableName, boolean caseSensitive) { CalciteSchema.TableEntry entry = schema.getTable(tableName, caseSensitive); if (entry != null) { return entry.getTable(); } // Check sub schemas for (CalciteSchema subSchema : schema.getSubSchemaMap().values()) { Table table = findTable(subSchema, tableName, caseSensitive); if (table != null) { return table; } } return null; }
@Test public void testSimpleCalciteSchemaWithView() throws Exception { final SchemaPlus rootSchema = CalciteSchema.createRootSchema(false, false).plus(); functionMap.put(viewName, view); final CalciteSchema calciteSchema = CalciteSchema.from(aSchema); assertThat( calciteSchema.getTableBasedOnNullaryFunction(viewName, true), notNullValue()); assertThat( calciteSchema.getTableBasedOnNullaryFunction(viewName, false), notNullValue()); assertThat( calciteSchema.getTableBasedOnNullaryFunction("V1", true), nullValue()); assertThat( calciteSchema.getTableBasedOnNullaryFunction("V1", false), nullValue()); assertThat(calciteSchema.getFunctions(viewName, true), hasItem(view)); assertThat(calciteSchema.getFunctions(viewName, false), hasItem(view)); assertThat(calciteSchema.getFunctions("V1", true), not(hasItem(view))); assertThat(calciteSchema.getFunctions("V1", false), not(hasItem(view)));
/** Executes a prepare action. */ public <R> R perform(CalciteServerStatement statement, Frameworks.PrepareAction<R> action) { final CalcitePrepare.Context prepareContext = statement.createPrepareContext(); final JavaTypeFactory typeFactory = prepareContext.getTypeFactory(); final CalciteSchema schema = action.getConfig().getDefaultSchema() != null ? CalciteSchema.from(action.getConfig().getDefaultSchema()) : prepareContext.getRootSchema(); CalciteCatalogReader catalogReader = new CalciteCatalogReader(schema.root(), schema.path(null), typeFactory, prepareContext.config()); final RexBuilder rexBuilder = new RexBuilder(typeFactory); final RelOptPlanner planner = createPlanner(prepareContext, action.getConfig().getContext(), action.getConfig().getCostFactory()); final RelOptCluster cluster = createCluster(planner, rexBuilder); return action.apply(cluster, catalogReader, prepareContext.getRootSchema().plus(), statement); }
/** Creates an operator table that contains functions in the given class. * * @see ModelHandler#addFunctions */ public static SqlOperatorTable operatorTable(String className) { // Dummy schema to collect the functions final CalciteSchema schema = CalciteSchema.createRootSchema(false, false); ModelHandler.addFunctions(schema.plus(), null, ImmutableList.of(), className, "*", true); // The following is technical debt; see [CALCITE-2082] Remove // RelDataTypeFactory argument from SqlUserDefinedAggFunction constructor final SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); final ListSqlOperatorTable table = new ListSqlOperatorTable(); for (String name : schema.getFunctionNames()) { for (Function function : schema.getFunctions(name, true)) { final SqlIdentifier id = new SqlIdentifier(name, SqlParserPos.ZERO); table.add( toOp(typeFactory, id, function)); } } return table; }
private ImmutableSet<DataSourceSchema> getDrivers(CalciteSchema tableSchema) { final ImmutableSet.Builder<DataSourceSchema> dsBuilder = new ImmutableSet.Builder<>(); SchemaPlus tableSchemaPlus = tableSchema.plus(); while (tableSchemaPlus != null) { Schema schema = CalciteSchema.from(tableSchemaPlus).schema; if (schema instanceof DataSourceSchema) { dsBuilder.add((DataSourceSchema) schema); } tableSchemaPlus = tableSchemaPlus.getParentSchema(); } return dsBuilder.build(); }
CalciteMetaImpl.connect(schema.root(), null); CalciteSchema.TableEntry tableEntry; tableEntry = schema.getTable(suggestedTableName, true); if (tableEntry == null) { tableEntry = schema.getTableBasedOnNullaryFunction(suggestedTableName, true); tableEntry = schema.getTableBySql(viewSql); final String tableName = Schemas.uniqueTableName(schema, Util.first(suggestedTableName, "m")); tableEntry = schema.add(tableName, table, ImmutableList.of(viewSql)); Hook.CREATE_MATERIALIZATION.run(tableName); rowType = table.getRowType(connection.getTypeFactory()); new MaterializationActor.Materialization(key, schema.root(), tableEntry, viewSql, rowType, viewSchemaPath); actor.keyMap.put(materialization.key, materialization);
private CalciteCatalogReader createCatalogReader() { SchemaPlus rootSchema = FlinkPlannerImpl.rootSchema(this.defaultSchema); Properties prop = new Properties(); prop.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), String.valueOf(parserConfig.caseSensitive())); CalciteConnectionConfig connectionConfig = new CalciteConnectionConfigImpl(prop); return new CalciteCatalogReader( CalciteSchema.from(rootSchema), CalciteSchema.from(this.defaultSchema).path(null), this.typeFactory, connectionConfig); }
private void visitNonQuarkScan(TableScan node) { final String schemaName = node.getTable().getQualifiedName().get(0); CalciteSchema schema = CalciteSchema.from(getRootSchma()).getSubSchema(schemaName, false); dsBuilder.addAll(getDrivers(schema)); }
Enumerable<MetaCatalog> catalogs() { return Linq4j.asEnumerable( CalciteSchema.from(getConnection().getRootSchema()).getSubSchemaMap().values()) .select( new Function1<CalciteSchema, MetaCatalog>() { public MetaCatalog apply(CalciteSchema calciteSchema) { return new MetaCatalog(calciteSchema.getName()); } }); }
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 SchemaPlus getSubSchema(String name) { final CalciteSchema subSchema = CalciteSchema.this.getSubSchema(name, true); return subSchema == null ? null : subSchema.plus(); }
private void registerNestedSchema(MockSchema parentSchema, MockSchema schema) { rootSchema.getSubSchema(parentSchema.getName(), true) .add(schema.name, new AbstractSchema()); }
public CalciteSchema getRootSchema() { return schema.root(); }
Enumerable<MetaTable> tables(final MetaSchema schema_) { final CalciteMetaSchema schema = (CalciteMetaSchema) schema_; return Linq4j.asEnumerable(schema.calciteSchema.getTableNames()) .select((Function1<String, MetaTable>) name -> { final Table table = schema.calciteSchema.getTable(name, true).getTable(); return new CalciteMetaTable(table, schema.tableCatalog, schema.tableSchem, name); }) .concat( Linq4j.asEnumerable( schema.calciteSchema.getTablesBasedOnNullaryFunctions() .entrySet()) .select(pair -> { final Table table = pair.getValue(); return new CalciteMetaTable(table, schema.tableCatalog, schema.tableSchem, pair.getKey()); })); }