MCPcopy Create free account
hub / github.com/awslabs/gap-text2sql / unparse_sql

Method unparse_sql

rat-sql-gap/seq2struct/grammars/spider.py:578–645  ·  view source on GitHub ↗
(self, tree)

Source from the content-addressed store, hash-verified

576
577
578 def unparse_sql(self, tree):
579 self.refine_from(tree)
580
581 result = [
582 # select select,
583 self.unparse_select(tree['select']),
584 # from from,
585 self.unparse_from(tree['from']),
586 ]
587
588 def find_subtree(_tree, name):
589 if self.factorize_sketch == 0:
590 return _tree, _tree
591 elif name in _tree:
592 if self.factorize_sketch == 1:
593 return _tree[name], _tree[name]
594 elif self.factorize_sketch == 2:
595 return _tree, _tree[name]
596 else:
597 raise NotImplementedError
598
599 tree, target_tree = find_subtree(tree, "sql_where")
600 # cond? where,
601 if 'where' in target_tree:
602 result += [
603 'WHERE',
604 self.unparse_cond(target_tree['where'])
605 ]
606
607 tree, target_tree = find_subtree(tree, "sql_groupby")
608 # col_unit* group_by,
609 if 'group_by' in target_tree:
610 result += [
611 'GROUP BY',
612 ', '.join(self.unparse_col_unit(c) for c in target_tree['group_by'])
613 ]
614
615 tree, target_tree = find_subtree(tree, "sql_orderby")
616 # order_by? order_by,
617 if 'order_by' in target_tree:
618 result.append(self.unparse_order_by(target_tree['order_by']))
619
620 tree, target_tree = find_subtree(tree, "sql_groupby")
621 # cond? having,
622 if 'having' in target_tree:
623 result += ['HAVING', self.unparse_cond(target_tree['having'])]
624
625 tree, target_tree = find_subtree(tree, "sql_orderby")
626 # int? limit,
627 if 'limit' in target_tree:
628 if isinstance(target_tree['limit'], bool):
629 if target_tree['limit']:
630 result += ['LIMIT', '1']
631 else:
632 result += ['LIMIT', str(target_tree['limit'])]
633
634 tree, target_tree = find_subtree(tree, "sql_ieu")
635 # sql? intersect,

Callers 3

unparseMethod · 0.95
unparse_valMethod · 0.95
unparse_fromMethod · 0.95

Calls 7

refine_fromMethod · 0.95
unparse_selectMethod · 0.95
unparse_fromMethod · 0.95
unparse_condMethod · 0.95
unparse_col_unitMethod · 0.95
unparse_order_byMethod · 0.95
appendMethod · 0.80

Tested by

no test coverage detected