SELECT_LEX *LEX::wrap_select_chain_into_derived(SELECT_LEX *sel)
{
  SELECT_LEX *dummy_select;
  SELECT_LEX_UNIT *unit;
  Table_ident *ti;
  DBUG_ENTER("LEX::wrap_select_chain_into_derived");

  if (!(dummy_select= alloc_select(TRUE)))
     DBUG_RETURN(NULL);
  Name_resolution_context *context= &dummy_select->context;
  dummy_select->automatic_brackets= FALSE;
  sel->distinct= TRUE; // First select has not this attribute (safety)

  if (!(unit= dummy_select->attach_selects_chain(sel, context)))
    DBUG_RETURN(NULL);

  /* stuff dummy SELECT * FROM (...) */

  if (push_select(dummy_select)) // for Items & TABLE_LIST
    DBUG_RETURN(NULL);

  /* add SELECT list*/
  {
    Item *item= new (thd->mem_root)
      Item_field(thd, context, NULL, NULL, &star_clex_str);
    if (item == NULL)
      goto err;
    if (add_item_to_list(thd, item))
      goto err;
    (dummy_select->with_wild)++;
  }

  sel->set_linkage(DERIVED_TABLE_TYPE);

  ti= new (thd->mem_root) Table_ident(unit);
  if (ti == NULL)
    goto err;
  {
    TABLE_LIST *table_list;
    LEX_CSTRING alias;
    if (dummy_select->make_unique_derived_name(thd, &alias))
      goto err;

    if (!(table_list= dummy_select->add_table_to_list(thd, ti, &alias,
                                                      0, TL_READ,
                                                      MDL_SHARED_READ)))
      goto err;

    context->resolve_in_table_list_only(table_list);
    dummy_select->add_joined_table(table_list);
  }

  pop_select();

  derived_tables|= DERIVED_SUBQUERY;

  DBUG_RETURN(dummy_select);

err:
  pop_select();
  DBUG_RETURN(NULL);
}
SELECT_LEX *LEX::wrap_select_chain_into_derived(SELECT_LEX *sel)
{
  SELECT_LEX *dummy_select;
  SELECT_LEX_UNIT *unit;
  Table_ident *ti;
  DBUG_ENTER("LEX::wrap_select_chain_into_derived");

  if (!(dummy_select= alloc_select(TRUE)))
     DBUG_RETURN(NULL);
  Name_resolution_context *context= &dummy_select->context;
  dummy_select->automatic_brackets= FALSE;
  sel->distinct= TRUE;  

  if (!(unit= dummy_select->attach_selects_chain(sel, context)))
    DBUG_RETURN(NULL);

   

  if (push_select(dummy_select))  
    DBUG_RETURN(NULL);

   
  {
    Item *item= new (thd->mem_root)
      Item_field(thd, context, NULL, NULL, &star_clex_str);
    if (item == NULL)
      goto err;
    if (add_item_to_list(thd, item))
      goto err;
    (dummy_select->with_wild)++;
  }

  sel->set_linkage(DERIVED_TABLE_TYPE);

  ti= new (thd->mem_root) Table_ident(unit);
  if (ti == NULL)
    goto err;
  {
    TABLE_LIST *table_list;
    LEX_CSTRING alias;
    if (dummy_select->make_unique_derived_name(thd, &alias))
      goto err;

    if (!(table_list= dummy_select->add_table_to_list(thd, ti, &alias,
                                                      0, TL_READ,
                                                      MDL_SHARED_READ)))
      goto err;

    context->resolve_in_table_list_only(table_list);
    dummy_select->add_joined_table(table_list);
  }

  pop_select();

  derived_tables|= DERIVED_SUBQUERY;

  DBUG_RETURN(dummy_select);

err:
  pop_select();
  DBUG_RETURN(NULL);
}