Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: support IfThen translation from Substrait to Calcite #151

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import io.substrait.type.Type;
import io.substrait.util.DecimalUtil;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.calcite.avatica.util.ByteString;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rex.*;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlIntervalQualifier;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.TimeString;
import org.apache.calcite.util.TimestampString;
Expand Down Expand Up @@ -99,6 +104,11 @@ public RexNode visit(Expression.FP64Literal expr) throws RuntimeException {
expr.value(), typeConverter.toCalcite(typeFactory, expr.getType()));
}

@Override
public RexNode visit(Expression.FixedCharLiteral expr) throws RuntimeException {
return rexBuilder.makeLiteral(expr.value());
}

@Override
public RexNode visit(Expression.StrLiteral expr) throws RuntimeException {
return rexBuilder.makeLiteral(
Expand Down Expand Up @@ -167,6 +177,19 @@ public RexNode visit(Expression.DecimalLiteral expr) throws RuntimeException {
return rexBuilder.makeLiteral(decimal, typeConverter.toCalcite(typeFactory, expr.getType()));
}

@Override
public RexNode visit(Expression.IfThen expr) throws RuntimeException {
// In Calcite, the arguments to the CASE operator are given as:
// <cond1> <value1> <cond2> <value2> ... <condN> <valueN> ... <else>
Stream<RexNode> ifThenArgs =
expr.ifClauses().stream()
.flatMap(
clause -> Stream.of(clause.condition().accept(this), clause.then().accept(this)));
Stream<RexNode> elseArg = Stream.of(expr.elseClause().accept(this));
List<RexNode> args = Stream.concat(ifThenArgs, elseArg).collect(Collectors.toList());
return rexBuilder.makeCall(SqlStdOperatorTable.CASE, args);
}

@Override
public RexNode visit(Expression.ScalarFunctionInvocation expr) throws RuntimeException {
var eArgs = expr.arguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,10 @@ public void simpleStringOpTest() throws Exception {
assertSqlSubstraitRelRoundTrip(
"select l_comment from lineitem where upper(l_comment) <> l_comment");
}

@Test
public void caseWhenTest() throws Exception {
assertSqlSubstraitRelRoundTrip(
"select case when p_size > 100 then 'large' when p_size > 50 then 'medium' else 'small' end from part");
}
}