| 380 | |
| 381 | @attr.s |
| 382 | class SpiderUnparser: |
| 383 | ast_wrapper = attr.ib() |
| 384 | schema = attr.ib() |
| 385 | factorize_sketch = attr.ib(default=0) |
| 386 | |
| 387 | UNIT_TYPES_B = { |
| 388 | 'Minus': '-', |
| 389 | 'Plus': '+', |
| 390 | 'Times': '*', |
| 391 | 'Divide': '/', |
| 392 | } |
| 393 | COND_TYPES_B = { |
| 394 | 'Between': 'BETWEEN', |
| 395 | 'Eq': '=', |
| 396 | 'Gt': '>', |
| 397 | 'Lt': '<', |
| 398 | 'Ge': '>=', |
| 399 | 'Le': '<=', |
| 400 | 'Ne': '!=', |
| 401 | 'In': 'IN', |
| 402 | 'Like': 'LIKE' |
| 403 | } |
| 404 | |
| 405 | @classmethod |
| 406 | def conjoin_conds(cls, conds): |
| 407 | if not conds: |
| 408 | return None |
| 409 | if len(conds) == 1: |
| 410 | return conds[0] |
| 411 | return {'_type': 'And', 'left': conds[0], 'right': cls.conjoin_conds(conds[1:])} |
| 412 | |
| 413 | @classmethod |
| 414 | def linearize_cond(cls, cond): |
| 415 | if cond['_type'] in ('And', 'Or'): |
| 416 | conds, keywords = cls.linearize_cond(cond['right']) |
| 417 | return [cond['left']] + conds, [cond['_type']] + keywords |
| 418 | else: |
| 419 | return [cond], [] |
| 420 | |
| 421 | def unparse_val(self, val): |
| 422 | if val['_type'] == 'Terminal': |
| 423 | return "'terminal'" |
| 424 | if val['_type'] == 'String': |
| 425 | return val['s'] |
| 426 | if val['_type'] == 'ColUnit': |
| 427 | return self.unparse_col_unit(val['c']) |
| 428 | if val['_type'] == 'Number': |
| 429 | return str(val['f']) |
| 430 | if val['_type'] == 'ValSql': |
| 431 | return '({})'.format(self.unparse_sql(val['s'])) |
| 432 | |
| 433 | def unparse_col_unit(self, col_unit): |
| 434 | if 'col_id' in col_unit: |
| 435 | column = self.schema.columns[col_unit['col_id']] |
| 436 | if column.table is None: |
| 437 | column_name = column.orig_name |
| 438 | else: |
| 439 | column_name = '{}.{}'.format(column.table.orig_name, column.orig_name) |