Search in sources :

Example 1 with SqlConnector

use of com.hazelcast.jet.sql.impl.connector.SqlConnector in project hazelcast by hazelcast.

the class HazelcastSqlValidator method containsStreamingSource.

/**
 * Goes over all the referenced tables in the given {@link SqlNode}
 * and returns true if any of them uses a streaming connector.
 */
public boolean containsStreamingSource(SqlNode node) {
    class FindStreamingTablesVisitor extends SqlBasicVisitor<Void> {

        boolean found;

        @Override
        public Void visit(SqlIdentifier id) {
            SqlValidatorTable table = getCatalogReader().getTable(id.names);
            // not every identifier is a table
            if (table != null) {
                HazelcastTable hazelcastTable = table.unwrap(HazelcastTable.class);
                if (hazelcastTable.getTarget() instanceof ViewTable) {
                    found = ((ViewTable) hazelcastTable.getTarget()).isStream();
                    return null;
                }
                SqlConnector connector = getJetSqlConnector(hazelcastTable.getTarget());
                if (connector.isStream()) {
                    found = true;
                    return null;
                }
            }
            return super.visit(id);
        }

        @Override
        public Void visit(SqlCall call) {
            SqlOperator operator = call.getOperator();
            if (operator instanceof HazelcastTableSourceFunction) {
                if (((HazelcastTableSourceFunction) operator).isStream()) {
                    found = true;
                    return null;
                }
            }
            return super.visit(call);
        }
    }
    FindStreamingTablesVisitor visitor = new FindStreamingTablesVisitor();
    node.accept(visitor);
    return visitor.found;
}
Also used : ViewTable(com.hazelcast.jet.sql.impl.connector.virtual.ViewTable) SqlCall(org.apache.calcite.sql.SqlCall) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) HazelcastTableSourceFunction(com.hazelcast.jet.sql.impl.schema.HazelcastTableSourceFunction) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlConnectorUtil.getJetSqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnectorUtil.getJetSqlConnector) SqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnector) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) SqlBasicVisitor(org.apache.calcite.sql.util.SqlBasicVisitor)

Example 2 with SqlConnector

use of com.hazelcast.jet.sql.impl.connector.SqlConnector 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 3 with SqlConnector

use of com.hazelcast.jet.sql.impl.connector.SqlConnector 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)

Example 4 with SqlConnector

use of com.hazelcast.jet.sql.impl.connector.SqlConnector in project hazelcast by hazelcast.

the class TableResolverImpl method resolveMapping.

private Mapping resolveMapping(Mapping mapping) {
    String type = mapping.type();
    Map<String, String> options = mapping.options();
    SqlConnector connector = connectorCache.forType(type);
    List<MappingField> resolvedFields = connector.resolveAndValidateFields(nodeEngine, options, mapping.fields());
    return new Mapping(mapping.name(), mapping.externalName(), type, new ArrayList<>(resolvedFields), new LinkedHashMap<>(options));
}
Also used : Mapping(com.hazelcast.sql.impl.schema.Mapping) SqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnector) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Aggregations

SqlConnector (com.hazelcast.jet.sql.impl.connector.SqlConnector)4 SqlConnectorUtil.getJetSqlConnector (com.hazelcast.jet.sql.impl.connector.SqlConnectorUtil.getJetSqlConnector)3 ViewTable (com.hazelcast.jet.sql.impl.connector.virtual.ViewTable)3 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3 SqlValidatorTable (org.apache.calcite.sql.validate.SqlValidatorTable)3 Table (com.hazelcast.sql.impl.schema.Table)2 SqlNode (org.apache.calcite.sql.SqlNode)2 SqlNodeList (org.apache.calcite.sql.SqlNodeList)2 SqlSelect (org.apache.calcite.sql.SqlSelect)2 HazelcastTableSourceFunction (com.hazelcast.jet.sql.impl.schema.HazelcastTableSourceFunction)1 Mapping (com.hazelcast.sql.impl.schema.Mapping)1 MappingField (com.hazelcast.sql.impl.schema.MappingField)1 SqlCall (org.apache.calcite.sql.SqlCall)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 SqlBasicVisitor (org.apache.calcite.sql.util.SqlBasicVisitor)1