Args: Returns:
(toks, start_idx, tables_with_alias, schema)
| 580 | |
| 581 | |
| 582 | def parse_sql(toks, start_idx, tables_with_alias, schema): |
| 583 | """ |
| 584 | Args: |
| 585 | |
| 586 | Returns: |
| 587 | """ |
| 588 | isBlock = False # indicate whether this is a block of sql/sub-sql |
| 589 | len_ = len(toks) |
| 590 | idx = start_idx |
| 591 | |
| 592 | sql = {} |
| 593 | if toks[idx] == '(': |
| 594 | isBlock = True |
| 595 | idx += 1 |
| 596 | |
| 597 | # parse from clause in order to get default tables |
| 598 | from_end_idx, table_units, conds, default_tables = parse_from(toks, start_idx, tables_with_alias, schema) |
| 599 | sql['from'] = {'table_units': table_units, 'conds': conds} |
| 600 | # select clause |
| 601 | _, select_col_units = parse_select(toks, idx, tables_with_alias, schema, default_tables) |
| 602 | idx = from_end_idx |
| 603 | sql['select'] = select_col_units |
| 604 | # where clause |
| 605 | idx, where_conds = parse_where(toks, idx, tables_with_alias, schema, default_tables) |
| 606 | sql['where'] = where_conds |
| 607 | # group by clause |
| 608 | idx, group_col_units = parse_group_by(toks, idx, tables_with_alias, schema, default_tables) |
| 609 | sql['groupBy'] = group_col_units |
| 610 | # having clause |
| 611 | idx, having_conds = parse_having(toks, idx, tables_with_alias, schema, default_tables) |
| 612 | sql['having'] = having_conds |
| 613 | # order by clause |
| 614 | idx, order_col_units = parse_order_by(toks, idx, tables_with_alias, schema, default_tables) |
| 615 | sql['orderBy'] = order_col_units |
| 616 | # limit clause |
| 617 | idx, limit_val = parse_limit(toks, idx) |
| 618 | sql['limit'] = limit_val |
| 619 | |
| 620 | idx = skip_semicolon(toks, idx) |
| 621 | if isBlock: |
| 622 | assert toks[idx] == ')' |
| 623 | idx += 1 # skip ')' |
| 624 | idx = skip_semicolon(toks, idx) |
| 625 | |
| 626 | # intersect/union/except clause |
| 627 | for op in SQL_OPS: # initialize IUE |
| 628 | sql[op] = None |
| 629 | if idx < len_ and toks[idx] in SQL_OPS: |
| 630 | sql_op = toks[idx] |
| 631 | idx += 1 |
| 632 | idx, IUE_sql = parse_sql(toks, idx, tables_with_alias, schema) |
| 633 | sql[sql_op] = IUE_sql |
| 634 | return idx, sql |
| 635 | |
| 636 | |
| 637 | def load_data(fpath): |
no test coverage detected