Java Code Examples for org.apache.calcite.jdbc.CalciteSchema#getSubSchema()

The following examples show how to use org.apache.calcite.jdbc.CalciteSchema#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: SqlDropTableExtension.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CalcitePrepare.Context context) {
    final List<String> path = context.getDefaultSchemaPath();
    CalciteSchema schema = context.getRootSchema();
    for (String p : path) {
        schema = schema.getSubSchema(p, true);
    }

    final Pair<CalciteSchema, String> pair =
        SqlDdlNodes.schema(context, true, name);
    switch (getKind()) {
        case DROP_TABLE:
            Schema schemaPlus = schema.plus().unwrap(Schema.class);
            boolean existed = schemaPlus.dropTable(name.getSimple());
            pair.left.removeTable(name.getSimple());
            if (!existed && !ifExists) {
                throw SqlUtil.newContextException(name.getParserPosition(),
                    RESOURCE.tableNotFound(name.getSimple()));
            }
            break;
        default:
            throw new AssertionError(getKind());
    }
}
 
Example 2
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a {@link org.apache.calcite.jdbc.CalciteSchema.TypeEntry} in a
 * given schema whose type has the given name, possibly qualified.
 *
 * @param rootSchema root schema
 * @param typeName name of the type, may be qualified or fully-qualified
 *
 * @return TypeEntry with a table with the given name, or null
 */
public static CalciteSchema.TypeEntry getTypeEntry(
    CalciteSchema rootSchema, SqlIdentifier typeName) {
  final String name;
  final List<String> path;
  if (typeName.isSimple()) {
    path = ImmutableList.of();
    name = typeName.getSimple();
  } else {
    path = Util.skipLast(typeName.names);
    name = Util.last(typeName.names);
  }
  CalciteSchema schema = rootSchema;
  for (String p : path) {
    if (schema == rootSchema
        && SqlNameMatchers.withCaseSensitive(true).matches(p, schema.getName())) {
      continue;
    }
    schema = schema.getSubSchema(p, true);
  }
  return schema == null ? null : schema.getType(name, false);
}
 
Example 3
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Finds and returns {@link CalciteSchema} nested to the given rootSchema
 * with specified schemaPath.
 *
 * <p>Uses the case-sensitivity policy of specified nameMatcher.
 *
 * <p>If not found, returns null.
 *
 * @param rootSchema root schema
 * @param schemaPath full schema path of required schema
 * @param nameMatcher name matcher
 *
 * @return CalciteSchema that corresponds specified schemaPath
 */
public static CalciteSchema getSchema(CalciteSchema rootSchema,
    Iterable<String> schemaPath, SqlNameMatcher nameMatcher) {
  CalciteSchema schema = rootSchema;
  for (String schemaName : schemaPath) {
    if (schema == rootSchema
        && nameMatcher.matches(schemaName, schema.getName())) {
      continue;
    }
    schema = schema.getSubSchema(schemaName,
        nameMatcher.isCaseSensitive());
    if (schema == null) {
      return null;
    }
  }
  return schema;
}
 
Example 4
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code DROP SCHEMA} command. */
public void execute(SqlDropSchema drop,
    CalcitePrepare.Context context) {
  final List<String> path = context.getDefaultSchemaPath();
  CalciteSchema schema = context.getRootSchema();
  for (String p : path) {
    schema = schema.getSubSchema(p, true);
  }
  final boolean existed = schema.removeSubSchema(drop.name.getSimple());
  if (!existed && !drop.ifExists) {
    throw SqlUtil.newContextException(drop.name.getParserPosition(),
        RESOURCE.schemaNotFound(drop.name.getSimple()));
  }
}
 
Example 5
Source File: Schemas.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a sub-schema of a given schema obtained by following a sequence
 * of names.
 *
 * <p>The result is null if the initial schema is null or any sub-schema does
 * not exist.
 */
public static CalciteSchema subSchema(CalciteSchema schema,
    Iterable<String> names) {
  for (String string : names) {
    if (schema == null) {
      return null;
    }
    schema = schema.getSubSchema(string, false);
  }
  return schema;
}
 
Example 6
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Executes {@code DROP FUNCTION}, {@code DROP TABLE},
 * {@code DROP MATERIALIZED VIEW}, {@code DROP TYPE},
 * {@code DROP VIEW} commands. */
public void execute(SqlDropObject drop,
    CalcitePrepare.Context context) {
  final List<String> path = context.getDefaultSchemaPath();
  CalciteSchema schema = context.getRootSchema();
  for (String p : path) {
    schema = schema.getSubSchema(p, true);
  }
  final boolean existed;
  switch (drop.getKind()) {
  case DROP_TABLE:
  case DROP_MATERIALIZED_VIEW:
    existed = schema.removeTable(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.tableNotFound(drop.name.getSimple()));
    }
    break;
  case DROP_VIEW:
    // Not quite right: removes any other functions with the same name
    existed = schema.removeFunction(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.viewNotFound(drop.name.getSimple()));
    }
    break;
  case DROP_TYPE:
    existed = schema.removeType(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.typeNotFound(drop.name.getSimple()));
    }
    break;
  case DROP_FUNCTION:
    existed = schema.removeFunction(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.functionNotFound(drop.name.getSimple()));
    }
    break;
  case OTHER_DDL:
  default:
    throw new AssertionError(drop.getKind());
  }
}
 
Example 7
Source File: EmptyScope.java    From calcite with Apache License 2.0 4 votes vote down vote up
private void resolve_(final CalciteSchema rootSchema, List<String> names,
    List<String> schemaNames, SqlNameMatcher nameMatcher, Path path,
    Resolved resolved) {
  final List<String> concat = ImmutableList.<String>builder()
      .addAll(schemaNames).addAll(names).build();
  CalciteSchema schema = rootSchema;
  SqlValidatorNamespace namespace = null;
  List<String> remainingNames = concat;
  for (String schemaName : concat) {
    if (schema == rootSchema
        && nameMatcher.matches(schemaName, schema.name)) {
      remainingNames = Util.skip(remainingNames);
      continue;
    }
    final CalciteSchema subSchema =
        schema.getSubSchema(schemaName, nameMatcher.isCaseSensitive());
    if (subSchema != null) {
      path = path.plus(null, -1, subSchema.name, StructKind.NONE);
      remainingNames = Util.skip(remainingNames);
      schema = subSchema;
      namespace = new SchemaNamespace(validator,
          ImmutableList.copyOf(path.stepNames()));
      continue;
    }
    CalciteSchema.TableEntry entry =
        schema.getTable(schemaName, nameMatcher.isCaseSensitive());
    if (entry == null) {
      entry = schema.getTableBasedOnNullaryFunction(schemaName,
          nameMatcher.isCaseSensitive());
    }
    if (entry != null) {
      path = path.plus(null, -1, entry.name, StructKind.NONE);
      remainingNames = Util.skip(remainingNames);
      final Table table = entry.getTable();
      SqlValidatorTable table2 = null;
      if (table instanceof Wrapper) {
        table2 = ((Wrapper) table).unwrap(Prepare.PreparingTable.class);
      }
      if (table2 == null) {
        final RelOptSchema relOptSchema =
            validator.catalogReader.unwrap(RelOptSchema.class);
        final RelDataType rowType = table.getRowType(validator.typeFactory);
        table2 = RelOptTableImpl.create(relOptSchema, rowType, entry, null);
      }
      namespace = new TableNamespace(validator, table2);
      resolved.found(namespace, false, null, path, remainingNames);
      return;
    }
    // neither sub-schema nor table
    if (namespace != null
        && !remainingNames.equals(names)) {
      resolved.found(namespace, false, null, path, remainingNames);
    }
    return;
  }
}