Search in sources :

Example 36 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class CalciteCatalogReader method getFunctionsFrom.

private Collection<Function> getFunctionsFrom(List<String> names) {
    final List<Function> functions2 = Lists.newArrayList();
    final List<List<String>> schemaNameList = new ArrayList<>();
    if (names.size() > 1) {
        // the last 2 items in the path.
        if (schemaPaths.size() > 1) {
            schemaNameList.addAll(Util.skip(schemaPaths));
        } else {
            schemaNameList.addAll(schemaPaths);
        }
    } else {
        for (List<String> schemaPath : schemaPaths) {
            CalciteSchema schema = SqlValidatorUtil.getSchema(rootSchema, schemaPath, nameMatcher);
            if (schema != null) {
                schemaNameList.addAll(schema.getPath());
            }
        }
    }
    for (List<String> schemaNames : schemaNameList) {
        CalciteSchema schema = SqlValidatorUtil.getSchema(rootSchema, Iterables.concat(schemaNames, Util.skipLast(names)), nameMatcher);
        if (schema != null) {
            final String name = Util.last(names);
            functions2.addAll(schema.getFunctions(name, true));
        }
    }
    return functions2;
}
Also used : SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) Function(org.apache.calcite.schema.Function) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) ScalarFunction(org.apache.calcite.schema.ScalarFunction) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 37 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class SqlValidatorUtil method getTableEntry.

/**
 * 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) {
    // 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;
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 38 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project drill by apache.

the class DrillValidator method replaceAliasWithActualName.

private void replaceAliasWithActualName(SqlIdentifier tempNode) {
    CalciteSchema schema = getCatalogReader().getRootSchema();
    if (schema instanceof DynamicRootSchema) {
        DynamicRootSchema rootSchema = (DynamicRootSchema) schema;
        String alias = SchemaPath.getCompoundPath(tempNode.names.toArray(new String[0])).toExpr();
        SchemaPath actualPath = rootSchema.resolveTableAlias(alias);
        if (actualPath != null) {
            List<String> names = new ArrayList<>();
            PathSegment pathSegment = actualPath.getRootSegment();
            while (pathSegment != null) {
                names.add(pathSegment.getNameSegment().getPath());
                pathSegment = pathSegment.getChild();
            }
            changeNames(tempNode, names);
        }
    }
}
Also used : DynamicRootSchema(org.apache.calcite.jdbc.DynamicRootSchema) SchemaPath(org.apache.drill.common.expression.SchemaPath) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ArrayList(java.util.ArrayList) PathSegment(org.apache.drill.common.expression.PathSegment)

Example 39 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project beam by apache.

the class JdbcFactory method newConnection.

@Override
public AvaticaConnection newConnection(UnregisteredDriver driver, AvaticaFactory avaticaFactory, String url, Properties info, @Nullable CalciteSchema rootSchema, @Nullable JavaTypeFactory typeFactory) {
    Properties connectionProps = ensureDefaultProperties(info);
    CalciteSchema actualRootSchema = rootSchema;
    if (rootSchema == null) {
        actualRootSchema = CalciteSchema.createRootSchema(true, false, "");
    }
    return super.newConnection(driver, avaticaFactory, url, connectionProps, actualRootSchema, typeFactory);
}
Also used : CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) Properties(java.util.Properties)

Example 40 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project beam by apache.

the class BeamZetaSqlCatalogTest method loadsUserDefinedFunctionsFromSchema.

@Test
public void loadsUserDefinedFunctionsFromSchema() throws NoSuchMethodException {
    JdbcConnection jdbcConnection = createJdbcConnection();
    SchemaPlus calciteSchema = jdbcConnection.getCurrentSchemaPlus();
    Method method = IncrementFn.class.getMethod("eval", Long.class);
    calciteSchema.add("increment", ScalarFunctionImpl.create(method));
    BeamZetaSqlCatalog beamCatalog = BeamZetaSqlCatalog.create(calciteSchema, jdbcConnection.getTypeFactory(), SqlAnalyzer.baseAnalyzerOptions());
    assertNotNull("ZetaSQL catalog contains function signature.", beamCatalog.getZetaSqlCatalog().getFunctionByFullName(USER_DEFINED_JAVA_SCALAR_FUNCTIONS + ":increment"));
    assertEquals("Beam catalog contains function definition.", UserFunctionDefinitions.JavaScalarFunction.create(method, ""), beamCatalog.getUserFunctionDefinitions().javaScalarFunctions().get(ImmutableList.of("increment")));
}
Also used : SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) JdbcConnection(org.apache.beam.sdk.extensions.sql.impl.JdbcConnection) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)31 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 CalciteSchema (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema)6 ArrayList (java.util.ArrayList)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 Test (org.junit.Test)5 Method (java.lang.reflect.Method)4 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)4 Schema (org.apache.calcite.schema.Schema)4 Table (org.apache.calcite.schema.Table)4 SqlNode (org.apache.calcite.sql.SqlNode)4 ImmutableList (com.google.common.collect.ImmutableList)3 BeamCalciteSchema (org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema)3 JdbcConnection (org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)3 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)3 TableFunction (org.apache.calcite.schema.TableFunction)3 TranslatableTable (org.apache.calcite.schema.TranslatableTable)3 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)3 ViewTableMacro (org.apache.calcite.schema.impl.ViewTableMacro)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3