Java Code Examples for org.apache.calcite.schema.SchemaPlus#getSubSchema()

The following examples show how to use org.apache.calcite.schema.SchemaPlus#getSubSchema() . 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: MycatCalciteDataContext.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public MycatLogicTable getLogicTable(String targetName, String schema, String table) {
    String uniqueName = targetName + "." + schema + "." + table;
    SchemaPlus rootSchema = getRootSchema();
    Set<String> subSchemaNames = rootSchema.getSubSchemaNames();
    for (String subSchemaName : subSchemaNames) {
        SchemaPlus subSchema = rootSchema.getSubSchema(subSchemaName);
        log.debug("schemaName:{}", subSchemaName);
        Set<String> tableNames = subSchema.getTableNames();
        log.debug("tableNames:{}", tableNames);
        for (String tableName : tableNames) {
            Table table1 = subSchema.getTable(tableName);
            if (table1 instanceof MycatLogicTable) {
                Map<String, MycatPhysicalTable> dataNodeMap = ((MycatLogicTable) table1).getDataNodeMap();
                log.debug("dataNodeMap:{}", dataNodeMap);
                if (dataNodeMap.containsKey(uniqueName)) {
                    return Objects.requireNonNull((MycatLogicTable) table1);
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: DatasetPath.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public Table getTable(SchemaPlus rootSchema){
  List<FolderName> components = this.getFolderPath();
  SchemaPlus schema = rootSchema.getSubSchema(this.getRoot().getName());
  if(schema == null){
    throw new IllegalStateException(String.format("Failure finding schema path %s in position 0 of path %s", getRoot().getName(), toPathString()));
  }

  int i = 1;
  for(FolderName folder : components){
    schema = schema.getSubSchema(folder.getName());
    if(schema == null){
      throw new IllegalStateException(String.format("Failure finding schema path %s in position %d of path %s", folder.getName(), i, toPathString()));
    }
    i++;
  }
  Table table = schema.getTable(getLeaf().getName());
  if(table == null){
    throw new IllegalStateException(String.format("Failure finding table in path %s. The schema exists but no table in that schema matches %s", toPathString(), getLeaf().getName()));
  }

  return table;
}
 
Example 3
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 6 votes vote down vote up
private SchemaPlus getSchemaForTableSpace(String defaultTableSpace) throws MetadataStorageManagerException {
    long startTs = System.currentTimeMillis();
    while (true) {
        SchemaPlus schema = getRootSchema();
        SchemaPlus result = schema.getSubSchema(defaultTableSpace);
        if (result != null) {
            return result;
        }
        long delta = System.currentTimeMillis() - startTs;
        LOG.log(Level.FINE, "schema {0} not available yet, after waiting {1}/{2} ms",
                new Object[]{defaultTableSpace, delta, WAIT_FOR_SCHEMA_UP_TIMEOUT});
        if (delta >= WAIT_FOR_SCHEMA_UP_TIMEOUT) {
            return null;
        }
        clearCache();
        try {
            Thread.sleep(100);
        } catch (InterruptedException err) {
            Thread.currentThread().interrupt();
        }
    }
}
 
Example 4
Source File: QueryPlanner.java    From samza with Apache License 2.0 6 votes vote down vote up
private void registerSourceSchemas(SchemaPlus rootSchema) {
  RelSchemaConverter relSchemaConverter = new RelSchemaConverter();

  for (SqlIOConfig ssc : systemStreamConfigBySource.values()) {
    SchemaPlus previousLevelSchema = rootSchema;
    List<String> sourceParts = ssc.getSourceParts();
    RelSchemaProvider relSchemaProvider = relSchemaProviders.get(ssc.getSource());

    for (int sourcePartIndex = 0; sourcePartIndex < sourceParts.size(); sourcePartIndex++) {
      String sourcePart = sourceParts.get(sourcePartIndex);
      if (sourcePartIndex < sourceParts.size() - 1) {
        SchemaPlus sourcePartSchema = previousLevelSchema.getSubSchema(sourcePart);
        if (sourcePartSchema == null) {
          sourcePartSchema = previousLevelSchema.add(sourcePart, new AbstractSchema());
        }
        previousLevelSchema = sourcePartSchema;
      } else {
        // If the source part is the last one, then fetch the schema corresponding to the stream and register.
        RelDataType relationalSchema = getSourceRelSchema(relSchemaProvider, relSchemaConverter);
        previousLevelSchema.add(sourcePart, createTableFromRelSchema(relationalSchema));
        break;
      }
    }
  }
}
 
Example 5
Source File: SchemaUtilites.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Utility method to search for schema path starting from the given <i>schema</i> reference */
private static SchemaPlus searchSchemaTree(SchemaPlus schema, final List<String> schemaPath) {
  for (String schemaName : schemaPath) {
    // schemas in Drill are case insensitive and stored in lower case
    schema = schema.getSubSchema(schemaName.toLowerCase());
    if (schema == null) {
      return null;
    }
  }
  return schema;
}
 
Example 6
Source File: SqlConverter.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelRoot expandView(RelDataType rowType, String queryString, SchemaPlus rootSchema, List<String> schemaPath) {
  final DrillCalciteCatalogReader catalogReader = new DrillCalciteCatalogReader(
      rootSchema,
      parserConfig.caseSensitive(),
      schemaPath,
      typeFactory,
      drillConfig,
      session);
  SchemaPlus schema = rootSchema;
  for (String s : schemaPath) {
    SchemaPlus newSchema = schema.getSubSchema(s);

    if (newSchema == null) {
      throw UserException
          .validationError()
          .message(
          "Failure while attempting to expand view. Requested schema %s not available in schema %s.", s,
              schema.getName())
          .addContext("View Context", Joiner.on(", ").join(schemaPath))
          .addContext("View SQL", queryString)
          .build(logger);
    }

    schema = newSchema;
  }
  SqlConverter parser = new SqlConverter(SqlConverter.this, schema, rootSchema, catalogReader);
  return expandView(queryString, parser);
}
 
Example 7
Source File: QueryContext.java    From quark with Apache License 2.0 5 votes vote down vote up
public SchemaPlus getDefaultSchema() {
  SchemaPlus defaultSchemaPlus = this.rootSchema;
  for (String schemaName : this.defaultSchema) {
    defaultSchemaPlus = defaultSchemaPlus.getSubSchema(schemaName);
  }
  return defaultSchemaPlus;
}
 
Example 8
Source File: CalciteAssert.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static SchemaPlus addSchemaIfNotExists(SchemaPlus rootSchema,
      SchemaSpec schemaSpec) {
  final SchemaPlus schema = rootSchema.getSubSchema(schemaSpec.schemaName);
  if (schema != null) {
    return schema;
  }
  return addSchema(rootSchema, schemaSpec);
}
 
Example 9
Source File: DynamicRootSchema.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Loads schema factory(storage plugin) for specified {@code schemaName}
 * @param schemaName the name of the schema
 * @param caseSensitive whether matching for the schema name is case sensitive
 */
public void loadSchemaFactory(String schemaName, boolean caseSensitive) {
  try {
    SchemaPlus schemaPlus = this.plus();
    StoragePlugin plugin = getSchemaFactories().getPlugin(schemaName);
    if (plugin != null && plugin.getConfig().isEnabled()) {
      plugin.registerSchemas(schemaConfig, schemaPlus);
      return;
    }

    // Could not find the plugin of schemaName. The schemaName could be `dfs.tmp`, a 2nd level schema under 'dfs'
    List<String> paths = SchemaUtilites.getSchemaPathAsList(schemaName);
    if (paths.size() == 2) {
      plugin = getSchemaFactories().getPlugin(paths.get(0));
      if (plugin == null) {
        return;
      }

      // Looking for the SchemaPlus for the top level (e.g. 'dfs') of schemaName (e.g. 'dfs.tmp')
      SchemaPlus firstLevelSchema = schemaPlus.getSubSchema(paths.get(0));
      if (firstLevelSchema == null) {
        // register schema for this storage plugin to 'this'.
        plugin.registerSchemas(schemaConfig, schemaPlus);
        firstLevelSchema = schemaPlus.getSubSchema(paths.get(0));
      }
      // Load second level schemas for this storage plugin
      List<SchemaPlus> secondLevelSchemas = new ArrayList<>();
      for (String secondLevelSchemaName : firstLevelSchema.getSubSchemaNames()) {
        secondLevelSchemas.add(firstLevelSchema.getSubSchema(secondLevelSchemaName));
      }

      for (SchemaPlus schema : secondLevelSchemas) {
        org.apache.drill.exec.store.AbstractSchema drillSchema;
        try {
          drillSchema = schema.unwrap(org.apache.drill.exec.store.AbstractSchema.class);
        } catch (ClassCastException e) {
          throw new RuntimeException(String.format("Schema '%s' is not expected under root schema", schema.getName()));
        }
        SubSchemaWrapper wrapper = new SubSchemaWrapper(drillSchema);
        schemaPlus.add(wrapper.getName(), wrapper);
      }
    }
  } catch(ExecutionSetupException | IOException ex) {
    logger.warn("Failed to load schema for \"" + schemaName + "\"!", ex);
    // We can't proceed further without a schema, throw a runtime exception.
    UserException.Builder exceptBuilder =
        UserException
            .resourceError(ex)
            .message("Failed to load schema for \"" + schemaName + "\"!")
            .addContext(ex.getClass().getName() + ": " + ex.getMessage())
            .addContext(UserExceptionUtils.getUserHint(ex)); //Provide hint if it exists
    throw exceptBuilder.build(logger);
  }
}