Repository: prestodb/presto
Issue: try(cast(...)) fails with multiple predicates in a where clause

When I run the following query:

``` sql
with example as (
    select
        id,
        other_id
    from (
      values
         (1, '23452'),
         (3, '2354')
    ) as t (id, other_id)
)

select
  try(cast(other_id as bigint)) as other_id
from example
where id = 1
  and try(cast(other_id as bigint)) is not null
```

It produces the error message: `Compiler failed and interpreter is disabled`

When I remove the `id = 1` in the final query, the query works as expected.


Changed files:
presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
presto-main/src/main/java/com/facebook/presto/sql/gen/TryCodeGenerator.java
presto-main/src/main/java/com/facebook/presto/sql/gen/CursorProcessorCompiler.java
presto-main/src/main/java/com/facebook/presto/sql/gen/BytecodeExpressionVisitor.java
presto-main/src/main/java/com/facebook/presto/sql/gen/PageProcessorCompiler.java

diff --git a/presto-main/src/main/java/com/facebook/presto/sql/gen/BytecodeExpressionVisitor.java b/presto-main/src/main/java/com/facebook/presto/sql/gen/BytecodeExpressionVisitor.java
index 4bc29acfa3..fae3a4ea5d 100644
--- a/presto-main/src/main/java/com/facebook/presto/sql/gen/BytecodeExpressionVisitor.java
+++ b/presto-main/src/main/java/com/facebook/presto/sql/gen/BytecodeExpressionVisitor.java
@@ -17,14 +17,12 @@ import com.facebook.presto.bytecode.BytecodeBlock;
 import com.facebook.presto.bytecode.BytecodeNode;
 import com.facebook.presto.bytecode.MethodDefinition;
 import com.facebook.presto.bytecode.Scope;
-import com.facebook.presto.bytecode.Variable;
 import com.facebook.presto.metadata.FunctionRegistry;
 import com.facebook.presto.sql.relational.CallExpression;
 import com.facebook.presto.sql.relational.ConstantExpression;
 import com.facebook.presto.sql.relational.InputReferenceExpression;
 import com.facebook.presto.sql.relational.RowExpressionVisitor;
 
-import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.bytecode.expression.BytecodeExpressions.constantTrue;
@@ -51,7 +49,6 @@ public class BytecodeExpressionVisitor
     private final CachedInstanceBinder cachedInstanceBinder;
     private final RowExpressionVisitor<Scope, BytecodeNode> fieldReferenceCompiler;
     private final FunctionRegistry registry;
-    private final List<? extends Variable> expressionInputs;
     private final Map<CallExpression, MethodDefinition> tryExpressionsMap;
 
     public BytecodeExpressionVisitor(
@@ -59,14 +56,12 @@ public class BytecodeExpressionVisitor
             CachedInstanceBinder cachedInstanceBinder,
             RowExpressionVisitor<Scope, BytecodeNode> fieldReferenceCompiler,
             FunctionRegistry registry,
-            List<? extends Variable> expressionInputs,
             Map<CallExpression, MethodDefinition> tryExpressionsMap)
     {
         this.callSiteBinder = callSiteBinder;
         this.cachedInstanceBinder = cachedInstanceBinder;
         this.fieldReferenceCompiler = fieldReferenceCompiler;
         this.registry = registry;
-        this.expressionInputs = expressionInputs;
         this.tryExpressionsMap = tryExpressionsMap;
     }
 
@@ -92,7 +87,7 @@ public class BytecodeExpressionVisitor
                     generator = new SwitchCodeGenerator();
                     break;
                 case TRY:
-                    generator = new TryCodeGenerator(tryExpressionsMap, expressionInputs);
+                    generator = new TryCodeGenerator(tryExpressionsMap);
                     break;
                 // functions that take null as input
                 case IS_NULL:
diff --git a/presto-main/src/main/java/com/facebook/presto/sql/gen/CursorProcessorCompiler.java b/presto-main/src/main/java/com/facebook/presto/sql/gen/CursorProcessorCompiler.java
index 93996bc763..656a41901a 100644
--- a/presto-main/src/main/java/com/facebook/presto/sql/gen/CursorProcessorCompiler.java
+++ b/presto-main/src/main/java/com/facebook/presto/sql/gen/CursorProcessorCompiler.java
@@ -204,7 +204,6 @@ public class CursorProcessorCompiler
                     cachedInstanceBinder,
                     fieldReferenceCompiler(cursor, wasNull),
                     metadata.getFunctionRegistry(),
-                    inputParameters,
                     tryMethodMap.build());
 
             MethodDefinition tryMethod = defineTryMethod(
@@ -241,7 +240,6 @@ public class CursorProcessorCompiler
                 cachedInstanceBinder,
                 fieldReferenceCompiler(cursor, wasNullVariable),
                 metadata.getFunctionRegistry(),
-                ImmutableList.of(session, cursor, wasNullVariable),
                 tryMethodMap);
 
         LabelNode end = new LabelNode("end");
@@ -282,7 +280,6 @@ public class CursorProcessorCompiler
                 cachedInstanceBinder,
                 fieldReferenceCompiler(cursor, wasNullVariable),
                 metadata.getFunctionRegistry(),
-                ImmutableList.of(session, cursor, wasNullVariable),
                 tryMethodMap);
 
         body.getVariable(output)
diff --git a/presto-main/src/main/java/com/facebook/presto/sql/gen/PageProcessorCompiler.java b/presto-main/src/main/java/com/facebook/presto/sql/gen/PageProcessorCompiler.java
index 53f3e408d4..4269cef01d 100644
--- a/presto-main/src/main/java/com/facebook/presto/sql/gen/PageProcessorCompiler.java
+++ b/presto-main/src/main/java/com/facebook/presto/sql/gen/PageProcessorCompiler.java
@@ -750,21 +750,20 @@ public class PageProcessorCompiler
             Parameter position = arg("position", int.class);
             Parameter wasNullVariable = arg("wasNull", boolean.class);
 
-            List<Parameter> inputParameters = ImmutableList.<Parameter>builder()
-                    .add(session)
-                    .addAll(blocks)
-                    .add(position)
-                    .add(wasNullVariable)
-                    .build();
-
             BytecodeExpressionVisitor innerExpressionVisitor = new BytecodeExpressionVisitor(
                     callSiteBinder,
                     cachedInstanceBinder,
                     fieldReferenceCompiler(callSiteBinder, position, wasNullVariable),
                     metadata.getFunctionRegistry(),
-                    inputParameters,
                     tryMethodMap.build());
 
+            List<Parameter> inputParameters = ImmutableList.<Parameter>builder()
+                    .add(session)
+                    .addAll(blocks)
+                    .add(position)
+                    .add(wasNullVariable)
+                    .build();
+
             MethodDefinition tryMethod = defineTryMethod(
                     innerExpressionVisitor,
                     containerClassDefinition,
@@ -789,17 +788,15 @@ public class PageProcessorCompiler
         List<Parameter> blocks = toBlockParameters(getInputChannels(filter));
         Parameter position = arg("position", int.class);
 
-        List<Parameter> expressionInputs = ImmutableList.<Parameter>builder()
-                .add(session)
-                .addAll(blocks)
-                .add(position)
-                .build();
-
         MethodDefinition method = classDefinition.declareMethod(
                 a(PUBLIC),
                 "filter",
                 type(boolean.class),
-                expressionInputs);
+                ImmutableList.<Parameter>builder()
+                        .add(session)
+                        .addAll(blocks)
+                        .add(position)
+                        .build());
 
         method.comment("Filter: %s", filter.toString());
         BytecodeBlock body = method.getBody();
@@ -812,10 +809,6 @@ public class PageProcessorCompiler
                 cachedInstanceBinder,
                 fieldReferenceCompiler(callSiteBinder, position, wasNullVariable),
                 metadata.getFunctionRegistry(),
-                ImmutableList.<Variable>builder()
-                        .addAll(expressionInputs)
-                        .add(wasNullVariable)
-                        .build(),
                 tryMethodMap);
 
         BytecodeNode visitorBody = filter.accept(visitor, scope);
@@ -838,18 +831,14 @@ public class PageProcessorCompiler
         Parameter position = arg("position", int.class);
         Parameter output = arg("output", BlockBuilder.class);
 
-        List<Parameter> expressionInputs = ImmutableList.<Parameter>builder()
-                .add(session)
-                .addAll(blocks)
-                .add(position)
-                .build();
-
         MethodDefinition method = classDefinition.declareMethod(
                 a(PUBLIC),
                 methodName,
                 type(void.class),
                 ImmutableList.<Parameter>builder()
-                        .addAll(expressionInputs)
+                        .add(session)
+                        .addAll(blocks)
+                        .add(position)
                         .add(output)
                         .build());
 
@@ -864,10 +853,6 @@ public class PageProcessorCompiler
                 cachedInstanceBinder,
                 fieldReferenceCompiler(callSiteBinder, position, wasNullVariable),
                 metadata.getFunctionRegistry(),
-                ImmutableList.<Variable>builder()
-                    .addAll(expressionInputs)
-                    .add(wasNullVariable)
-                    .build(),
                 tryMethodMap
         );
 
diff --git a/presto-main/src/main/java/com/facebook/presto/sql/gen/TryCodeGenerator.java b/presto-main/src/main/java/com/facebook/presto/sql/gen/TryCodeGenerator.java
index 25e23d17d7..f3cfb733c1 100644
--- a/presto-main/src/main/java/com/facebook/presto/sql/gen/TryCodeGenerator.java
+++ b/presto-main/src/main/java/com/facebook/presto/sql/gen/TryCodeGenerator.java
@@ -20,7 +20,6 @@ import com.facebook.presto.bytecode.MethodDefinition;
 import com.facebook.presto.bytecode.Parameter;
 import com.facebook.presto.bytecode.ParameterizedType;
 import com.facebook.presto.bytecode.Scope;
-import com.facebook.presto.bytecode.Variable;
 import com.facebook.presto.bytecode.control.TryCatch;
 import com.facebook.presto.metadata.Signature;
 import com.facebook.presto.spi.PrestoException;
@@ -56,12 +55,10 @@ public class TryCodeGenerator
     private static final String EXCEPTION_HANDLER_NAME = "tryExpressionExceptionHandler";
 
     private final Map<CallExpression, MethodDefinition> tryMethodsMap;
-    private final List<? extends Variable> inputParameters;
 
-    public TryCodeGenerator(Map<CallExpression, MethodDefinition> tryMethodsMap, List<? extends Variable> inputParameters)
+    public TryCodeGenerator(Map<CallExpression, MethodDefinition> tryMethodsMap)
     {
         this.tryMethodsMap = tryMethodsMap;
-        this.inputParameters = inputParameters;
     }
 
     @Override
@@ -77,11 +74,14 @@ public class TryCodeGenerator
                 .comment("load required variables")
                 .getVariable(context.getScope().getVariable("this"));
 
-        inputParameters.stream()
+        MethodDefinition definition = tryMethodsMap.get(innerCallExpression);
+
+        definition.getParameters().stream()
+                .map(parameter -> context.getScope().getVariable(parameter.getName()))
                 .forEach(bytecodeBlock::getVariable);
 
-        bytecodeBlock.comment("call dynamic try method: " + tryMethodsMap.get(innerCallExpression).getName())
-                .invokeVirtual(tryMethodsMap.get(innerCallExpression))
+        bytecodeBlock.comment("call dynamic try method: " + definition.getName())
+                .invokeVirtual(definition)
                 .append(unboxPrimitiveIfNecessary(context.getScope(), Primitives.wrap(innerCallExpression.getType().getJavaType())));
 
         return bytecodeBlock;
diff --git a/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java b/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
index eae6e30275..70819db65c 100644
--- a/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
+++ b/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
@@ -4306,6 +4306,11 @@ public abstract class AbstractTestQueries
         assertQuery(
                 "SELECT COUNT(TRY(to_base(100, CAST(round(totalprice/100) AS BIGINT)))) FROM orders",
                 "SELECT SUM(CASE WHEN CAST(round(totalprice/100) AS BIGINT) BETWEEN 2 AND 36 THEN 1 ELSE 0 END) FROM orders");
+
+        // as part of a complex expression
+        assertQuery(
+                "SELECT COUNT(CAST(orderkey AS VARCHAR) || TRY(to_base(100, CAST(round(totalprice/100) AS BIGINT)))) FROM orders",
+                "SELECT SUM(CASE WHEN CAST(round(totalprice/100) AS BIGINT) BETWEEN 2 AND 36 THEN 1 ELSE 0 END) FROM orders");
     }
 
     @Test