Java Code Examples for org.apache.calcite.jdbc.CalciteSchema#TableEntry

The following examples show how to use org.apache.calcite.jdbc.CalciteSchema#TableEntry . 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: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Given a table alias, find the corresponding {@link Table} associated with it
 * */
private Table findTable(String alias) {
	List<String> names = null;
	if (tableScope == null) {
		// no tables to find
		return null;
	}

	for (ScopeChild child : tableScope.children) {
		if (catalogReader.nameMatcher().matches(child.name, alias)) {
			names = ((SqlIdentifier) child.namespace.getNode()).names;
			break;
		}
	}
	if (names == null || names.size() == 0) {
		return null;
	} else if (names.size() == 1) {
		return findTable(catalogReader.getRootSchema(), names.get(0),
			catalogReader.nameMatcher().isCaseSensitive());
	}

	CalciteSchema.TableEntry entry =
		SqlValidatorUtil.getTableEntry(catalogReader, names);

	return entry == null ? null : entry.getTable();
}
 
Example 2
Source File: SqlWorker.java    From quark with Apache License 2.0 6 votes vote down vote up
private void populateMaterializationsAndLattice(
    QuarkMaterializeCluster.RelOptPlannerHolder plannerHolder,
    CalciteSchema rootSchema) {
  if (materializations == null) {
    materializations =
        MaterializationService.instance().query(rootSchema);
  }
  Materializer materializer = new Materializer(materializations);

  materializer.populateMaterializations(context.getPrepareContext(), plannerHolder);

  List<CalciteSchema.LatticeEntry> lattices = Schemas.getLatticeEntries(rootSchema);

  for (CalciteSchema.LatticeEntry lattice : lattices) {
    final CalciteSchema.TableEntry starTable = lattice.getStarTable();
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final RelOptTableImpl starRelOptTable =
        RelOptTableImpl.create(catalogReader,
            starTable.getTable().getRowType(typeFactory), starTable, null);
    plannerHolder.getPlanner().addLattice(
        new RelOptLattice(lattice.getLattice(), starRelOptTable));
  }
}
 
Example 3
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
RelNode toRel(Queryable<T> queryable) {
  if (queryable instanceof QueryableDefaults.Replayable) {
    //noinspection unchecked
    ((QueryableDefaults.Replayable) queryable).replay(this);
    return rel;
  }
  if (queryable instanceof AbstractTableQueryable) {
    final AbstractTableQueryable tableQueryable =
        (AbstractTableQueryable) queryable;
    final QueryableTable table = tableQueryable.table;
    final CalciteSchema.TableEntry tableEntry =
        CalciteSchema.from(tableQueryable.schema)
            .add(tableQueryable.tableName, tableQueryable.table);
    final RelOptTableImpl relOptTable =
        RelOptTableImpl.create(null, table.getRowType(translator.typeFactory),
            tableEntry, null);
    if (table instanceof TranslatableTable) {
      return ((TranslatableTable) table).toRel(translator.toRelContext(),
          relOptTable);
    } else {
      return LogicalTableScan.create(translator.cluster, relOptTable, ImmutableList.of());
    }
  }
  return translator.translate(queryable.getExpression());
}
 
Example 4
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Prepare.PreparingTable getTable(final List<String> names) {
  // First look in the default schema, if any.
  // If not found, look in the root schema.
  CalciteSchema.TableEntry entry = SqlValidatorUtil.getTableEntry(this, names);
  if (entry != null) {
    final Table table = entry.getTable();
    if (table instanceof Wrapper) {
      final Prepare.PreparingTable relOptTable =
          ((Wrapper) table).unwrap(Prepare.PreparingTable.class);
      if (relOptTable != null) {
        return relOptTable;
      }
    }
    return RelOptTableImpl.create(this,
        table.getRowType(typeFactory), entry, null);
  }
  return null;
}
 
Example 6
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.TableEntry} in a
 * given catalog reader whose table has the given name, possibly qualified.
 *
 * <p>Uses the case-sensitivity policy of the specified catalog reader.
 *
 * <p>If not found, returns null.
 *
 * @param catalogReader accessor to the table metadata
 * @param names Name of table, may be qualified or fully-qualified
 *
 * @return TableEntry with a table with the given name, or null
 */
public static CalciteSchema.TableEntry getTableEntry(
    SqlValidatorCatalogReader catalogReader, List<String> names) {
  // First look in the default schema, if any.
  // If not found, look in the root schema.
  for (List<String> schemaPath : catalogReader.getSchemaPaths()) {
    CalciteSchema schema =
        getSchema(catalogReader.getRootSchema(),
            Iterables.concat(schemaPath, Util.skipLast(names)),
            catalogReader.nameMatcher());
    if (schema == null) {
      continue;
    }
    CalciteSchema.TableEntry entry =
        getTableEntryFrom(schema, Util.last(names),
            catalogReader.nameMatcher().isCaseSensitive());
    if (entry != null) {
      return entry;
    }
  }
  return null;
}
 
Example 7
Source File: MaterializationActor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a materialization.
 *
 * @param key  Unique identifier of this materialization
 * @param materializedTable Table that currently materializes the query.
 *                          That is, executing "select * from table" will
 *                          give the same results as executing the query.
 *                          May be null when the materialization is created;
 *                          materialization service will change the value as
 * @param sql  Query that is materialized
 * @param rowType Row type
 */
Materialization(MaterializationKey key,
    CalciteSchema rootSchema,
    CalciteSchema.TableEntry materializedTable,
    String sql,
    RelDataType rowType,
    List<String> viewSchemaPath) {
  this.key = key;
  this.rootSchema = Objects.requireNonNull(rootSchema);
  Preconditions.checkArgument(rootSchema.isRoot(), "must be root schema");
  this.materializedTable = materializedTable; // may be null
  this.sql = sql;
  this.rowType = rowType;
  this.viewSchemaPath = viewSchemaPath;
}
 
Example 8
Source File: MaterializedViewTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelNode toRel(RelOptTable.ToRelContext context,
    RelOptTable relOptTable) {
  final CalciteSchema.TableEntry tableEntry =
      MaterializationService.instance().checkValid(key);
  if (tableEntry != null) {
    Table materializeTable = tableEntry.getTable();
    if (materializeTable instanceof TranslatableTable) {
      TranslatableTable table = (TranslatableTable) materializeTable;
      return table.toRel(context, relOptTable);
    }
  }
  return super.toRel(context, relOptTable);
}
 
Example 9
Source File: MaterializationService.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Checks whether a materialization is valid, and if so, returns the table
 * where the data are stored. */
public CalciteSchema.TableEntry checkValid(MaterializationKey key) {
  final MaterializationActor.Materialization materialization =
      actor.keyMap.get(key);
  if (materialization != null) {
    return materialization.materializedTable;
  }
  return null;
}
 
Example 10
Source File: Schemas.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the star tables defined in a schema.
 *
 * @param schema Schema */
public static List<CalciteSchema.TableEntry> getStarTables(
    CalciteSchema schema) {
  final List<CalciteSchema.LatticeEntry> list = getLatticeEntries(schema);
  return Lists.transform(list, entry -> {
    final CalciteSchema.TableEntry starTable =
        Objects.requireNonNull(entry).getStarTable();
    assert starTable.getTable().getJdbcTableType()
        == Schema.TableType.STAR;
    return entry.getStarTable();
  });
}
 
Example 11
Source File: CalciteMaterializer.java    From calcite with Apache License 2.0 5 votes vote down vote up
Callback(RelNode rel,
    CalciteSchema.TableEntry starTable,
    RelOptTableImpl starRelOptTable) {
  this.rel = rel;
  this.starTable = starTable;
  this.starRelOptTable = starRelOptTable;
}
 
Example 12
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static CalciteSchema.TableEntry getTableEntryFrom(
    CalciteSchema schema, String name, boolean caseSensitive) {
  CalciteSchema.TableEntry entry =
      schema.getTable(name, caseSensitive);
  if (entry == null) {
    entry = schema.getTableBasedOnNullaryFunction(name,
        caseSensitive);
  }
  return entry;
}
 
Example 13
Source File: Prepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Materialization(CalciteSchema.TableEntry materializedTable,
    String sql, List<String> viewSchemaPath) {
  assert materializedTable != null;
  assert sql != null;
  this.materializedTable = materializedTable;
  this.sql = sql;
  this.viewSchemaPath = viewSchemaPath;
}
 
Example 14
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType,
    final CalciteSchema.TableEntry tableEntry, Double rowCount) {
  final Table table = tableEntry.getTable();
  return new RelOptTableImpl(schema, rowType, tableEntry.path(),
      table, getClassExpressionFunction(tableEntry, table), rowCount);
}
 
Example 15
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;
  }
}
 
Example 16
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
private static Function<Class, Expression> getClassExpressionFunction(
    CalciteSchema.TableEntry tableEntry, Table table) {
  return getClassExpressionFunction(tableEntry.schema.plus(), tableEntry.name,
      table);
}
 
Example 17
Source File: MaterializationService.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Defines a new materialization. Returns its key. */
public MaterializationKey defineMaterialization(final CalciteSchema schema,
    TileKey tileKey, String viewSql, List<String> viewSchemaPath,
    String suggestedTableName, TableFactory tableFactory, boolean create,
    boolean existing) {
  final MaterializationActor.QueryKey queryKey =
      new MaterializationActor.QueryKey(viewSql, schema, viewSchemaPath);
  final MaterializationKey existingKey = actor.keyBySql.get(queryKey);
  if (existingKey != null) {
    return existingKey;
  }
  if (!create) {
    return null;
  }

  final CalciteConnection connection =
      CalciteMetaImpl.connect(schema.root(), null);
  CalciteSchema.TableEntry tableEntry;
  // If the user says the materialization exists, first try to find a table
  // with the name and if none can be found, lookup a view in the schema
  if (existing) {
    tableEntry = schema.getTable(suggestedTableName, true);
    if (tableEntry == null) {
      tableEntry = schema.getTableBasedOnNullaryFunction(suggestedTableName, true);
    }
  } else {
    tableEntry = null;
  }
  if (tableEntry == null) {
    tableEntry = schema.getTableBySql(viewSql);
  }

  RelDataType rowType = null;
  if (tableEntry == null) {
    Table table = tableFactory.createTable(schema, viewSql, viewSchemaPath);
    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());
  }

  if (rowType == null) {
    // If we didn't validate the SQL by populating a table, validate it now.
    final CalcitePrepare.ParseResult parse =
        Schemas.parse(connection, schema, viewSchemaPath, viewSql);
    rowType = parse.rowType;
  }
  final MaterializationKey key = new MaterializationKey();
  final MaterializationActor.Materialization materialization =
      new MaterializationActor.Materialization(key, schema.root(),
          tableEntry, viewSql, rowType, viewSchemaPath);
  actor.keyMap.put(materialization.key, materialization);
  actor.keyBySql.put(queryKey, materialization.key);
  if (tileKey != null) {
    actor.keyByTile.put(tileKey, materialization.key);
  }
  return key;
}
 
Example 18
Source File: MaterializationService.java    From calcite with Apache License 2.0 3 votes vote down vote up
/**
 * Defines a tile.
 *
 * <p>Setting the {@code create} flag to false prevents a materialization
 * from being created if one does not exist. Critically, it is set to false
 * during the recursive SQL that populates a materialization. Otherwise a
 * materialization would try to create itself to populate itself!
 */
public Pair<CalciteSchema.TableEntry, TileKey> defineTile(Lattice lattice,
    ImmutableBitSet groupSet, List<Lattice.Measure> measureList,
    CalciteSchema schema, boolean create, boolean exact) {
  return defineTile(lattice, groupSet, measureList, schema, create, exact,
      "m" + groupSet, tableFactory);
}
 
Example 19
Source File: RelOptLattice.java    From calcite with Apache License 2.0 3 votes vote down vote up
/** 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);
}