|
| 1 | +/* |
| 2 | + * Copyright 2020 Alibaba Group Holding Limited. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.graphscope.common.ir.rex.operator; |
| 18 | + |
| 19 | +import static org.apache.calcite.util.Static.RESOURCE; |
| 20 | + |
| 21 | +import static java.util.Objects.requireNonNull; |
| 22 | + |
| 23 | +import com.alibaba.graphscope.common.ir.rex.RexCallBinding; |
| 24 | +import com.google.common.base.Preconditions; |
| 25 | +import com.google.common.collect.Iterables; |
| 26 | + |
| 27 | +import org.apache.calcite.rel.type.RelDataType; |
| 28 | +import org.apache.calcite.rel.type.RelDataTypeFactory; |
| 29 | +import org.apache.calcite.sql.*; |
| 30 | +import org.apache.calcite.sql.type.SqlOperandCountRanges; |
| 31 | +import org.apache.calcite.sql.type.SqlOperandTypeInference; |
| 32 | +import org.apache.calcite.sql.type.SqlTypeUtil; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +public class CaseOperator extends SqlOperator { |
| 38 | + |
| 39 | + public CaseOperator(SqlOperandTypeInference operandTypeInference) { |
| 40 | + super("CASE", SqlKind.CASE, MDX_PRECEDENCE, true, null, operandTypeInference, null); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) { |
| 45 | + Preconditions.checkArgument(callBinding instanceof RexCallBinding); |
| 46 | + boolean foundNotNull = false; |
| 47 | + int operandCount = callBinding.getOperandCount(); |
| 48 | + for (int i = 0; i < operandCount - 1; ++i) { |
| 49 | + RelDataType type = callBinding.getOperandType(i); |
| 50 | + if ((i & 1) == 0) { // when expression, should be boolean |
| 51 | + if (!SqlTypeUtil.inBooleanFamily(type)) { |
| 52 | + if (throwOnFailure) { |
| 53 | + throw new IllegalArgumentException( |
| 54 | + "Expected a boolean type at operand idx = " + i); |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + } else { // then expression |
| 59 | + if (!callBinding.isOperandNull(i, false)) { |
| 60 | + foundNotNull = true; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (operandCount > 2 && !callBinding.isOperandNull(operandCount - 1, false)) { |
| 66 | + foundNotNull = true; |
| 67 | + } |
| 68 | + |
| 69 | + if (!foundNotNull) { |
| 70 | + // according to the sql standard we can not have all of the THEN |
| 71 | + // statements and the ELSE returning null |
| 72 | + if (throwOnFailure && !callBinding.isTypeCoercionEnabled()) { |
| 73 | + throw callBinding.newValidationError(RESOURCE.mustNotNullInElse()); |
| 74 | + } |
| 75 | + return false; |
| 76 | + } |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public RelDataType inferReturnType(SqlOperatorBinding opBinding) { |
| 82 | + return inferTypeFromOperands(opBinding); |
| 83 | + } |
| 84 | + |
| 85 | + private static RelDataType inferTypeFromOperands(SqlOperatorBinding opBinding) { |
| 86 | + final RelDataTypeFactory typeFactory = opBinding.getTypeFactory(); |
| 87 | + final List<RelDataType> argTypes = opBinding.collectOperandTypes(); |
| 88 | + assert (argTypes.size() % 2) == 1 : "odd number of arguments expected: " + argTypes.size(); |
| 89 | + assert argTypes.size() > 1 |
| 90 | + : "CASE must have more than 1 argument. Given " + argTypes.size() + ", " + argTypes; |
| 91 | + List<RelDataType> thenTypes = new ArrayList<>(); |
| 92 | + for (int j = 1; j < (argTypes.size() - 1); j += 2) { |
| 93 | + RelDataType argType = argTypes.get(j); |
| 94 | + thenTypes.add(argType); |
| 95 | + } |
| 96 | + |
| 97 | + thenTypes.add(Iterables.getLast(argTypes)); |
| 98 | + return requireNonNull( |
| 99 | + typeFactory.leastRestrictive(thenTypes), |
| 100 | + () -> "Can't find leastRestrictive type for " + thenTypes); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public SqlOperandCountRange getOperandCountRange() { |
| 105 | + return SqlOperandCountRanges.any(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public SqlSyntax getSyntax() { |
| 110 | + return SqlSyntax.SPECIAL; |
| 111 | + } |
| 112 | +} |
0 commit comments