-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GIE Compiler] Support Case When Expression in Logical and Physical P…
…lan (#2918) <!-- Thanks for your contribution! please review https://github.com/alibaba/GraphScope/blob/main/CONTRIBUTING.md before opening an issue. --> ## What do these changes do? as titled. <!-- Please give a short brief about these changes. --> ## Related issue number <!-- Are there any issues opened that will be resolved by merging this change? --> #2686 --------- Co-authored-by: BingqingLyu <bingqing.lbq@alibaba-inc.com> Co-authored-by: Longbin Lai <longbin.lailb@alibaba-inc.com>
- Loading branch information
1 parent
b82b748
commit 7fc14b5
Showing
15 changed files
with
661 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...ne/compiler/src/main/java/com/alibaba/graphscope/common/ir/rex/operator/CaseOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2020 Alibaba Group Holding Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.graphscope.common.ir.rex.operator; | ||
|
||
import static org.apache.calcite.util.Static.RESOURCE; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.alibaba.graphscope.common.ir.rex.RexCallBinding; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Iterables; | ||
|
||
import org.apache.calcite.rel.type.RelDataType; | ||
import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
import org.apache.calcite.sql.*; | ||
import org.apache.calcite.sql.type.SqlOperandCountRanges; | ||
import org.apache.calcite.sql.type.SqlOperandTypeInference; | ||
import org.apache.calcite.sql.type.SqlTypeUtil; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CaseOperator extends SqlOperator { | ||
|
||
public CaseOperator(SqlOperandTypeInference operandTypeInference) { | ||
super("CASE", SqlKind.CASE, MDX_PRECEDENCE, true, null, operandTypeInference, null); | ||
} | ||
|
||
@Override | ||
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) { | ||
Preconditions.checkArgument(callBinding instanceof RexCallBinding); | ||
boolean foundNotNull = false; | ||
int operandCount = callBinding.getOperandCount(); | ||
for (int i = 0; i < operandCount - 1; ++i) { | ||
RelDataType type = callBinding.getOperandType(i); | ||
if ((i & 1) == 0) { // when expression, should be boolean | ||
if (!SqlTypeUtil.inBooleanFamily(type)) { | ||
if (throwOnFailure) { | ||
throw new IllegalArgumentException( | ||
"Expected a boolean type at operand idx = " + i); | ||
} | ||
return false; | ||
} | ||
} else { // then expression | ||
if (!callBinding.isOperandNull(i, false)) { | ||
foundNotNull = true; | ||
} | ||
} | ||
} | ||
|
||
if (operandCount > 2 && !callBinding.isOperandNull(operandCount - 1, false)) { | ||
foundNotNull = true; | ||
} | ||
|
||
if (!foundNotNull) { | ||
// according to the sql standard we can not have all of the THEN | ||
// statements and the ELSE returning null | ||
if (throwOnFailure && !callBinding.isTypeCoercionEnabled()) { | ||
throw callBinding.newValidationError(RESOURCE.mustNotNullInElse()); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public RelDataType inferReturnType(SqlOperatorBinding opBinding) { | ||
return inferTypeFromOperands(opBinding); | ||
} | ||
|
||
private static RelDataType inferTypeFromOperands(SqlOperatorBinding opBinding) { | ||
final RelDataTypeFactory typeFactory = opBinding.getTypeFactory(); | ||
final List<RelDataType> argTypes = opBinding.collectOperandTypes(); | ||
assert (argTypes.size() % 2) == 1 : "odd number of arguments expected: " + argTypes.size(); | ||
assert argTypes.size() > 1 | ||
: "CASE must have more than 1 argument. Given " + argTypes.size() + ", " + argTypes; | ||
List<RelDataType> thenTypes = new ArrayList<>(); | ||
for (int j = 1; j < (argTypes.size() - 1); j += 2) { | ||
RelDataType argType = argTypes.get(j); | ||
thenTypes.add(argType); | ||
} | ||
|
||
thenTypes.add(Iterables.getLast(argTypes)); | ||
return requireNonNull( | ||
typeFactory.leastRestrictive(thenTypes), | ||
() -> "Can't find leastRestrictive type for " + thenTypes); | ||
} | ||
|
||
@Override | ||
public SqlOperandCountRange getOperandCountRange() { | ||
return SqlOperandCountRanges.any(); | ||
} | ||
|
||
@Override | ||
public SqlSyntax getSyntax() { | ||
return SqlSyntax.SPECIAL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.