diff options
-rw-r--r-- | include/mysql_com.h | 2 | ||||
-rw-r--r-- | mysql-test/r/ndb_condition_pushdown.result | 32 | ||||
-rw-r--r-- | mysql-test/t/ndb_condition_pushdown.test | 28 | ||||
-rw-r--r-- | ndb/src/ndbapi/NdbDictionaryImpl.hpp | 4 | ||||
-rw-r--r-- | sql/filesort.cc | 3 | ||||
-rw-r--r-- | sql/ha_ndbcluster.cc | 1003 | ||||
-rw-r--r-- | sql/ha_ndbcluster.h | 222 | ||||
-rw-r--r-- | sql/handler.h | 11 | ||||
-rw-r--r-- | sql/item.h | 13 | ||||
-rw-r--r-- | sql/item_cmpfunc.cc | 24 | ||||
-rw-r--r-- | sql/item_cmpfunc.h | 3 | ||||
-rw-r--r-- | sql/item_func.cc | 26 | ||||
-rw-r--r-- | sql/item_func.h | 3 | ||||
-rw-r--r-- | sql/mysqld.cc | 7 | ||||
-rw-r--r-- | sql/set_var.cc | 6 | ||||
-rw-r--r-- | sql/sql_class.h | 1 | ||||
-rw-r--r-- | sql/sql_select.cc | 3 |
17 files changed, 1355 insertions, 36 deletions
diff --git a/include/mysql_com.h b/include/mysql_com.h index 59b2ee743ec..8d4340f2cb6 100644 --- a/include/mysql_com.h +++ b/include/mysql_com.h @@ -341,7 +341,7 @@ struct rand_struct { /* The following is for user defined functions */ -enum Item_result {STRING_RESULT, REAL_RESULT, INT_RESULT, ROW_RESULT}; +enum Item_result {STRING_RESULT= 0, REAL_RESULT, INT_RESULT, ROW_RESULT}; typedef struct st_udf_args { diff --git a/mysql-test/r/ndb_condition_pushdown.result b/mysql-test/r/ndb_condition_pushdown.result new file mode 100644 index 00000000000..f87661daae9 --- /dev/null +++ b/mysql-test/r/ndb_condition_pushdown.result @@ -0,0 +1,32 @@ +DROP TABLE IF EXISTS t1,t2; +CREATE TABLE t1 (pk1 int unsigned NOT NULL PRIMARY KEY, attr1 int unsigned NOT NULL, attr2 int unsigned, attr3 VARCHAR(10) ) ENGINE=ndbcluster; +insert into t1 values (0,0,0, "a"),(1,1,1,"b"),(2,2,NULL,NULL),(3,3,3,"d"),(4,4,4,"e"),(5,5,5,"f"); +CREATE TABLE t2 (pk1 int unsigned NOT NULL PRIMARY KEY, attr1 int unsigned NOT NULL, attr2 bigint unsigned, attr3 tinyint unsigned, attr4 VARCHAR(10) ) ENGINE=ndbcluster; +insert into t2 values (0,0,0,0,"a"),(1,1,9223372036854775803,1,"b"),(2,2,9223372036854775804,2,"c"),(3,3,9223372036854775805,3,"d"),(4,4,9223372036854775806,4,"e"),(5,5,9223372036854775807,5,"f"); +set @old_ndbcpd = @@session.ndb_condition_pushdown; +set ndb_condition_pushdown = off; +select * from t1 where attr3 is null or attr1 > 2 and pk1= 3 order by pk1; +pk1 attr1 attr2 attr3 +2 2 NULL NULL +3 3 3 d +select * from t2 where attr2 > 9223372036854775803 and attr3 != 3 order by pk1; +pk1 attr1 attr2 attr3 attr4 +2 2 9223372036854775804 2 c +4 4 9223372036854775806 4 e +5 5 9223372036854775807 5 f +select * from t1,t2 where t1.attr1 > 1 and t1.attr2 = t2.attr2 and t2.attr1 < 5 order by t1.pk1; +pk1 attr1 attr2 attr3 pk1 attr1 attr2 attr3 attr4 +set ndb_condition_pushdown = on; +select * from t1 where attr3 is null or attr1 > 2 and pk1= 3 order by pk1; +pk1 attr1 attr2 attr3 +2 2 NULL NULL +3 3 3 d +select * from t2 where attr2 > 9223372036854775803 and attr3 != 3 order by pk1; +pk1 attr1 attr2 attr3 attr4 +2 2 9223372036854775804 2 c +4 4 9223372036854775806 4 e +5 5 9223372036854775807 5 f +select * from t1,t2 where t1.attr1 > 1 and t1.attr2 = t2.attr2 and t2.attr1 < 5 order by t1.pk1; +pk1 attr1 attr2 attr3 pk1 attr1 attr2 attr3 attr4 +set ndb_condition_pushdown = @old_ndbcpd; +DROP TABLE t1,t2; diff --git a/mysql-test/t/ndb_condition_pushdown.test b/mysql-test/t/ndb_condition_pushdown.test new file mode 100644 index 00000000000..dcf42773f4c --- /dev/null +++ b/mysql-test/t/ndb_condition_pushdown.test @@ -0,0 +1,28 @@ +-- source include/have_ndb.inc + +--disable_warnings +DROP TABLE IF EXISTS t1,t2; +--enable_warnings + +# +# Test of condition pushdown to storage engine +# +CREATE TABLE t1 (pk1 int unsigned NOT NULL PRIMARY KEY, attr1 int unsigned NOT NULL, attr2 int unsigned, attr3 VARCHAR(10) ) ENGINE=ndbcluster; + +insert into t1 values (0,0,0, "a"),(1,1,1,"b"),(2,2,NULL,NULL),(3,3,3,"d"),(4,4,4,"e"),(5,5,5,"f"); + +CREATE TABLE t2 (pk1 int unsigned NOT NULL PRIMARY KEY, attr1 int unsigned NOT NULL, attr2 bigint unsigned, attr3 tinyint unsigned, attr4 VARCHAR(10) ) ENGINE=ndbcluster; + +insert into t2 values (0,0,0,0,"a"),(1,1,9223372036854775803,1,"b"),(2,2,9223372036854775804,2,"c"),(3,3,9223372036854775805,3,"d"),(4,4,9223372036854775806,4,"e"),(5,5,9223372036854775807,5,"f"); + +set @old_ndbcpd = @@session.ndb_condition_pushdown; +set ndb_condition_pushdown = off; +select * from t1 where attr3 is null or attr1 > 2 and pk1= 3 order by pk1; +select * from t2 where attr2 > 9223372036854775803 and attr3 != 3 order by pk1; +select * from t1,t2 where t1.attr1 > 1 and t1.attr2 = t2.attr2 and t2.attr1 < 5 order by t1.pk1; +set ndb_condition_pushdown = on; +select * from t1 where attr3 is null or attr1 > 2 and pk1= 3 order by pk1; +select * from t2 where attr2 > 9223372036854775803 and attr3 != 3 order by pk1; +select * from t1,t2 where t1.attr1 > 1 and t1.attr2 = t2.attr2 and t2.attr1 < 5 order by t1.pk1; +set ndb_condition_pushdown = @old_ndbcpd; +DROP TABLE t1,t2; diff --git a/ndb/src/ndbapi/NdbDictionaryImpl.hpp b/ndb/src/ndbapi/NdbDictionaryImpl.hpp index 2d20a0a52db..b42c5416d83 100644 --- a/ndb/src/ndbapi/NdbDictionaryImpl.hpp +++ b/ndb/src/ndbapi/NdbDictionaryImpl.hpp @@ -547,7 +547,7 @@ NdbTableImpl::getColumn(const char * name){ do { if(hashValue == (tmp & 0xFFFE)){ NdbColumnImpl* col = cols[tmp >> 16]; - if(strcmp(name, col->m_name.c_str()) == 0){ + if(strncmp(name, col->m_name.c_str(), NDB_MAX_ATTR_NAME_SIZE-1) == 0){ return col; } } @@ -565,7 +565,7 @@ NdbTableImpl::getColumn(const char * name){ } else { for(Uint32 i = 0; i<sz; i++){ NdbColumnImpl* col = * cols++; - if(col != 0 && strcmp(name, col->m_name.c_str()) == 0) + if(col != 0 && strncmp(name, col->m_name.c_str(), NDB_MAX_ATTR_NAME_SIZE-1) == 0) return col; } } diff --git a/sql/filesort.cc b/sql/filesort.cc index 1665358dbf0..c4dbb82d8a5 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -420,9 +420,6 @@ static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select, next_pos=ref_pos; if (! indexfile && ! quick_select) { - file->reset(); // QQ; Shouldn't be needed - if (sort_form->key_read) // QQ Can be removed after the reset - file->extra(HA_EXTRA_KEYREAD); // QQ is removed next_pos=(byte*) 0; /* Find records in sequence */ file->ha_rnd_init(1); file->extra_opt(HA_EXTRA_CACHE, diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index f2b159353e3..c12055a1d63 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -244,9 +244,212 @@ int execute_no_commit_ie(ha_ndbcluster *h, NdbTransaction *trans) } /* - Place holder for ha_ndbcluster thread specific data + CPDH condition storage support */ +Ndb_item::Ndb_item(NDB_ITEM_TYPE item_type, + NDB_ITEM_QUALIFICATION item_qualification, + const Item *item_value) + : type(item_type), qualification(item_qualification) +{ + switch(item_type) { + case(NDB_VALUE): + { + switch(item_qualification.value_type) { + case(Item::STRING_ITEM): { + Ndb_item_string_value *string_value = new Ndb_item_string_value(); + Item_string *string_item= (Item_string *)item_value; + string_value->s= string_item->str_value; + string_value->c= string_item->collation.collation; + value.string_value= string_value; + break; + } + case(Item::INT_ITEM): { + value.int_value= ((Item_int *)item_value)->val_int(); + break; + } + case(Item::REAL_ITEM): { + value.real_value= ((Item_real *)item_value)->val_real(); + break; + } + case(Item::NULL_ITEM): + break; + case(Item::VARBIN_ITEM): { + Ndb_item_string_value *string_value = new Ndb_item_string_value(); + Item_bin_string *varbin_item= (Item_bin_string *)item_value; + string_value->s= varbin_item->str_value; + string_value->c= varbin_item->collation.collation; + value.string_value= string_value; + break; + } + default: + break; + } + } + break; + case(NDB_FIELD): { + NDB_ITEM_FIELD_VALUE *field_value= new NDB_ITEM_FIELD_VALUE(); + Item_field *field_item= (Item_field *) item_value; + field_value->field= field_item->field; + field_value->column_no= -1; // Will be fetched at scan filter generation + value.field_value= field_value; + break; + } + case(NDB_FUNCTION): + case(NDB_END_COND): + break; + } +} + +Ndb_item::Ndb_item(longlong int_value) : type(NDB_VALUE) +{ + qualification.value_type= Item::INT_ITEM; + value.int_value= int_value; +} + +Ndb_item::Ndb_item(double real_value) : type(NDB_VALUE) +{ + qualification.value_type= Item::REAL_ITEM; + value.real_value= real_value; +} + +Ndb_item::Ndb_item(NDB_ITEM_TYPE item_type): type(item_type) {} + +Ndb_item::Ndb_item(Field *field, int column_no) : type(NDB_FIELD) +{ + NDB_ITEM_FIELD_VALUE *field_value= new NDB_ITEM_FIELD_VALUE(); + qualification.field_type= field->type(); + field_value->field= field; + field_value->column_no= column_no; + value.field_value= field_value; +} +Ndb_item::Ndb_item(Item_func::Functype func_type) : type(NDB_FUNCTION) +{ + qualification.function_type= func_type; +} + +Ndb_item::~Ndb_item() +{ + if (type == NDB_VALUE && + (qualification.value_type == Item::STRING_ITEM || + qualification.value_type == Item::VARBIN_ITEM)) + { + delete value.string_value; + value.string_value= NULL; + } + else if (type == NDB_FIELD) + { + delete value.field_value; + value.field_value= NULL; + } +} + +void Ndb_item::print(String* str) +{ + switch(type) { + case(NDB_VALUE): + str->append("[#NDB_VALUE "); + switch(qualification.value_type) { + case (Item::INT_ITEM): { + String tmp; + tmp.set(value.int_value, &my_charset_bin); + str->append(tmp); + break; + } + case (Item::REAL_ITEM): { + String tmp; + tmp.set(value.real_value, 4 , &my_charset_bin); + str->append(tmp); + break; + } + case (Item::STRING_ITEM): { + str->append(value.string_value->s.ptr()); + break; + } + case (Item::VARBIN_ITEM): { + str->append(value.string_value->s.ptr()); + break; + } + case (Item::NULL_ITEM): + str->append("NULL"); + break; + default: + str->append("ILLEGAL VALUE"); + } + str->append("]"); + break; + case(NDB_FIELD): + str->append("[#NDB_FIELD "); + str->append(value.field_value->field->field_name); + str->append("]"); + break; + case(NDB_FUNCTION): + str->append("[#NDB_FUNCTION "); + switch(qualification.function_type) { + case(Item_func::UNKNOWN_FUNC): { + str->append("UNKNOWN]"); + break; + } + case(Item_func::EQ_FUNC): { + str->append("=]"); + break; + } + case(Item_func::NE_FUNC): { + str->append("!=]"); + break; + } + case(Item_func::LT_FUNC): { + str->append("<]"); + break; + } + case(Item_func::LE_FUNC): { + str->append("<=]"); + break; + } + case(Item_func::GE_FUNC): { + str->append(">=]"); + break; + } + case(Item_func::GT_FUNC): { + str->append(">]"); + break; + } + case(Item_func::LIKE_FUNC): { + str->append("like]"); + break; + } + case(Item_func::NOTLIKE_FUNC): { + str->append("notlike]"); + break; + } + case(Item_func::ISNULL_FUNC): { + str->append("isnull]"); + break; + } + case(Item_func::ISNOTNULL_FUNC): { + str->append("isnotnull]"); + break; + } + case(Item_func::COND_AND_FUNC): { + str->append("and]"); + break; + } + case(Item_func::COND_OR_FUNC): { + str->append("or]"); + break; + } + default: + str->append("UNSUPPORTED]"); + } + break; + case(NDB_END_COND): + str->append("[#NDB_END_COND]"); + } +} + +/* + Place holder for ha_ndbcluster thread specific data +*/ Thd_ndb::Thd_ndb() { ndb= new Ndb(g_ndb_cluster_connection, ""); @@ -1761,6 +1964,9 @@ int ha_ndbcluster::ordered_index_scan(const key_range *start_key, if (res) DBUG_RETURN(res); } + + if (!restart && generate_scan_filter(m_cond_stack, op)) + DBUG_RETURN(ndb_err(trans)); if (!restart && (res= define_read_attrs(buf, op))) { @@ -1882,7 +2088,8 @@ int ha_ndbcluster::full_table_scan(byte *buf) op->readTuples(lm, 0, parallelism)) ERR_RETURN(trans->getNdbError()); m_active_cursor= op; - + if (generate_scan_filter(m_cond_stack, op)) + DBUG_RETURN(ndb_err(trans)); if((res= define_read_attrs(buf, op))) DBUG_RETURN(res); @@ -2333,7 +2540,7 @@ void ha_ndbcluster::unpack_record(byte* buf) DBUG_PRINT("hidden", ("%d: %s \"%llu\"", hidden_no, hidden_col->getName(), rec->u_64_value())); } - print_results(); + //print_results(); #endif DBUG_VOID_RETURN; } @@ -2853,6 +3060,8 @@ int ha_ndbcluster::extra(enum ha_extra_function operation) break; case HA_EXTRA_RESET: /* Reset database to after open */ DBUG_PRINT("info", ("HA_EXTRA_RESET")); + DBUG_PRINT("info", ("Clearing condition stack")); + cond_clear(); break; case HA_EXTRA_CACHE: /* Cash record in HA_rrnd() */ DBUG_PRINT("info", ("HA_EXTRA_CACHE")); @@ -3046,14 +3255,6 @@ int ha_ndbcluster::extra_opt(enum ha_extra_function operation, ulong cache_size) } -int ha_ndbcluster::reset() -{ - DBUG_ENTER("reset"); - // Reset what? - DBUG_RETURN(1); -} - - const char **ha_ndbcluster::bas_ext() const { static const char *ext[]= { ha_ndb_ext, NullS }; return ext; } @@ -4018,7 +4219,8 @@ ha_ndbcluster::ha_ndbcluster(TABLE *table_arg): m_autoincrement_prefetch(32), m_transaction_on(TRUE), m_use_local_query_cache(FALSE), - m_multi_cursor(NULL) + m_multi_cursor(NULL), + m_cond_stack(NULL) { int i; @@ -4064,6 +4266,10 @@ ha_ndbcluster::~ha_ndbcluster() } DBUG_ASSERT(m_active_trans == NULL); + // Discard the condition stack + DBUG_PRINT("info", ("Clearing condition stack")); + cond_clear(); + DBUG_VOID_RETURN; } @@ -5145,6 +5351,7 @@ ha_ndbcluster::read_multi_range_first(KEY_MULTI_RANGE **found_range_p, } else if ((scanOp= m_active_trans->getNdbIndexScanOperation(idx, tab)) &&!scanOp->readTuples(lm, 0, parallelism, sorted, false, true) + &&!generate_scan_filter(m_cond_stack, scanOp) &&!define_read_attrs(end_of_buffer-reclength, scanOp)) { m_multi_cursor= scanOp; @@ -5156,6 +5363,7 @@ ha_ndbcluster::read_multi_range_first(KEY_MULTI_RANGE **found_range_p, m_active_trans->getNdbError()); } } + const key_range *keys[2]= { &multi_range_curr->start_key, &multi_range_curr->end_key }; if ((res= set_bounds(scanOp, keys, multi_range_curr-ranges))) @@ -5368,6 +5576,777 @@ ha_ndbcluster::setup_recattr(const NdbRecAttr* curr) DBUG_RETURN(0); } +/* + Condition pushdown +*/ +const +COND* +ha_ndbcluster::cond_push(const COND *cond) +{ + THD *thd= current_thd; + Ndb_cond_stack *ndb_cond = new Ndb_cond_stack(); + DBUG_ENTER("cond_push"); + + if (thd->variables.ndb_condition_pushdown) + { + DBUG_EXECUTE("where",print_where((COND *)cond, m_tabname);); + if (m_cond_stack) + ndb_cond->next= m_cond_stack; + else + ndb_cond->next= NULL; + m_cond_stack= ndb_cond; + + if (serialize_cond(cond, ndb_cond)) + { + DBUG_RETURN(NULL); + } + else + { + cond_pop(); + } + } + DBUG_RETURN(cond); +} + +inline +void +ha_ndbcluster::cond_pop() +{ + Ndb_cond_stack *ndb_cond_stack= m_cond_stack; + if (ndb_cond_stack) + { + m_cond_stack= ndb_cond_stack->next; + delete ndb_cond_stack; + } +}; + +void +ha_ndbcluster::cond_clear() +{ + DBUG_ENTER("cond_clear"); + while (m_cond_stack) + cond_pop(); + + DBUG_VOID_RETURN; +} + +void ndb_serialize_cond(const Item *item, void *arg) +{ + Ndb_cond_traverse_context *context= (Ndb_cond_traverse_context *) arg; + DBUG_ENTER("ndb_serialize_cond"); + + if (*context->supported_ptr) + { + Ndb_cond_stack *ndb_stack= context->stack_ptr; + Ndb_cond *prev_cond= context->cond_ptr; + Ndb_cond *curr_cond= context->cond_ptr= new Ndb_cond(); + if (!ndb_stack->ndb_cond) + ndb_stack->ndb_cond= curr_cond; + curr_cond->prev= prev_cond; + if (prev_cond) prev_cond->next= curr_cond; + + if (!item) + // End marker for condition group + curr_cond->ndb_item= new Ndb_item(NDB_END_COND); + else + switch(item->type()) { + case(Item::FIELD_ITEM): { + Item_field *field_item= (Item_field *) item; + Field *field= field_item->field; + enum_field_types type= field->type(); + /* + Check that the field is part of the table of the handler + instance and that we expect a field with of this result type. + */ + if (context->table == field->table) + { + const NDBTAB *tab= (const NDBTAB *) context->ndb_table; + DBUG_PRINT("info", ("FIELD_ITEM")); + DBUG_PRINT("info", ("table %s", tab->getName())); + DBUG_PRINT("info", ("column %s", field->field_name)); + DBUG_PRINT("info", ("result type %d", field->result_type())); + + // Check that we are expecting a field and with the correct + // result type + if(context->expecting(Item::FIELD_ITEM) && + ((type == MYSQL_TYPE_DATE || type == MYSQL_TYPE_YEAR) + ? context->expecting_field_result(STRING_RESULT) : true) && + context->expecting_field_result(field->result_type())) + { + const NDBCOL *col= tab->getColumn(field->field_name); + DBUG_ASSERT(col); + curr_cond->ndb_item= new Ndb_item(field, col->getColumnNo()); + context->dont_expect(Item::FIELD_ITEM); + context->expect_no_field_result(); + if (context->expect_mask) + { + // We have not seen second argument yet + if (type == MYSQL_TYPE_DATE || type == MYSQL_TYPE_YEAR) + context->expect_only(Item::STRING_ITEM); + else + switch(field->result_type()) { + case(STRING_RESULT): + context->expect_only(Item::STRING_ITEM); + break; + case(REAL_RESULT): + context->expect_only(Item::REAL_ITEM); + break; + case(INT_RESULT): + context->expect_only(Item::INT_ITEM); + break; + default: + break; + } + } + break; + } + } + *context->supported_ptr= FALSE; + break; + } + case(Item::FUNC_ITEM): { + Item_func *func_item= (Item_func *) item; + + context->expect_nothing(); + switch(func_item->functype()) { + case(Item_func::UNKNOWN_FUNC): { + DBUG_PRINT("info", ("UNKNOWN_FUNC")); + DBUG_PRINT("info", ("value %d", func_item->val_int())); + break; + } + case(Item_func::EQ_FUNC): { + DBUG_PRINT("info", ("EQ_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + //context->expect(Item::STRING_ITEM); + context->expect(Item::INT_ITEM); + context->expect(Item::REAL_ITEM); + context->expect(Item::VARBIN_ITEM); + context->expect(Item::FIELD_ITEM); + //context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + case(Item_func::NE_FUNC): { + DBUG_PRINT("info", ("NE_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + //context->expect(Item::STRING_ITEM); + context->expect(Item::INT_ITEM); + context->expect(Item::REAL_ITEM); + context->expect(Item::VARBIN_ITEM); + context->expect(Item::FIELD_ITEM); + //context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + case(Item_func::LT_FUNC): { + DBUG_PRINT("info", ("LT_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + //context->expect(Item::STRING_ITEM); + context->expect(Item::INT_ITEM); + context->expect(Item::REAL_ITEM); + context->expect(Item::VARBIN_ITEM); + context->expect(Item::FIELD_ITEM); + //context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + case(Item_func::LE_FUNC): { + DBUG_PRINT("info", ("LE_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + //context->expect(Item::STRING_ITEM); + context->expect(Item::INT_ITEM); + context->expect(Item::REAL_ITEM); + context->expect(Item::VARBIN_ITEM); + context->expect(Item::FIELD_ITEM); + //context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + case(Item_func::GE_FUNC): { + DBUG_PRINT("info", ("GE_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + //context->expect(Item::STRING_ITEM); + context->expect(Item::INT_ITEM); + context->expect(Item::REAL_ITEM); + context->expect(Item::VARBIN_ITEM); + context->expect(Item::FIELD_ITEM); + //context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + case(Item_func::GT_FUNC): { + DBUG_PRINT("info", ("GT_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + //context->expect(Item::STRING_ITEM); + context->expect(Item::REAL_ITEM); + context->expect(Item::INT_ITEM); + context->expect(Item::VARBIN_ITEM); + context->expect(Item::FIELD_ITEM); + //context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + case(Item_func::LIKE_FUNC): { + DBUG_PRINT("info", ("LIKE_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + context->expect(Item::STRING_ITEM); + *context->supported_ptr= FALSE; // Currently not supported + break; + } + case(Item_func::NOTLIKE_FUNC): { + DBUG_PRINT("info", ("NOTLIKE_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + context->expect(Item::STRING_ITEM); + *context->supported_ptr= FALSE; // Currently not supported + break; + } + case(Item_func::ISNULL_FUNC): { + DBUG_PRINT("info", ("ISNULL_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + context->expect(Item::FIELD_ITEM); + context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + case(Item_func::ISNOTNULL_FUNC): { + DBUG_PRINT("info", ("ISNOTNULL_FUNC")); + curr_cond->ndb_item= new Ndb_item(func_item->functype()); + context->expect(Item::FIELD_ITEM); + context->expect_field_result(STRING_RESULT); + context->expect_field_result(REAL_RESULT); + context->expect_field_result(INT_RESULT); + break; + } + default: { + DBUG_PRINT("info", ("Found func_item of type %d", + func_item->functype())); + *context->supported_ptr= FALSE; + } + } + break; + } + case(Item::STRING_ITEM): + if (context->expecting(Item::STRING_ITEM)) + { + char buff[256]; + String str(buff,(uint32) sizeof(buff), system_charset_info); + str.length(0); + Item_string *string_item= (Item_string *) item; + DBUG_PRINT("info", ("STRING_ITEM")); + DBUG_PRINT("info", ("value \"%s\"", + string_item->val_str(&str)->ptr())); + NDB_ITEM_QUALIFICATION q; + q.value_type= Item::STRING_ITEM; + curr_cond->ndb_item= new Ndb_item(NDB_VALUE, q, item); + context->dont_expect(Item::STRING_ITEM); + if (context->expect_field_result_mask) + // We have not seen the field argument yet + context->expect_only_field_result(STRING_RESULT); + } + else + *context->supported_ptr= FALSE; + break; + case(Item::INT_ITEM): + if (context->expecting(Item::INT_ITEM)) + { + Item_int *int_item= (Item_int *) item; + DBUG_PRINT("info", ("INT_ITEM")); + DBUG_PRINT("info", ("value %d", int_item->value)); + curr_cond->ndb_item= new Ndb_item(int_item->value); + context->dont_expect(Item::INT_ITEM); + if (context->expect_field_result_mask) + // We have not seen the field argument yet + context->expect_only_field_result(INT_RESULT); + } + else + *context->supported_ptr= FALSE; + break; + case(Item::REAL_ITEM): + if (context->expecting(Item::REAL_ITEM)) + { + Item_real *real_item= (Item_real *) item; + DBUG_PRINT("info", ("REAL_ITEM %s")); + DBUG_PRINT("info", ("value %f", real_item->value)); + curr_cond->ndb_item= new Ndb_item(real_item->value); + context->dont_expect(Item::REAL_ITEM); + if (context->expect_field_result_mask) + // We have not seen the field argument yet + context->expect_only_field_result(REAL_RESULT); + } + else + *context->supported_ptr= FALSE; + break; + case(Item::VARBIN_ITEM): + if (context->expecting(Item::VARBIN_ITEM)) + { + char buff[256]; + String str(buff,(uint32) sizeof(buff), system_charset_info); + str.length(0); + Item_hex_string *varbin_item= (Item_hex_string *) item; + DBUG_PRINT("info", ("VARBIN_ITEM %s")); + DBUG_PRINT("info", ("value \"%s\"", + varbin_item->val_str(&str)->ptr())); + NDB_ITEM_QUALIFICATION q; + q.value_type= Item::VARBIN_ITEM; + curr_cond->ndb_item= new Ndb_item(NDB_VALUE, q, item); + context->dont_expect(Item::VARBIN_ITEM); + if (context->expect_field_result_mask) + // We have not seen the field argument yet + context->expect_only_field_result(STRING_RESULT); + } + else + *context->supported_ptr= FALSE; + break; + case(Item::COND_ITEM): { + Item_cond *cond_item= (Item_cond *) item; + switch(cond_item->functype()) { + case(Item_func::COND_AND_FUNC): + DBUG_PRINT("info", ("COND_AND_FUNC")); + curr_cond->ndb_item= new Ndb_item(cond_item->functype()); + break; + case(Item_func::COND_OR_FUNC): + DBUG_PRINT("info", ("COND_OR_FUNC")); + curr_cond->ndb_item= new Ndb_item(cond_item->functype()); + break; + default: + DBUG_PRINT("info", ("COND_ITEM %d", cond_item->functype())); + *context->supported_ptr= FALSE; + break; + } + break; + } + default: { + DBUG_PRINT("info", ("Found item of type %d", item->type())); + *context->supported_ptr= FALSE; + } + } + } + + DBUG_VOID_RETURN; +} + +bool +ha_ndbcluster::serialize_cond(const COND *cond, Ndb_cond_stack *ndb_cond) +{ + DBUG_ENTER("serialize_cond"); + Item *item= (Item *) cond; + bool supported= TRUE; + Ndb_cond_traverse_context context(table, (void *)m_table, + &supported, ndb_cond); + item->traverse_cond(&ndb_serialize_cond, (void *) &context, Item::PREFIX); + DBUG_PRINT("info", ("The pushed condition is %ssupported", (supported)?"":"not ")); + + DBUG_RETURN(supported); +} + +int +ha_ndbcluster::build_scan_filter_predicate(Ndb_cond * &cond, + NdbScanFilter *filter) +{ + DBUG_ENTER("build_scan_filter_predicate"); + switch(cond->ndb_item->type) { + case(NDB_FUNCTION): { + if (!cond->next) + break; + Ndb_item *a= cond->next->ndb_item; + switch(cond->ndb_item->qualification.function_type) { + case(Item_func::EQ_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + DBUG_PRINT("info", ("Generating EQ filter")); + const void* value_ptr = value->get_value(); + + if (filter->cmp(NdbScanFilter::COND_EQ, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::NE_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + DBUG_PRINT("info", ("Generating NE filter")); + const void* value_ptr = value->get_value(); + + if (filter->cmp(NdbScanFilter::COND_NE, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::LT_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + const void* value_ptr = value->get_value(); + + if (a == field) + { + DBUG_PRINT("info", ("Generating LT filter")); + if (filter->cmp(NdbScanFilter::COND_LT, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + else + { + DBUG_PRINT("info", ("Generating GT filter")); + if (filter->cmp(NdbScanFilter::COND_GT, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::LE_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + const void* value_ptr = value->get_value(); + + if (a == field) + { + DBUG_PRINT("info", ("Generating LE filter")); + if (filter->cmp(NdbScanFilter::COND_LE, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + else + { + DBUG_PRINT("info", ("Generating GE filter")); + if (filter->cmp(NdbScanFilter::COND_GE, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::GE_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + const void* value_ptr = value->get_value(); + + if (a == field) + { + DBUG_PRINT("info", ("Generating GE filter")); + if (filter->cmp(NdbScanFilter::COND_GE, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + else + { + DBUG_PRINT("info", ("Generating LE filter")); + if (filter->cmp(NdbScanFilter::COND_LE, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::GT_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + const void* value_ptr = value->get_value(); + + if (!value) + DBUG_RETURN(1); + + if (a == field) + { + DBUG_PRINT("info", ("Generating GT filter")); + if (filter->cmp(NdbScanFilter::COND_GT, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + else + { + DBUG_PRINT("info", ("Generating LT filter")); + if (filter->cmp(NdbScanFilter::COND_LT, + field->get_field_no(), + value_ptr, + field->pack_length()) == -1) + DBUG_RETURN(1); + } + + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::LIKE_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + if (value->qualification.value_type != Item::STRING_ITEM) break; + String *str= value->get_string_value(); + DBUG_PRINT("info", ("Generating LIKE filter: like(%d,%s,%d)", field->get_field_no(), str->ptr(), str->length())); + //if (filter->like(field->get_field_no(), + // str->ptr(), str->length(), TRUE) == -1) + // DBUG_RETURN(1); + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::NOTLIKE_FUNC): { + if (!cond->next->next) + break; + Ndb_item *b= cond->next->next->ndb_item; + Ndb_item *value= + (a->type == NDB_VALUE)? a + : (b->type == NDB_VALUE)? b + : NULL; + Ndb_item *field= + (a->type == NDB_FIELD)? a + : (b->type == NDB_FIELD)? b + : NULL; + if (!value || !field) break; + if (value->qualification.value_type != Item::STRING_ITEM) break; + String *str= value->get_string_value(); + DBUG_PRINT("info", ("Generating NOTLIKE filter: notlike(%d,%s,%d)", field->get_field_no(), str->ptr(), str->length())); + //if (filter->notlike(field->get_field_no(), + // str->ptr(), str->length()) == -1) + // DBUG_RETURN(1); + cond= cond->next->next->next; + DBUG_RETURN(0); + } + case(Item_func::ISNULL_FUNC): + if (a->type == NDB_FIELD) { + DBUG_PRINT("info", ("Generating ISNULL filter")); + if (filter->isnull(a->get_field_no()) == -1) + DBUG_RETURN(1); + } + cond= cond->next->next; + DBUG_RETURN(0); + case(Item_func::ISNOTNULL_FUNC): { + if (a->type == NDB_FIELD) { + DBUG_PRINT("info", ("Generating ISNOTNULL filter")); + if (filter->isnotnull(a->get_field_no()) == -1) + DBUG_RETURN(1); + } + cond= cond->next->next; + DBUG_RETURN(0); + } + default: + break; + } + break; + } + default: + break; + } + DBUG_PRINT("info", ("Found illegal condition")); + DBUG_RETURN(1); +} + +int +ha_ndbcluster::build_scan_filter_group(Ndb_cond* &cond, NdbScanFilter *filter) +{ + DBUG_ENTER("build_scan_filter_group"); + switch(cond->ndb_item->type) { + case(NDB_FUNCTION): + switch(cond->ndb_item->qualification.function_type) { + case(Item_func::COND_AND_FUNC): { + DBUG_PRINT("info", ("Generating AND group")); + if (filter->begin(NdbScanFilter::AND) == -1) + DBUG_RETURN(1); + cond= cond->next; + do + { + if (build_scan_filter_group(cond, filter)) + DBUG_RETURN(1); + } while (cond && cond->ndb_item->type != NDB_END_COND); + if (cond) cond= cond->next; + if (filter->end() == -1) + DBUG_RETURN(1); + DBUG_PRINT("info", ("End of AND group")); + break; + } + case(Item_func::COND_OR_FUNC): { + DBUG_PRINT("info", ("Generating OR group")); + if (filter->begin(NdbScanFilter::OR) == -1) + DBUG_RETURN(1); + cond= cond->next; + do + { + if (build_scan_filter_group(cond, filter)) + DBUG_RETURN(1); + } while (cond && cond->ndb_item->type != NDB_END_COND); + if (cond) cond= cond->next; + if (filter->end() == -1) + DBUG_RETURN(1); + DBUG_PRINT("info", ("End of OR group")); + break; + } + default: + if (build_scan_filter_predicate(cond, filter)) + DBUG_RETURN(1); + } + break; + default: { + DBUG_PRINT("info", ("Illegal scan filter")); + } + } + + DBUG_RETURN(0); +} + +int +ha_ndbcluster::build_scan_filter(Ndb_cond * &cond, NdbScanFilter *filter) +{ + bool simple_cond= TRUE; + DBUG_ENTER("build_scan_filter"); + + switch(cond->ndb_item->type) { + case(Item_func::COND_AND_FUNC): + simple_cond= FALSE; + break; + case(Item_func::COND_OR_FUNC): + simple_cond= FALSE; + break; + default: + break; + } + if (simple_cond && filter->begin() == -1) + DBUG_RETURN(1); + if (build_scan_filter_group(cond, filter)) + DBUG_RETURN(1); + if (simple_cond && filter->end() == -1) + DBUG_RETURN(1); + + DBUG_RETURN(0); +} + +int +ha_ndbcluster::generate_scan_filter(Ndb_cond_stack *ndb_cond_stack, + NdbScanOperation *op) +{ + DBUG_ENTER("generate_scan_filter"); + if (ndb_cond_stack) + { + NdbScanFilter filter(op); + bool multiple_cond= FALSE; + // Wrap an AND group around multiple conditions + if (ndb_cond_stack->next) { + multiple_cond= TRUE; + if (filter.begin() == -1) + DBUG_RETURN(1); + } + for (Ndb_cond_stack *stack= ndb_cond_stack; + (stack); + stack= stack->next) + { + Ndb_cond *cond= stack->ndb_cond; + + if (build_scan_filter(cond, &filter)) + { + DBUG_PRINT("info", ("build_scan_filter failed")); + DBUG_RETURN(1); + } + } + if (multiple_cond && filter.end() == -1) + DBUG_RETURN(1); + } + else + { + DBUG_PRINT("info", ("Empty stack")); + } + + DBUG_RETURN(0); +} + char* ha_ndbcluster::update_table_comment( /* out: table comment + additional */ diff --git a/sql/ha_ndbcluster.h b/sql/ha_ndbcluster.h index 942a4988252..c660d8f618d 100644 --- a/sql/ha_ndbcluster.h +++ b/sql/ha_ndbcluster.h @@ -32,6 +32,7 @@ class NdbOperation; // Forward declaration class NdbTransaction; // Forward declaration class NdbRecAttr; // Forward declaration class NdbScanOperation; +class NdbScanFilter; class NdbIndexScanOperation; class NdbBlob; @@ -61,6 +62,177 @@ typedef struct st_ndbcluster_share { uint table_name_length,use_count; } NDB_SHARE; +typedef enum ndb_item_type { + NDB_VALUE = 0, // Qualified more with Item::Type + NDB_FIELD = 1, // Qualified from table definition + NDB_FUNCTION = 2,// Qualified from Item_func::Functype + NDB_END_COND = 3 // End marker for condition group +} NDB_ITEM_TYPE; + +typedef union ndb_item_qualification { + Item::Type value_type; + enum_field_types field_type; // Instead of Item::FIELD_ITEM + Item_func::Functype function_type; // Instead of Item::FUNC_ITEM +} NDB_ITEM_QUALIFICATION; + +class Ndb_item_string_value { + public: + String s; + CHARSET_INFO *c; +}; + +typedef struct ndb_item_field_value { + Field* field; + int column_no; +} NDB_ITEM_FIELD_VALUE; + +typedef union ndb_item_value { + longlong int_value; + double real_value; + Ndb_item_string_value *string_value; + NDB_ITEM_FIELD_VALUE *field_value; +} NDB_ITEM_VALUE; + +class Ndb_item { + public: + Ndb_item(NDB_ITEM_TYPE item_type); + Ndb_item(NDB_ITEM_TYPE item_type, + NDB_ITEM_QUALIFICATION item_qualification, + const Item *item_value); + Ndb_item(longlong int_value); + Ndb_item(double real_value); + Ndb_item(Field *field, int column_no); + Ndb_item(Item_func::Functype func_type); + ~Ndb_item(); + void print(String *str); + uint32 pack_length() { return value.field_value->field->pack_length(); }; + // Getters and Setters + longlong get_int_value() { return value.int_value; }; + double get_real_value() { return value.real_value; }; + String * get_string_value() { return &value.string_value->s; }; + CHARSET_INFO * get_string_charset() { return value.string_value->c; }; + Field * get_field() { return value.field_value->field; }; + int get_field_no() { return value.field_value->column_no; }; + + const void * get_value() + { + switch(qualification.value_type) { + case(Item::INT_ITEM): { + return (void *) &value.int_value; + } + case(Item::REAL_ITEM): { + return (void *) &value.real_value; + break; + } + case(Item::STRING_ITEM): + case(Item::VARBIN_ITEM): { + return value.string_value->s.ptr(); + } + default: + break; + } + + return NULL; + } + + public: + NDB_ITEM_TYPE type; + NDB_ITEM_QUALIFICATION qualification; + + + private: + NDB_ITEM_VALUE value; + +}; + +class Ndb_cond { + public: + Ndb_cond() : ndb_item(NULL), next(NULL), prev(NULL) {}; + ~Ndb_cond() + { + if (ndb_item) delete ndb_item; + ndb_item= NULL; + if (next) delete next; + next= prev= NULL; + }; + Ndb_item *ndb_item; + Ndb_cond *next; + Ndb_cond *prev; +}; + +class Ndb_cond_stack { + public: + Ndb_cond_stack() : ndb_cond(NULL), next(NULL) {}; + ~Ndb_cond_stack() + { + if (ndb_cond) delete ndb_cond; + ndb_cond= NULL; + next= NULL; + }; + Ndb_cond *ndb_cond; + Ndb_cond_stack *next; +}; + +class Ndb_cond_traverse_context { + public: + Ndb_cond_traverse_context(TABLE *tab, void* ndb_tab, + bool *supported, Ndb_cond_stack* stack) + : table(tab), ndb_table(ndb_tab), + supported_ptr(supported), stack_ptr(stack), cond_ptr(NULL), + expect_mask(0), expect_field_result_mask(0) + { + if (stack) + cond_ptr= stack->ndb_cond; + }; + void expect(Item::Type type) + { + expect_mask|= (1 << type); + }; + void dont_expect(Item::Type type) + { + expect_mask&= ~(1 << type); + }; + bool expecting(Item::Type type) + { + return (expect_mask & (1 << type)); + }; + void expect_nothing() + { + expect_mask= 0; + }; + void expect_only(Item::Type type) + { + expect_mask= 0; + expect(type); + }; + + void expect_field_result(Item_result result) + { + expect_field_result_mask|= (1 << result); + }; + bool expecting_field_result(Item_result result) + { + return (expect_field_result_mask & (1 << result)); + }; + void expect_no_field_result() + { + expect_field_result_mask= 0; + }; + void expect_only_field_result(Item_result result) + { + expect_field_result_mask= 0; + expect_field_result(result); + }; + + TABLE* table; + void* ndb_table; + bool *supported_ptr; + Ndb_cond_stack* stack_ptr; + Ndb_cond* cond_ptr; + uint expect_mask; + uint expect_field_result_mask; +}; + /* Place holder for ha_ndbcluster thread specific data */ @@ -124,7 +296,6 @@ class ha_ndbcluster: public handler void info(uint); int extra(enum ha_extra_function operation); int extra_opt(enum ha_extra_function operation, ulong cache_size); - int reset(); int external_lock(THD *thd, int lock_type); int start_stmt(THD *thd); const char * table_type() const; @@ -154,6 +325,13 @@ class ha_ndbcluster: public handler static Thd_ndb* seize_thd_ndb(); static void release_thd_ndb(Thd_ndb* thd_ndb); + + /* + Condition pushdown + */ + const COND *cond_push(const COND *cond); + void cond_pop(); + uint8 table_cache_type(); private: @@ -214,13 +392,36 @@ class ha_ndbcluster: public handler int ndb_err(NdbTransaction*); bool uses_blob_value(bool all_fields); - int write_ndb_file(); - char *update_table_comment(const char * comment); - private: + int write_ndb_file(); + int check_ndb_connection(); + void set_rec_per_key(); + void records_update(); + void no_uncommitted_rows_execute_failure(); + void no_uncommitted_rows_update(int); + void no_uncommitted_rows_init(THD *); + void no_uncommitted_rows_reset(THD *); + + /* + Condition Pushdown to Handler (CPDH), private methods + */ + void cond_clear(); + bool serialize_cond(const COND *cond, Ndb_cond_stack *ndb_cond); + int build_scan_filter_predicate(Ndb_cond* &cond, + NdbScanFilter* filter); + int build_scan_filter_group(Ndb_cond* &cond, + NdbScanFilter* filter); + int build_scan_filter(Ndb_cond* &cond, NdbScanFilter* filter); + int generate_scan_filter(Ndb_cond_stack* cond_stack, + NdbScanOperation* op); + + friend int execute_commit(ha_ndbcluster*, NdbTransaction*); + friend int execute_no_commit(ha_ndbcluster*, NdbTransaction*); + friend int execute_no_commit_ie(ha_ndbcluster*, NdbTransaction*); + NdbTransaction *m_active_trans; NdbScanOperation *m_active_cursor; void *m_table; @@ -257,7 +458,7 @@ class ha_ndbcluster: public handler ha_rows m_autoincrement_prefetch; bool m_transaction_on; bool m_use_local_query_cache; - + Ndb_cond_stack *m_cond_stack; bool m_disable_multi_read; byte *m_multi_range_result_ptr; KEY_MULTI_RANGE *m_multi_ranges; @@ -266,18 +467,7 @@ class ha_ndbcluster: public handler NdbIndexScanOperation *m_multi_cursor; byte *m_multi_range_cursor_result_ptr; int setup_recattr(const NdbRecAttr*); - Ndb *get_ndb(); - void set_rec_per_key(); - void records_update(); - void no_uncommitted_rows_execute_failure(); - void no_uncommitted_rows_update(int); - void no_uncommitted_rows_init(THD *); - void no_uncommitted_rows_reset(THD *); - - friend int execute_no_commit(ha_ndbcluster*, NdbTransaction*); - friend int execute_commit(ha_ndbcluster*, NdbTransaction*); - friend int execute_no_commit_ie(ha_ndbcluster*, NdbTransaction*); }; extern struct show_var_st ndb_status_variables[]; diff --git a/sql/handler.h b/sql/handler.h index e5a794ca1b2..47bfcdfcb91 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -268,6 +268,9 @@ typedef struct st_table TABLE; struct st_foreign_key_info; typedef struct st_foreign_key_info FOREIGN_KEY_INFO; +/* Forward declaration for Condition Pushdown to Handler (CPDH) */ +typedef struct Item COND; + typedef struct st_ha_check_opt { ulong sort_buffer_size; @@ -592,7 +595,7 @@ public: /* Type of table for caching query */ virtual uint8 table_cache_type() { return HA_CACHE_TBL_NONTRANSACT; } - + /* RETURN true Primary key (if there is one) is clustered key covering all fields @@ -604,6 +607,12 @@ public: { return memcmp(ref1, ref2, ref_length); } + + /* + Condition pushdown to storage engines + */ + virtual const COND *cond_push(const COND *cond) { return cond; }; + virtual void cond_pop() { return; }; }; /* Some extern variables used with handlers */ diff --git a/sql/item.h b/sql/item.h index 6857b4acf82..c8b2d774696 100644 --- a/sql/item.h +++ b/sql/item.h @@ -113,6 +113,8 @@ public: typedef bool (Item::*Item_processor)(byte *arg); typedef Item* (Item::*Item_transformer) (byte *arg); +typedef void (*Item_cond_traverser) (const Item *item, void *arg); + class Item { Item(const Item &); /* Prevent use of these */ void operator=(Item &); @@ -124,7 +126,7 @@ public: static void operator delete(void *ptr,size_t size, MEM_ROOT *mem_root) { TRASH(ptr, size); } - enum Type {FIELD_ITEM, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM, + enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM, INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM, COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM, PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM, @@ -133,6 +135,8 @@ public: PARAM_ITEM, TRIGGER_FIELD_ITEM}; enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE }; + + enum traverse_order { POSTFIX, PREFIX }; /* str_values's main purpose is to be used to cache the value in @@ -314,6 +318,13 @@ public: { return (this->*transformer)(arg); } + + virtual void traverse_cond(Item_cond_traverser traverser, + void *arg, + traverse_order order = POSTFIX) + { + (*traverser)(this, arg); + } virtual bool remove_dependence_processor(byte * arg) { return 0; } virtual bool remove_fixed(byte * arg) { fixed= 0; return 0; } diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index 96689bd0ac2..a8c580d45f1 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -2024,6 +2024,30 @@ Item *Item_cond::transform(Item_transformer transformer, byte *arg) return Item_func::transform(transformer, arg); } +void Item_cond::traverse_cond(Item_cond_traverser traverser, + void *arg, + traverse_order order) +{ + List_iterator<Item> li(list); + Item *item; + + switch(order) { + case(PREFIX): + (*traverser)(this, arg); + while ((item= li++)) + { + item->traverse_cond(traverser, arg, order); + } + (*traverser)(NULL, arg); + break; + case(POSTFIX): + while ((item= li++)) + { + item->traverse_cond(traverser, arg, order); + } + (*traverser)(this, arg); + } +} void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields) diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index a156322e13c..ccc3fb2eb60 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -988,6 +988,9 @@ public: void copy_andor_arguments(THD *thd, Item_cond *item); bool walk(Item_processor processor, byte *arg); Item *transform(Item_transformer transformer, byte *arg); + void traverse_cond(Item_cond_traverser, + void *arg, + traverse_order order = POSTFIX); void neg_arguments(THD *thd); }; diff --git a/sql/item_func.cc b/sql/item_func.cc index 434a4741cf3..607efe06e77 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -342,6 +342,32 @@ bool Item_func::walk (Item_processor processor, byte *argument) return (this->*processor)(argument); } +void Item_func::traverse_cond(Item_cond_traverser traverser, + void *argument, + traverse_order order) +{ + if (arg_count) + { + Item **arg,**arg_end; + + switch (order) { + case(PREFIX): + (traverser)(this, argument); + for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++) + { + (*arg)->traverse_cond(traverser, argument, order); + } + break; + case (POSTFIX): + for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++) + { + (*arg)->traverse_cond(traverser, argument, order); + } + (traverser)(this, argument); + } + } +} + /* Transform an Item_func object with a transformer callback function diff --git a/sql/item_func.h b/sql/item_func.h index fb8d77d5b83..cb6cc00c046 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -154,6 +154,9 @@ public: uint flags= 0); bool walk(Item_processor processor, byte *arg); Item *transform(Item_transformer transformer, byte *arg); + void traverse_cond(Item_cond_traverser traverser, + void * arg, + traverse_order order = POSTFIX); }; diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 9b39f51fc5b..ae27af1e755 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -4174,6 +4174,7 @@ enum options_mysqld OPT_INNODB, OPT_ISAM, OPT_NDBCLUSTER, OPT_NDB_CONNECTSTRING, OPT_NDB_USE_EXACT_COUNT, OPT_NDB_FORCE_SEND, OPT_NDB_AUTOINCREMENT_PREFETCH_SZ, + OPT_NDB_CONDITION_PUSHDOWN, OPT_NDB_SHM, OPT_NDB_OPTIMIZED_NODE_SELECTION, OPT_SKIP_SAFEMALLOC, OPT_TEMP_POOL, OPT_TX_ISOLATION, @@ -4656,6 +4657,12 @@ Disable with --skip-ndbcluster (will save memory).", (gptr*) &opt_ndbcluster, (gptr*) &opt_ndbcluster, 0, GET_BOOL, NO_ARG, OPT_NDBCLUSTER_DEFAULT, 0, 0, 0, 0, 0}, #ifdef HAVE_NDBCLUSTER_DB + {"ndb-condition-pushdown", + OPT_NDB_CONDITION_PUSHDOWN, + "Push supported query conditions to the ndbcluster storage engine.", + (gptr*) &global_system_variables.ndb_condition_pushdown, + (gptr*) &global_system_variables.ndb_condition_pushdown, + 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"ndb-connectstring", OPT_NDB_CONNECTSTRING, "Connect string for ndbcluster.", (gptr*) &opt_ndbcluster_connectstring, diff --git a/sql/set_var.cc b/sql/set_var.cc index 4d29529b256..d09de8550f0 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -399,6 +399,9 @@ sys_var_thd_bool sys_ndb_use_exact_count("ndb_use_exact_count", &SV::ndb_use_exact_count); sys_var_thd_bool sys_ndb_use_transactions("ndb_use_transactions", &SV::ndb_use_transactions); +sys_var_thd_bool +sys_ndb_condition_pushdown("ndb_condition_pushdown", + &SV::ndb_condition_pushdown); #endif /* Time/date/datetime formats */ @@ -670,6 +673,7 @@ sys_var *sys_variables[]= &sys_ndb_force_send, &sys_ndb_use_exact_count, &sys_ndb_use_transactions, + &sys_ndb_condition_pushdown, #endif &sys_unique_checks, &sys_updatable_views_with_limit, @@ -849,6 +853,8 @@ struct show_var_st init_vars[]= { {sys_ndb_force_send.name, (char*) &sys_ndb_force_send, SHOW_SYS}, {sys_ndb_use_exact_count.name,(char*) &sys_ndb_use_exact_count, SHOW_SYS}, {sys_ndb_use_transactions.name,(char*) &sys_ndb_use_transactions, SHOW_SYS}, + {sys_ndb_condition_pushdown.name, (char*) &sys_ndb_condition_pushdown, + SHOW_SYS}, #endif {sys_net_buffer_length.name,(char*) &sys_net_buffer_length, SHOW_SYS}, {sys_net_read_timeout.name, (char*) &sys_net_read_timeout, SHOW_SYS}, diff --git a/sql/sql_class.h b/sql/sql_class.h index 8e6204ab3a3..240208584e7 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -438,6 +438,7 @@ struct system_variables my_bool ndb_force_send; my_bool ndb_use_exact_count; my_bool ndb_use_transactions; + my_bool ndb_condition_pushdown; #endif /* HAVE_NDBCLUSTER_DB */ my_bool old_passwords; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 69b5c667f6b..60273d21d9f 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -5300,6 +5300,7 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0))) DBUG_RETURN(1); tab->select_cond=sel->cond=tmp; + tab->table->file->cond_push(tmp); // Push condition to handler } else tab->select_cond= sel->cond= NULL; @@ -5421,6 +5422,8 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) join->thd->memdup((gptr) sel, sizeof(SQL_SELECT)); tab->cache.select->cond=tmp; tab->cache.select->read_tables=join->const_table_map; + if (tmp != tab->select_cond) + tab->table->file->cond_push(tmp); // Push condition to handler } } } |