Search in sources :

Example 71 with SqlNodeList

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

the class SqlShuttle method visit.

public SqlNode visit(SqlNodeList nodeList) {
    boolean update = false;
    List<SqlNode> exprs = nodeList.getList();
    int exprCount = exprs.size();
    List<SqlNode> newList = new ArrayList<SqlNode>(exprCount);
    for (SqlNode operand : exprs) {
        SqlNode clonedOperand;
        if (operand == null) {
            clonedOperand = null;
        } else {
            clonedOperand = operand.accept(this);
            if (clonedOperand != operand) {
                update = true;
            }
        }
        newList.add(clonedOperand);
    }
    if (update) {
        return new SqlNodeList(newList, nodeList.getParserPosition());
    } else {
        return nodeList;
    }
}
Also used : ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 72 with SqlNodeList

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

the class AggChecker method visit.

public Void visit(SqlCall call) {
    final SqlValidatorScope scope = scopes.peek();
    if (call.getOperator().isAggregator()) {
        if (distinct) {
            if (scope instanceof AggregatingSelectScope) {
                SqlNodeList selectList = ((SqlSelect) scope.getNode()).getSelectList();
                // Check if this aggregation function is just an element in the select
                for (SqlNode sqlNode : selectList) {
                    if (sqlNode.getKind() == SqlKind.AS) {
                        sqlNode = ((SqlCall) sqlNode).operand(0);
                    }
                    if (validator.expand(sqlNode, scope).equalsDeep(call, Litmus.IGNORE)) {
                        return null;
                    }
                }
            }
            // Cannot use agg fun in ORDER BY clause if have SELECT DISTINCT.
            SqlNode originalExpr = validator.getOriginal(call);
            final String exprString = originalExpr.toString();
            throw validator.newValidationError(call, RESOURCE.notSelectDistinctExpr(exprString));
        }
        // BY deptno'
        return null;
    }
    if (call.getKind() == SqlKind.FILTER) {
        call.operand(0).accept(this);
        return null;
    }
    // Visit the operand in window function
    if (call.getKind() == SqlKind.OVER) {
        for (SqlNode operand : call.<SqlCall>operand(0).getOperandList()) {
            operand.accept(this);
        }
        // Check the OVER clause
        final SqlNode over = call.operand(1);
        if (over instanceof SqlCall) {
            over.accept(this);
        } else if (over instanceof SqlIdentifier) {
            // Check the corresponding SqlWindow in WINDOW clause
            final SqlWindow window = scope.lookupWindow(((SqlIdentifier) over).getSimple());
            window.getPartitionList().accept(this);
            window.getOrderList().accept(this);
        }
    }
    if (isGroupExpr(call)) {
        // This call matches an expression in the GROUP BY clause.
        return null;
    }
    final SqlCall groupCall = SqlStdOperatorTable.convertAuxiliaryToGroupCall(call);
    if (groupCall != null) {
        if (isGroupExpr(groupCall)) {
            // TUMBLE(rowtime, INTERVAL '1' HOUR')
            return null;
        }
        throw validator.newValidationError(groupCall, RESOURCE.auxiliaryWithoutMatchingGroupCall(call.getOperator().getName(), groupCall.getOperator().getName()));
    }
    if (call.isA(SqlKind.QUERY)) {
        // references to forbidden columns.
        return null;
    }
    // Switch to new scope.
    SqlValidatorScope newScope = scope.getOperandScope(call);
    scopes.push(newScope);
    // Visit the operands (only expressions).
    call.getOperator().acceptCall(this, call, true, ArgHandlerImpl.<Void>instance());
    // Restore scope.
    scopes.pop();
    return null;
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 73 with SqlNodeList

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

the class SqlAddHivePartitions method toProps.

private static List<SqlNodeList> toProps(List<SqlCharStringLiteral> partLocations) {
    List<SqlNodeList> res = new ArrayList<>(partLocations.size());
    for (SqlCharStringLiteral partLocation : partLocations) {
        SqlNodeList prop = null;
        if (partLocation != null) {
            prop = new SqlNodeList(partLocation.getParserPosition());
            prop.add(HiveDDLUtils.toTableOption(SqlCreateHiveTable.TABLE_LOCATION_URI, partLocation, partLocation.getParserPosition()));
        }
        res.add(prop);
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral)

Example 74 with SqlNodeList

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

the class SqlAlterHiveTableAddReplaceColumn method unparse.

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.keyword("ALTER TABLE");
    tableIdentifier.unparse(writer, leftPrec, rightPrec);
    SqlNodeList partitionSpec = getPartitionSpec();
    if (partitionSpec != null && partitionSpec.size() > 0) {
        writer.keyword("PARTITION");
        partitionSpec.unparse(writer, getOperator().getLeftPrec(), getOperator().getRightPrec());
    }
    if (isReplace()) {
        writer.keyword("REPLACE");
    } else {
        writer.keyword("ADD");
    }
    writer.keyword("COLUMNS");
    SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.create("sds"), "(", ")");
    for (SqlNode column : origColumns) {
        printIndent(writer);
        column.unparse(writer, leftPrec, rightPrec);
    }
    writer.newlineAndIndent();
    writer.endList(frame);
    if (cascade) {
        writer.keyword("CASCADE");
    } else {
        writer.keyword("RESTRICT");
    }
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 75 with SqlNodeList

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

the class SqlAlterHiveTableFileFormat method createPropList.

private static SqlNodeList createPropList(SqlIdentifier format) {
    SqlNodeList res = new SqlNodeList(format.getParserPosition());
    res.add(HiveDDLUtils.toTableOption(SqlCreateHiveTable.HiveTableStoredAs.STORED_AS_FILE_FORMAT, format.getSimple(), format.getParserPosition()));
    return res;
}
Also used : SqlNodeList(org.apache.calcite.sql.SqlNodeList)

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