Search in sources :

Example 31 with SqlSelect

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

the class ParserNameResolutionTest method checkSuccess.

private static void checkSuccess(OptimizerContext context, String fieldName, String tableFqn, String... tableComponents) {
    QueryParseResult res = context.parse(composeSelect(fieldName, tableComponents));
    SqlSelect select = (SqlSelect) res.getNode();
    SqlNodeList selectList = select.getSelectList();
    assertEquals(1, selectList.size());
    SqlIdentifier fieldIdentifier = (SqlIdentifier) selectList.get(0);
    assertEquals(SqlIdentifier.getString(Arrays.asList(last(tableComponents), fieldName)), fieldIdentifier.toString());
    SqlCall from = (SqlCall) select.getFrom();
    assertEquals(from.getKind(), SqlKind.AS);
    assertEquals(tableFqn, from.operand(0).toString());
    assertEquals(last(tableComponents), from.operand(1).toString());
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 32 with SqlSelect

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

the class HazelcastTypeCoercion method rowTypeCoercion.

/**
 * {@inheritDoc}
 * <p>
 * We change the contract of the superclass' return type. According to the
 * superclass contract we're supposed to return true iff we successfully
 * added a CAST. This method returns true if the expression can now be
 * assigned to {@code targetType}, either because a CAST was added, or
 * because it already was assignable (e.g. the type was same). This is
 * needed for {@link #querySourceCoercion} method, which calls this method.
 *
 * @return True, if the source column can now be assigned to {@code
 *      targetType}
 */
@Override
public boolean rowTypeCoercion(SqlValidatorScope scope, SqlNode query, int columnIndex, RelDataType targetType) {
    switch(query.getKind()) {
        case SELECT:
            SqlSelect selectNode = (SqlSelect) query;
            SqlValidatorScope selectScope = validator.getSelectScope(selectNode);
            if (!rowTypeElementCoercion(selectScope, selectNode.getSelectList().get(columnIndex), targetType, newNode -> selectNode.getSelectList().set(columnIndex, newNode))) {
                return false;
            }
            updateInferredColumnType(selectScope, query, columnIndex, targetType);
            return true;
        case VALUES:
            for (SqlNode rowConstructor : ((SqlCall) query).getOperandList()) {
                if (!rowTypeElementCoercion(scope, ((SqlCall) rowConstructor).operand(columnIndex), targetType, newNode -> ((SqlCall) rowConstructor).setOperand(columnIndex, newNode))) {
                    return false;
                }
            }
            updateInferredColumnType(scope, query, columnIndex, targetType);
            return true;
        default:
            throw new UnsupportedOperationException("unexpected: " + query.getKind());
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlUpdate(org.apache.calcite.sql.SqlUpdate) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlInsert(org.apache.calcite.sql.SqlInsert) TypeCoercionImpl(org.apache.calcite.sql.validate.implicit.TypeCoercionImpl) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) RESOURCE(org.apache.calcite.util.Static.RESOURCE) SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode) SqlUtil(org.apache.calcite.sql.SqlUtil) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlSelect(org.apache.calcite.sql.SqlSelect) OBJECT(com.hazelcast.sql.impl.type.QueryDataType.OBJECT) ANY(org.apache.calcite.sql.type.SqlTypeName.ANY) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlTypeUtil(org.apache.calcite.sql.type.SqlTypeUtil) SqlUserDefinedTypeNameSpec(org.apache.calcite.sql.SqlUserDefinedTypeNameSpec) Consumer(java.util.function.Consumer) List(java.util.List) SqlFunction(org.apache.calcite.sql.SqlFunction) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) HazelcastSqlOperatorTable(com.hazelcast.jet.sql.impl.validate.HazelcastSqlOperatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) NULL(org.apache.calcite.sql.type.SqlTypeName.NULL) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 33 with SqlSelect

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

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

use of org.apache.calcite.sql.SqlSelect in project samza by apache.

the class SamzaSqlQueryParser method getSource.

private static void getSource(SqlNode node, ArrayList<String> sourceList) {
    if (node instanceof SqlJoin) {
        SqlJoin joinNode = (SqlJoin) node;
        ArrayList<String> sourcesLeft = new ArrayList<>();
        ArrayList<String> sourcesRight = new ArrayList<>();
        getSource(joinNode.getLeft(), sourcesLeft);
        getSource(joinNode.getRight(), sourcesRight);
        sourceList.addAll(sourcesLeft);
        sourceList.addAll(sourcesRight);
    } else if (node instanceof SqlIdentifier) {
        sourceList.add(node.toString());
    } else if (node instanceof SqlBasicCall) {
        SqlBasicCall basicCall = (SqlBasicCall) node;
        if (basicCall.getOperator() instanceof SqlAsOperator) {
            getSource(basicCall.operand(0), sourceList);
        } else if (basicCall.getOperator() instanceof SqlUnnestOperator && basicCall.operand(0) instanceof SqlSelect) {
            sourceList.addAll(getSourcesFromSelectQuery(basicCall.operand(0)));
        }
    } else if (node instanceof SqlSelect) {
        getSource(((SqlSelect) node).getFrom(), sourceList);
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlJoin(org.apache.calcite.sql.SqlJoin) ArrayList(java.util.ArrayList) SqlUnnestOperator(org.apache.calcite.sql.SqlUnnestOperator) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlAsOperator(org.apache.calcite.sql.SqlAsOperator)

Aggregations

SqlSelect (org.apache.calcite.sql.SqlSelect)62 SqlNode (org.apache.calcite.sql.SqlNode)45 SqlNodeList (org.apache.calcite.sql.SqlNodeList)29 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)20 SqlCall (org.apache.calcite.sql.SqlCall)17 RelDataType (org.apache.calcite.rel.type.RelDataType)15 BitString (org.apache.calcite.util.BitString)12 ArrayList (java.util.ArrayList)9 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlInsert (org.apache.calcite.sql.SqlInsert)7 SchemaPlus (org.apache.calcite.schema.SchemaPlus)6 SqlMerge (org.apache.calcite.sql.SqlMerge)6 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)5 SqlDelete (org.apache.calcite.sql.SqlDelete)5 SqlJoin (org.apache.calcite.sql.SqlJoin)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlOrderBy (org.apache.calcite.sql.SqlOrderBy)4 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)4 RelOptTable (org.apache.calcite.plan.RelOptTable)3 RelNode (org.apache.calcite.rel.RelNode)3