23 #include "ha_ndbcluster_glue.h" 
   25 #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE 
   26 #include <ndbapi/NdbApi.hpp> 
   27 #include "ha_ndbcluster_cond.h" 
   40 ndb_serialize_cond(
const Item *item, 
void *arg)
 
   43   DBUG_ENTER(
"ndb_serialize_cond");  
 
   50       DBUG_PRINT(
"info", (
"Unexpected mismatch of found and expected number of function arguments %u", context->skip));
 
   51       sql_print_error(
"ndb_serialize_cond: Unexpected mismatch of found and " 
   52                       "expected number of function arguments %u", context->skip);
 
   56     DBUG_PRINT(
"info", (
"Skiping argument %d", context->skip));
 
   58     switch (item->type()) {
 
   62       context->skip+= func_item->argument_count();
 
   67     case Item::STRING_ITEM:
 
   68     case Item::VARBIN_ITEM:
 
   69     case Item::DECIMAL_ITEM:
 
   72       context->supported= FALSE;
 
   79   if (context->supported)
 
   84     if (rewrite_context2 &&
 
   85         (rewrite_func_item= rewrite_context2->func_item) &&
 
   86         rewrite_context2->count++ == 0)
 
   88       switch (rewrite_func_item->functype()) {
 
   89       case Item_func::BETWEEN:
 
   99       case Item_func::IN_FUNC:
 
  112         if (context->expecting(item->type()))
 
  115           rewrite_context2->left_hand_item= item;
 
  116           if (item->type() == Item::FUNC_ITEM)
 
  119             if ((func_item->functype() == Item_func::UNKNOWN_FUNC ||
 
  120                  func_item->functype() == Item_func::NEG_FUNC) &&
 
  121                 func_item->const_item())
 
  124               DBUG_PRINT(
"info", (
"Skip until end of arguments marker"));
 
  125               context->skip= func_item->argument_count();
 
  129               DBUG_PRINT(
"info", (
"Found unsupported functional expression in BETWEEN|IN"));
 
  130               context->supported= FALSE;
 
  139           DBUG_PRINT(
"info", (
"Found unexpected item of type %u in BETWEEN|IN",
 
  141           context->supported= FALSE;
 
  147         context->supported= FALSE;
 
  155       Ndb_cond *prev_cond= context->cond_ptr;
 
  157       if (!ndb_stack->ndb_cond)
 
  158         ndb_stack->ndb_cond= curr_cond;
 
  159       curr_cond->prev= prev_cond;
 
  160       if (prev_cond) prev_cond->next= curr_cond;
 
  162       if (context->rewrite_stack)
 
  165         const Item_func *func_item= rewrite_context->func_item;
 
  166         switch (func_item->functype()) {
 
  167         case Item_func::BETWEEN:
 
  178           if (rewrite_context->count == 2)
 
  181             DBUG_PRINT(
"info", (
"GE_FUNC"));      
 
  182             curr_cond->ndb_item= 
new Ndb_item(Item_func::GE_FUNC, 2);
 
  184           else if (rewrite_context->count == 3)
 
  187             DBUG_PRINT(
"info", (
"LE_FUNC"));      
 
  188             curr_cond->ndb_item= 
new Ndb_item(Item_func::LE_FUNC, 2);
 
  193             DBUG_PRINT(
"info", (
"Illegal BETWEEN expression"));
 
  194             context->supported= FALSE;
 
  199         case Item_func::IN_FUNC:
 
  212           DBUG_PRINT(
"info", (
"EQ_FUNC"));      
 
  213           curr_cond->ndb_item= 
new Ndb_item(Item_func::EQ_FUNC, 2);
 
  217           context->supported= FALSE;
 
  220         context->rewrite_stack= NULL; 
 
  221         context->expect_only(Item::FIELD_ITEM);
 
  222         context->expect_field_result(STRING_RESULT);
 
  223         context->expect_field_result(REAL_RESULT);
 
  224         context->expect_field_result(INT_RESULT);
 
  225         context->expect_field_result(DECIMAL_RESULT);
 
  226         context->expect(Item::INT_ITEM);
 
  227         context->expect(Item::STRING_ITEM);
 
  228         context->expect(Item::VARBIN_ITEM);
 
  229         context->expect(Item::FUNC_ITEM);
 
  230         context->expect(Item::CACHE_ITEM);
 
  231         ndb_serialize_cond(rewrite_context->left_hand_item, arg);
 
  233         context->rewrite_stack= rewrite_context; 
 
  234         if (!context->supported)
 
  237         prev_cond= context->cond_ptr;
 
  238         curr_cond= context->cond_ptr= 
new Ndb_cond();
 
  239         prev_cond->next= curr_cond;
 
  246         DBUG_PRINT(
"info", (
"End of condition group"));
 
  247         context->expect_no_length();
 
  248         curr_cond->ndb_item= 
new Ndb_item(NDB_END_COND);
 
  254         switch (item->type()) {
 
  255         case Item::FIELD_ITEM:
 
  258           Field *field= field_item->field;
 
  259           const enum_field_types 
type= field->real_type();
 
  264           if (context->table->s == field->table->s)
 
  266             const NDBTAB *tab= context->ndb_table;
 
  267             DBUG_PRINT(
"info", (
"FIELD_ITEM"));
 
  268             DBUG_PRINT(
"info", (
"table %s", tab->
getName()));
 
  269             DBUG_PRINT(
"info", (
"column %s", field->field_name));
 
  270             DBUG_PRINT(
"info", (
"column length %u", field->field_length));
 
  271             DBUG_PRINT(
"info", (
"type %d", type));
 
  272             DBUG_PRINT(
"info", (
"result type %d", field->result_type()));
 
  276             if (context->expecting(Item::FIELD_ITEM) &&
 
  277                 context->expecting_field_type(type) &&
 
  278                 context->expecting_max_length(field->field_length) &&
 
  279                 (context->expecting_field_result(field->result_type()) ||
 
  281                  ((type == MYSQL_TYPE_TIME ||
 
  282                    type == MYSQL_TYPE_DATE || 
 
  283                    type == MYSQL_TYPE_NEWDATE || 
 
  284                    type == MYSQL_TYPE_YEAR ||
 
  285                    type == MYSQL_TYPE_DATETIME)
 
  286                   ? (context->expecting_field_result(STRING_RESULT) ||
 
  287                      context->expecting_field_result(INT_RESULT))
 
  290                 type != MYSQL_TYPE_BIT &&
 
  292                 type != MYSQL_TYPE_TINY_BLOB &&
 
  293                 type != MYSQL_TYPE_MEDIUM_BLOB &&
 
  294                 type != MYSQL_TYPE_LONG_BLOB &&
 
  295                 type != MYSQL_TYPE_BLOB)
 
  300               context->dont_expect(Item::FIELD_ITEM);
 
  301               context->expect_no_field_result();
 
  302               if (! context->expecting_nothing())
 
  305                 if (type == MYSQL_TYPE_TIME ||
 
  306                     type == MYSQL_TYPE_DATE || 
 
  307                     type == MYSQL_TYPE_NEWDATE || 
 
  308                     type == MYSQL_TYPE_YEAR ||
 
  309                     type == MYSQL_TYPE_DATETIME)
 
  311                   context->expect_only(Item::STRING_ITEM);
 
  312                   context->expect(Item::INT_ITEM);
 
  315                   switch (field->result_type()) {
 
  318                     context->expect_only(Item::STRING_ITEM);
 
  319                     context->expect(Item::VARBIN_ITEM);
 
  320                     context->expect_collation(field_item->collation.collation);
 
  321                     context->expect_max_length(field->field_length);
 
  324                     context->expect_only(Item::REAL_ITEM);
 
  325                     context->expect(Item::DECIMAL_ITEM);
 
  326                     context->expect(Item::INT_ITEM);
 
  329                     context->expect_only(Item::INT_ITEM);
 
  330                     context->expect(Item::VARBIN_ITEM);
 
  333                     context->expect_only(Item::DECIMAL_ITEM);
 
  334                     context->expect(Item::REAL_ITEM);
 
  335                     context->expect(Item::INT_ITEM);
 
  344                 context->expect_only(Item::FUNC_ITEM);
 
  345                 context->expect(Item::COND_ITEM);
 
  347                 if ((field->result_type() == STRING_RESULT) &&
 
  348                     !context->expecting_collation(item->collation.collation)
 
  349                     && type != MYSQL_TYPE_TIME
 
  350                     && type != MYSQL_TYPE_DATE
 
  351                     && type != MYSQL_TYPE_NEWDATE
 
  352                     && type != MYSQL_TYPE_YEAR
 
  353                     && type != MYSQL_TYPE_DATETIME)
 
  355                   DBUG_PRINT(
"info", (
"Found non-matching collation %s",  
 
  356                                       item->collation.collation->name)); 
 
  357                   context->supported= FALSE;                
 
  364               DBUG_PRINT(
"info", (
"Was not expecting field of type %u(%u)",
 
  365                                   field->result_type(), 
type));
 
  366               context->supported= FALSE;
 
  371             DBUG_PRINT(
"info", (
"Was not expecting field from table %s (%s)",
 
  372                                 context->table->s->table_name.str, 
 
  373                                 field->table->s->table_name.str));
 
  374             context->supported= FALSE;
 
  378         case Item::FUNC_ITEM:
 
  382           if (context->expecting(Item::FUNC_ITEM) ||
 
  383               func_item->functype() == Item_func::UNKNOWN_FUNC ||
 
  384               func_item->functype() == Item_func::NEG_FUNC)
 
  385             context->expect_nothing();
 
  389             context->supported= FALSE;
 
  392           context->expect_no_length();
 
  393           switch (func_item->functype()) {
 
  394           case Item_func::EQ_FUNC:
 
  396             DBUG_PRINT(
"info", (
"EQ_FUNC"));      
 
  397             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(), 
 
  399             context->expect(Item::STRING_ITEM);
 
  400             context->expect(Item::INT_ITEM);
 
  401             context->expect(Item::REAL_ITEM);
 
  402             context->expect(Item::DECIMAL_ITEM);
 
  403             context->expect(Item::VARBIN_ITEM);
 
  404             context->expect(Item::FIELD_ITEM);
 
  405             context->expect_field_result(STRING_RESULT);
 
  406             context->expect_field_result(REAL_RESULT);
 
  407             context->expect_field_result(INT_RESULT);
 
  408             context->expect_field_result(DECIMAL_RESULT);
 
  411           case Item_func::NE_FUNC:
 
  413             DBUG_PRINT(
"info", (
"NE_FUNC"));      
 
  414             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  416             context->expect(Item::STRING_ITEM);
 
  417             context->expect(Item::INT_ITEM);
 
  418             context->expect(Item::REAL_ITEM);
 
  419             context->expect(Item::DECIMAL_ITEM);
 
  420             context->expect(Item::VARBIN_ITEM);
 
  421             context->expect(Item::FIELD_ITEM);
 
  422             context->expect_field_result(STRING_RESULT);
 
  423             context->expect_field_result(REAL_RESULT);
 
  424             context->expect_field_result(INT_RESULT);
 
  425             context->expect_field_result(DECIMAL_RESULT);
 
  428           case Item_func::LT_FUNC:
 
  430             DBUG_PRINT(
"info", (
"LT_FUNC"));      
 
  431             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  433             context->expect(Item::STRING_ITEM);
 
  434             context->expect(Item::INT_ITEM);
 
  435             context->expect(Item::REAL_ITEM);
 
  436             context->expect(Item::DECIMAL_ITEM);
 
  437             context->expect(Item::VARBIN_ITEM);
 
  438             context->expect(Item::FIELD_ITEM);
 
  439             context->expect_field_result(STRING_RESULT);
 
  440             context->expect_field_result(REAL_RESULT);
 
  441             context->expect_field_result(INT_RESULT);
 
  442             context->expect_field_result(DECIMAL_RESULT);
 
  445           case Item_func::LE_FUNC:
 
  447             DBUG_PRINT(
"info", (
"LE_FUNC"));      
 
  448             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  450             context->expect(Item::STRING_ITEM);
 
  451             context->expect(Item::INT_ITEM);
 
  452             context->expect(Item::REAL_ITEM);
 
  453             context->expect(Item::DECIMAL_ITEM);
 
  454             context->expect(Item::VARBIN_ITEM);
 
  455             context->expect(Item::FIELD_ITEM);
 
  456             context->expect_field_result(STRING_RESULT);
 
  457             context->expect_field_result(REAL_RESULT);
 
  458             context->expect_field_result(INT_RESULT);
 
  459             context->expect_field_result(DECIMAL_RESULT);
 
  462           case Item_func::GE_FUNC:
 
  464             DBUG_PRINT(
"info", (
"GE_FUNC"));      
 
  465             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  467             context->expect(Item::STRING_ITEM);
 
  468             context->expect(Item::INT_ITEM);
 
  469             context->expect(Item::REAL_ITEM);
 
  470             context->expect(Item::DECIMAL_ITEM);
 
  471             context->expect(Item::VARBIN_ITEM);
 
  472             context->expect(Item::FIELD_ITEM);
 
  473             context->expect_field_result(STRING_RESULT);
 
  474             context->expect_field_result(REAL_RESULT);
 
  475             context->expect_field_result(INT_RESULT);
 
  476             context->expect_field_result(DECIMAL_RESULT);
 
  479           case Item_func::GT_FUNC:
 
  481             DBUG_PRINT(
"info", (
"GT_FUNC"));      
 
  482             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  484             context->expect(Item::STRING_ITEM);
 
  485             context->expect(Item::REAL_ITEM);
 
  486             context->expect(Item::DECIMAL_ITEM);
 
  487             context->expect(Item::INT_ITEM);
 
  488             context->expect(Item::VARBIN_ITEM);
 
  489             context->expect(Item::FIELD_ITEM);
 
  490             context->expect_field_result(STRING_RESULT);
 
  491             context->expect_field_result(REAL_RESULT);
 
  492             context->expect_field_result(INT_RESULT);
 
  493             context->expect_field_result(DECIMAL_RESULT);
 
  496           case Item_func::LIKE_FUNC:
 
  499             DBUG_PRINT(
"info", (
"LIKE_FUNC"));      
 
  501             if (((
const Item_func_like *)func_item)->escape_was_used_in_parsing())
 
  503               DBUG_PRINT(
"info", (
"LIKE expressions with ESCAPE not supported"));
 
  504               context->supported= FALSE;
 
  507             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  517             context->expect(Item::FIELD_ITEM);
 
  518             context->expect_only_field_type(MYSQL_TYPE_STRING);
 
  519             context->expect_field_type(MYSQL_TYPE_VAR_STRING);
 
  520             context->expect_field_type(MYSQL_TYPE_VARCHAR);
 
  521             context->expect_field_result(STRING_RESULT);
 
  522             expect_next->expect(Item::STRING_ITEM);
 
  523             expect_next->expect(Item::FUNC_ITEM);
 
  524             context->expect_stack.push(expect_next);
 
  528           case Item_func::ISNULL_FUNC:
 
  530             DBUG_PRINT(
"info", (
"ISNULL_FUNC"));      
 
  531             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  533             context->expect(Item::FIELD_ITEM);
 
  534             context->expect_field_result(STRING_RESULT);
 
  535             context->expect_field_result(REAL_RESULT);
 
  536             context->expect_field_result(INT_RESULT);
 
  537             context->expect_field_result(DECIMAL_RESULT);
 
  540           case Item_func::ISNOTNULL_FUNC:
 
  542             DBUG_PRINT(
"info", (
"ISNOTNULL_FUNC"));      
 
  543             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  545             context->expect(Item::FIELD_ITEM);
 
  546             context->expect_field_result(STRING_RESULT);
 
  547             context->expect_field_result(REAL_RESULT);
 
  548             context->expect_field_result(INT_RESULT);
 
  549             context->expect_field_result(DECIMAL_RESULT);
 
  552           case Item_func::NOT_FUNC:
 
  554             DBUG_PRINT(
"info", (
"NOT_FUNC"));      
 
  555             curr_cond->ndb_item= 
new Ndb_item(func_item->functype(),
 
  557             context->expect(Item::FUNC_ITEM);
 
  558             context->expect(Item::COND_ITEM);
 
  561           case Item_func::BETWEEN:
 
  563             DBUG_PRINT(
"info", (
"BETWEEN, rewriting using AND"));
 
  567             rewrite_context->next= context->rewrite_stack;
 
  568             context->rewrite_stack= rewrite_context;
 
  569             if (between_func->negated)
 
  571               DBUG_PRINT(
"info", (
"NOT_FUNC"));
 
  572               curr_cond->ndb_item= 
new Ndb_item(Item_func::NOT_FUNC, 1);
 
  573               prev_cond= curr_cond;
 
  574               curr_cond= context->cond_ptr= 
new Ndb_cond();
 
  575               curr_cond->prev= prev_cond;
 
  576               prev_cond->next= curr_cond;
 
  578             DBUG_PRINT(
"info", (
"COND_AND_FUNC"));
 
  580               new Ndb_item(Item_func::COND_AND_FUNC, 
 
  581                            func_item->argument_count() - 1);
 
  582             context->expect_only(Item::FIELD_ITEM);
 
  583             context->expect(Item::INT_ITEM);
 
  584             context->expect(Item::STRING_ITEM);
 
  585             context->expect(Item::VARBIN_ITEM);
 
  586             context->expect(Item::FUNC_ITEM);
 
  587             context->expect(Item::CACHE_ITEM);
 
  590           case Item_func::IN_FUNC:
 
  592             DBUG_PRINT(
"info", (
"IN_FUNC, rewriting using OR"));
 
  596             rewrite_context->next= context->rewrite_stack;
 
  597             context->rewrite_stack= rewrite_context;
 
  598             if (in_func->negated)
 
  600               DBUG_PRINT(
"info", (
"NOT_FUNC"));
 
  601               curr_cond->ndb_item= 
new Ndb_item(Item_func::NOT_FUNC, 1);
 
  602               prev_cond= curr_cond;
 
  603               curr_cond= context->cond_ptr= 
new Ndb_cond();
 
  604               curr_cond->prev= prev_cond;
 
  605               prev_cond->next= curr_cond;
 
  607             DBUG_PRINT(
"info", (
"COND_OR_FUNC"));
 
  608             curr_cond->ndb_item= 
new Ndb_item(Item_func::COND_OR_FUNC, 
 
  609                                               func_item->argument_count() - 1);
 
  610             context->expect_only(Item::FIELD_ITEM);
 
  611             context->expect(Item::INT_ITEM);
 
  612             context->expect(Item::STRING_ITEM);
 
  613             context->expect(Item::VARBIN_ITEM);
 
  614             context->expect(Item::FUNC_ITEM);
 
  615             context->expect(Item::CACHE_ITEM);
 
  618           case Item_func::NEG_FUNC:
 
  619           case Item_func::UNKNOWN_FUNC:
 
  626             DBUG_PRINT(
"info", (
"Function %s", 
 
  627                                 func_item->const_item()?
"const":
""));  
 
  628             DBUG_PRINT(
"info", (
"result type %d", func_item->result_type()));
 
  634             if (context->rewrite_stack &&
 
  635                 context->rewrite_stack->left_hand_item &&
 
  636                 context->rewrite_stack->left_hand_item->type()
 
  639               DBUG_PRINT(
"info", (
"Function during rewrite not supported"));
 
  640               context->supported= FALSE;
 
  642             if (func_item->const_item())
 
  644               switch (func_item->result_type()) {
 
  648                 q.value_type= Item::STRING_ITEM;
 
  649                 curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item); 
 
  650                 if (! context->expecting_no_field_result())
 
  653                   context->expect_only(Item::FIELD_ITEM);
 
  654                   context->expect_only_field_result(STRING_RESULT);
 
  655                   context->expect_collation(func_item->collation.collation);
 
  660                   context->expect_only(Item::FUNC_ITEM);
 
  661                   context->expect(Item::COND_ITEM);
 
  663                   if (!context->expecting_collation(item->collation.collation))
 
  665                     DBUG_PRINT(
"info", (
"Found non-matching collation %s",  
 
  666                                         item->collation.collation->name));
 
  667                     context->supported= FALSE;
 
  671                 DBUG_PRINT(
"info", (
"Skip until end of arguments marker"));
 
  672                 context->skip= func_item->argument_count();
 
  678                 q.value_type= Item::REAL_ITEM;
 
  679                 curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  680                 if (! context->expecting_no_field_result()) 
 
  683                   context->expect_only(Item::FIELD_ITEM);
 
  684                   context->expect_only_field_result(REAL_RESULT);
 
  689                   context->expect_only(Item::FUNC_ITEM);
 
  690                   context->expect(Item::COND_ITEM);
 
  694                 DBUG_PRINT(
"info", (
"Skip until end of arguments marker"));
 
  695                 context->skip= func_item->argument_count();
 
  701                 q.value_type= Item::INT_ITEM;
 
  702                 curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  703                 if (! context->expecting_no_field_result()) 
 
  706                   context->expect_only(Item::FIELD_ITEM);
 
  707                   context->expect_only_field_result(INT_RESULT);
 
  712                   context->expect_only(Item::FUNC_ITEM);
 
  713                   context->expect(Item::COND_ITEM);
 
  717                 DBUG_PRINT(
"info", (
"Skip until end of arguments marker"));
 
  718                 context->skip= func_item->argument_count();
 
  724                 q.value_type= Item::DECIMAL_ITEM;
 
  725                 curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  726                 if (! context->expecting_no_field_result()) 
 
  729                   context->expect_only(Item::FIELD_ITEM);
 
  730                   context->expect_only_field_result(DECIMAL_RESULT);
 
  735                   context->expect_only(Item::FUNC_ITEM);
 
  736                   context->expect(Item::COND_ITEM);
 
  739                 DBUG_PRINT(
"info", (
"Skip until end of arguments marker"));
 
  740                 context->skip= func_item->argument_count();
 
  749               context->supported= FALSE;
 
  754             DBUG_PRINT(
"info", (
"Found func_item of type %d", 
 
  755                                 func_item->functype()));
 
  756             context->supported= FALSE;
 
  761         case Item::STRING_ITEM:
 
  762           DBUG_PRINT(
"info", (
"STRING_ITEM")); 
 
  764           if (context->expecting(Item::STRING_ITEM) &&
 
  765               context->expecting_length(item->max_length)) 
 
  769             String str(buff,0, system_charset_info);
 
  772             DBUG_PRINT(
"info", (
"value \"%s\"", 
 
  773                                 string_item->val_str(&str)->ptr()));
 
  776             q.value_type= Item::STRING_ITEM;
 
  777             curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);      
 
  778             if (! context->expecting_no_field_result())
 
  781               context->expect_only(Item::FIELD_ITEM);
 
  782               context->expect_only_field_result(STRING_RESULT);
 
  783               context->expect_collation(item->collation.collation);
 
  784               context->expect_length(item->max_length);
 
  789               context->expect_only(Item::FUNC_ITEM);
 
  790               context->expect(Item::COND_ITEM);
 
  791               context->expect_no_length();
 
  793               if (!context->expecting_collation(item->collation.collation))
 
  795                 DBUG_PRINT(
"info", (
"Found non-matching collation %s",  
 
  796                                     item->collation.collation->name));
 
  797                 context->supported= FALSE;
 
  802             context->supported= FALSE;
 
  805           DBUG_PRINT(
"info", (
"INT_ITEM"));
 
  806           if (context->expecting(Item::INT_ITEM)) 
 
  808             DBUG_PRINT(
"info", (
"value %ld",
 
  811             q.value_type= Item::INT_ITEM;
 
  812             curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  813             if (! context->expecting_no_field_result()) 
 
  816               context->expect_only(Item::FIELD_ITEM);
 
  817               context->expect_only_field_result(INT_RESULT);
 
  818               context->expect_field_result(REAL_RESULT);
 
  819               context->expect_field_result(DECIMAL_RESULT);
 
  824               context->expect_only(Item::FUNC_ITEM);
 
  825               context->expect(Item::COND_ITEM);
 
  829             context->supported= FALSE;
 
  831         case Item::REAL_ITEM:
 
  832           DBUG_PRINT(
"info", (
"REAL_ITEM"));
 
  833           if (context->expecting(Item::REAL_ITEM)) 
 
  835             DBUG_PRINT(
"info", (
"value %f", ((
Item_float*) item)->value));
 
  837             q.value_type= Item::REAL_ITEM;
 
  838             curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  839             if (! context->expecting_no_field_result()) 
 
  842               context->expect_only(Item::FIELD_ITEM);
 
  843               context->expect_only_field_result(REAL_RESULT);
 
  848               context->expect_only(Item::FUNC_ITEM);
 
  849               context->expect(Item::COND_ITEM);
 
  853             context->supported= FALSE;
 
  855         case Item::VARBIN_ITEM:
 
  856           DBUG_PRINT(
"info", (
"VARBIN_ITEM"));
 
  857           if (context->expecting(Item::VARBIN_ITEM)) 
 
  860             q.value_type= Item::VARBIN_ITEM;
 
  861             curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);      
 
  862             if (! context->expecting_no_field_result())
 
  865               context->expect_only(Item::FIELD_ITEM);
 
  866               context->expect_only_field_result(STRING_RESULT);
 
  871               context->expect_only(Item::FUNC_ITEM);
 
  872               context->expect(Item::COND_ITEM);
 
  876             context->supported= FALSE;
 
  878         case Item::DECIMAL_ITEM:
 
  879           DBUG_PRINT(
"info", (
"DECIMAL_ITEM"));
 
  880           if (context->expecting(Item::DECIMAL_ITEM)) 
 
  882             DBUG_PRINT(
"info", (
"value %f",
 
  885             q.value_type= Item::DECIMAL_ITEM;
 
  886             curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  887             if (! context->expecting_no_field_result()) 
 
  890               context->expect_only(Item::FIELD_ITEM);
 
  891               context->expect_only_field_result(REAL_RESULT);
 
  892               context->expect_field_result(DECIMAL_RESULT);
 
  897               context->expect_only(Item::FUNC_ITEM);
 
  898               context->expect(Item::COND_ITEM);
 
  902             context->supported= FALSE;
 
  904         case Item::COND_ITEM:
 
  908           if (context->expecting(Item::COND_ITEM))
 
  910             switch (cond_item->functype()) {
 
  911             case Item_func::COND_AND_FUNC:
 
  912               DBUG_PRINT(
"info", (
"COND_AND_FUNC"));
 
  913               curr_cond->ndb_item= 
new Ndb_item(cond_item->functype(),
 
  916             case Item_func::COND_OR_FUNC:
 
  917               DBUG_PRINT(
"info", (
"COND_OR_FUNC"));
 
  918               curr_cond->ndb_item= 
new Ndb_item(cond_item->functype(),
 
  922               DBUG_PRINT(
"info", (
"COND_ITEM %d", cond_item->functype()));
 
  923               context->supported= FALSE;
 
  930             context->supported= FALSE;          
 
  934         case Item::CACHE_ITEM:
 
  936           DBUG_PRINT(
"info", (
"CACHE_ITEM"));
 
  938           DBUG_PRINT(
"info", (
"result type %d", cache_item->result_type()));
 
  942           switch(cache_item->result_type())
 
  945             DBUG_PRINT(
"info", (
"INT_RESULT"));
 
  946             if (context->expecting(Item::INT_ITEM)) 
 
  948               DBUG_PRINT(
"info", (
"value %ld",
 
  951               q.value_type= Item::INT_ITEM;
 
  952               curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  953               if (! context->expecting_no_field_result()) 
 
  956                 context->expect_only(Item::FIELD_ITEM);
 
  957                 context->expect_only_field_result(INT_RESULT);
 
  958                 context->expect_field_result(REAL_RESULT);
 
  959                 context->expect_field_result(DECIMAL_RESULT);
 
  964                 context->expect_only(Item::FUNC_ITEM);
 
  965                 context->expect(Item::COND_ITEM);
 
  969               context->supported= FALSE;
 
  973             DBUG_PRINT(
"info", (
"REAL_RESULT"));
 
  974             if (context->expecting(Item::REAL_ITEM)) 
 
  976               DBUG_PRINT(
"info", (
"value %f", ((
Item_float*) item)->value));
 
  978               q.value_type= Item::REAL_ITEM;
 
  979               curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
  980               if (! context->expecting_no_field_result()) 
 
  983                 context->expect_only(Item::FIELD_ITEM);
 
  984                 context->expect_only_field_result(REAL_RESULT);
 
  989                 context->expect_only(Item::FUNC_ITEM);
 
  990                 context->expect(Item::COND_ITEM);
 
  994               context->supported= FALSE;
 
  998             DBUG_PRINT(
"info", (
"DECIMAL_RESULT"));
 
  999             if (context->expecting(Item::DECIMAL_ITEM)) 
 
 1001               DBUG_PRINT(
"info", (
"value %f",
 
 1004               q.value_type= Item::DECIMAL_ITEM;
 
 1005               curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);
 
 1006               if (! context->expecting_no_field_result()) 
 
 1009                 context->expect_only(Item::FIELD_ITEM);
 
 1010                 context->expect_only_field_result(REAL_RESULT);
 
 1011                 context->expect_field_result(DECIMAL_RESULT);
 
 1016                 context->expect_only(Item::FUNC_ITEM);
 
 1017                 context->expect(Item::COND_ITEM);
 
 1021               context->supported= FALSE;
 
 1025             DBUG_PRINT(
"info", (
"STRING_RESULT")); 
 
 1027             if (context->expecting(Item::STRING_ITEM) &&
 
 1028                 context->expecting_length(item->max_length)) 
 
 1032               String str(buff,0, system_charset_info);
 
 1035               DBUG_PRINT(
"info", (
"value \"%s\"", 
 
 1036                                   string_item->val_str(&str)->ptr()));
 
 1039               q.value_type= Item::STRING_ITEM;
 
 1040               curr_cond->ndb_item= 
new Ndb_item(NDB_VALUE, q, item);      
 
 1041               if (! context->expecting_no_field_result())
 
 1044                 context->expect_only(Item::FIELD_ITEM);
 
 1045                 context->expect_only_field_result(STRING_RESULT);
 
 1046                 context->expect_collation(item->collation.collation);
 
 1047                 context->expect_length(item->max_length);
 
 1052                 context->expect_only(Item::FUNC_ITEM);
 
 1053                 context->expect(Item::COND_ITEM);
 
 1054                 context->expect_no_length();
 
 1056                 if (!context->expecting_collation(item->collation.collation))
 
 1058                   DBUG_PRINT(
"info", (
"Found non-matching collation %s",  
 
 1059                                       item->collation.collation->name));
 
 1060                   context->supported= FALSE;
 
 1065               context->supported= FALSE;
 
 1069             context->supported= FALSE;
 
 1077           DBUG_PRINT(
"info", (
"Found unsupported item of type %d",
 
 1079           context->supported= FALSE;
 
 1083           context->expect_stack.pop();
 
 1085       if (context->supported && context->rewrite_stack)
 
 1088         if (rewrite_context->count == 
 
 1089             rewrite_context->func_item->argument_count())
 
 1092           DBUG_PRINT(
"info", (
"End of condition group"));
 
 1093           prev_cond= curr_cond;
 
 1094           curr_cond= context->cond_ptr= 
new Ndb_cond();
 
 1095           curr_cond->prev= prev_cond;
 
 1096           prev_cond->next= curr_cond;
 
 1097           context->expect_no_length();
 
 1098           curr_cond->ndb_item= 
new Ndb_item(NDB_END_COND);
 
 1100           context->rewrite_stack=  rewrite_context->next;
 
 1101           rewrite_context->next= NULL;
 
 1102           delete(rewrite_context);
 
 1116 ha_ndbcluster_cond::cond_push(
const Item *cond, 
 
 1119   DBUG_ENTER(
"ha_ndbcluster_cond::cond_push");
 
 1121   if (ndb_cond == NULL)
 
 1123     my_errno= HA_ERR_OUT_OF_MEM;
 
 1127     ndb_cond->next= m_cond_stack;
 
 1129     ndb_cond->next= NULL;
 
 1130   m_cond_stack= ndb_cond;
 
 1132   if (serialize_cond(cond, ndb_cond, table, ndb_table))
 
 1147 ha_ndbcluster_cond::cond_pop() 
 
 1152     m_cond_stack= ndb_cond_stack->next;
 
 1153     ndb_cond_stack->next= NULL;
 
 1154     delete ndb_cond_stack;
 
 1162 ha_ndbcluster_cond::cond_clear()
 
 1164   DBUG_ENTER(
"cond_clear");
 
 1165   while (m_cond_stack)
 
 1175   DBUG_ENTER(
"serialize_cond");
 
 1179   context.expect(Item::FUNC_ITEM);
 
 1180   context.expect(Item::COND_ITEM);
 
 1181   item->traverse_cond(&ndb_serialize_cond, (
void *) &context, Item::PREFIX);
 
 1182   DBUG_PRINT(
"info", (
"The pushed condition is %ssupported", (context.supported)?
"":
"not "));
 
 1184   DBUG_RETURN(context.supported);
 
 1188 ha_ndbcluster_cond::build_scan_filter_predicate(
Ndb_cond * &cond, 
 
 1192   DBUG_ENTER(
"build_scan_filter_predicate");  
 
 1193   switch (cond->ndb_item->type) {
 
 1201     switch (cond->ndb_item->argument_count()) {
 
 1203       field= (a->type == NDB_FIELD)? a : NULL;
 
 1206       if (!cond->next->next)
 
 1211       b= cond->next->next->ndb_item;
 
 1212       value= ((a->type == NDB_VALUE) ? a :
 
 1213               (b->type == NDB_VALUE) ? b :
 
 1215       field= ((a->type == NDB_FIELD) ? a :
 
 1216               (b->type == NDB_FIELD) ? b :
 
 1225             Ndb_item::negate(cond->ndb_item->qualification.function_type)
 
 1226             : cond->ndb_item->qualification.function_type) {
 
 1229       if (!value || !field) 
break;
 
 1231       value->save_in_field(field);
 
 1232       DBUG_PRINT(
"info", (
"Generating EQ filter"));
 
 1234                       field->get_field_no(),
 
 1236                       field->pack_length()) == -1)
 
 1238       cond= cond->next->next->next;
 
 1243       if (!value || !field) 
break;
 
 1245       value->save_in_field(field);
 
 1246       DBUG_PRINT(
"info", (
"Generating NE filter"));
 
 1248                       field->get_field_no(),
 
 1250                       field->pack_length()) == -1)
 
 1252       cond= cond->next->next->next;
 
 1257       if (!value || !field) 
break;
 
 1259       value->save_in_field(field);
 
 1262         DBUG_PRINT(
"info", (
"Generating LT filter")); 
 
 1264                         field->get_field_no(),
 
 1266                         field->pack_length()) == -1)
 
 1271         DBUG_PRINT(
"info", (
"Generating GT filter")); 
 
 1273                         field->get_field_no(),
 
 1275                         field->pack_length()) == -1)
 
 1278       cond= cond->next->next->next;
 
 1283       if (!value || !field) 
break;
 
 1285       value->save_in_field(field);
 
 1288         DBUG_PRINT(
"info", (
"Generating LE filter")); 
 
 1290                         field->get_field_no(),
 
 1292                         field->pack_length()) == -1)
 
 1297         DBUG_PRINT(
"info", (
"Generating GE filter")); 
 
 1299                         field->get_field_no(),
 
 1301                         field->pack_length()) == -1)
 
 1304       cond= cond->next->next->next;
 
 1309       if (!value || !field) 
break;
 
 1311       value->save_in_field(field);
 
 1314         DBUG_PRINT(
"info", (
"Generating GE filter")); 
 
 1316                         field->get_field_no(),
 
 1318                         field->pack_length()) == -1)
 
 1323         DBUG_PRINT(
"info", (
"Generating LE filter")); 
 
 1325                         field->get_field_no(),
 
 1327                         field->pack_length()) == -1)
 
 1330       cond= cond->next->next->next;
 
 1335       if (!value || !field) 
break;
 
 1337       value->save_in_field(field);
 
 1340         DBUG_PRINT(
"info", (
"Generating GT filter"));
 
 1342                         field->get_field_no(),
 
 1344                         field->pack_length()) == -1)
 
 1349         DBUG_PRINT(
"info", (
"Generating LT filter"));
 
 1351                         field->get_field_no(),
 
 1353                         field->pack_length()) == -1)
 
 1356       cond= cond->next->next->next;
 
 1361       if (!value || !field) 
break;
 
 1362       if ((value->qualification.value_type != Item::STRING_ITEM) &&
 
 1363           (value->qualification.value_type != Item::VARBIN_ITEM))
 
 1366       value->save_in_field(field);
 
 1367       DBUG_PRINT(
"info", (
"Generating LIKE filter: like(%d,%s,%d)", 
 
 1368                           field->get_field_no(), value->get_val(), 
 
 1369                           value->pack_length()));
 
 1371                       field->get_field_no(),
 
 1373                       value->pack_length()) == -1)
 
 1375       cond= cond->next->next->next;
 
 1378     case NDB_NOTLIKE_FUNC:
 
 1380       if (!value || !field) 
break;
 
 1381       if ((value->qualification.value_type != Item::STRING_ITEM) &&
 
 1382           (value->qualification.value_type != Item::VARBIN_ITEM))
 
 1385       value->save_in_field(field);
 
 1386       DBUG_PRINT(
"info", (
"Generating NOTLIKE filter: notlike(%d,%s,%d)", 
 
 1387                           field->get_field_no(), value->get_val(), 
 
 1388                           value->pack_length()));
 
 1390                       field->get_field_no(),
 
 1392                       value->pack_length()) == -1)
 
 1394       cond= cond->next->next->next;
 
 1397     case NDB_ISNULL_FUNC:
 
 1400       DBUG_PRINT(
"info", (
"Generating ISNULL filter"));
 
 1401       if (filter->
isnull(field->get_field_no()) == -1)
 
 1403       cond= cond->next->next;
 
 1405     case NDB_ISNOTNULL_FUNC:
 
 1409       DBUG_PRINT(
"info", (
"Generating ISNOTNULL filter"));
 
 1410       if (filter->
isnotnull(field->get_field_no()) == -1)
 
 1412       cond= cond->next->next;
 
 1423   DBUG_PRINT(
"info", (
"Found illegal condition"));
 
 1429 ha_ndbcluster_cond::build_scan_filter_group(
Ndb_cond* &cond, 
 
 1433   bool negated= FALSE;
 
 1434   DBUG_ENTER(
"build_scan_filter_group");
 
 1440     switch (cond->ndb_item->type) {
 
 1443       switch (cond->ndb_item->qualification.function_type) {
 
 1444       case NDB_COND_AND_FUNC:
 
 1447         DBUG_PRINT(
"info", (
"Generating %s group %u", (negated)?
"NAND":
"AND",
 
 1456       case NDB_COND_OR_FUNC:
 
 1459         DBUG_PRINT(
"info", (
"Generating %s group %u", (negated)?
"NOR":
"OR",
 
 1470         DBUG_PRINT(
"info", (
"Generating negated query"));
 
 1476         if (build_scan_filter_predicate(cond, filter, negated))
 
 1484       DBUG_PRINT(
"info", (
"End of group %u", level));
 
 1486       if (cond) cond= cond->next;
 
 1487       if (filter->
end() == -1)
 
 1494       DBUG_PRINT(
"info", (
"Illegal scan filter"));
 
 1497   }  
while (level > 0 || negated);
 
 1506   bool simple_cond= TRUE;
 
 1507   DBUG_ENTER(
"build_scan_filter");  
 
 1509     switch (cond->ndb_item->type) {
 
 1511       switch (cond->ndb_item->qualification.function_type) {
 
 1512       case NDB_COND_AND_FUNC:
 
 1513       case NDB_COND_OR_FUNC:
 
 1523   if (simple_cond && filter->
begin() == -1)
 
 1525   if (build_scan_filter_group(cond, filter))
 
 1527   if (simple_cond && filter->
end() == -1)
 
 1537   DBUG_ENTER(
"generate_scan_filter");
 
 1543     int ret= generate_scan_filter_from_cond(filter);
 
 1547       if (err.
code == NdbScanFilter::FilterTooLarge)
 
 1550         DBUG_PRINT(
"info", (
"%s", err.
message));
 
 1551         push_warning(current_thd, Sql_condition::WARN_LEVEL_WARN,
 
 1557     else if (options!=NULL)
 
 1559       options->interpretedCode= code;
 
 1560       options->optionsPresent|= NdbScanOperation::ScanOptions::SO_INTERPRETED;
 
 1565     DBUG_PRINT(
"info", (
"Empty stack"));
 
 1573 ha_ndbcluster_cond::generate_scan_filter_from_cond(
NdbScanFilter& filter)
 
 1575   bool multiple_cond= FALSE;
 
 1576   DBUG_ENTER(
"generate_scan_filter_from_cond");
 
 1579   if (m_cond_stack->next) 
 
 1581     multiple_cond= TRUE;
 
 1582     if (filter.
begin() == -1)
 
 1591     if (build_scan_filter(cond, &filter))
 
 1593       DBUG_PRINT(
"info", (
"build_scan_filter failed"));
 
 1597   if (multiple_cond && filter.
end() == -1)
 
 1613                                                       const KEY* key_info, 
 
 1618   DBUG_ENTER(
"generate_scan_filter_from_key");
 
 1622     DBUG_PRINT(
"info", (
"key parts:%u length:%u",
 
 1624     const key_range* keylist[2]={ start_key, end_key };
 
 1625     for (uint j=0; j <= 1; j++)
 
 1631         sprintf(buf, 
"key range %u: none", j);
 
 1635         sprintf(buf, 
"key range %u: flag:%u part", j, key->flag);
 
 1637         const uchar* ptr=key->key;
 
 1640           sprintf(buf+strlen(buf), 
" %u:", 
i);
 
 1641           for (uint k=0; k < key_part->store_length; k++)
 
 1643             sprintf(buf+strlen(buf), 
" %02x", ptr[k]);
 
 1645           ptr+=key_part->store_length;
 
 1646           if (ptr - key->key >= (ptrdiff_t)key->length)
 
 1658       DBUG_PRINT(
"info", (
"%s", buf));
 
 1674     if (start_key != 0 &&
 
 1675         start_key->flag == HA_READ_AFTER_KEY &&
 
 1680       if (key_part->null_bit != 0) 
 
 1682         const uchar* ptr= start_key->key;
 
 1685           DBUG_PRINT(
"info", (
"Generating ISNOTNULL filter for nullable %s",
 
 1686                               key_part->field->field_name));
 
 1687           if (filter.
isnotnull(key_part->fieldnr-1) == -1)
 
 1702     if (start_key != 0 &&
 
 1703         start_key->flag == HA_READ_KEY_EXACT &&
 
 1705         end_key->flag == HA_READ_AFTER_KEY &&
 
 1706         start_key->length == end_key->length &&
 
 1707         memcmp(start_key->key, end_key->key, start_key->length) == 0)
 
 1710       const uchar* ptr=start_key->key;
 
 1713         const Field* field=key_part->field;
 
 1714         if (key_part->null_bit) 
 
 1718             DBUG_PRINT(
"info", (
"Generating ISNULL filter for nullable %s",
 
 1719                                 field->field_name));
 
 1720             if (filter.
isnull(key_part->fieldnr-1) == -1)
 
 1725             DBUG_PRINT(
"info", (
"Generating EQ filter for nullable %s",
 
 1726                                 field->field_name));
 
 1728                            key_part->fieldnr-1,
 
 1730                            field->pack_length()) == -1)
 
 1736           DBUG_PRINT(
"info", (
"Generating EQ filter for non-nullable %s",
 
 1737                               field->field_name));
 
 1739                          key_part->fieldnr-1,
 
 1741                          field->pack_length()) == -1)
 
 1744         ptr+=key_part->store_length;
 
 1745         if (ptr - start_key->key >= (ptrdiff_t)start_key->length)
 
 1754     DBUG_PRINT(
"info", (
"Unknown hash index scan"));
 
 1762       (res= generate_scan_filter_from_cond(filter)))
 
 1765   if (filter.
end() == -1)
 
 1770     options->interpretedCode= code;
 
 1771     options->optionsPresent|= NdbScanOperation::ScanOptions::SO_INTERPRETED;