--disable_warnings
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;
--enable_warnings

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'0'; 
SELECT 0 + (0<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'0');
INSERT INTO t2 VALUES (b'0', b'0', b'0');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1'; 
SELECT 0 + (1<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1');
INSERT INTO t2 VALUES (b'1', b'1', b'1');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'10'; 
SELECT 0 + (10<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10');
INSERT INTO t2 VALUES (b'10', b'10', b'10');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010'; 
SELECT 0 + (1010<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010');
INSERT INTO t2 VALUES (b'1010', b'1010', b'1010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'1010101010101010'; 
SELECT 0 + (1010101010101010<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010', b'1010101010101010', b'1010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
SELECT 0 + b'101010101010101010101010101010'; 
SELECT 0 + (101010101010101010101010101010<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'101010101010101010101010101010');
INSERT INTO t2 VALUES (b'101010101010101010101010101010', b'101010101010101010101010101010', b'101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output on different platforms
#SELECT 0 + b'1010101010101010101010101010101010101010101010101010101010101010'; 
SELECT 0 + (1010101010101010101010101010101010101010101010101010101010101010<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010');
INSERT INTO t2 VALUES (b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010', b'1010101010101010101010101010101010101010101010101010101010101010');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<0);
######### INSERT/SELECT######### 

#### Insert and select 0 bit data into the table####
CREATE TABLE t1(c1 BIT(0));
CREATE TABLE t2(c1 BIT(0), c2 BIT(0), c3 BIT(0));
let $i=0;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(0) NOT NULL PRIMARY KEY, c2  BIT(0));
CREATE TABLE t6(c1  BIT(0), c2  BIT(0));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '0' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '0'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '0' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '0' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '0' AND c1 < '64' AND c2 = '0' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(0));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<1);
######### INSERT/SELECT######### 

#### Insert and select 1 bit data into the table####
CREATE TABLE t1(c1 BIT(1));
CREATE TABLE t2(c1 BIT(1), c2 BIT(1), c3 BIT(1));
let $i=1;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(1) NOT NULL PRIMARY KEY, c2  BIT(1));
CREATE TABLE t6(c1  BIT(1), c2  BIT(1));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '1' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '1'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '1' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '1' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '1' AND c1 < '64' AND c2 = '1' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(1));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<2);
######### INSERT/SELECT######### 

#### Insert and select 2 bit data into the table####
CREATE TABLE t1(c1 BIT(2));
CREATE TABLE t2(c1 BIT(2), c2 BIT(2), c3 BIT(2));
let $i=2;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(2) NOT NULL PRIMARY KEY, c2  BIT(2));
CREATE TABLE t6(c1  BIT(2), c2  BIT(2));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '2' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '2'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '2' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '2' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '2' AND c1 < '64' AND c2 = '2' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(2));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<4);
######### INSERT/SELECT######### 

#### Insert and select 4 bit data into the table####
CREATE TABLE t1(c1 BIT(4));
CREATE TABLE t2(c1 BIT(4), c2 BIT(4), c3 BIT(4));
let $i=4;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(4) NOT NULL PRIMARY KEY, c2  BIT(4));
CREATE TABLE t6(c1  BIT(4), c2  BIT(4));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '4' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '4'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '4' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '4' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '4' AND c1 < '64' AND c2 = '4' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(4));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<8);
######### INSERT/SELECT######### 

#### Insert and select 8 bit data into the table####
CREATE TABLE t1(c1 BIT(8));
CREATE TABLE t2(c1 BIT(8), c2 BIT(8), c3 BIT(8));
let $i=8;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(8) NOT NULL PRIMARY KEY, c2  BIT(8));
CREATE TABLE t6(c1  BIT(8), c2  BIT(8));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '8' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8'  ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '8'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'8' ORDER BY c1 DESC LIMIT 2 ;

SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '8' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '8' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '8' AND c1 < '64' AND c2 = '8' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(8));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<16);
######### INSERT/SELECT######### 

#### Insert and select 16 bit data into the table####
CREATE TABLE t1(c1 BIT(16));
CREATE TABLE t2(c1 BIT(16), c2 BIT(16), c3 BIT(16));
let $i=16;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(16) NOT NULL PRIMARY KEY, c2  BIT(16));
CREATE TABLE t6(c1  BIT(16), c2  BIT(16));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '16' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '16'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '16' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '16' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '16' AND c1 < '64' AND c2 = '16' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(16));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<32);
######### INSERT/SELECT######### 

#### Insert and select 32 bit data into the table####
CREATE TABLE t1(c1 BIT(32));
CREATE TABLE t2(c1 BIT(32), c2 BIT(32), c3 BIT(32));
let $i=32;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(32) NOT NULL PRIMARY KEY, c2  BIT(32));
CREATE TABLE t6(c1  BIT(32), c2  BIT(32));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '32' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '32'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '32' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '32' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '32' AND c1 < '64' AND c2 = '32' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(32));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

####Checking the bit values####
# Produces different output for different platform
#SELECT 0 + b'10101010101010101010101010101010101010101010101010101010101010101'; 
SELECT 0 + (10101010101010101010101010101010101010101010101010101010101010101<<64);
######### INSERT/SELECT######### 

#### Insert and select 64 bit data into the table####
CREATE TABLE t1(c1 BIT(64));
CREATE TABLE t2(c1 BIT(64), c2 BIT(64), c3 BIT(64));
let $i=64;
while($i){
let $j=$i/2;
eval set @v1=$i;
eval set @v2=$j;
set @v3=repeat('1',@v1);
let $STR0= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR0');
set @v3=repeat('0',@v1);
let $STR1= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR1');
set @v3=repeat('01',@v2);
let $STR2= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR2');
set @v3=repeat('10',@v2);
let $STR3= query_get_value(select @v3 as I,I,1);
eval INSERT INTO t1 VALUES (b'$STR3');
set @v3=repeat('1',@v1);
set @v4=repeat('01',@v2);
set @v5=repeat('10',@v2);
let $STR0= query_get_value(select @v3 as I,I,1);
let $STR2= query_get_value(select @v4 as I,I,1);
let $STR3= query_get_value(select @v5 as I,I,1);
eval INSERT INTO t2 VALUES ("b'$STR0'","b'$STR2","b'$STR3'");
dec $i;
}
INSERT INTO t1 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101');
INSERT INTO t2 VALUES (b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101', b'10101010101010101010101010101010101010101010101010101010101010101');
--sorted_result
SELECT HEX(c1) FROM t1;
--sorted_result
SELECT HEX(c1),HEX(c2),HEX(c3) FROM t2;
--sorted_result
SELECT OCT(c1) FROM t1;
--sorted_result
SELECT OCT(c1),OCT(c2),OCT(c3) FROM t2;
--sorted_result
SELECT BIN(c1) FROM t1;
--sorted_result
SELECT BIN(c1),BIN(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1),OCT(c2),BIN(c3) FROM t2;
--sorted_result
SELECT HEX(c1+0),HEX(c2+1),HEX(c3+2) FROM t2;
--sorted_result
SELECT OCT(c1+0),OCT(c2+1),OCT(c3+2) FROM t2;
--sorted_result
SELECT BIN(c1+0),BIN(c2+1),BIN(c3+2) FROM t2;
--sorted_result
SELECT CONCAT(HEX(c1),HEX(c2),HEX(c3)) FROM t2;
--sorted_result
SELECT CONCAT(OCT(c1),OCT(c2),OCT(c3)) FROM t2;
--sorted_result
SELECT CONCAT(BIN(c1),BIN(c2),BIN(c3)) FROM t2;
--error 1439
CREATE TABLE t3 (c1 BIT(65));
# Checking for bug #9571
CREATE TABLE t4(i INT, b BIT NOT NULL);
ALTER TABLE t4 ADD PRIMARY KEY (i);
CREATE TABLE t5(c1  BIT(64) NOT NULL PRIMARY KEY, c2  BIT(64));
CREATE TABLE t6(c1  BIT(64), c2  BIT(64));
#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;

#Insert permissible NULLs
INSERT IGNORE INTO t5 VALUES(96,null);
INSERT INTO t6 VALUES(null,null);
# Now select using various table access methods (full table scan, range scan, index scan etc.)
## Full table scan ##
--sorted_result
SELECT HEX(c1),HEX(c2) FROM t5;
--sorted_result
SELECT count(*) as total_rows, min(c1) as min_value, max(c1) FROM t5;
--sorted_result
SELECT OCT(c2) FROM t5 WHERE c1 = 68;
## Forward index scan, covering ##
--sorted_result
SELECT bin(c1) FROM t5;
## Backward index scan, covering ##
SELECT oct(c1) FROM t5 ORDER BY c1 DESC;
## Forward index scan, non-covering ##
SELECT hex(c1),hex(c2) FROM t5 ORDER BY c1 LIMIT 2 ;
## Backward index scan, non-covering ##
SELECT * FROM t5 ORDER BY c1 DESC LIMIT 2 /* comment7 */;
## ref type access
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 LIMIT 2;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 = '64' ORDER BY c1 DESC LIMIT 2 ;
## Range access, ordered ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64'  ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1; 
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 LIMIT 2 ;
## Range access, backwards scan ##
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <> '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >  '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'  ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <  '64'   ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <= '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64'  ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 <=>'64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 BETWEEN '64' AND '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IN ( '64' , '64' ) ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 >= '64' AND c1 < '64' AND c2 = '64' ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NULL ORDER BY c1 DESC LIMIT 2 ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC ;
SELECT hex(c1),hex(c2) FROM t5 WHERE c1 IS NOT NULL ORDER BY c1 DESC LIMIT 2 ;
#Inserting different charachers
CREATE TABLE t7(c1  BIT(64));
INSERT INTO t7 VALUES('a'),('A'),('z'),('Z'),('!'),('`'),('@'),('#'),('~'),('$'),('%'),('^'),('&'),('*'),('('),(')'),('-'),('_'),('+'),('='),('1'),('9');
--sorted_result
SELECT * FROM t7;
############# UPDATE ###########

# Update order by limit
UPDATE t6 SET c1='10' WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Update with arithmetic operations 
UPDATE t6 SET c1=c2+c1 WHERE c2 < 100;

UPDATE t6 SET c2=c2+100 WHERE c1 >=100 ORDER BY c1 LIMIT 4;


# Update with NULL ( NULL to number & number to NULL)
UPDATE t6 SET c2=NULL WHERE c1=23;

UPDATE t6 SET c2=10 WHERE c2=NULL;


# Update range values
UPDATE t6 SET c1=12 WHERE c1=b'101' AND c2=178;

UPDATE t6 SET c1=c1+2,c2=c1+c2 WHERE c2=46 OR c2=b'1001';

UPDATE t6 SET c2=123 WHERE c1 IN (30,b'101',88);


# Update outside range would be clipped to closest endpoints
UPDATE t6 SET c1=b'1111111111111111111111111111111111111111111111111111111111111111111' WHERE c2 < 10; 

UPDATE t6 SET c2=-1 WHERE c1=94;


# Update ignore on bad null error
--sorted_result
SELECT hex(c1) FROM t5 WHERE c1<>0;
SET SQL_MODE=STRICT_ALL_TABLES;
--error ER_BAD_NULL_ERROR
UPDATE t5 SET c1=NULL WHERE c1<>0;
UPDATE IGNORE t5 SET c1=NULL WHERE c1>100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
SET SQL_MODE=DEFAULT;

#Multi table update
--disable_warnings
UPDATE t5,t6 SET t5.c2=t6.c1+t5.c1, t6.c2=t5.c1+t6.c1 WHERE t5.c1 > 100 OR t6.c2 > 100;
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Update using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
UPDATE t5 SET c2='a' WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
UPDATE t5 SET c2=13 where c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--disable_warnings
UPDATE t5 SET c2=c2+10 WHERE c1 IN (b'001',b'101',b'111');
--enable_warnings
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
UPDATE t5,t6 SET t5.c2=t6.c1+t6.c2 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

############# DELETE ###########

TRUNCATE t5;
TRUNCATE t6;

#Borrowed from suite/ndb/t/ndb_bitfield.test
INSERT IGNORE INTO t5 VALUES (95, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (69, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135);
INSERT INTO t6 VALUES (94, 46), (31, 438), (61, 152), (78, 123), (88, 411), (122, 118), (0, 177),(75, 42), (108, 67), (79, 349), (59, 188), (68, 206), (49, 345), (118, 380),(111, 368), (94, 468), (56, 379), (77, 133), (29, 399), (9, 363), (23, 36),(116, 390), (119, 368), (87, 351), (123, 411), (24, 398), (34, 202), (28, 499),(30, 83), (5, 178), (60, 343), (4, 245), (104, 280), (106, 446), (127, 403),(44, 307), (68, 454), (57, 135),(null,23),(1,null),(null,null);

# Delete by order by limit
DELETE FROM t6  WHERE c2 > 100 ORDER BY c2 LIMIT 1;


# Delete rows with NULL column
DELETE FROM t6 WHERE c2=NULL;


# Delete with range values 
DELETE FROM t6 WHERE c1=b'101' AND c2=178;

DELETE FROM t6  WHERE c2=46 OR c2=b'1001';

DELETE FROM t6 WHERE c1 IN (30,b'101',88);


#Multi table delete
DELETE t5,t6 FROM t5,t6 WHERE t5.c1 > 100 OR t6.c2 > 100;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;


# Delete using various access methods
TRUNCATE t5;
TRUNCATE t6;
INSERT IGNORE INTO t5 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);
INSERT INTO t6 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

# Update using Const
# EXPLAIN SELECT * FROM t5 WHERE c1=6 AND c2=6;
DELETE FROM t5  WHERE c1=6 AND c2=6;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using range
# EXPLAIN SELECT * FROM t5 WHERE c1 BETWEEN 5 AND 9;
DELETE FROM t5 WHERE c1 BETWEEN 5 AND 9;
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;
# EXPLAIN SELECT * FROM t5 WHERE c1 IN (b'001',b'101',b'111');
DELETE FROM t5 WHERE c1 IN (b'001',b'101',b'111');
--sorted_result
SELECT hex(c1),hex(c2) FROM t5;

# Update using eq_ref
# EXPLAIN SELECT * FROM t5,t6 WHERE t5.c1=t6.c1 AND t5.c2=t6.c2;
DELETE t5,t6 FROM t5,t6 WHERE  t5.c1=t6.c1 AND t5.c2=t6.c2;

# Droping the tables
DROP TABLE IF EXISTS t1,t2,t3,t4,t5,t6,t7;

