Search in sources :

Example 61 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project hazelcast by hazelcast.

the class HazelcastTypeCoercion method coerceSourceRowType.

// originally copied from TypeCoercionImpl
private boolean coerceSourceRowType(SqlValidatorScope sourceScope, SqlNode query, int columnIndex, int totalColumns, RelDataType targetType) {
    switch(query.getKind()) {
        case INSERT:
            SqlInsert insert = (SqlInsert) query;
            return coerceSourceRowType(sourceScope, insert.getSource(), columnIndex, totalColumns, targetType);
        case UPDATE:
            // trailing elements of selectList are equal to elements of sourceExpressionList
            // see JetSqlValidator.validateUpdate()
            SqlUpdate update = (SqlUpdate) query;
            SqlNodeList selectList = update.getSourceSelect().getSelectList();
            return coerceSourceRowType(sourceScope, selectList, selectList.size() - totalColumns + columnIndex, targetType);
        default:
            return rowTypeCoercion(sourceScope, query, columnIndex, targetType);
    }
}
Also used : SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlUpdate(org.apache.calcite.sql.SqlUpdate)

Example 62 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project hazelcast by hazelcast.

the class SqlExtendedInsert method validate.

@Override
public void validate(SqlValidator validator, SqlValidatorScope scope) {
    SqlValidatorTable table0 = validator.getCatalogReader().getTable(tableNames());
    if (table0 == null) {
        super.validate(validator, scope);
        // should have failed with "Object not found"
        assert false;
    }
    HazelcastTable table = table0.unwrap(HazelcastTable.class);
    if (getTargetColumnList() == null) {
        RelDataType rowType = table.getRowType(validator.getTypeFactory());
        List<SqlNode> columnListWithoutHidden = new ArrayList<>();
        for (RelDataTypeField f : rowType.getFieldList()) {
            if (!table.isHidden(f.getName())) {
                columnListWithoutHidden.add(new SqlIdentifier(f.getName(), SqlParserPos.ZERO));
            }
        }
        overrideColumnList = new SqlNodeList(columnListWithoutHidden, SqlParserPos.ZERO);
    }
    super.validate(validator, scope);
    Map<String, TableField> fieldsMap = table.getTarget().getFields().stream().collect(Collectors.toMap(TableField::getName, f -> f));
    for (SqlNode fieldNode : getTargetColumnList()) {
        TableField field = fieldsMap.get(((SqlIdentifier) fieldNode).getSimple());
        if (field instanceof MapTableField) {
            QueryPath path = ((MapTableField) field).getPath();
            if (path.getPath() == null && field.getType().getTypeFamily() == QueryDataTypeFamily.OBJECT) {
                throw validator.newValidationError(fieldNode, RESOURCE.insertToTopLevelObject());
            }
        }
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) RESOURCE(com.hazelcast.jet.sql.impl.parse.ParserResource.RESOURCE) Collectors(java.util.stream.Collectors) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) ArrayList(java.util.ArrayList) TableField(com.hazelcast.sql.impl.schema.TableField) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) List(java.util.List) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) ImmutableList(com.google.common.collect.ImmutableList) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Map(java.util.Map) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 63 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList 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 64 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList 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 65 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project hazelcast by hazelcast.

the class HazelcastCaseOperator method unparse.

@Override
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    assert call instanceof HazelcastSqlCase;
    final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.CASE, "CASE", "END");
    HazelcastSqlCase sqlCase = (HazelcastSqlCase) call;
    SqlNodeList whenList = sqlCase.getWhenOperands();
    SqlNodeList thenList = sqlCase.getThenOperands();
    assert whenList.size() == thenList.size();
    for (Pair<SqlNode, SqlNode> pair : Pair.zip(whenList, thenList)) {
        writer.sep("WHEN");
        pair.left.unparse(writer, 0, 0);
        writer.sep("THEN");
        pair.right.unparse(writer, 0, 0);
    }
    writer.sep("ELSE");
    SqlNode elseExpr = sqlCase.getElseOperand();
    elseExpr.unparse(writer, 0, 0);
    writer.endList(frame);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) HazelcastSqlCase(com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNodeList (org.apache.calcite.sql.SqlNodeList)123 SqlNode (org.apache.calcite.sql.SqlNode)97 ArrayList (java.util.ArrayList)45 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)43 RelDataType (org.apache.calcite.rel.type.RelDataType)39 SqlCall (org.apache.calcite.sql.SqlCall)30 SqlSelect (org.apache.calcite.sql.SqlSelect)29 BitString (org.apache.calcite.util.BitString)23 RexNode (org.apache.calcite.rex.RexNode)13 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 NlsString (org.apache.calcite.util.NlsString)9 RelNode (org.apache.calcite.rel.RelNode)8 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)7 SqlWriter (org.apache.calcite.sql.SqlWriter)7 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)7 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)6