[ 
https://issues.apache.org/jira/browse/CALCITE-5272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17601471#comment-17601471
 ] 

Julian Hyde commented on CALCITE-5272:
--------------------------------------

I am inclined to say that a CASE with zero THEN branches, while degenerate, 
should be considered valid in Rex land (but not in SQL or SqlNode land).  There 
are many code paths that might remove branches.

Perhaps builders (say RelBuilder) should optimize the Rex equivalent of 'case 
else x end' to 'x'. But we should still allow it.

If we consider the zero-THEN-branch case to be valid Rex, then 
RelToSqlConverter should generate valid SQL.

> RelBuilder/RexBuilder allow creation of invalid CASE expression
> ---------------------------------------------------------------
>
>                 Key: CALCITE-5272
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5272
>             Project: Calcite
>          Issue Type: Bug
>    Affects Versions: 1.30.0
>            Reporter: Ian Bertolacci
>            Priority: Minor
>
> It is possible (and easy) to create the invalid CASE expression† {{`CASE ELSE 
> 1 END`}} using either RelBuilder or RexBuilder.
> († Or at least and expression which Calcite then cannot parse.)
> {code:scala}
> val relBuilder: RelBuilder = ...
> val rexBuilder: RexBuilder = ...
> val typeFactory = relBuilder.getTypeFactory
> // Invalid case expression that can be built
> val caseExprRel = relBuilder.call(SqlStdOperatorTable.CASE, 
> relBuilder.literal(1))
> val caseExprRex = rexBuilder.makeCall(SqlStdOperatorTable.CASE, rexBuilder.
> makeBigintLiteral(BigDecimal.valueOf(1)))
> // RelNode tree with project containing invalid case expressions
> val node =
>   relBuilder.values(
>     java.util.Arrays.asList( java.util.Arrays.asList(relBuilder.literal(0)) ),
>     typeFactory.builder().add("x", 
> typeFactory.createSqlType(SqlTypeName.INTEGER)).build()
>   )
>   .project(caseExprRel, caseExprRex)
>   .build()
> {code}
> Gives the RelNode tree:
> {code}
> 1:LogicalProject($f0=[CASE(1)], $f1=[CASE(1:BIGINT)])
>   0:LogicalValues(tuples=[[{ 0 }]])
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to