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

Remus Rusanu commented on CALCITE-1790:
---------------------------------------

With a test like CalciteSqlOperatorTest>SqlOperatorBaseTest.testAndOperator2 
I'm seeing incorrect Java code being generated:

{code}
org.apache.calcite.DataContext root;

public org.apache.calcite.linq4j.Enumerable bind(final 
org.apache.calcite.DataContext root0) {
  root = root0;
  final org.apache.calcite.linq4j.Enumerable _inputEnumerable = 
org.apache.calcite.linq4j.Linq4j.asEnumerable(new Object[] {
    new Object[] {
      true,
      null,
      false}});
  return new org.apache.calcite.linq4j.AbstractEnumerable(){
      public org.apache.calcite.linq4j.Enumerator enumerator() {
        return new org.apache.calcite.linq4j.Enumerator(){
            public final org.apache.calcite.linq4j.Enumerator inputEnumerator = 
_inputEnumerable.enumerator();
            public void reset() {
              inputEnumerator.reset();
            }

            public boolean moveNext() {
              return inputEnumerator.moveNext();
            }

            public void close() {
              inputEnumerator.close();
            }

            public Object current() {
              final Object[] current = (Object[]) inputEnumerator.current();
              final boolean inp2_ = 
org.apache.calcite.runtime.SqlFunctions.toBoolean(current[2]);
              final Boolean inp1_ = (Boolean) current[1];
              final boolean inp0_ = 
org.apache.calcite.runtime.SqlFunctions.toBoolean(current[0]);
              final boolean v0 = !inp2_;
              return inp2_ && 
org.apache.calcite.runtime.SqlFunctions.isTrue(inp1_) || inp0_ && v0 ? 
Boolean.TRUE : (inp2_ && 
org.apache.calcite.runtime.SqlFunctions.isNotFalse(inp1_) ? (inp1_ == null ? 
(Boolean) null : Boolean.TRUE) : Boolean.FALSE) == null || (inp0_ && v0 || 
$L4J$C$_null) ? (Boolean) null : Boolean.FALSE;
            }

            static final Object $L4J$C$_null = !null;
          };
      }:

    };
}


public Class getElementType() {
  return java.lang.Boolean.class;
}
{code}

The line {{static final Object $L4J$C$_null = !null;}} triggers compile error:
{code}
"pool-1-thread-7"
        at 
org.codehaus.janino.UnitCompiler.compileError(UnitCompiler.java:10092)
        at 
org.codehaus.janino.UnitCompiler.compileBoolean2(UnitCompiler.java:2848)
        at org.codehaus.janino.UnitCompiler.access$4800(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$8.visitNullLiteral(UnitCompiler.java:2810)
        at org.codehaus.janino.Java$NullLiteral.accept(Java.java:4396)
        at 
org.codehaus.janino.UnitCompiler.compileBoolean(UnitCompiler.java:2830)
        at 
org.codehaus.janino.UnitCompiler.compileBoolean2(UnitCompiler.java:2860)
        at org.codehaus.janino.UnitCompiler.access$4900(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$8.visitUnaryOperation(UnitCompiler.java:2796)
        at org.codehaus.janino.Java$UnaryOperation.accept(Java.java:3647)
        at 
org.codehaus.janino.UnitCompiler.compileBoolean(UnitCompiler.java:2830)
        at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:3288)
        at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:3711)
        at org.codehaus.janino.UnitCompiler.access$6200(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$10.visitUnaryOperation(UnitCompiler.java:3244)
        at org.codehaus.janino.Java$UnaryOperation.accept(Java.java:3647)
        at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:3278)
        at 
org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:4345)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:1649)
        at org.codehaus.janino.UnitCompiler.access$800(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$4.visitFieldDeclaration(UnitCompiler.java:931)
        at org.codehaus.janino.Java$FieldDeclaration.accept(Java.java:1818)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:956)
        at org.codehaus.janino.UnitCompiler.fakeCompile(UnitCompiler.java:968)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:652)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:620)
        at org.codehaus.janino.UnitCompiler.access$200(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$2.visitAnonymousClassDeclaration(UnitCompiler.java:343)
        at 
org.codehaus.janino.Java$AnonymousClassDeclaration.accept(Java.java:894)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:352)
        at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:4194)
        at org.codehaus.janino.UnitCompiler.access$7300(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$10.visitNewAnonymousClassInstance(UnitCompiler.java:3260)
        at 
org.codehaus.janino.Java$NewAnonymousClassInstance.accept(Java.java:4131)
        at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:3278)
        at 
org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:4345)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:1901)
        at org.codehaus.janino.UnitCompiler.access$2100(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$4.visitReturnStatement(UnitCompiler.java:944)
        at org.codehaus.janino.Java$ReturnStatement.accept(Java.java:2544)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:956)
        at 
org.codehaus.janino.UnitCompiler.compileStatements(UnitCompiler.java:997)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:2283)
        at 
org.codehaus.janino.UnitCompiler.compileDeclaredMethods(UnitCompiler.java:820)
        at 
org.codehaus.janino.UnitCompiler.compileDeclaredMethods(UnitCompiler.java:792)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:505)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:656)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:620)
        at org.codehaus.janino.UnitCompiler.access$200(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$2.visitAnonymousClassDeclaration(UnitCompiler.java:343)
        at 
org.codehaus.janino.Java$AnonymousClassDeclaration.accept(Java.java:894)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:352)
        at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:4194)
        at org.codehaus.janino.UnitCompiler.access$7300(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$10.visitNewAnonymousClassInstance(UnitCompiler.java:3260)
        at 
org.codehaus.janino.Java$NewAnonymousClassInstance.accept(Java.java:4131)
        at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:3278)
        at 
org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:4345)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:1901)
        at org.codehaus.janino.UnitCompiler.access$2100(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$4.visitReturnStatement(UnitCompiler.java:944)
        at org.codehaus.janino.Java$ReturnStatement.accept(Java.java:2544)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:956)
        at 
org.codehaus.janino.UnitCompiler.compileStatements(UnitCompiler.java:997)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:2283)
        at 
org.codehaus.janino.UnitCompiler.compileDeclaredMethods(UnitCompiler.java:820)
        at 
org.codehaus.janino.UnitCompiler.compileDeclaredMethods(UnitCompiler.java:792)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:505)
        at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:391)
        at org.codehaus.janino.UnitCompiler.access$400(UnitCompiler.java:183)
        at 
org.codehaus.janino.UnitCompiler$2.visitPackageMemberClassDeclaration(UnitCompiler.java:345)
        at 
org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:1139)
        at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:352)
        at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:320)
        at 
org.codehaus.janino.SimpleCompiler.compileToClassLoader(SimpleCompiler.java:383)
        at 
org.codehaus.janino.ClassBodyEvaluator.compileToClass(ClassBodyEvaluator.java:315)
        at 
org.codehaus.janino.ClassBodyEvaluator.cook(ClassBodyEvaluator.java:233)
        at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:192)
        at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:47)
        at 
org.codehaus.janino.ClassBodyEvaluator.createInstance(ClassBodyEvaluator.java:340)
        at 
org.apache.calcite.adapter.enumerable.EnumerableInterpretable.getBindable(EnumerableInterpretable.java:140)
        at 
org.apache.calcite.adapter.enumerable.EnumerableInterpretable.toBindable(EnumerableInterpretable.java:105)
        at 
org.apache.calcite.prepare.CalcitePrepareImpl$CalcitePreparingStmt.implement(CalcitePrepareImpl.java:1255)
        at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:331)
        at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:228)
        at 
org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:783)
        at 
org.apache.calcite.prepare.CalcitePrepareImpl.prepare_(CalcitePrepareImpl.java:639)
        at 
org.apache.calcite.prepare.CalcitePrepareImpl.prepareSql(CalcitePrepareImpl.java:609)
        at 
org.apache.calcite.jdbc.CalciteConnectionImpl.parseQuery(CalciteConnectionImpl.java:214)
        at 
org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:600)
        at 
org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:615)
        at 
org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:148)
        at 
org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:218)
        at 
org.apache.calcite.sql.test.SqlOperatorBaseTest$TesterImpl.check(SqlOperatorBaseTest.java:6821)
        at 
org.apache.calcite.sql.test.SqlTesterImpl.check(SqlTesterImpl.java:433)
        at 
org.apache.calcite.sql.test.SqlTesterImpl.checkBoolean(SqlTesterImpl.java:402)
        at 
org.apache.calcite.sql.test.SqlOperatorBaseTest.testAndOperator2(SqlOperatorBaseTest.java:1872)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at 
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at 
org.apache.maven.surefire.junitcore.pc.Scheduler$1.run(Scheduler.java:393)
        at 
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
{code}

Original rel expression:
{code}
Root {kind: SELECT, rel: 
rel#79480:EnumerableCalc.ENUMERABLE.[](input=rel#79475:EnumerableValues.ENUMERABLE.[[0,
 1, 2], [1, 2], [2]](type=RecordType(BOOLEAN EXPR$0, BOOLEAN EXPR$1, BOOLEAN 
EXPR$2),tuples=[{ true, null, false }]),expr#0..2={inputs},expr#3=AND($t2, 
$t1),expr#4=CAST($t0):BOOLEAN,expr#5=NOT($t2),expr#6=AND($t4, 
$t5),expr#7=OR($t3, $t6),EXPR$0=$t7), rowType: RecordType(BOOLEAN EXPR$0), 
fields: [<0, EXPR$0>], collation: []}
{code}

> Simplify CASE P1 THEN <boolean> P@ THEN <booleans> ...  ELSE TRUE/FALSE
> -----------------------------------------------------------------------
>
>                 Key: CALCITE-1790
>                 URL: https://issues.apache.org/jira/browse/CALCITE-1790
>             Project: Calcite
>          Issue Type: Improvement
>          Components: core
>            Reporter: Remus Rusanu
>            Assignee: Remus Rusanu
>            Priority: Minor
>
> In HIVE-14431 [~jcamachorodriguez] proposed a simplification for CASE when 
> all branches are not nullable boolean expression into an alternative 
> AND/OR/NOT based expression. This allows for more aggressive reductions and 
> split/push-down on the whole.  Meantime the simplifier code migrated to 
> Calcite so I'm reviving this here.
> The proposed simplification is:
> {code}
> CASE
> WHEN p1 THEN ex1
> WHEN p2 THEN ex2
> ...
> WHEN pn THEN exn
> ELSE TRUE/FALSE
> END
> {code}
> to be transformed into:
> {code}
> (p1 AND ex1)
> OR (not(p1) AND p2 AND x2)
> ...
> OR (not(p1) AND not(p2) ... AND not(pn-1) AND Pn AND exn)
> [OR (not(p1) AND not(p2) ... AND not(pn))]
> {code}
> The last OR is depending on the ELSE branch being TRUE/FALSE.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to