--source include/have_falcon.inc

#
# Bug #41582: Falcon index access returns no result for indexes with
# numeric datatype
#
--echo *** Bug #41582 ***

# ----------------------------------------------------- #
# --- Initialisation                                --- #
# ----------------------------------------------------- #
let $engine = 'Falcon';
eval SET @@storage_engine = $engine;

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

# ----------------------------------------------------- #
# --- Test                                          --- #
# ----------------------------------------------------- #

# Testing first for the decimal type, then for the numeric type, which both map
# the same internal type.

# Using values with a precision greater than 9, because these will
# not fit within a group of 4 bytes. For more information about the
# storage representation of the decimal type, please see:
#
# http://dev.mysql.com/doc/refman/6.0/en/precision-math-decimal-changes.html
#
CREATE TABLE t1(id decimal(11,0), key(id));
INSERT INTO t1 VALUES (4299999998), (4299999999), (4300000000), (4300000001), (4300000002);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 4300000000;
SELECT * FROM t1 WHERE id = 4300000000;
SELECT * FROM t1 WHERE id < 4300000000;
DROP TABLE t1;

CREATE TABLE t1(id numeric(11), key(id));
INSERT INTO t1 VALUES (4299999998), (4299999999), (4300000000), (4300000001), (4300000002);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 4300000000;
SELECT * FROM t1 WHERE id = 4300000000;
SELECT * FROM t1 WHERE id < 4300000000;
DROP TABLE t1;

# Testing again using values with a precision less than 9
CREATE TABLE t1(id decimal(11,0), key(id));
INSERT INTO t1 VALUES (11), (12), (13), (14), (15);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 13;
SELECT * FROM t1 WHERE id = 13;
SELECT * FROM t1 WHERE id < 13;
DROP TABLE t1;

CREATE TABLE t1(id numeric(11), key(id));
INSERT INTO t1 VALUES (11), (12), (13), (14), (15);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 13;
SELECT * FROM t1 WHERE id = 13;
SELECT * FROM t1 WHERE id < 13;
DROP TABLE t1;

# We also check some numbers with a scale different than 0 just to be
# on the safe side. We do this for precision both greater than and
# less than 9
CREATE TABLE t1(id decimal(13,3), key(id));
INSERT INTO t1 VALUES (4299999998.123), (4299999998.125), (4300000000.123), (4300000000.125), (4300000002.123);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 4300000000.123;
SELECT * FROM t1 WHERE id = 4300000000.123;
SELECT * FROM t1 WHERE id < 4300000000.123;
DROP TABLE t1;

CREATE TABLE t1(id numeric(13,3), key(id));
INSERT INTO t1 VALUES (4299999998.123), (4299999998.125), (4300000000.123), (4300000000.125), (4300000002.123);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 4300000000.123;
SELECT * FROM t1 WHERE id = 4300000000.123;
SELECT * FROM t1 WHERE id < 4300000000.123;
DROP TABLE t1;

CREATE TABLE t1(id decimal(7,2), key(id));
INSERT INTO t1 VALUES (12.75), (13.10), (13.15), (13.27), (15.01);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 13.15;
SELECT * FROM t1 WHERE id = 13.15;
SELECT * FROM t1 WHERE id < 13.15;
DROP TABLE t1;

CREATE TABLE t1(id numeric(7,2), key(id));
INSERT INTO t1 VALUES (12.75), (13.10), (13.15), (13.27), (15.01);
SELECT count(*) FROM t1;
SELECT * FROM t1 WHERE id > 13.15;
SELECT * FROM t1 WHERE id = 13.15;
SELECT * FROM t1 WHERE id < 13.15;

# ----------------------------------------------------- #
# --- Check                                         --- #
# ----------------------------------------------------- #
SELECT count(*) FROM t1;

# ----------------------------------------------------- #
# --- Final cleanup                                 --- #
# ----------------------------------------------------- #
DROP TABLE t1;
