docs: def parse_field(fld, selectable, aggregated=True, default_aggregation='sum'):
      """ Parse a field object from yaml into a sqlalchemy expression """
      # An aggregation is a callable that takes a single field expression
      # None will perform no aggregation
      aggregation_lookup = {
          'sum': func.sum,
          'min': func.min,
          'max': func.max,
          'avg': func.avg,
          'count': func.count,
          'count_distinct': lambda fld: func.count(distinct(fld)),
          'month': lambda fld: func.date_trunc('month', fld),
          'week': lambda fld: func.date_trunc('week', fld),
          'year': lambda fld: func.date_trunc('year', fld),
          'quarter': lambda fld: func.date_trunc('quarter', fld),
          'age': lambda fld: func.date_part('year', func.age(fld)),
          None: lambda fld: fld,
      }
  
      # Ensure that the dictionary contains:
      # {
      #     'value': str,
      #     'aggregation': str|None,
      #     'condition': dict|None
      # }
      if isinstance(fld, basestring):
          fld = {
              'value': fld,
          }
      if not isinstance(fld, dict):
          raise BadIngredient('fields must be a string or a dict')
      if 'value' not in fld:
          raise BadIngredient('fields must contain a value')
      if not isinstance(fld['value'], basestring):
          raise BadIngredient('field value must be a string')
  
      # Ensure a condition
      if 'condition' in fld:
          if not isinstance(fld['condition'], dict) and \
                  not fld['condition'] is None:
              raise BadIngredient('condition must be null or an object')
      else:
          fld['condition'] = None
  
      # Ensure an aggregation
      initial_aggregation = default_aggregation if aggregated else None
      if 'aggregation' in fld:
          if not isinstance(fld['aggregation'], basestring) and \
                  not fld['aggregation'] is None:
              raise BadIngredient('aggregation must be null or an string')
          if fld['aggregation'] is None:
              fld['aggregation'] = initial_aggregation
      else:
          fld['aggregation'] = initial_aggregation
  
      value = fld.get('value', None)
      if value is None:
          raise BadIngredient('field value is not defined')
  
      field_parts = []
      for word in tokenize(value):
          if word in ('MINUS', 'PLUS', 'DIVIDE', 'MULTIPLY'):
              field_parts.append(word)
          else:
              field_parts.append(find_column(selectable, word))
  
      if len(field_parts) is None:
          raise BadIngredient('field is not defined.')
      # Fields should have an odd number of parts
      if len(field_parts) % 2 != 1:
          raise BadIngredient('field does not have the right number of parts')
  
      field = field_parts[0]
      if len(field_parts) > 1:
          # if we need to add and subtract from the field
          # join the field parts into pairs, for instance if field parts is
          # [MyTable.first, 'MINUS', MyTable.second, 'PLUS', MyTable.third]
          # we will get two pairs here
          # [('MINUS', MyTable.second), ('PLUS', MyTable.third)]
          for operator, other_field in zip(field_parts[1::2], field_parts[2::2]):
              if operator == 'PLUS':
                  field = field.__add__(other_field)
              elif operator == 'MINUS':
                  field = field.__sub__(other_field)
              elif operator == 'DIVIDE':
                  field = field.__div__(other_field)
              elif operator == 'MULTIPLY':
                  field = field.__mul__(other_field)
              else:
                  raise BadIngredient('Unknown operator {}'.format(operator))
  
      # Handle the aggregator
      aggr = fld.get('aggregation', 'sum')
      if aggr is not None:
          aggr = aggr.strip()
  
      if aggr not in aggregation_lookup:
          raise BadIngredient('unknown aggregation {}'.format(aggr))
  
      aggregator = aggregation_lookup[aggr]
  
      condition = parse_condition(
          fld.get('condition', None),
          selectable,
          aggregated=False,
          default_aggregation=default_aggregation
      )
  
      if condition is not None:
          field = case([(condition, field)])
  
      return aggregator(field)