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

Method unparse_from

rat-sql-gap/seq2struct/grammars/spider.py:662–706  ·  view source on GitHub ↗
(self, from_)

Source from the content-addressed store, hash-verified

660 return '{}({})'.format(agg_type, unparsed_val_unit)
661
662 def unparse_from(self, from_):
663 if 'conds' in from_:
664 all_conds, keywords = self.linearize_cond(from_['conds'])
665 else:
666 all_conds, keywords = [], []
667 assert all(keyword == 'And' for keyword in keywords)
668
669 cond_indices_by_table = collections.defaultdict(set)
670 tables_involved_by_cond_idx = collections.defaultdict(set)
671 for i, cond in enumerate(all_conds):
672 for column in self.ast_wrapper.find_all_descendants_of_type(cond, 'column'):
673 table = self.schema.columns[column].table
674 if table is None:
675 continue
676 cond_indices_by_table[table.id].add(i)
677 tables_involved_by_cond_idx[i].add(table.id)
678
679 output_table_ids = set()
680 output_cond_indices = set()
681 tokens = ['FROM']
682 for i, table_unit in enumerate(from_.get('table_units', [])):
683 if i > 0:
684 tokens += ['JOIN']
685
686 if table_unit['_type'] == 'TableUnitSql':
687 tokens.append('({})'.format(self.unparse_sql(table_unit['s'])))
688 elif table_unit['_type'] == 'Table':
689 table_id = table_unit['table_id']
690 tokens += [self.schema.tables[table_id].orig_name]
691 output_table_ids.add(table_id)
692
693 # Output "ON <cond>" if all tables involved in the condition have been output
694 conds_to_output = []
695 for cond_idx in sorted(cond_indices_by_table[table_id]):
696 if cond_idx in output_cond_indices:
697 continue
698 if tables_involved_by_cond_idx[cond_idx] <= output_table_ids:
699 conds_to_output.append(all_conds[cond_idx])
700 output_cond_indices.add(cond_idx)
701 if conds_to_output:
702 tokens += ['ON']
703 tokens += list(intersperse(
704 'AND',
705 (self.unparse_cond(cond) for cond in conds_to_output)))
706 return ' '.join(tokens)
707
708 def unparse_order_by(self, order_by):
709 return 'ORDER BY {} {}'.format(

Callers 1

unparse_sqlMethod · 0.95

Calls 7

linearize_condMethod · 0.95
unparse_sqlMethod · 0.95
unparse_condMethod · 0.95
intersperseFunction · 0.85
appendMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected