-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expression Type Inference
Wenlei Xie edited this page Feb 17, 2020
·
1 revision
In Presto, expression type inference is done recursively by post-order traversing over the expression tree. Consider the following expression
foo(x) + bar(y, z)
where x
is with type array(double)
, y
is with type integer
, and z
is with type array(integer)
. The type resolution is demonstrated as follows:
-
X
is visited and decided with typeARRAY(DOUBLE)
-
foo
is visited, and by searching the functions with namefoo
and input types,foo: ARRAY(T) -> T
is decided as the best match. Thusfoo(x)
is decided with typeDOUBLE
. -
Y
is visited and decided with typeINTEGER
-
Z
is visited and decided with typeARRAY(INTEGER)
-
bar
is visited, and after searching the functions with namebar
with input types,bar: (T, ARRAY(T)) -> T
is decided as the best match. Thusbar(y, z)
is decided with typeINTEGER
. -
+
is visited, and after searching functions with name+
with input types,+: (DOUBLE, DOUBLE) -> DOUBLE
is decided as the best match.
This logic procedure is reflected in part ExpressionAnalyzer#Vistor#visitFunctionCall before lambda is implemented. The actual implementation contains many other details.