Search in sources :

Example 1 with SimpleCatalog

use of com.google.zetasql.SimpleCatalog in project beam by apache.

the class BeamZetaSqlCatalog method addTableToLeafCatalog.

/**
 * Assume last element in tablePath is a table name, and everything before is catalogs. So the
 * logic is to create nested catalogs until the last level, then add a table at the last level.
 *
 * <p>Table schema is extracted from Calcite schema based on the table name resolution strategy,
 * e.g. either by drilling down the schema.getSubschema() path or joining the table name with dots
 * to construct a single compound identifier (e.g. Data Catalog use case).
 */
private void addTableToLeafCatalog(List<String> tablePath, QueryTrait queryTrait) {
    SimpleCatalog leafCatalog = createNestedCatalogs(zetaSqlCatalog, tablePath);
    org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table calciteTable = TableResolution.resolveCalciteTable(calciteSchema, tablePath);
    if (calciteTable == null) {
        throw new ZetaSqlException("Wasn't able to resolve the path " + tablePath + " in schema: " + calciteSchema.getName());
    }
    RelDataType rowType = calciteTable.getRowType(typeFactory);
    TableResolution.SimpleTableWithPath tableWithPath = TableResolution.SimpleTableWithPath.of(tablePath);
    queryTrait.addResolvedTable(tableWithPath);
    addFieldsToTable(tableWithPath, rowType);
    leafCatalog.addSimpleTable(tableWithPath.getTable());
}
Also used : SimpleCatalog(com.google.zetasql.SimpleCatalog) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)

Example 2 with SimpleCatalog

use of com.google.zetasql.SimpleCatalog in project beam by apache.

the class BeamZetaSqlCatalog method createNestedCatalogs.

/**
 * For table path like a.b.c we assume c is the table and a.b are the nested catalogs/schemas.
 */
private static SimpleCatalog createNestedCatalogs(SimpleCatalog catalog, List<String> tablePath) {
    SimpleCatalog currentCatalog = catalog;
    for (int i = 0; i < tablePath.size() - 1; i++) {
        String nextCatalogName = tablePath.get(i);
        Optional<SimpleCatalog> existing = tryGetExisting(currentCatalog, nextCatalogName);
        currentCatalog = existing.isPresent() ? existing.get() : addNewCatalog(currentCatalog, nextCatalogName);
    }
    return currentCatalog;
}
Also used : SimpleCatalog(com.google.zetasql.SimpleCatalog)

Example 3 with SimpleCatalog

use of com.google.zetasql.SimpleCatalog in project beam by apache.

the class BeamZetaSqlCatalog method addNewCatalog.

private static SimpleCatalog addNewCatalog(SimpleCatalog currentCatalog, String nextCatalogName) {
    SimpleCatalog nextCatalog = new SimpleCatalog(nextCatalogName);
    currentCatalog.addSimpleCatalog(nextCatalog);
    return nextCatalog;
}
Also used : SimpleCatalog(com.google.zetasql.SimpleCatalog)

Example 4 with SimpleCatalog

use of com.google.zetasql.SimpleCatalog in project beam by apache.

the class BeamZetaSqlCatalog method create.

/**
 * Return catalog pre-populated with builtin functions.
 */
static BeamZetaSqlCatalog create(SchemaPlus calciteSchema, JavaTypeFactory typeFactory, AnalyzerOptions options) {
    BeamZetaSqlCatalog catalog = new BeamZetaSqlCatalog(calciteSchema, new SimpleCatalog(calciteSchema.getName()), typeFactory);
    catalog.addFunctionsToCatalog(options);
    return catalog;
}
Also used : SimpleCatalog(com.google.zetasql.SimpleCatalog)

Aggregations

SimpleCatalog (com.google.zetasql.SimpleCatalog)4 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)1