Search in sources :

Example 31 with SqlCall

use of org.apache.calcite.sql.SqlCall in project drill by apache.

the class DrillValidator method validateFrom.

@Override
protected void validateFrom(SqlNode node, RelDataType targetRowType, SqlValidatorScope scope) {
    if (node.getKind() == SqlKind.AS) {
        SqlCall sqlCall = (SqlCall) node;
        SqlNode sqlNode = sqlCall.operand(0);
        switch(sqlNode.getKind()) {
            case IDENTIFIER:
                SqlIdentifier tempNode = (SqlIdentifier) sqlNode;
                changeNamesIfTableIsTemporary(tempNode);
                replaceAliasWithActualName(tempNode);
                // Check the schema and throw a valid SchemaNotFound exception instead of TableNotFound exception.
                ((DrillCalciteCatalogReader) getCatalogReader()).isValidSchema(tempNode.names);
                break;
            case UNNEST:
                if (sqlCall.operandCount() < 3) {
                    throw Static.RESOURCE.validationError("Alias table and column name are required for UNNEST").ex();
                }
        }
    }
    if (isImpersonationEnabled) {
        ImpersonationUtil.getProcessUserUGI().doAs((PrivilegedAction<Void>) () -> {
            super.validateFrom(node, targetRowType, scope);
            return null;
        });
    } else {
        super.validateFrom(node, targetRowType, scope);
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 32 with SqlCall

use of org.apache.calcite.sql.SqlCall in project drill by apache.

the class UnsupportedOperatorsVisitor method detectMultiplePartitions.

/**
 * Disable multiple partitions in a SELECT-CLAUSE
 * If multiple partitions are defined in the query,
 * SqlUnsupportedException would be thrown to inform
 * @param sqlSelect SELECT-CLAUSE in the query
 */
private void detectMultiplePartitions(SqlSelect sqlSelect) {
    for (SqlNode nodeInSelectList : sqlSelect.getSelectList()) {
        // enter the first operand of AS operator
        if (nodeInSelectList.getKind() == SqlKind.AS && (((SqlCall) nodeInSelectList).getOperandList().get(0).getKind() == SqlKind.OVER)) {
            nodeInSelectList = ((SqlCall) nodeInSelectList).getOperandList().get(0);
        }
        if (nodeInSelectList.getKind() != SqlKind.OVER) {
            continue;
        }
        // This is used to keep track of the window function which has been defined
        SqlNode definedWindow = null;
        SqlNode window = ((SqlCall) nodeInSelectList).operand(1);
        // which is defined in the window list
        if (window instanceof SqlIdentifier) {
            // Expand the SqlIdentifier as the expression defined in the window list
            for (SqlNode sqlNode : sqlSelect.getWindowList()) {
                if (((SqlWindow) sqlNode).getDeclName().equalsDeep(window, Litmus.IGNORE)) {
                    window = sqlNode;
                    break;
                }
            }
            assert !(window instanceof SqlIdentifier) : "Identifier should have been expanded as a window defined in the window list";
        }
        // In a SELECT-SCOPE, only a partition can be defined
        if (definedWindow == null) {
            definedWindow = window;
        } else {
            if (!definedWindow.equalsDeep(window, Litmus.IGNORE)) {
                unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Multiple window definitions in a single SELECT list is not currently supported \n" + "See Apache Drill JIRA: DRILL-3196");
                throw new UnsupportedOperationException();
            }
        }
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 33 with SqlCall

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

the class HiveParserBaseSemanticAnalyzer method getBound.

public static RexWindowBound getBound(HiveParserWindowingSpec.BoundarySpec spec, RelOptCluster cluster) {
    RexWindowBound res = null;
    if (spec != null) {
        SqlParserPos dummyPos = new SqlParserPos(1, 1);
        SqlNode amt = spec.getAmt() == 0 || spec.getAmt() == HiveParserWindowingSpec.BoundarySpec.UNBOUNDED_AMOUNT ? null : SqlLiteral.createExactNumeric(String.valueOf(spec.getAmt()), new SqlParserPos(2, 2));
        RexNode amtLiteral = amt == null ? null : cluster.getRexBuilder().makeLiteral(spec.getAmt(), cluster.getTypeFactory().createSqlType(SqlTypeName.INTEGER), true);
        switch(spec.getDirection()) {
            case PRECEDING:
                if (amt == null) {
                    res = RexWindowBound.create(SqlWindow.createUnboundedPreceding(dummyPos), null);
                } else {
                    SqlCall call = (SqlCall) SqlWindow.createPreceding(amt, dummyPos);
                    res = RexWindowBound.create(call, cluster.getRexBuilder().makeCall(call.getOperator(), amtLiteral));
                }
                break;
            case CURRENT:
                res = RexWindowBound.create(SqlWindow.createCurrentRow(dummyPos), null);
                break;
            case FOLLOWING:
                if (amt == null) {
                    res = RexWindowBound.create(SqlWindow.createUnboundedFollowing(dummyPos), null);
                } else {
                    SqlCall call = (SqlCall) SqlWindow.createFollowing(amt, dummyPos);
                    res = RexWindowBound.create(call, cluster.getRexBuilder().makeCall(call.getOperator(), amtLiteral));
                }
                break;
        }
    }
    return res;
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlCall(org.apache.calcite.sql.SqlCall) RexWindowBound(org.apache.calcite.rex.RexWindowBound) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 34 with SqlCall

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

the class SqlValidatorImpl method findAllValidFunctionNames.

private static void findAllValidFunctionNames(List<String> names, SqlValidator validator, Collection<SqlMoniker> result, SqlParserPos pos) {
    // a function name can only be 1 part
    if (names.size() > 1) {
        return;
    }
    for (SqlOperator op : validator.getOperatorTable().getOperatorList()) {
        SqlIdentifier curOpId = new SqlIdentifier(op.getName(), pos);
        final SqlCall call = validator.makeNullaryCall(curOpId);
        if (call != null) {
            result.add(new SqlMonikerImpl(op.getName(), SqlMonikerType.FUNCTION));
        } else {
            if ((op.getSyntax() == SqlSyntax.FUNCTION) || (op.getSyntax() == SqlSyntax.PREFIX)) {
                if (op.getOperandTypeChecker() != null) {
                    String sig = op.getAllowedSignatures();
                    sig = sig.replace("'", "");
                    result.add(new SqlMonikerImpl(sig, SqlMonikerType.FUNCTION));
                    continue;
                }
                result.add(new SqlMonikerImpl(op.getName(), SqlMonikerType.FUNCTION));
            }
        }
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 35 with SqlCall

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

the class SqlValidatorImpl method validateQuery.

public void validateQuery(SqlNode node, SqlValidatorScope scope, RelDataType targetRowType) {
    final SqlValidatorNamespace ns = getNamespace(node, scope);
    if (node.getKind() == SqlKind.TABLESAMPLE) {
        List<SqlNode> operands = ((SqlCall) node).getOperandList();
        SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands.get(1));
        if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
            validateFeature(RESOURCE.sQLFeature_T613(), node.getParserPosition());
        } else if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
            validateFeature(RESOURCE.sQLFeatureExt_T613_Substitution(), node.getParserPosition());
        }
    }
    validateNamespace(ns, targetRowType);
    switch(node.getKind()) {
        case EXTEND:
            // Until we have a dedicated namespace for EXTEND
            deriveType(scope, node);
    }
    if (node == top) {
        validateModality(node);
    }
    validateAccess(node, ns.getTable(), SqlAccessEnum.SELECT);
    validateSnapshot(node, scope, ns);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlSampleSpec(org.apache.calcite.sql.SqlSampleSpec) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlCall (org.apache.calcite.sql.SqlCall)108 SqlNode (org.apache.calcite.sql.SqlNode)81 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)32 SqlNodeList (org.apache.calcite.sql.SqlNodeList)32 RelDataType (org.apache.calcite.rel.type.RelDataType)30 SqlOperator (org.apache.calcite.sql.SqlOperator)26 BitString (org.apache.calcite.util.BitString)19 ArrayList (java.util.ArrayList)18 SqlSelect (org.apache.calcite.sql.SqlSelect)17 RexNode (org.apache.calcite.rex.RexNode)13 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)12 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)11 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 SqlKind (org.apache.calcite.sql.SqlKind)9 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)9 List (java.util.List)8 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)8 Pair (org.apache.calcite.util.Pair)8 RelNode (org.apache.calcite.rel.RelNode)7