Exemplo n.º 1
0
def _parse_identifier(sql, example, anonymize_values):
  """Parse the part relative to an Identifier in the SQL query."""
  for item in sql:
    if item.ttype == sqlparse.tokens.Text.Whitespace:
      continue

    if _is_identifier(item):
      _parse_identifier(item, example, anonymize_values)
      continue

    if _is_order(item):
      _add_simple_step(item, example)
      continue

    if (_is_table_alias(item) or (_is_punctuation(item) and item.value == '.')):
      _add_simple_step(item, example)
      continue

    if _is_keyword(item) and item.value in ('as',):
      _add_simple_step(item, example)
      continue

    if _is_name(item):
      entity = _find_simple_entity(item.value, example)
      if entity is not None:
        schema_copy_action = None

        if isinstance(entity, DatabaseTable):
          schema_copy_action = SchemaEntityCopy(copied_table=entity)
        elif isinstance(entity, TableColumn):
          schema_copy_action = SchemaEntityCopy(copied_column=entity)
        else:
          raise ValueError('Type of entity is unexpected: ' + str(type(entity)))

        copy_action = SQLAction(entity_copy=schema_copy_action)
        example.gold_sql_query.actions.append(copy_action)
      else:
        try:
          _resolve_reference(item, example)
        except AttributeError as e:
          # Generally this means the reference wasn't found i.e., in WikiSQL, a
          # value didn't have quotes, so just add it as a value
          print(e)
          _add_simple_value(item, example, anonymize_values)
      continue

    if _is_literal(item):
      prev_len = len(example.gold_sql_query.actions)
      _add_simple_value(item, example, anonymize_values)
      if len(example.gold_sql_query.actions) == prev_len:
        raise ValueError(
            'Gold query did not change length when adding simple value!')
      continue

    _debug_state(item, example)
    raise ParseError('Incomplete _parse_identifier')
Exemplo n.º 2
0
  def _find_direction(reverse):
    """Finds a column annotation in a given direction."""
    table_entities = _find_from_table(item, example, reverse=reverse)

    if not table_entities:
      return False

    for table_entity in table_entities:
      entity = _find_column_entities(item.value, example, table_entity)

      if entity:
        example.gold_sql_query.actions.append(
            SQLAction(entity_copy=SchemaEntityCopy(copied_column=entity)))
        return True

    raise ParseError('Unable to find annotation of table ' + str(item))
Exemplo n.º 3
0
def _add_column_copy(table_name, column_name, example):
    column = _find_column(table_name, column_name, example)
    example.gold_sql_query.actions.append(
        SQLAction(entity_copy=SchemaEntityCopy(copied_column=column)))
Exemplo n.º 4
0
def _add_table_copy(table_name, example):
    table = _find_table(table_name, example)
    example.gold_sql_query.actions.append(
        SQLAction(entity_copy=SchemaEntityCopy(copied_table=table)))
Exemplo n.º 5
0
def _resolve_reference(item, example):
  """Resolves an ambiguous token that matches multiple annotations.

  Args:
    item: position in the SQL parse where the search will start.
    example: the QuikExample containing table and column annotations.

  Raises:
    ParseError: if the ambiguity cannot be resolved.
  """
  prev_symbol = example.gold_sql_query.actions[
      len(example.gold_sql_query.actions) - 1].symbol

  if prev_symbol in ('join', 'from'):
    table_annotation = _find_table_annotation(item, example)
    assert table_annotation, ('Cannot find a table annotation for item %s' %
                              item.value)

    example.gold_sql_query.actions.append(
        SQLAction(entity_copy=SchemaEntityCopy(copied_table=table_annotation)))
    return

  parent = item.parent

  # We try the simple case, that is aliases
  parent_tokens = _get_tokens(parent)
  if _is_table_alias(parent_tokens[0]) and parent_tokens[1].value == '.':
    aliases = _get_all_aliases(item, example)

    table_annotation = aliases[parent.tokens[0].value.lower()]
    found_column = _find_column_entities(item.value, example, table_annotation)

    assert found_column, 'Could not find column with name ' + str(item.value)
    example.gold_sql_query.actions.append(
        SQLAction(entity_copy=SchemaEntityCopy(copied_column=found_column)))
    return

  def _find_direction(reverse):
    """Finds a column annotation in a given direction."""
    table_entities = _find_from_table(item, example, reverse=reverse)

    if not table_entities:
      return False

    for table_entity in table_entities:
      entity = _find_column_entities(item.value, example, table_entity)

      if entity:
        example.gold_sql_query.actions.append(
            SQLAction(entity_copy=SchemaEntityCopy(copied_column=entity)))
        return True

    raise ParseError('Unable to find annotation of table ' + str(item))

  if (prev_symbol in ('where', 'by') or _is_where(parent) or
      _is_where(parent.parent)):
    if _find_direction(reverse=True):
      return

  if _find_direction(reverse=False):
    return

  if _find_direction(reverse=True):
    return

  raise ParseError('Unable to find final annotation in any direction')