def get_action_sequence_and_all_actions(self, query: List[str] = None, prelinked_entities: Dict[str, Dict[str, str]] = None) -> Tuple[List[str], List[str]]: # pylint: disable=line-too-long grammar_with_context = deepcopy(self.base_grammar_dictionary) if not self.use_prelinked_entities and prelinked_entities is not None: raise ConfigurationError( "The Text2SqlWorld was specified to not use prelinked " "entities, but prelinked entities were passed.") prelinked_entities = prelinked_entities or {} if self.use_untyped_entities: update_grammar_values_with_variables(grammar_with_context, prelinked_entities) else: update_grammar_numbers_and_strings_with_variables( grammar_with_context, prelinked_entities, self.columns) grammar = Grammar(format_grammar_string(grammar_with_context)) valid_actions = initialize_valid_actions(grammar) all_actions = set() for action_list in valid_actions.values(): all_actions.update(action_list) sorted_actions = sorted(all_actions) sql_visitor = SqlVisitor(grammar) try: action_sequence = sql_visitor.parse( " ".join(query)) if query else [] except ParseError: action_sequence = None return action_sequence, sorted_actions
def test_variable_free_world_cannot_parse_as_statements(self): world = Text2SqlWorld(self.schema) grammar_dictionary = world.base_grammar_dictionary for productions in grammar_dictionary.items(): assert "AS" not in productions sql_with_as = [ 'SELECT', 'COUNT', '(', '*', ')', 'FROM', 'LOCATION', 'AS', 'LOCATIONalias0', ',', 'RESTAURANT', 'WHERE', 'LOCATION', '.', 'CITY_NAME', '=', "'city_name0'", 'AND', 'RESTAURANT', '.', 'NAME', '=', 'LOCATION', '.', 'RESTAURANT_ID', 'AND', 'RESTAURANT', '.', 'NAME', '=', "'name0'", ';' ] grammar = Grammar(format_grammar_string(world.base_grammar_dictionary)) sql_visitor = SqlVisitor(grammar) with self.assertRaises(ParseError): sql_visitor.parse(" ".join(sql_with_as)) sql = [ 'SELECT', 'COUNT', '(', '*', ')', 'FROM', 'LOCATION', ',', 'RESTAURANT', 'WHERE', 'LOCATION', '.', 'CITY_NAME', '=', "'city_name0'", 'AND', 'RESTAURANT', '.', 'NAME', '=', 'LOCATION', '.', 'RESTAURANT_ID', 'AND', 'RESTAURANT', '.', 'NAME', '=', "'name0'", ';' ] # Without the AS we should still be able to parse it. sql_visitor = SqlVisitor(grammar) sql_visitor.parse(" ".join(sql))
def test_grammar_from_world_can_parse_statements(self): world = Text2SqlWorld(self.schema) sql = [ 'SELECT', 'COUNT', '(', '*', ')', 'FROM', 'LOCATION', ',', 'RESTAURANT', 'WHERE', 'LOCATION', '.', 'CITY_NAME', '=', "'city_name0'", 'AND', 'RESTAURANT', '.', 'NAME', '=', 'LOCATION', '.', 'RESTAURANT_ID', 'AND', 'RESTAURANT', '.', 'NAME', '=', "'name0'", ';' ] grammar = Grammar(format_grammar_string(world.base_grammar_dictionary)) sql_visitor = SqlVisitor(grammar) sql_visitor.parse(" ".join(sql))
def test_grammar_from_world_can_parse_statements(self): world = Text2SqlWorld(self.schema) sql = [ "SELECT", "COUNT", "(", "*", ")", "FROM", "LOCATION", ",", "RESTAURANT", "WHERE", "LOCATION", ".", "CITY_NAME", "=", "'city_name0'", "AND", "RESTAURANT", ".", "NAME", "=", "LOCATION", ".", "RESTAURANT_ID", "AND", "RESTAURANT", ".", "NAME", "=", "'name0'", ";", ] grammar = Grammar(format_grammar_string(world.base_grammar_dictionary)) sql_visitor = SqlVisitor(grammar) sql_visitor.parse(" ".join(sql))
def get_grammar_string(self): return format_grammar_string(self.grammar_dictionary)
def test_variable_free_world_cannot_parse_as_statements(self): world = Text2SqlWorld(self.schema) grammar_dictionary = world.base_grammar_dictionary for productions in grammar_dictionary.items(): assert "AS" not in productions sql_with_as = [ "SELECT", "COUNT", "(", "*", ")", "FROM", "LOCATION", "AS", "LOCATIONalias0", ",", "RESTAURANT", "WHERE", "LOCATION", ".", "CITY_NAME", "=", "'city_name0'", "AND", "RESTAURANT", ".", "NAME", "=", "LOCATION", ".", "RESTAURANT_ID", "AND", "RESTAURANT", ".", "NAME", "=", "'name0'", ";", ] grammar = Grammar(format_grammar_string(world.base_grammar_dictionary)) sql_visitor = SqlVisitor(grammar) with self.assertRaises(ParseError): sql_visitor.parse(" ".join(sql_with_as)) sql = [ "SELECT", "COUNT", "(", "*", ")", "FROM", "LOCATION", ",", "RESTAURANT", "WHERE", "LOCATION", ".", "CITY_NAME", "=", "'city_name0'", "AND", "RESTAURANT", ".", "NAME", "=", "LOCATION", ".", "RESTAURANT_ID", "AND", "RESTAURANT", ".", "NAME", "=", "'name0'", ";", ] # Without the AS we should still be able to parse it. sql_visitor = SqlVisitor(grammar) sql_visitor.parse(" ".join(sql))