<?xml version="1.0" encoding="utf-8"?>
<test>
<name>select expressions, functions, operators</name> <!-- all the misc tests -->

<config>
indexer
{
	mem_limit = 16M
}

searchd
{
	<searchd_settings/>
	workers = threads
	thread_stack = 256K
}

source test
{
	type = mysql
	<sql_settings/>
	sql_query = SELECT *, '1,2,3,4,5,6.0' AS poly2d_attr FROM test_table
	sql_attr_uint = aa
	sql_attr_multi = uint mm from field
	sql_attr_string = kk
	sql_attr_float = f1
	sql_attr_float = f2
	sql_attr_string = poly2d_attr
}

index test
{
	source	= test
	path	= <data_path/>/test
}

source cmp
{
	type = mysql
	<sql_settings/>
	sql_query = SELECT * FROM cmp_table
	sql_attr_multi = uint mva32 from field
	sql_attr_multi = bigint mva64 from field
	sql_attr_float = f1
	sql_attr_uint = gid
}
index cmp
{
	source	= cmp
	path	= <data_path/>/cmp
}

source cmp2
{
	type = mysql
	<sql_settings/>
	sql_query = SELECT * FROM cmp_table
	sql_attr_uint = f1
	sql_attr_uint = gid
}
index cmp2
{
	source	= cmp2
	path	= <data_path/>/cmp2
}
</config>

<db_create>
CREATE TABLE test_table
(
	id INT NOT NULL,
	aa INT NOT NULL,
	title VARCHAR(255) NOT NULL,
	mm VARCHAR(255) NOT NULL,
	kk VARCHAR(255) NOT NULL,
	f1 FLOAT NOT NULL,
	f2 FLOAT NOT NULL
);
</db_create>

<db_create>
CREATE TABLE cmp_table
(
	id INT NOT NULL,
	title VARCHAR(255) NOT NULL,
	mva32 VARCHAR(255) NOT NULL,
	mva64 VARCHAR(255) NOT NULL,
	f1 FLOAT NOT NULL,
	gid INT NOT NULL
);
</db_create>

<db_drop>DROP TABLE IF EXISTS test_table;</db_drop>
<db_drop>DROP TABLE IF EXISTS cmp_table;</db_drop>


<db_insert>
INSERT INTO test_table ( id, aa, title, mm, kk, f1, f2 ) VALUES
( 1, 2, 'dummy', '', 'one', 0, 0 ),
( 10, 3, 'ohai', '7 40', 'ten', 0, 0 ),
( 11, 4, 'ohai2', '', 'eleventy', 0.123123, 0.456789 )
</db_insert>

<db_insert>
INSERT INTO cmp_table VALUES
( 1, 'dummy', '101, 107, 102', '-100, -50, 20, 70', 1.2, 10 ),
( 2, 'dummy', '201, 220, 210', '-200, -350 ', 21.2, 20 ),
( 3, 'dummy', '310, 330, 301', '-300, -350, 320', 31.3, 33 ),
( 4, 'dummy', '900, 400, 601', '1000, 500, 401', 101.2, 110 )
</db_insert>

<query_attributes>
	<attr>aa</attr>
	<attr>sel</attr>
</query_attributes>

<queries>
	<query select="aa div 2 as sel, *" index="test"/>
	<query select="aa / 2 as sel, *" index="test"/>
	<query select="aa mod 2 as sel, *" index="test"/>
	<query select="aa % 2 as sel, *" index="test"/>
</queries>
	

<sphqueries>

<!-- test boolean operator precedence -->
<sphinxql>SELECT 0 AND 0 OR 1 AS a, 0 AND 1 OR 1 AS b, 1 OR 0 AND 0 AS c, 1 OR 1 AND 0 AS d FROM test WHERE id=1</sphinxql>

<!-- test common ariphmetic -->
<sphinxql>SELECT id, aa div 2 as sel from test</sphinxql>
<sphinxql>SELECT id, aa / 2 as sel from test</sphinxql>
<sphinxql>SELECT id, aa mod 2 as sel from test</sphinxql>
<sphinxql>SELECT id, aa % 2 as sel from test</sphinxql>

<!-- test argument type check -->
<sphinxql>SELECT id, CRC32('test') FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT id, ABS(CRC32('test')) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT id, IF(IN(mm,7),111,222) FROM test</sphinxql>
<sphinxql>SELECT IN(aa-(aa-15), 15) FROM test WHERE id=10</sphinxql>

<!-- various function and operators for test coverage -->
<sphinxql>SELECT SINT(1-2) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT FIBONACCI(5) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT INTERVAL(1, -1,3,5.0) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT INTERVAL(id, id-1, id+2) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT BITDOT(5, 11,33,55) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT GEODIST(0,0,1,1) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT CONTAINS(POLY2D(1,2,3,4,5,6.0),2,3), CONTAINS(POLY2D(1.0,1+1,aa,4,5,6),22,3) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT CONTAINS(GEOPOLY2D(55.7522222, 37.6155556, 40.7141667, -74.0063889, 37.9833333, 23.7333333), 51.5, -0.116667) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT CONTAINS(GEOPOLY2D(55.7522222, 37.6155556, 40.7141667, -74.0063889, 37.9833333, 23.7333333), 43.25, -2.9666667) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT CRC32(TO_STRING(123 + 877)) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT ABS(-1), ABS(-1.1), CEIL(1.3), FLOOR(1.3), COS(3.14/2), LN(2.7), LOG10(100), EXP(1) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT SINT(aa), NOT SINT(aa), CRC32(kk) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT IN(id, 10.1) FROM test WHERE id=10;</sphinxql>

<sphinxql>SELECT (1=1 AND 1=0), (1=1 AND 1=1), (1=0 AND 1=0), (1=0 AND 1=1) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT (1=1 OR 1=0), (1=1 OR 1=1), (1=0 OR 1=0), (1=0 OR 1=1) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT NOT 1=1, NOT 1=0 FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT 1&lt;=1, 1&lt;=2, 1&lt;&gt;1, 1&lt;&gt;0 FROM test WHERE id=10</sphinxql>

<sphinxql>SELECT 3/2, id*2-2.1, 1.0-(1.0+id), 2*(3*id), 2.0/(3.0*id), 3 div 2, 3.1 div 2.2 FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT (1-id)+id, (1.0/id)*id, id/2.5 FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT 1&amp;3, 1&amp;2, 1|3, 1|2, 13%10, 3%10 FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+11 FROM test WHERE id=10</sphinxql>

<sphinxql>SET GLOBAL @happyvar = (432)</sphinxql>
<sphinxql>SELECT IN(mm, @happyvar) FROM test WHERE id=10</sphinxql>

<sphinxql>SELECT FLOOR(f1*1000)*100000 + FLOOR(f2*1000) FROM test WHERE id=11</sphinxql>
<sphinxql>SELECT FLOOR(f1*1000)*10000000 + FLOOR(f2*1000) FROM test WHERE id=11</sphinxql>

<!-- added <,> comparsion to float and int to float conversion at filter -->
<sphinxql>SELECT id FROM cmp, cmp2 WHERE f1>22</sphinxql>
<sphinxql>SELECT id FROM cmp, cmp2 WHERE f1 between 21 and 32</sphinxql>
<sphinxql>SELECT id FROM cmp WHERE f1&lt;31.3</sphinxql>
<sphinxql>SELECT id FROM cmp WHERE f1&gt;=31.3</sphinxql>
<sphinxql>SELECT id, mva32 FROM cmp WHERE mva32&lt;=310</sphinxql>
<sphinxql>SELECT id, mva32 FROM cmp WHERE mva32&gt;220</sphinxql>
<sphinxql>SELECT id, mva32 FROM cmp WHERE mva32&lt;301</sphinxql>
<sphinxql>SELECT id, mva32 FROM cmp WHERE mva32&gt;=330</sphinxql>
<sphinxql>SELECT id, mva64 FROM cmp WHERE mva64&lt;=-350</sphinxql>
<sphinxql>SELECT id, mva64 FROM cmp WHERE mva64&gt;-200</sphinxql>
<sphinxql>SELECT id, mva64 FROM cmp WHERE mva64 between -100 and 400</sphinxql>
<sphinxql>SELECT id, mva32 FROM cmp WHERE mva32 between 105 and 205</sphinxql>

<!-- lets check that WEIGHT() works in expressions -->
<sphinxql>SELECT id, WEIGHT()*10 FROM test WHERE MATCH('ohai')</sphinxql>

<!-- check strings vs unescaping -->
<sphinxql>SELECT crc32('one two'), crc32('one\ttwo'), crc32('one\ntwo') FROM test WHERE id=1</sphinxql>

<!-- check misc crazy stuff -->
<sphinxql>SELECT @@session.auto_increment_increment</sphinxql>
<sphinxql>SHOW COLLATION</sphinxql>
<sphinxql>SHOW CHARACTER SET</sphinxql>

<!-- added check of different functions against MVA attributes -->
<sphinxql>SELECT id, IN(mva32,220,230) from cmp WHERE id=2</sphinxql>
<!--fixme! The query below must be processed without errors -->
<sphinxql>SELECT id, IN(mva64,-350,230) from cmp WHERE id=2</sphinxql>
<sphinxql>SELECT id, IN(mva64,320) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, to_string(mva32) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, to_string(mva64) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, length(mva32) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, length(mva64) from cmp WHERE id=3</sphinxql>
<!-- Not in REL21 <sphinxql>SELECT id, least(mva32) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, least(mva64) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, greatest(mva32) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, greatest(mva64) from cmp WHERE id=3</sphinxql> -->

<sphinxql>SELECT id, min(mva32) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, max(mva32) from cmp WHERE id=3</sphinxql>

<sphinxql>SELECT id, min(mva64) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, max(mva64) from cmp WHERE id=3</sphinxql>


<sphinxql>SELECT id, log10(mva32) from cmp WHERE id=3</sphinxql>
<sphinxql>SELECT id, log10(mva64) from cmp WHERE id=3</sphinxql>

<!-- .NET connector uses "select timediff(curtime(),utc_time());" -->
<sphinxql>SELECT bigint(timediff(curtime(),utc_time())) % 15</sphinxql>

<!-- MySQL Workbench 6.0 and above -->
<sphinxql>SHOW SESSION VARIABLES LIKE 'lower_case_table_names'</sphinxql>
<sphinxql>SELECT current_user()</sphinxql>
<sphinxql>SELECT connection_id()</sphinxql>
<sphinxql>SET CHARACTER SET utf8</sphinxql>
<sphinxql>SET SQL_SAFE_UPDATES=1</sphinxql>
<sphinxql>SHOW SESSION VARIABLES LIKE 'sql_mode'</sphinxql>
<sphinxql>SHOW DATABASES</sphinxql>

<sphinxql>SELECT CONTAINS(POLY2D(poly2d_attr),2,3) FROM test LIMIT 1</sphinxql>

<sphinxql>SELECT 0 zero, 1/0, 1/zero FROM cmp LIMIT 1</sphinxql>
<sphinxql>SELECT 0 zero, SINT(-1) negative, ln(0), ln(zero), ln(-1), ln(negative) FROM cmp LIMIT 1</sphinxql>
<sphinxql>SELECT 0 zero, SINT(-1) negative, log2(0), log2(zero), log2(-1), log2(negative) FROM cmp LIMIT 1</sphinxql>
<sphinxql>SELECT 0 zero, SINT(-1) negative, log10(0), log10(zero), log10(-1), ln(negative) FROM cmp LIMIT 1</sphinxql>
<sphinxql>SELECT 0 zero, SINT(-1) negative, sqrt(0), sqrt(zero), sqrt(-1), sqrt(negative) FROM cmp LIMIT 1</sphinxql>

<!-- IN() function for string attributes -->
<sphinxql>SELECT IN(kk,'one','ten','eleventy') FROM test</sphinxql>
<sphinxql>SELECT IN(kk,'is','not','there') FROM test</sphinxql>

<!-- ATAN2() function -->
<sphinxql>SELECT ATAN2(0,1) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT ATAN2(1,0) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT ATAN2(0.75,0.25) FROM test WHERE id=10</sphinxql>
<sphinxql>SELECT ATAN2(0.25,0.75) FROM test WHERE id=10</sphinxql>

<sphinxql>SELECT GEODIST(0,0,1,1,{in=radians,out=kilometers}) FROM test WHERE id=10</sphinxql>

<!-- .NET connector 6.9.6 uses select timediff(now(), utc_timestamp()) -->
<sphinxql>SELECT bigint(timediff(now(),utc_timestamp())) % 15</sphinxql>

</sphqueries>

</test>
