@@ -92,37 +92,25 @@ def __init__(self, functions_catalog, schema):
92
92
self ._schema = schema
93
93
self ._counter = itertools .count ()
94
94
95
+ self ._parse_expression = self .DISPATCH_REGISTRY .bind (self )
96
+
95
97
def expression_from_sqlglot (self , sqlglot_node ):
96
98
"""Parse a SQLGlot expression into a Substrait Expression."""
97
99
return self ._parse_expression (sqlglot_node )
98
100
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
-
109
101
@DISPATCH_REGISTRY .register (sqlglot .expressions .Literal )
110
102
def _parse_Literal (self , expr ):
111
103
if expr .is_string :
112
104
return ParsedSubstraitExpression (
113
105
f"literal${ next (self ._counter )} " ,
114
106
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 )),
118
108
)
119
109
elif expr .is_int :
120
110
return ParsedSubstraitExpression (
121
111
f"literal${ next (self ._counter )} " ,
122
112
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 ))),
126
114
)
127
115
elif sqlglot .helper .is_float (expr .name ):
128
116
return ParsedSubstraitExpression (
@@ -134,7 +122,7 @@ def _parse_Literal(self, expr):
134
122
)
135
123
else :
136
124
raise ValueError (f"Unsupporter literal: { expr .text } " )
137
-
125
+
138
126
@DISPATCH_REGISTRY .register (sqlglot .expressions .Column )
139
127
def _parse_Column (self , expr ):
140
128
column_name = expr .output_name
@@ -164,10 +152,8 @@ def _parser_Binary(self, expr):
164
152
left_parsed_expr = self ._parse_expression (expr .left )
165
153
right_parsed_expr = self ._parse_expression (expr .right )
166
154
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
171
157
)
172
158
result_name = f"{ function_name } _{ left_parsed_expr .output_name } _{ right_parsed_expr .output_name } _{ next (self ._counter )} "
173
159
return ParsedSubstraitExpression (
@@ -183,10 +169,12 @@ def _parser_Binary(self, expr):
183
169
def _parse_Unary (self , expr ):
184
170
argument_parsed_expr = self ._parse_expression (expr .this )
185
171
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 )} "
188
177
)
189
- result_name = f"{ function_name } _{ argument_parsed_expr .output_name } _{ next (self ._counter )} "
190
178
return ParsedSubstraitExpression (
191
179
result_name ,
192
180
result_type ,
0 commit comments