Search in sources :

Example 51 with SqlCall

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

the class NamedOperandCheckerProgram method check.

public boolean check(HazelcastCallBinding callBinding, boolean throwOnFailure) {
    boolean res = true;
    SqlCall call = callBinding.getCall();
    SqlFunction operator = (SqlFunction) call.getOperator();
    for (int i = 0; i < call.operandCount(); i++) {
        SqlNode operand = call.operand(i);
        assert operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT;
        SqlIdentifier id = ((SqlCall) operand).operand(1);
        OperandChecker checker = findOperandChecker(id, operator);
        res &= checker.check(callBinding, false, i);
    }
    if (!res && throwOnFailure) {
        throw callBinding.newValidationSignatureError();
    }
    return res;
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlFunction(org.apache.calcite.sql.SqlFunction) SqlNode(org.apache.calcite.sql.SqlNode)

Example 52 with SqlCall

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

the class HazelcastSqlValidator method validateUpdate.

@Override
public void validateUpdate(SqlUpdate update) {
    super.validateUpdate(update);
    // hack around Calcite deficiency of not deriving types for fields in sourceExpressionList...
    // see HazelcastTypeCoercion.coerceSourceRowType()
    SqlNodeList selectList = update.getSourceSelect().getSelectList();
    SqlNodeList sourceExpressionList = update.getSourceExpressionList();
    for (int i = 0; i < sourceExpressionList.size(); i++) {
        update.getSourceExpressionList().set(i, selectList.get(selectList.size() - sourceExpressionList.size() + i));
    }
    // UPDATE FROM SELECT is transformed into join (which is not supported yet):
    // UPDATE m1 SET __key = m2.this FROM m2 WHERE m1.__key = m2.__key
    // UPDATE m1 SET __key = (SELECT this FROM m2) WHERE __key = 1
    // UPDATE m1 SET __key = (SELECT m2.this FROM m2 WHERE m1.__key = m2.__key)
    update.getSourceSelect().getSelectList().accept(new SqlBasicVisitor<Void>() {

        @Override
        public Void visit(SqlCall call) {
            if (call.getKind() == SqlKind.SELECT) {
                throw newValidationError(update, RESOURCE.updateFromSelectNotSupported());
            }
            return call.getOperator().acceptCall(this, call);
        }
    });
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList)

Example 53 with SqlCall

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

the class HazelcastCaseOperator method checkOperandTypes.

@Override
@SuppressWarnings("checkstyle:MethodLength")
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    HazelcastSqlValidator validator = (HazelcastSqlValidator) callBinding.getValidator();
    HazelcastSqlCase sqlCall = (HazelcastSqlCase) callBinding.getCall();
    // at this point `CASE x WHEN y ...` is already converted to `CASE WHEN x=y ...`
    assert sqlCall.getValueOperand() == null;
    SqlNodeList whenList = sqlCall.getWhenOperands();
    SqlNodeList thenList = sqlCall.getThenOperands();
    SqlNode elseOperand = sqlCall.getElseOperand();
    assert whenList.size() > 0 : "no WHEN clause";
    assert whenList.size() == thenList.size();
    SqlValidatorScope scope = callBinding.getScope();
    if (!typeCheckWhen(scope, validator, whenList)) {
        if (throwOnFailure) {
            throw callBinding.newError(RESOURCE.expectedBoolean());
        }
        return false;
    }
    List<RelDataType> argTypes = new ArrayList<>(thenList.size() + 1);
    for (SqlNode node : thenList) {
        argTypes.add(validator.deriveType(scope, node));
    }
    argTypes.add(validator.deriveType(scope, elseOperand));
    // noinspection OptionalGetWithoutIsPresent
    RelDataType caseReturnType = argTypes.stream().reduce(HazelcastTypeUtils::withHigherPrecedence).get();
    Supplier<CalciteContextException> exceptionSupplier = () -> validator.newValidationError(sqlCall, HazelcastResources.RESOURCES.cannotInferCaseResult(argTypes.toString(), "CASE"));
    for (int i = 0; i < thenList.size(); i++) {
        int finalI = i;
        if (!coerceItem(validator, scope, thenList.get(i), caseReturnType, sqlNode -> thenList.getList().set(finalI, sqlNode), throwOnFailure, exceptionSupplier)) {
            return false;
        }
    }
    return coerceItem(validator, scope, elseOperand, caseReturnType, sqlNode -> sqlCall.setOperand(ELSE_BRANCH_OPERAND_INDEX, sqlNode), throwOnFailure, exceptionSupplier);
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlSyntax(org.apache.calcite.sql.SqlSyntax) SqlCaseOperator(org.apache.calcite.sql.fun.SqlCaseOperator) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) HazelcastReturnTypeInference.wrap(com.hazelcast.jet.sql.impl.validate.operators.typeinference.HazelcastReturnTypeInference.wrap) SqlOperandCountRanges(org.apache.calcite.sql.type.SqlOperandCountRanges) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) HazelcastResources(com.hazelcast.jet.sql.impl.validate.HazelcastResources) Supplier(java.util.function.Supplier) RESOURCE(org.apache.calcite.util.Static.RESOURCE) ArrayList(java.util.ArrayList) SqlCall(org.apache.calcite.sql.SqlCall) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) Pair(org.apache.calcite.util.Pair) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlOperatorBinding(org.apache.calcite.sql.SqlOperatorBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) SqlKind(org.apache.calcite.sql.SqlKind) SqlWriter(org.apache.calcite.sql.SqlWriter) HazelcastTypeUtils(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlTypeUtil(org.apache.calcite.sql.type.SqlTypeUtil) HazelcastSqlCase(com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase) Consumer(java.util.function.Consumer) SqlReturnTypeInference(org.apache.calcite.sql.type.SqlReturnTypeInference) List(java.util.List) SqlOperandCountRange(org.apache.calcite.sql.SqlOperandCountRange) SqlNodeList(org.apache.calcite.sql.SqlNodeList) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) HazelcastSqlCase(com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 54 with SqlCall

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

the class HazelcastOperandTypeInference method inferOperandTypes.

@Override
public void inferOperandTypes(SqlCallBinding callBinding, RelDataType returnType, RelDataType[] operandTypes) {
    SqlCall call = callBinding.getCall();
    if (ValidationUtil.hasAssignment(call)) {
        RelDataTypeFactory typeFactory = callBinding.getTypeFactory();
        RelDataType[] parameterTypes = new RelDataType[parametersByName.size()];
        for (int i = 0; i < call.operandCount(); i++) {
            SqlCall assignment = call.operand(i);
            SqlIdentifier id = assignment.operand(1);
            String name = id.getSimple();
            HazelcastTableFunctionParameter parameter = parametersByName.get(name);
            if (parameter != null) {
                SqlTypeName parameterType = parameter.type();
                parameterTypes[parameter.ordinal()] = toType(parameterType, typeFactory);
            } else {
                throw SqlUtil.newContextException(id.getParserPosition(), RESOURCE.unknownArgumentName(name));
            }
        }
        // noinspection ResultOfMethodCallIgnored
        Arrays.stream(parameterTypes).filter(Objects::nonNull).toArray(ignored -> operandTypes);
    } else {
        positionalOperandTypeInference.inferOperandTypes(callBinding, returnType, operandTypes);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlCall(org.apache.calcite.sql.SqlCall) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) HazelcastTableFunctionParameter(com.hazelcast.jet.sql.impl.schema.HazelcastTableFunctionParameter) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 55 with SqlCall

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

the class UnsupportedOperatorsVisitor method visit.

@Override
public SqlNode visit(SqlCall sqlCall) {
    // Inspect the window functions
    if (sqlCall instanceof SqlSelect) {
        SqlSelect sqlSelect = (SqlSelect) sqlCall;
        checkGrouping((sqlSelect));
        checkRollupCubeGrpSets(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) {
                // Throw exceptions if window functions are disabled
                if (!context.getOptions().getOption(ExecConstants.ENABLE_WINDOW_FUNCTIONS).bool_val) {
                    unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Window functions are disabled\n" + "See Apache Drill JIRA: DRILL-2559");
                    throw new UnsupportedOperationException();
                }
                // DRILL-3182, DRILL-3195
                SqlCall over = (SqlCall) nodeInSelectList;
                if (over.getOperandList().get(0) instanceof SqlCall) {
                    SqlCall function = (SqlCall) over.getOperandList().get(0);
                    // Window function with DISTINCT qualifier is temporarily disabled
                    if (function.getFunctionQuantifier() != null && function.getFunctionQuantifier().getValue() == SqlSelectKeyword.DISTINCT) {
                        unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "DISTINCT for window aggregate functions is not currently supported\n" + "See Apache Drill JIRA: DRILL-3182");
                        throw new UnsupportedOperationException();
                    }
                    // DRILL-3596: we only allow (<column-name>) or (<column-name>, 1)
                    final String functionName = function.getOperator().getName().toUpperCase();
                    if ("LEAD".equals(functionName) || "LAG".equals(functionName)) {
                        boolean supported = true;
                        if (function.operandCount() > 2) {
                            // we don't support more than 2 arguments
                            supported = false;
                        } else if (function.operandCount() == 2) {
                            SqlNode operand = function.operand(1);
                            if (operand instanceof SqlNumericLiteral) {
                                SqlNumericLiteral offsetLiteral = (SqlNumericLiteral) operand;
                                try {
                                    if (offsetLiteral.intValue(true) != 1) {
                                        // we don't support offset != 1
                                        supported = false;
                                    }
                                } catch (AssertionError e) {
                                    // we only support offset as an integer
                                    supported = false;
                                }
                            } else {
                                // we only support offset as a numeric literal
                                supported = false;
                            }
                        }
                        if (!supported) {
                            unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Function " + functionName + " only supports (<value expression>) or (<value expression>, 1)\n" + "See Apache DRILL JIRA: DRILL-3596");
                            throw new UnsupportedOperationException();
                        }
                    }
                }
            }
        }
    }
    // (i.e., BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    if (sqlCall instanceof SqlWindow) {
        SqlWindow window = (SqlWindow) sqlCall;
        SqlNode lowerBound = window.getLowerBound();
        SqlNode upperBound = window.getUpperBound();
        // If no frame is specified
        // it is a default frame
        boolean isSupported = (lowerBound == null && upperBound == null);
        // RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
        if (window.getOrderList().size() != 0 && !window.isRows() && SqlWindow.isUnboundedPreceding(lowerBound) && (upperBound == null || SqlWindow.isCurrentRow(upperBound) || SqlWindow.isUnboundedFollowing(upperBound))) {
            isSupported = true;
        }
        // is supported with and without the ORDER BY clause
        if (window.isRows() && SqlWindow.isUnboundedPreceding(lowerBound) && (upperBound == null || SqlWindow.isCurrentRow(upperBound))) {
            isSupported = true;
        }
        // is supported with and without an ORDER BY clause
        if (!window.isRows() && SqlWindow.isCurrentRow(lowerBound) && SqlWindow.isCurrentRow(upperBound)) {
            isSupported = true;
        }
        // ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
        if (window.getOrderList().size() == 0 && SqlWindow.isUnboundedPreceding(lowerBound) && SqlWindow.isUnboundedFollowing(upperBound)) {
            isSupported = true;
        }
        if (!isSupported) {
            unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "This type of window frame is currently not supported \n" + "See Apache Drill JIRA: DRILL-3188");
            throw new UnsupportedOperationException();
        }
        // DRILL-3189: Disable DISALLOW PARTIAL
        if (!window.isAllowPartial()) {
            unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Disallowing partial windows is currently not supported \n" + "See Apache Drill JIRA: DRILL-3189");
            throw new UnsupportedOperationException();
        }
    }
    // Disable unsupported Intersect, Except
    if (sqlCall.getKind() == SqlKind.INTERSECT || sqlCall.getKind() == SqlKind.EXCEPT) {
        unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.RELATIONAL, sqlCall.getOperator().getName() + " is not supported\n" + "See Apache Drill JIRA: DRILL-1921");
        throw new UnsupportedOperationException();
    }
    // Disable unsupported JOINs
    if (sqlCall.getKind() == SqlKind.JOIN) {
        SqlJoin join = (SqlJoin) sqlCall;
        // Block Natural Join
        if (join.isNatural()) {
            unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.RELATIONAL, "NATURAL JOIN is not supported\n" + "See Apache Drill JIRA: DRILL-1986");
            throw new UnsupportedOperationException();
        }
        // Block Cross Join
        if (join.getJoinType() == JoinType.CROSS) {
            unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.RELATIONAL, "CROSS JOIN is not supported\n" + "See Apache Drill JIRA: DRILL-1921");
            throw new UnsupportedOperationException();
        }
    }
    // Disable Function
    for (String strOperator : disabledOperators) {
        if (sqlCall.getOperator().isName(strOperator)) {
            unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, sqlCall.getOperator().getName() + " is not supported\n" + "See Apache Drill JIRA: DRILL-2115");
            throw new UnsupportedOperationException();
        }
    }
    // Disable complex functions incorrect placement
    if (sqlCall instanceof SqlSelect) {
        SqlSelect sqlSelect = (SqlSelect) sqlCall;
        for (SqlNode nodeInSelectList : sqlSelect.getSelectList()) {
            if (checkDirExplorers(nodeInSelectList)) {
                unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Directory explorers " + dirExplorers + " functions are not supported in Select List\n" + "See Apache Drill JIRA: DRILL-3944");
                throw new UnsupportedOperationException();
            }
        }
        if (sqlSelect.hasWhere()) {
            if (checkDirExplorers(sqlSelect.getWhere()) && !context.getPlannerSettings().isConstantFoldingEnabled()) {
                unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Directory explorers " + dirExplorers + " functions can not be used " + "when " + PlannerSettings.CONSTANT_FOLDING.getOptionName() + " option is set to false\n" + "See Apache Drill JIRA: DRILL-3944");
                throw new UnsupportedOperationException();
            }
        }
        if (sqlSelect.hasOrderBy()) {
            for (SqlNode sqlNode : sqlSelect.getOrderList()) {
                if (containsFlatten(sqlNode)) {
                    unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Flatten function is not supported in Order By\n" + "See Apache Drill JIRA: DRILL-2181");
                    throw new UnsupportedOperationException();
                } else if (checkDirExplorers(sqlNode)) {
                    unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Directory explorers " + dirExplorers + " functions are not supported in Order By\n" + "See Apache Drill JIRA: DRILL-3944");
                    throw new UnsupportedOperationException();
                }
            }
        }
        if (sqlSelect.getGroup() != null) {
            for (SqlNode sqlNode : sqlSelect.getGroup()) {
                if (containsFlatten(sqlNode)) {
                    unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Flatten function is not supported in Group By\n" + "See Apache Drill JIRA: DRILL-2181");
                    throw new UnsupportedOperationException();
                } else if (checkDirExplorers(sqlNode)) {
                    unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Directory explorers " + dirExplorers + " functions are not supported in Group By\n" + "See Apache Drill JIRA: DRILL-3944");
                    throw new UnsupportedOperationException();
                }
            }
        }
        if (sqlSelect.isDistinct()) {
            for (SqlNode column : sqlSelect.getSelectList()) {
                if (column.getKind() == SqlKind.AS) {
                    if (containsFlatten(((SqlCall) column).getOperandList().get(0))) {
                        unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Flatten function is not supported in Distinct\n" + "See Apache Drill JIRA: DRILL-2181");
                        throw new UnsupportedOperationException();
                    }
                } else {
                    if (containsFlatten(column)) {
                        unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Flatten function is not supported in Distinct\n" + "See Apache Drill JIRA: DRILL-2181");
                        throw new UnsupportedOperationException();
                    }
                }
            }
        }
    }
    if (DrillCalciteWrapperUtility.extractSqlOperatorFromWrapper(sqlCall.getOperator()) instanceof SqlCountAggFunction) {
        for (SqlNode sqlNode : sqlCall.getOperandList()) {
            if (containsFlatten(sqlNode)) {
                unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Flatten function in aggregate functions is not supported\n" + "See Apache Drill JIRA: DRILL-2181");
                throw new UnsupportedOperationException();
            }
        }
    }
    return sqlCall.getOperator().acceptCall(this, sqlCall);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) SqlJoin(org.apache.calcite.sql.SqlJoin) SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNumericLiteral(org.apache.calcite.sql.SqlNumericLiteral) SqlCountAggFunction(org.apache.calcite.sql.fun.SqlCountAggFunction) 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