Skip to content

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:

Expression Tree
  1. X is visited and decided with type ARRAY(DOUBLE)
  2. foo is visited, and by searching the functions with name foo and input types, foo: ARRAY(T) -> T is decided as the best match. Thus foo(x) is decided with type DOUBLE.
  3. Y is visited and decided with type INTEGER
  4. Z is visited and decided with type ARRAY(INTEGER)
  5. bar is visited, and after searching the functions with name bar with input types, bar: (T, ARRAY(T)) -> T is decided as the best match. Thus bar(y, z) is decided with type INTEGER.
  6. + 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.

Clone this wiki locally