int ha_partition::rnd_init(bool scan)
{
  int error;
  uint i= 0;
  uint32 part_id;
  DBUG_ENTER("ha_partition::rnd_init");

  /*
    For operations that may need to change data, we may need to extend
    read_set.
  */
  if (m_lock_type == F_WRLCK)
  {
    /*
      If write_set contains any of the fields used in partition and
      subpartition expression, we need to set all bits in read_set because
      the row may need to be inserted in a different [sub]partition. In
      other words update_row() can be converted into write_row(), which
      requires a complete record.
    */
    if (bitmap_is_overlapping(&m_part_info->full_part_field_set,
                              table->write_set))
      bitmap_set_all(table->read_set);
    else
    {
      /*
        Some handlers only read fields as specified by the bitmap for the
        read set. For partitioned handlers we always require that the
        fields of the partition functions are read such that we can
        calculate the partition id to place updated and deleted records.
      */
      bitmap_union(table->read_set, &m_part_info->full_part_field_set);
    }
  }

  /* Now we see what the index of our first important partition is */
  DBUG_PRINT("info", ("m_part_info->used_partitions: 0x%lx",
                      (long) m_part_info->used_partitions.bitmap));
  part_id= bitmap_get_first_set(&(m_part_info->used_partitions));
  DBUG_PRINT("info", ("m_part_spec.start_part %d", part_id));

  if (MY_BIT_NONE == part_id)
  {
    error= 0;
    goto err1;
  }

  /*
    We have a partition and we are scanning with rnd_next
    so we bump our cache
  */
  DBUG_PRINT("info", ("rnd_init on partition %d", part_id));
  if (scan)
  {
    /*
      rnd_end() is needed for partitioning to reset internal data if scan
      is already in use
    */
    rnd_end();
    late_extra_cache(part_id);
    if ((error= m_file[part_id]->ha_rnd_init(scan)))
      goto err;
  }
  else
  {
    for (i= part_id; i < m_tot_parts; i++)
    {
      if (bitmap_is_set(&(m_part_info->used_partitions), i))
      {
        if ((error= m_file[i]->ha_rnd_init(scan)))
          goto err;
      }
    }
  }
  m_scan_value= scan;
  m_part_spec.start_part= part_id;
  m_part_spec.end_part= m_tot_parts - 1;
  DBUG_PRINT("info", ("m_scan_value=%d", m_scan_value));
  DBUG_RETURN(0);

err:
  while ((int)--i >= (int)part_id)
  {
    if (bitmap_is_set(&(m_part_info->used_partitions), i))
      m_file[i]->ha_rnd_end();
  }
err1:
  m_scan_value= 2;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  DBUG_RETURN(error);
}
int ha_partition::rnd_init(bool scan)
{
  int error;
  uint i= 0;
  uint32 part_id;
  DBUG_ENTER("ha_partition::rnd_init");

   
  if (m_lock_type == F_WRLCK)
  {
     
    if (bitmap_is_overlapping(&m_part_info->full_part_field_set,
                              table->write_set))
      bitmap_set_all(table->read_set);
    else
    {
       
      bitmap_union(table->read_set, &m_part_info->full_part_field_set);
    }
  }

   
  DBUG_PRINT("info", ("m_part_info->used_partitions: 0x%lx",
                      (long) m_part_info->used_partitions.bitmap));
  part_id= bitmap_get_first_set(&(m_part_info->used_partitions));
  DBUG_PRINT("info", ("m_part_spec.start_part %d", part_id));

  if (MY_BIT_NONE == part_id)
  {
    error= 0;
    goto err1;
  }

   
  DBUG_PRINT("info", ("rnd_init on partition %d", part_id));
  if (scan)
  {
     
    rnd_end();
    late_extra_cache(part_id);
    if ((error= m_file[part_id]->ha_rnd_init(scan)))
      goto err;
  }
  else
  {
    for (i= part_id; i < m_tot_parts; i++)
    {
      if (bitmap_is_set(&(m_part_info->used_partitions), i))
      {
        if ((error= m_file[i]->ha_rnd_init(scan)))
          goto err;
      }
    }
  }
  m_scan_value= scan;
  m_part_spec.start_part= part_id;
  m_part_spec.end_part= m_tot_parts - 1;
  DBUG_PRINT("info", ("m_scan_value=%d", m_scan_value));
  DBUG_RETURN(0);

err:
  while ((int)--i >= (int)part_id)
  {
    if (bitmap_is_set(&(m_part_info->used_partitions), i))
      m_file[i]->ha_rnd_end();
  }
err1:
  m_scan_value= 2;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  DBUG_RETURN(error);
}