1+ Test for issue #3844: MAX()/AVG()/MIN()/SUM() on text field leads to crash
2+ https://github.com/manticoresoftware/manticoresearch/issues/3844
3+
4+ Bug: Using MAX(), AVG(), MIN(), or SUM() on a text/string column previously caused a crash or freeze.
5+ Fix: Now returns proper error message instead of crashing.
6+
7+ This test verifies:
8+ 1. Aggregation functions (MAX, MIN, SUM, AVG) properly error on full-text text fields instead of crashing
9+ 2. Aggregation functions (MAX, MIN, SUM, AVG) properly error on string attributes instead of crashing (row-wise storage)
10+ 3. Aggregation functions (MAX, MIN, SUM, AVG) properly error on string attributes instead of crashing (columnar storage)
11+ 4. COUNT(DISTINCT) on string attributes correctly identifies unique values with duplicates
12+ 5. Aggregation functions work on numeric fields when grouping BY string attributes
13+
14+ ––– comment –––
15+ Start Manticore Search
16+ ––– block: ../base/start-searchd –––
17+ ––– comment –––
18+ === Test 1: Aggregations on full-text text field (should fail) ===
19+ ––– comment –––
20+ Drop table if exists and create table with text field
21+ ––– input –––
22+ mysql -h0 -P9306 -e "drop table if exists t; create table t(s text);"
23+ ––– output –––
24+ ––– comment –––
25+ Insert test data
26+ ––– input –––
27+ mysql -h0 -P9306 -e "insert into t values(1, 'a');"
28+ ––– output –––
29+ ––– comment –––
30+ Test MAX() on text field - should return error instead of crashing
31+ ––– input –––
32+ mysql -h0 -P9306 -e "select max(s) from t;"
33+ ––– output –––
34+ ERROR 1064 (42000) at line 1: table t: MAX() cannot be used on text/string column 's'
35+ ––– comment –––
36+ Test AVG() on text field - should return error instead of crashing
37+ ––– input –––
38+ mysql -h0 -P9306 -e "select avg(s) from t;"
39+ ––– output –––
40+ ERROR 1064 (42000) at line 1: table t: AVG() cannot be used on text/string column 's'
41+ ––– comment –––
42+ Test MIN() on text field - should return error instead of crashing
43+ ––– input –––
44+ mysql -h0 -P9306 -e "select min(s) from t;"
45+ ––– output –––
46+ ERROR 1064 (42000) at line 1: table t: MIN() cannot be used on text/string column 's'
47+ ––– comment –––
48+ Test SUM() on text field - should return error instead of crashing
49+ ––– input –––
50+ mysql -h0 -P9306 -e "select sum(s) from t;"
51+ ––– output –––
52+ ERROR 1064 (42000) at line 1: table t: SUM() cannot be used on text/string column 's'
53+ ––– comment –––
54+ === Test 2: MAX/MIN on string attributes when grouping BY numeric fields (row-wise storage) ===
55+ ––– comment –––
56+ Create table with string attribute, numeric category, and numeric attributes (row-wise storage)
57+ Test that MAX/MIN work on string attributes when grouping BY numeric fields
58+ ––– input –––
59+ mysql -h0 -P9306 -e "drop table if exists t_str_rowwise; create table t_str_rowwise(category_id int, name string, price int);"
60+ ––– output –––
61+ ––– comment –––
62+ Insert test data with categories and duplicate strings
63+ ––– input –––
64+ mysql -h0 -P9306 -e "insert into t_str_rowwise(id, category_id, name, price) values(1, 1, 'apple', 10), (2, 1, 'banana', 15), (3, 2, 'apple', 20), (4, 2, 'cherry', 25), (5, 1, 'date', 30);"
65+ ––– output –––
66+ ––– comment –––
67+ Test MAX on string attribute without GROUP BY - should return error (but not crash)
68+ String attributes cannot be aggregated without GROUP BY context
69+ ––– input –––
70+ mysql -h0 -P9306 -e "select max(name) from t_str_rowwise;"
71+ ––– output –––
72+ ERROR 1064 (42000) at line 1: table t_str_rowwise: can not aggregate non-scalar attribute 'name'
73+ ––– comment –––
74+ Test MIN on string attribute without GROUP BY - should return error (but not crash)
75+ ––– input –––
76+ mysql -h0 -P9306 -e "select min(name) from t_str_rowwise;"
77+ ––– output –––
78+ ERROR 1064 (42000) at line 1: table t_str_rowwise: can not aggregate non-scalar attribute 'name'
79+ ––– comment –––
80+ Test AVG on string attribute without GROUP BY - should return error (but not crash)
81+ ––– input –––
82+ mysql -h0 -P9306 -e "select avg(name) from t_str_rowwise;"
83+ ––– output –––
84+ ERROR 1064 (42000) at line 1: table t_str_rowwise: can not aggregate non-scalar attribute 'name'
85+ ––– comment –––
86+ Test SUM on string attribute without GROUP BY - should return error (but not crash)
87+ ––– input –––
88+ mysql -h0 -P9306 -e "select sum(name) from t_str_rowwise;"
89+ ––– output –––
90+ ERROR 1064 (42000) at line 1: table t_str_rowwise: can not aggregate non-scalar attribute 'name'
91+ ––– comment –––
92+ Test GROUP BY string attribute with aggregations on numeric field (price) - should work
93+ Group BY string attribute and aggregate numeric values within each string group
94+ ––– input –––
95+ mysql -h0 -P9306 -e "select name, count(*), sum(price), max(price), min(price), avg(price) from t_str_rowwise group by name order by count(*) desc;"
96+ ––– output –––
97+ +--------+----------+------------+------------+------------+------------+
98+ | name | count(*) | sum(price) | max(price) | min(price) | avg(price) |
99+ +--------+----------+------------+------------+------------+------------+
100+ | apple | 2 | 30 | 20 | 10 | 15.000000 |
101+ | banana | 1 | 15 | 15 | 15 | 15.000000 |
102+ | cherry | 1 | 25 | 25 | 25 | 25.000000 |
103+ | date | 1 | 30 | 30 | 30 | 30.000000 |
104+ +--------+----------+------------+------------+------------+------------+
105+ ––– comment –––
106+ Test COUNT(DISTINCT) on string attribute with duplicates - should show unique count
107+ ––– input –––
108+ mysql -h0 -P9306 -e "select count(distinct name) from t_str_rowwise;"
109+ ––– output –––
110+ +----------------------+
111+ | count(distinct name) |
112+ +----------------------+
113+ | 4 |
114+ +----------------------+
115+ ––– comment –––
116+ === Test 3: MAX/MIN on string attributes when grouping BY numeric fields (columnar storage) ===
117+ ––– comment –––
118+ Create table with string attribute, numeric category, and numeric attributes using columnar storage
119+ Test that MAX/MIN work on string attributes when grouping BY numeric fields
120+ ––– input –––
121+ mysql -h0 -P9306 -e "drop table if exists t_str_columnar; create table t_str_columnar(category_id int, name string, price int) engine='columnar';"
122+ ––– output –––
123+ ––– comment –––
124+ Insert test data with categories and duplicate strings
125+ ––– input –––
126+ mysql -h0 -P9306 -e "insert into t_str_columnar(id, category_id, name, price) values(1, 1, 'apple', 10), (2, 1, 'banana', 15), (3, 2, 'apple', 20), (4, 2, 'cherry', 25), (5, 1, 'date', 30);"
127+ ––– output –––
128+ ––– comment –––
129+ Test MAX on string attribute without GROUP BY in columnar storage - should return error (but not crash)
130+ String attributes cannot be aggregated without GROUP BY context
131+ ––– input –––
132+ mysql -h0 -P9306 -e "select max(name) from t_str_columnar;"
133+ ––– output –––
134+ ERROR 1064 (42000) at line 1: table t_str_columnar: can not aggregate non-scalar attribute 'name'
135+ ––– comment –––
136+ Test MIN on string attribute without GROUP BY in columnar storage - should return error (but not crash)
137+ ––– input –––
138+ mysql -h0 -P9306 -e "select min(name) from t_str_columnar;"
139+ ––– output –––
140+ ERROR 1064 (42000) at line 1: table t_str_columnar: can not aggregate non-scalar attribute 'name'
141+ ––– comment –––
142+ Test AVG on string attribute without GROUP BY in columnar storage - should return error (but not crash)
143+ ––– input –––
144+ mysql -h0 -P9306 -e "select avg(name) from t_str_columnar;"
145+ ––– output –––
146+ ERROR 1064 (42000) at line 1: table t_str_columnar: can not aggregate non-scalar attribute 'name'
147+ ––– comment –––
148+ Test SUM on string attribute without GROUP BY in columnar storage - should return error (but not crash)
149+ ––– input –––
150+ mysql -h0 -P9306 -e "select sum(name) from t_str_columnar;"
151+ ––– output –––
152+ ERROR 1064 (42000) at line 1: table t_str_columnar: can not aggregate non-scalar attribute 'name'
153+ ––– comment –––
154+ Test GROUP BY string attribute with aggregations on numeric field (price) in columnar storage - should work
155+ Group BY string attribute and aggregate numeric values within each string group
156+ ––– input –––
157+ mysql -h0 -P9306 -e "select name, count(*), sum(price), max(price), min(price), avg(price) from t_str_columnar group by name order by count(*) desc;"
158+ ––– output –––
159+ +--------+----------+------------+------------+------------+------------+
160+ | name | count(*) | sum(price) | max(price) | min(price) | avg(price) |
161+ +--------+----------+------------+------------+------------+------------+
162+ | apple | 2 | 30 | 20 | 10 | 15.000000 |
163+ | banana | 1 | 15 | 15 | 15 | 15.000000 |
164+ | cherry | 1 | 25 | 25 | 25 | 25.000000 |
165+ | date | 1 | 30 | 30 | 30 | 30.000000 |
166+ +--------+----------+------------+------------+------------+------------+
167+ ––– comment –––
168+ Test COUNT(DISTINCT) on string attribute with duplicates in columnar storage - should show unique count
169+ ––– input –––
170+ mysql -h0 -P9306 -e "select count(distinct name) from t_str_columnar;"
171+ ––– output –––
172+ +----------------------+
173+ | count(distinct name) |
174+ +----------------------+
175+ | 4 |
176+ +----------------------+
177+ ––– comment –––
0 commit comments