Commit 4c308dd2 authored by Roman Nozdrin's avatar Roman Nozdrin Committed by Sergei Petrunia

CLX-14 This commit adds support for pushed conditions for table API.

parent f2994aaf
CREATE DATABASE xpd;
USE xpd;
DROP TABLE IF EXISTS cx1;
CREATE TABLE cx1(i BIGINT, i2 BIGINT, t TEXT)ENGINE=xpand;
INSERT INTO cx1 VALUES (41, 43, 'some1'), (42, 42, 'some2'), (43, 41, 'some3');
SELECT * FROM cx1 ORDER BY i;
i i2 t
41 43 some1
42 42 some2
43 41 some3
SET xpand_select_handler=OFF;
SELECT * FROM cx1 WHERE i>41 AND i2>41;
i i2 t
42 42 some2
EXPLAIN SELECT * FROM cx1 WHERE i>41 AND i2>41;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE cx1 ALL NULL NULL NULL NULL 10000 Using where with pushed condition
SELECT * FROM cx1 WHERE i>41 AND i2>41 AND t='some2';
i i2 t
42 42 some2
EXPLAIN SELECT * FROM cx1 WHERE i>41 AND i2>41 AND t='some2';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE cx1 ALL NULL NULL NULL NULL 10000 Using where with pushed condition
SELECT * FROM cx1 WHERE i>i2+1;
i i2 t
43 41 some3
EXPLAIN SELECT * FROM cx1 WHERE i>i2+1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE cx1 ALL NULL NULL NULL NULL 10000 Using where with pushed condition
SET @@optimizer_switch='derived_merge=OFF';
SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
i i2 t
43 41 some3
EXPLAIN SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10000 Using filesort
2 PUSHED DERIVED NULL NULL NULL NULL NULL NULL NULL NULL
SET xpand_derived_handler=OFF;
SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
i i2 t
43 41 some3
EXPLAIN SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10000 Using filesort
2 DERIVED cx1 ALL NULL NULL NULL NULL 10000 Using where with pushed condition
USE test;
DROP DATABASE xpd;
CREATE DATABASE xpd;
USE xpd;
--disable_warnings
DROP TABLE IF EXISTS cx1;
--enable_warnings
CREATE TABLE cx1(i BIGINT, i2 BIGINT, t TEXT)ENGINE=xpand;
INSERT INTO cx1 VALUES (41, 43, 'some1'), (42, 42, 'some2'), (43, 41, 'some3');
SELECT * FROM cx1 ORDER BY i;
SET xpand_select_handler=OFF;
SELECT * FROM cx1 WHERE i>41 AND i2>41;
EXPLAIN SELECT * FROM cx1 WHERE i>41 AND i2>41;
SELECT * FROM cx1 WHERE i>41 AND i2>41 AND t='some2';
EXPLAIN SELECT * FROM cx1 WHERE i>41 AND i2>41 AND t='some2';
SELECT * FROM cx1 WHERE i>i2+1;
EXPLAIN SELECT * FROM cx1 WHERE i>i2+1;
# The plugin doesn't use pushdown conditions for DH as of 10.5.1
# but it is worth to test memory leaks.
SET @@optimizer_switch='derived_merge=OFF';
SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
EXPLAIN SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
SET xpand_derived_handler=OFF;
SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
EXPLAIN SELECT * FROM (SELECT * FROM cx1 WHERE i>i2+1) a1 ORDER BY i;
# SELECT * FROM (SELECT i FROM cx1 WHERE i=42)a1,(SELECT i FROM cx1 WHERE i =42)a2 WHERE a1.i=a2.i;
# EXPLAIN SELECT * FROM (SELECT i FROM cx1 WHERE i=42)a1,(SELECT i FROM cx1 WHERE i =42)a2 WHERE a1.i=a2.i;
USE test;
DROP DATABASE xpd;
......@@ -593,6 +593,7 @@ int ha_xpand::reset()
upsert_flag &= ~XPAND_HAS_UPSERT;
upsert_flag &= ~XPAND_UPSERT_SENT;
xpd_lock_type = XPAND_NO_LOCKS;
pushdown_cond_list.empty();
return 0;
}
......@@ -1079,8 +1080,25 @@ int ha_xpand::rnd_init(bool scan)
bitmap_set_all(&scan_fields);
#endif
String* pushdown_cond_sql = nullptr;
if (pushdown_cond_list.elements) {
pushdown_cond_sql = new String();
while (pushdown_cond_list.elements > 0) {
COND* cond = pushdown_cond_list.pop();
String sql_predicate;
cond->print_for_table_def(&sql_predicate);
pushdown_cond_sql->append(sql_predicate);
if ( pushdown_cond_list.elements > 0)
pushdown_cond_sql->append(" AND ");
}
}
error_code = trx->scan_table(xpand_table_oid, xpd_lock_type, &scan_fields,
THDVAR(thd, row_buffer), &scan_cur);
THDVAR(thd, row_buffer), &scan_cur,
pushdown_cond_sql);
if (pushdown_cond_sql != nullptr)
delete pushdown_cond_sql;
if (error_code == HA_ERR_TABLE_DEF_CHANGED)
xpand_mark_table_for_discovery(table);
......@@ -1255,11 +1273,17 @@ int ha_xpand::external_lock(THD *thd, int lock_type)
const COND *ha_xpand::cond_push(const COND *cond)
{
return cond;
THD *thd= ha_thd();
if (!thd->lex->describe) {
pushdown_cond_list.push_front(const_cast<COND*>(cond));
}
return NULL;
}
void ha_xpand::cond_pop()
{
pushdown_cond_list.pop();
}
int ha_xpand::info_push(uint info_type, void *info)
......
......@@ -68,6 +68,8 @@ class ha_xpand : public handler
} xpd_upsert_flags_t;
int upsert_flag;
List<COND> pushdown_cond_list;
Xpand_share *get_share(); ///< Get the share
public:
......
......@@ -83,6 +83,7 @@ enum xpand_commands {
XPAND_UPDATE_QUERY,
XPAND_COMMIT,
XPAND_ROLLBACK,
XPAND_SCAN_TABLE_COND,
};
/****************************************************************************
......@@ -732,7 +733,8 @@ int xpand_connection::allocate_cursor(MYSQL *xpand_net, ulong buffer_size,
int xpand_connection::scan_table(ulonglong xpand_table_oid,
xpand_lock_mode_t lock_mode,
MY_BITMAP *read_set, ushort row_req,
xpand_connection_cursor **scan)
xpand_connection_cursor **scan,
String* pushdown_cond_sql)
{
int error_code;
command_length = 0;
......@@ -741,8 +743,13 @@ int xpand_connection::scan_table(ulonglong xpand_table_oid,
if (trans_flags & XPAND_TRANS_AUTOCOMMIT)
return HA_ERR_INTERNAL_ERROR;
if ((error_code = begin_command(XPAND_SCAN_TABLE)))
return error_code;
if (pushdown_cond_sql != nullptr) {
if ((error_code= begin_command(XPAND_SCAN_TABLE_COND)))
return error_code;
} else {
if ((error_code= begin_command(XPAND_SCAN_TABLE)))
return error_code;
}
if ((error_code = add_command_operand_ushort(row_req)))
return error_code;
......@@ -756,6 +763,14 @@ int xpand_connection::scan_table(ulonglong xpand_table_oid,
if ((error_code = add_command_operand_bitmap(read_set)))
return error_code;
if (pushdown_cond_sql != nullptr) {
if ((error_code= add_command_operand_str(
reinterpret_cast<const uchar*>(pushdown_cond_sql->ptr()),
pushdown_cond_sql->length()))) {
return error_code;
}
}
if ((error_code = send_command()))
return error_code;
......
......@@ -91,7 +91,7 @@ class xpand_connection
int scan_table(ulonglong xpand_table_oid,
xpand_lock_mode_t lock_mode,
MY_BITMAP *read_set, ushort row_req,
xpand_connection_cursor **scan);
xpand_connection_cursor **scan, String* pushdown_cond_sql);
int scan_query(String &stmt, uchar *fieldtype, uint fields, uchar *null_bits,
uint null_bits_size, uchar *field_metadata,
uint field_metadata_size, ushort row_req, ulonglong *oids,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment