Search in sources :

Example 26 with CalciteSchema

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

the class CassandraSchema method addMaterializedViews.

/**
 * Add all materialized views defined in the schema to this column family
 */
private void addMaterializedViews() {
    // Close the hook use to get us here
    hook.close();
    for (MaterializedViewMetadata view : getKeyspace().getMaterializedViews()) {
        String tableName = view.getBaseTable().getName();
        StringBuilder queryBuilder = new StringBuilder("SELECT ");
        // Add all the selected columns to the query
        List<String> columnNames = new ArrayList<String>();
        for (ColumnMetadata column : view.getColumns()) {
            columnNames.add("\"" + column.getName() + "\"");
        }
        queryBuilder.append(Util.toString(columnNames, "", ", ", ""));
        queryBuilder.append(" FROM \"" + tableName + "\"");
        // Get the where clause from the system schema
        String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName() + "'";
        queryBuilder.append(" WHERE " + session.execute(whereQuery).one().getString(0));
        // Parse and unparse the view query to get properly quoted field names
        String query = queryBuilder.toString();
        SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();
        configBuilder.setUnquotedCasing(Casing.UNCHANGED);
        SqlSelect parsedQuery;
        try {
            parsedQuery = (SqlSelect) SqlParser.create(query, configBuilder.build()).parseQuery();
        } catch (SqlParseException e) {
            LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName());
            continue;
        }
        StringWriter stringWriter = new StringWriter(query.length());
        PrintWriter printWriter = new PrintWriter(stringWriter);
        SqlWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, true, printWriter);
        parsedQuery.unparse(writer, 0, 0);
        query = stringWriter.toString();
        // Add the view for this query
        String viewName = "$" + getTableNames().size();
        SchemaPlus schema = parentSchema.getSubSchema(name);
        CalciteSchema calciteSchema = CalciteSchema.from(schema);
        List<String> viewPath = calciteSchema.path(viewName);
        schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName(), true));
    }
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MaterializedViewMetadata(com.datastax.driver.core.MaterializedViewMetadata) SqlSelect(org.apache.calcite.sql.SqlSelect) StringWriter(java.io.StringWriter) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) PrintWriter(java.io.PrintWriter)

Example 27 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.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;
    }
}
Also used : Wrapper(org.apache.calcite.schema.Wrapper) Table(org.apache.calcite.schema.Table) RelOptSchema(org.apache.calcite.plan.RelOptSchema) RelDataType(org.apache.calcite.rel.type.RelDataType) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 28 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.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()));
    }
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 29 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.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);
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JsonSchema(org.apache.calcite.model.JsonSchema) JsonSchema(org.apache.calcite.model.JsonSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SchemaFactory(org.apache.calcite.schema.SchemaFactory) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) LinkedHashMap(java.util.LinkedHashMap) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlNode(org.apache.calcite.sql.SqlNode)

Example 30 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.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);
}
Also used : Function(org.apache.calcite.schema.Function) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) SchemaPlus(org.apache.calcite.schema.SchemaPlus) TranslatableTable(org.apache.calcite.schema.TranslatableTable) SqlNode(org.apache.calcite.sql.SqlNode)

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