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

Ian Bertolacci commented on CALCITE-5272:
-----------------------------------------

{quote}I am inclined to say that a CASE with zero THEN branches, while 
degenerate, should be considered valid in Rex land{quote}
My only issue with that is there is no interface (or maybe there is an I havent 
seen it) for working with Rex case expressions and their constituent 
when-then-else expressions.
(for example having method which give me a list of when-then expressions and 
the else expression).
If there were, then I'd agree with this.

But right now, there is not indicator to whoever is interacting with this 
degenerate expression that it is still valid.
For example {{`case else a < b end`}} might appear to be a mangled case 
expression and if you encountered it (say in debugging or triage) you'd be 
concerned that somehow case expressions were being mangled in this way.

{quote}Perhaps builders (say RelBuilder) should optimize the Rex equivalent of 
'case else x end' to 'x'.{quote}
My preference would be to add a specific case builder method to both RelBuilder 
and RexBuilder which prevents the creation of degenerate expressions instead of 
silently folding them into the `else` expression.
Then maybe `call`/`makeCall` on the CASE operator invokes that method so that 
there is no path to construct such degenerate cases?

{quote}If we consider the zero-THEN-branch case to be valid Rex, then 
RelToSqlConverter should generate valid SQL.{quote}
I'm inclined to agree, but I think my preference is that the creation of such 
RexCall nodes is impossible, which makes handing such a case pointless.


> 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