Search in sources :

Example 11 with Table

use of com.hazelcast.sql.impl.schema.Table in project hazelcast by hazelcast.

the class CreateDagVisitor method onDelete.

public Vertex onDelete(DeletePhysicalRel rel) {
    Table table = rel.getTable().unwrap(HazelcastTable.class).getTarget();
    Vertex vertex = getJetSqlConnector(table).deleteProcessor(dag, table);
    connectInput(rel.getInput(), vertex, null);
    return vertex;
}
Also used : Vertex(com.hazelcast.jet.core.Vertex) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) Table(com.hazelcast.sql.impl.schema.Table) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable)

Example 12 with Table

use of com.hazelcast.sql.impl.schema.Table in project hazelcast by hazelcast.

the class HazelcastSchemaUtils method createRootSchema.

/**
 * Construct a schema from the given table resolvers.
 * <p>
 * Currently we assume that all tables are resolved upfront by querying a table resolver. It works well for predefined
 * objects such as IMap and ReplicatedMap as well as external tables created by Jet. This approach will not work well
 * should we need a relaxed/dynamic object resolution at some point in future.
 *
 * @return Top-level schema.
 */
public static HazelcastSchema createRootSchema(SqlCatalog catalog) {
    // Create schemas.
    Map<String, Schema> schemaMap = new HashMap<>();
    for (Map.Entry<String, Map<String, Table>> currentSchemaEntry : catalog.getSchemas().entrySet()) {
        String schemaName = currentSchemaEntry.getKey();
        Map<String, org.apache.calcite.schema.Table> schemaTables = new HashMap<>();
        for (Map.Entry<String, Table> tableEntry : currentSchemaEntry.getValue().entrySet()) {
            String tableName = tableEntry.getKey();
            Table table = tableEntry.getValue();
            HazelcastTable convertedTable = new HazelcastTable(table, createTableStatistic(table));
            schemaTables.put(tableName, convertedTable);
        }
        HazelcastSchema currentSchema = new HazelcastSchema(Collections.emptyMap(), schemaTables);
        schemaMap.put(schemaName, currentSchema);
    }
    HazelcastSchema rootSchema = new HazelcastSchema(schemaMap, Collections.emptyMap());
    return createCatalog(rootSchema);
}
Also used : Table(com.hazelcast.sql.impl.schema.Table) HashMap(java.util.HashMap) Schema(org.apache.calcite.schema.Schema) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with Table

use of com.hazelcast.sql.impl.schema.Table in project hazelcast by hazelcast.

the class HazelcastSqlValidator method createSourceSelectForUpdate.

@Override
protected SqlSelect createSourceSelectForUpdate(SqlUpdate update) {
    SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
    Table table = extractTable((SqlIdentifier) update.getTargetTable());
    if (table != null) {
        if (table instanceof ViewTable) {
            throw QueryException.error("DML operations not supported for views");
        }
        SqlConnector connector = getJetSqlConnector(table);
        // only tables with primary keys can be updated
        if (connector.getPrimaryKey(table).isEmpty()) {
            throw QueryException.error("Cannot UPDATE " + update.getTargetTable() + ": it doesn't have a primary key");
        }
        // add all fields, even hidden ones...
        table.getFields().forEach(field -> selectList.add(new SqlIdentifier(field.getName(), SqlParserPos.ZERO)));
    }
    int ordinal = 0;
    for (SqlNode exp : update.getSourceExpressionList()) {
        // Force unique aliases to avoid a duplicate for Y with
        // SET X=Y
        String alias = SqlUtil.deriveAliasFromOrdinal(ordinal);
        selectList.add(SqlValidatorUtil.addAlias(exp, alias));
        ++ordinal;
    }
    SqlNode sourceTable = update.getTargetTable();
    if (update.getAlias() != null) {
        sourceTable = SqlValidatorUtil.addAlias(sourceTable, update.getAlias().getSimple());
    }
    return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, update.getCondition(), null, null, null, null, null, null, null);
}
Also used : HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) ViewTable(com.hazelcast.jet.sql.impl.connector.virtual.ViewTable) Table(com.hazelcast.sql.impl.schema.Table) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlSelect(org.apache.calcite.sql.SqlSelect) ViewTable(com.hazelcast.jet.sql.impl.connector.virtual.ViewTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlConnectorUtil.getJetSqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnectorUtil.getJetSqlConnector) SqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnector) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 14 with Table

use of com.hazelcast.sql.impl.schema.Table in project hazelcast by hazelcast.

the class HazelcastSqlValidator method createSourceSelectForDelete.

@Override
protected SqlSelect createSourceSelectForDelete(SqlDelete delete) {
    SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
    Table table = extractTable((SqlIdentifier) delete.getTargetTable());
    if (table != null) {
        if (table instanceof ViewTable) {
            throw QueryException.error("DML operations not supported for views");
        }
        SqlConnector connector = getJetSqlConnector(table);
        // We need to feed primary keys to the delete processor so that it can directly delete the records.
        // Therefore we use the primary key for the select list.
        connector.getPrimaryKey(table).forEach(name -> selectList.add(new SqlIdentifier(name, SqlParserPos.ZERO)));
        if (selectList.size() == 0) {
            throw QueryException.error("Cannot DELETE from " + delete.getTargetTable() + ": it doesn't have a primary key");
        }
    }
    SqlNode sourceTable = delete.getTargetTable();
    if (delete.getAlias() != null) {
        sourceTable = SqlValidatorUtil.addAlias(sourceTable, delete.getAlias().getSimple());
    }
    return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, delete.getCondition(), null, null, null, null, null, null, null);
}
Also used : HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) ViewTable(com.hazelcast.jet.sql.impl.connector.virtual.ViewTable) Table(com.hazelcast.sql.impl.schema.Table) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlSelect(org.apache.calcite.sql.SqlSelect) ViewTable(com.hazelcast.jet.sql.impl.connector.virtual.ViewTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlConnectorUtil.getJetSqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnectorUtil.getJetSqlConnector) SqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnector) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

Table (com.hazelcast.sql.impl.schema.Table)14 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)9 Vertex (com.hazelcast.jet.core.Vertex)7 SqlConnector (com.hazelcast.jet.sql.impl.connector.SqlConnector)5 Map (java.util.Map)4 NodeEngine (com.hazelcast.spi.impl.NodeEngine)3 QueryException (com.hazelcast.sql.impl.QueryException)3 PlanObjectKey (com.hazelcast.sql.impl.optimizer.PlanObjectKey)3 ConstantTableStatistics (com.hazelcast.sql.impl.schema.ConstantTableStatistics)3 MappingField (com.hazelcast.sql.impl.schema.MappingField)3 TableField (com.hazelcast.sql.impl.schema.TableField)3 List (java.util.List)3 Nonnull (javax.annotation.Nonnull)3 FunctionEx (com.hazelcast.function.FunctionEx)2 DAG (com.hazelcast.jet.core.DAG)2 EventTimePolicy (com.hazelcast.jet.core.EventTimePolicy)2 ProcessorMetaSupplier (com.hazelcast.jet.core.ProcessorMetaSupplier)2 SourceBuilder (com.hazelcast.jet.pipeline.SourceBuilder)2 ExpressionUtil (com.hazelcast.jet.sql.impl.ExpressionUtil)2 VertexWithInputConfig (com.hazelcast.jet.sql.impl.connector.SqlConnector.VertexWithInputConfig)2