use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.
the class EmptyScope method resolve_.
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, this, path, remainingNames);
return;
}
// neither sub-schema nor table
if (namespace != null && !remainingNames.equals(names)) {
resolved.found(namespace, false, this, path, remainingNames);
}
return;
}
}
use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.
the class SqlDropSchema method execute.
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 boolean existed = schema.removeSubSchema(name.getSimple());
if (!existed && !ifExists) {
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaNotFound(name.getSimple()));
}
}
use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.
the class SqlCreateForeignSchema method execute.
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
if (subSchema0 != null) {
if (!getReplace() && !ifNotExists) {
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaExists(pair.right));
}
}
final Schema subSchema;
final String libraryName;
if (type != null) {
Preconditions.checkArgument(library == null);
final String typeName = (String) value(this.type);
final JsonSchema.Type type = Util.enumVal(JsonSchema.Type.class, typeName.toUpperCase(Locale.ROOT));
if (type != null) {
switch(type) {
case JDBC:
libraryName = JdbcSchema.Factory.class.getName();
break;
default:
libraryName = null;
}
} else {
libraryName = null;
}
if (libraryName == null) {
throw SqlUtil.newContextException(this.type.getParserPosition(), RESOURCE.schemaInvalidType(typeName, Arrays.toString(JsonSchema.Type.values())));
}
} else {
Preconditions.checkArgument(library != null);
libraryName = (String) value(library);
}
final SchemaFactory schemaFactory = AvaticaUtils.instantiatePlugin(SchemaFactory.class, libraryName);
final Map<String, Object> operandMap = new LinkedHashMap<>();
for (Pair<SqlIdentifier, SqlNode> option : options(optionList)) {
operandMap.put(option.left.getSimple(), value(option.right));
}
subSchema = schemaFactory.create(pair.left.plus(), pair.right, operandMap);
pair.left.add(pair.right, subSchema);
}
use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.
the class SqlCreateView method execute.
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
final SchemaPlus schemaPlus = pair.left.plus();
for (Function function : schemaPlus.getFunctions(pair.right)) {
if (function.getParameters().isEmpty()) {
if (!getReplace()) {
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.viewExists(pair.right));
}
pair.left.removeFunction(pair.right);
}
}
final SqlNode q = SqlDdlNodes.renameColumns(columnList, query);
final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
final ViewTableMacro viewTableMacro = ViewTable.viewMacro(schemaPlus, sql, pair.left.path(null), context.getObjectPath(), false);
final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
Util.discard(x);
schemaPlus.add(pair.right, viewTableMacro);
}
use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.
the class RelOptLattice method getAggregate.
/**
* Retrieves a materialized table that will satisfy an aggregate query on
* the star table.
*
* <p>The current implementation creates a materialization and populates it,
* provided that {@link Lattice#auto} is true.
*
* <p>Future implementations might return materializations at a different
* level of aggregation, from which the desired result can be obtained by
* rolling up.
*
* @param planner Current planner
* @param groupSet Grouping key
* @param measureList Calls to aggregate functions
* @return Materialized table
*/
public Pair<CalciteSchema.TableEntry, TileKey> getAggregate(RelOptPlanner planner, ImmutableBitSet groupSet, List<Lattice.Measure> measureList) {
final CalciteConnectionConfig config = planner.getContext().unwrap(CalciteConnectionConfig.class);
if (config == null) {
return null;
}
final MaterializationService service = MaterializationService.instance();
boolean create = lattice.auto && config.createMaterializations();
final CalciteSchema schema = starRelOptTable.unwrap(CalciteSchema.class);
return service.defineTile(lattice, groupSet, measureList, schema, create, false);
}
Aggregations