From 15278593181c817d5048d6c146345b83d9e9e86d Mon Sep 17 00:00:00 2001 From: Seth Shelnutt Date: Tue, 1 May 2018 18:16:14 -0400 Subject: [PATCH] Add support for auto_increment in primary keys. #17 --- mysql-test/mytile/r/auto_increment.result | 20 +++++ mysql-test/mytile/t/auto_increment.test | 11 +++ mytile/ha_mytile.cc | 96 +++++++++++++++-------- mytile/ha_mytile.h | 2 + 4 files changed, 97 insertions(+), 32 deletions(-) create mode 100644 mysql-test/mytile/r/auto_increment.result create mode 100644 mysql-test/mytile/t/auto_increment.test diff --git a/mysql-test/mytile/r/auto_increment.result b/mysql-test/mytile/r/auto_increment.result new file mode 100644 index 0000000..b655399 --- /dev/null +++ b/mysql-test/mytile/r/auto_increment.result @@ -0,0 +1,20 @@ +#Primary Key Auto Increment Support Test +CREATE TABLE t1 ( +column1 integer auto_increment, +column2 varchar(255), +primary key(column1) +) ENGINE=mytile; +INSERT INTO t1(column2) VALUES ('aHR0cHM6Ly9naXRodWIuY29tL1NoZWxudXR0Mi9jcnVuY2g='), ('dmFsdWUy'), ('dmFsdWU'); +select column1, column2 from t1 ORDER BY column1; +column1 column2 +1 aHR0cHM6Ly9naXRodWIuY29tL1NoZWxudXR0Mi9jcnVuY2g= +2 dmFsdWUy +3 dmFsdWU +INSERT INTO t1(column2) VALUES ('row4'); +select column1, column2 from t1 ORDER BY column1; +column1 column2 +1 aHR0cHM6Ly9naXRodWIuY29tL1NoZWxudXR0Mi9jcnVuY2g= +2 dmFsdWUy +3 dmFsdWU +4 row4 +DROP TABLE t1; diff --git a/mysql-test/mytile/t/auto_increment.test b/mysql-test/mytile/t/auto_increment.test new file mode 100644 index 0000000..1708fd9 --- /dev/null +++ b/mysql-test/mytile/t/auto_increment.test @@ -0,0 +1,11 @@ +--echo #Primary Key Auto Increment Support Test +CREATE TABLE t1 ( + column1 integer auto_increment, + column2 varchar(255), + primary key(column1) +) ENGINE=mytile; +INSERT INTO t1(column2) VALUES ('aHR0cHM6Ly9naXRodWIuY29tL1NoZWxudXR0Mi9jcnVuY2g='), ('dmFsdWUy'), ('dmFsdWU'); +select column1, column2 from t1 ORDER BY column1; +INSERT INTO t1(column2) VALUES ('row4'); +select column1, column2 from t1 ORDER BY column1; +DROP TABLE t1; diff --git a/mytile/ha_mytile.cc b/mytile/ha_mytile.cc index 95a97d1..5422ff6 100644 --- a/mytile/ha_mytile.cc +++ b/mytile/ha_mytile.cc @@ -749,43 +749,62 @@ int tile::mytile::index_read_idx_map(uchar *buf, uint idx, const uchar *key, key //for (auto mapItem : this->map) { std::vector prevKey; std::vector itemKey; - while (mapItemIterator != this->map->end()) { - // Only check the item if it is not deleted - if (!mapItemIterator->get(MYTILE_DELETE_ATTRIBUTE)) { - itemKey = mapItemIterator->key>(); - if (!tile::cmpKeys(key, itemKey.data(), table->s->key_info + idx)) { - switch (find_flag) { - // Currently only support AFTER, BEFORE and EXACT. - case HA_READ_AFTER_KEY: - mapItemIterator.operator++(); - itemKey = mapItemIterator->key>(); - break; - case HA_READ_BEFORE_KEY: - itemKey = prevKey; - break; - case HA_READ_KEY_EXACT: - break; - case HA_READ_KEY_OR_NEXT: - case HA_READ_KEY_OR_PREV: - case HA_READ_PREFIX: - case HA_READ_PREFIX_LAST: - case HA_READ_PREFIX_LAST_OR_PREV: - case HA_READ_MBR_CONTAIN: - case HA_READ_MBR_INTERSECT: - case HA_READ_MBR_WITHIN: - case HA_READ_MBR_DISJOINT: - case HA_READ_MBR_EQUAL: - /* This flag is not used by the SQL layer, so we don't support it yet. */ - rc = HA_ERR_UNSUPPORTED; - break; + + // TODO: The keys are ordered, but for auto increment this happens to work by the nature of auto increment + if(key == NULL) { + sql_print_information("find_flag = %d", find_flag); + if(find_flag == HA_READ_PREFIX_LAST) { + while (mapItemIterator != this->map->end()) { + itemKey = mapItemIterator->key>(); + sql_print_information("key found: %s", itemKey.data()); + prevKey = itemKey; + mapItemIterator.operator++(); + } + itemKey = prevKey; + sql_print_information("key found final size of: %s", itemKey.size()); + } + } else { //Key is not null + while (mapItemIterator != this->map->end()) { + // Only check the item if it is not deleted + if (!mapItemIterator->get(MYTILE_DELETE_ATTRIBUTE)) { + itemKey = mapItemIterator->key>(); + if (!tile::cmpKeys(key, itemKey.data(), table->s->key_info + idx)) { + switch (find_flag) { + // Currently only support AFTER, BEFORE and EXACT. + case HA_READ_AFTER_KEY: + mapItemIterator.operator++(); + itemKey = mapItemIterator->key>(); + break; + case HA_READ_BEFORE_KEY: + itemKey = prevKey; + break; + case HA_READ_KEY_EXACT: + break; + case HA_READ_KEY_OR_NEXT: + case HA_READ_KEY_OR_PREV: + case HA_READ_PREFIX: + case HA_READ_PREFIX_LAST: + case HA_READ_PREFIX_LAST_OR_PREV: + case HA_READ_MBR_CONTAIN: + case HA_READ_MBR_INTERSECT: + case HA_READ_MBR_WITHIN: + case HA_READ_MBR_DISJOINT: + case HA_READ_MBR_EQUAL: + /* This flag is not used by the SQL layer, so we don't support it yet. */ + rc = HA_ERR_UNSUPPORTED; + break; + } + break; } - break; + prevKey = itemKey; } - prevKey = itemKey; + mapItemIterator.operator++(); } - mapItemIterator.operator++(); } + if(itemKey.size() == 0) + rc = HA_ERR_KEY_NOT_FOUND; + if (!rc) { tiledb::MapItem mapItem = this->map->get_item(itemKey); if (mapItem.good()) { @@ -798,6 +817,18 @@ int tile::mytile::index_read_idx_map(uchar *buf, uint idx, const uchar *key, key DBUG_RETURN(rc); } +int tile::mytile::index_last(uchar *buf) { + DBUG_ENTER("tile::mytile::index_last"); + + int error = index_read_map(buf, NULL, 0, HA_READ_BEFORE_KEY); + + /* MySQL does not seem to allow this to return HA_ERR_KEY_NOT_FOUND */ + + if (error == HA_ERR_KEY_NOT_FOUND) { + error = HA_ERR_END_OF_FILE; + } + DBUG_RETURN(error); +} /** * Store a lock, we aren't using table or row locking at this point. * @param thd @@ -867,6 +898,7 @@ ha_rows tile::mytile::records_in_range(uint inx, key_range *min_key, key_range * ulonglong tile::mytile::table_flags(void) const { DBUG_ENTER("tile::mytile::table_flags"); DBUG_RETURN(HA_REC_NOT_IN_SEQ | HA_CAN_SQL_HANDLER | HA_NULL_IN_KEY | HA_REQUIRE_PRIMARY_KEY + | HA_AUTO_PART_KEY | HA_NO_TRANSACTIONS | HA_CAN_BIT_FIELD | HA_FILE_BASED | HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE); }; diff --git a/mytile/ha_mytile.h b/mytile/ha_mytile.h index 4d0dd62..fb52190 100644 --- a/mytile/ha_mytile.h +++ b/mytile/ha_mytile.h @@ -75,6 +75,8 @@ namespace tile { int index_read_idx_map(uchar *buf, uint idx, const uchar *key, key_part_map keypart_map, enum ha_rkey_function find_flag) override; + int index_last(uchar *buf); + THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, enum thr_lock_type lock_type) override; int external_lock(THD *thd, int lock_type) override;