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

Rui Wang commented on BEAM-7724:
--------------------------------

A little more background about code generation in BeamSQL:

The code generation starts from RexNode, it follows RexNode -> Expression 
(Calcite's ling4j library) -> Expression.toString() gives human written style 
code -> [Janino|https://janino-compiler.github.io/janino/] complier to compile 
the code and run it at runtime. 

What is RexNode? It's a Class representation of SQL expression. For example, 
SELECT "abc". "abc" will be converted to a child class of RexNode (which is 
called RexLiteral).

What is Expression (Calcite's ling4j library)? You can start thinking from a 
small piece of code: int a = 10; This assignment contains three pieces: an int 
with variable name "a", an assignment and a constant. Therefore, you can 
actually use an object oriented way to represent "int a = 10". You can start 
from a class called "ExpressionVariable". You can then do 
ExpressionVariable.of(type.INT, "a", New 
ExpressionAssignment(ExpressionConstant(10)). ExpressionVariable will include 
enough information to tell you 10 is assigned to an int with name "a". If you 
think in this way, you will see that given an Expression, we can generate code 
reversely. For example, if you call ExpressionVariable.toString(), it will very 
likely do something like this:


{code:java}
ExpressionVariable.toString() {
   return "int a" + ExpressionAssignment.toString();
}

ExpressionAssignment.toString() {
  return " = " +  ExpressionConstant.toString();
}

ExpressionConstant.toString() {
   return "10";
}
{code}

We reuse many of Calcite code to generate Expression from RexNode. Calcite has 
already many functions implementation and it knows how to convert those to 
Expression. After have an Expression, you can call string to get code.


So the resolution I was looking for, was to find the place where RexLiteral 
(which is null in our case) is converted to Expression. So in that conversion, 
I expect add something similar to ExpressionCast whose toString gives "(toType) 
ExpressionConstant.toString()"



> Codegen should cast(null) to a type to match exact function signature
> ---------------------------------------------------------------------
>
>                 Key: BEAM-7724
>                 URL: https://issues.apache.org/jira/browse/BEAM-7724
>             Project: Beam
>          Issue Type: Improvement
>          Components: dsl-sql
>            Reporter: Rui Wang
>            Assignee: sridhar Reddy
>            Priority: Major
>
> If there are two function signatures for the same function name, when input 
> parameter is null, Janino will throw exception due to vagueness:
> A(String)
> A(Integer)
> Janino does not know how to match A(null).  



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

Reply via email to