Search in sources :

Example 21 with CalciteSchema

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

the class SqlCreateExternalTable method execute.

@Override
public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    if (pair.left.plus().getTable(pair.right) != null) {
        // Table exists.
        if (!ifNotExists) {
            // They did not specify IF NOT EXISTS, so give error.
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableExists(pair.right));
        }
        return;
    }
    // Table does not exist. Create it.
    if (!(pair.left.schema instanceof BeamCalciteSchema)) {
        throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.internal("Schema is not instanceof BeamCalciteSchema"));
    }
    BeamCalciteSchema schema = (BeamCalciteSchema) pair.left.schema;
    schema.getTableProvider().createTable(toTable());
}
Also used : BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema)

Example 22 with CalciteSchema

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

the class SqlDdlNodes method schema.

/**
 * Returns the schema in which to create an object.
 */
static Pair<CalciteSchema, String> schema(CalcitePrepare.Context context, boolean mutable, SqlIdentifier id) {
    final List<String> path;
    if (id.isSimple()) {
        path = context.getDefaultSchemaPath();
    } else {
        path = Util.skipLast(id.names);
    }
    CalciteSchema schema = mutable ? context.getMutableRootSchema() : context.getRootSchema();
    for (String p : path) {
        schema = schema.getSubSchema(p, true);
        if (schema == null) {
            throw new AssertionError(String.format("Got null sub-schema for path '%s' in %s", p, path));
        }
    }
    return Pair.of(schema, name(id));
}
Also used : CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) NlsString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString)

Example 23 with CalciteSchema

use of org.apache.calcite.jdbc.CalciteSchema in project drill by axbaretto.

the class DynamicSchema method getSubSchema.

@Override
public CalciteSchema getSubSchema(String schemaName, boolean caseSensitive) {
    Schema s = schema.getSubSchema(schemaName);
    if (s != null) {
        return new DynamicSchema(this, s, schemaName);
    }
    CalciteSchema ret = getSubSchemaMap().get(schemaName);
    return ret;
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SimpleCalciteSchema(org.apache.calcite.jdbc.SimpleCalciteSchema) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SimpleCalciteSchema(org.apache.calcite.jdbc.SimpleCalciteSchema)

Example 24 with CalciteSchema

use of 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 25 with CalciteSchema

use of 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)

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 Schema (org.apache.calcite.schema.Schema)4 Table (org.apache.calcite.schema.Table)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)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 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 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)2 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)2