@@ -92,37 +92,25 @@ def __init__(self, functions_catalog, schema):
9292 self ._schema = schema
9393 self ._counter = itertools .count ()
9494
95+ self ._parse_expression = self .DISPATCH_REGISTRY .bind (self )
96+
9597 def expression_from_sqlglot (self , sqlglot_node ):
9698 """Parse a SQLGlot expression into a Substrait Expression."""
9799 return self ._parse_expression (sqlglot_node )
98100
99- def _parse_expression (self , expr ):
100- """Parse a SQLGlot node and return a Substrait expression.
101-
102- This is the internal implementation, expected to be
103- invoked in a recursive manner to parse the whole
104- expression tree.
105- """
106- expr_class = expr .__class__
107- return self .DISPATCH_REGISTRY [expr_class ](self , expr )
108-
109101 @DISPATCH_REGISTRY .register (sqlglot .expressions .Literal )
110102 def _parse_Literal (self , expr ):
111103 if expr .is_string :
112104 return ParsedSubstraitExpression (
113105 f"literal${ next (self ._counter )} " ,
114106 proto .Type (string = proto .Type .String ()),
115- proto .Expression (
116- literal = proto .Expression .Literal (string = expr .text )
117- ),
107+ proto .Expression (literal = proto .Expression .Literal (string = expr .text )),
118108 )
119109 elif expr .is_int :
120110 return ParsedSubstraitExpression (
121111 f"literal${ next (self ._counter )} " ,
122112 proto .Type (i32 = proto .Type .I32 ()),
123- proto .Expression (
124- literal = proto .Expression .Literal (i32 = int (expr .name ))
125- ),
113+ proto .Expression (literal = proto .Expression .Literal (i32 = int (expr .name ))),
126114 )
127115 elif sqlglot .helper .is_float (expr .name ):
128116 return ParsedSubstraitExpression (
@@ -134,7 +122,7 @@ def _parse_Literal(self, expr):
134122 )
135123 else :
136124 raise ValueError (f"Unsupporter literal: { expr .text } " )
137-
125+
138126 @DISPATCH_REGISTRY .register (sqlglot .expressions .Column )
139127 def _parse_Column (self , expr ):
140128 column_name = expr .output_name
@@ -164,10 +152,8 @@ def _parser_Binary(self, expr):
164152 left_parsed_expr = self ._parse_expression (expr .left )
165153 right_parsed_expr = self ._parse_expression (expr .right )
166154 function_name = SQL_BINARY_FUNCTIONS [expr .key ]
167- signature , result_type , function_expression = (
168- self ._parse_function_invokation (
169- function_name , left_parsed_expr , right_parsed_expr
170- )
155+ signature , result_type , function_expression = self ._parse_function_invokation (
156+ function_name , left_parsed_expr , right_parsed_expr
171157 )
172158 result_name = f"{ function_name } _{ left_parsed_expr .output_name } _{ right_parsed_expr .output_name } _{ next (self ._counter )} "
173159 return ParsedSubstraitExpression (
@@ -183,10 +169,12 @@ def _parser_Binary(self, expr):
183169 def _parse_Unary (self , expr ):
184170 argument_parsed_expr = self ._parse_expression (expr .this )
185171 function_name = SQL_UNARY_FUNCTIONS [expr .key ]
186- signature , result_type , function_expression = (
187- self ._parse_function_invokation (function_name , argument_parsed_expr )
172+ signature , result_type , function_expression = self ._parse_function_invokation (
173+ function_name , argument_parsed_expr
174+ )
175+ result_name = (
176+ f"{ function_name } _{ argument_parsed_expr .output_name } _{ next (self ._counter )} "
188177 )
189- result_name = f"{ function_name } _{ argument_parsed_expr .output_name } _{ next (self ._counter )} "
190178 return ParsedSubstraitExpression (
191179 result_name ,
192180 result_type ,
0 commit comments