1+ use std:: fmt;
2+ use std:: fmt:: Formatter ;
13use std:: sync:: Arc ;
24use itertools:: Itertools ;
35
@@ -59,6 +61,14 @@ pub enum ScalarExpression {
5961}
6062
6163impl ScalarExpression {
64+ pub fn unpack_alias ( & self ) -> & ScalarExpression {
65+ if let ScalarExpression :: Alias { expr, .. } = self {
66+ expr. unpack_alias ( )
67+ } else {
68+ self
69+ }
70+ }
71+
6272 pub fn nullable ( & self ) -> bool {
6373 match self {
6474 ScalarExpression :: Constant ( _) => false ,
@@ -154,14 +164,14 @@ impl ScalarExpression {
154164 }
155165 }
156166
157- pub fn output_column ( & self , tuple : & Tuple ) -> ColumnRef {
167+ pub fn output_columns ( & self , tuple : & Tuple ) -> ColumnRef {
158168 match self {
159169 ScalarExpression :: ColumnRef ( col) => {
160170 col. clone ( )
161171 }
162172 ScalarExpression :: Constant ( value) => {
163173 Arc :: new ( ColumnCatalog :: new (
164- String :: new ( ) ,
174+ format ! ( "{}" , value ) ,
165175 true ,
166176 ColumnDesc :: new ( value. logical_type ( ) , false )
167177 ) )
@@ -175,7 +185,7 @@ impl ScalarExpression {
175185 }
176186 ScalarExpression :: AggCall { kind, args, ty, distinct } => {
177187 let args_str = args. iter ( )
178- . map ( |expr| expr. output_column ( tuple) . name . clone ( ) )
188+ . map ( |expr| expr. output_columns ( tuple) . name . clone ( ) )
179189 . join ( ", " ) ;
180190 let op = |allow_distinct, distinct| {
181191 if allow_distinct && distinct {
@@ -200,6 +210,25 @@ impl ScalarExpression {
200210 ScalarExpression :: InputRef { index, .. } => {
201211 tuple. columns [ * index] . clone ( )
202212 }
213+ ScalarExpression :: Binary {
214+ left_expr,
215+ right_expr,
216+ op,
217+ ty
218+ } => {
219+ let column_name = format ! (
220+ "({} {} {})" ,
221+ left_expr. output_columns( tuple) . name,
222+ op,
223+ right_expr. output_columns( tuple) . name,
224+ ) ;
225+
226+ Arc :: new ( ColumnCatalog :: new (
227+ column_name,
228+ true ,
229+ ColumnDesc :: new ( ty. clone ( ) , false )
230+ ) )
231+ }
203232 _ => unreachable ! ( )
204233 }
205234 }
@@ -238,9 +267,29 @@ pub enum BinaryOperator {
238267 And ,
239268 Or ,
240269 Xor ,
241- BitwiseOr ,
242- BitwiseAnd ,
243- BitwiseXor ,
270+ }
271+
272+ impl fmt:: Display for BinaryOperator {
273+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
274+ match self {
275+ BinaryOperator :: Plus => write ! ( f, "+" ) ,
276+ BinaryOperator :: Minus => write ! ( f, "-" ) ,
277+ BinaryOperator :: Multiply => write ! ( f, "*" ) ,
278+ BinaryOperator :: Divide => write ! ( f, "/" ) ,
279+ BinaryOperator :: Modulo => write ! ( f, "mod" ) ,
280+ BinaryOperator :: StringConcat => write ! ( f, "&" ) ,
281+ BinaryOperator :: Gt => write ! ( f, ">" ) ,
282+ BinaryOperator :: Lt => write ! ( f, "<" ) ,
283+ BinaryOperator :: GtEq => write ! ( f, ">=" ) ,
284+ BinaryOperator :: LtEq => write ! ( f, "<=" ) ,
285+ BinaryOperator :: Spaceship => write ! ( f, "<=>" ) ,
286+ BinaryOperator :: Eq => write ! ( f, "=" ) ,
287+ BinaryOperator :: NotEq => write ! ( f, "!=" ) ,
288+ BinaryOperator :: And => write ! ( f, "&&" ) ,
289+ BinaryOperator :: Or => write ! ( f, "||" ) ,
290+ BinaryOperator :: Xor => write ! ( f, "^" ) ,
291+ }
292+ }
244293}
245294
246295impl From < SqlBinaryOperator > for BinaryOperator {
@@ -262,9 +311,6 @@ impl From<SqlBinaryOperator> for BinaryOperator {
262311 SqlBinaryOperator :: And => BinaryOperator :: And ,
263312 SqlBinaryOperator :: Or => BinaryOperator :: Or ,
264313 SqlBinaryOperator :: Xor => BinaryOperator :: Xor ,
265- SqlBinaryOperator :: BitwiseOr => BinaryOperator :: BitwiseOr ,
266- SqlBinaryOperator :: BitwiseAnd => BinaryOperator :: BitwiseAnd ,
267- SqlBinaryOperator :: BitwiseXor => BinaryOperator :: BitwiseXor ,
268314 _ => todo ! ( )
269315 }
270316 }
0 commit comments