2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package testsuite.clusterj;
22 import com.mysql.clusterj.ClusterJUserException;
23 import com.mysql.clusterj.Constants;
24 import com.mysql.clusterj.Query;
25 import com.mysql.clusterj.query.QueryBuilder;
26 import com.mysql.clusterj.query.QueryDomainType;
28 import testsuite.clusterj.AbstractQueryTest.QueryHolder;
29 import testsuite.clusterj.model.AllPrimitives;
32 drop table if exists allprimitives;
33 create table allprimitives (
34 id int not null primary key,
36 int_not_null_hash int not null,
37 int_not_null_btree int not null,
38 int_not_null_both int not null,
39 int_not_null_none int not null,
45 byte_not_null_hash tinyint not null,
46 byte_not_null_btree tinyint not null,
47 byte_not_null_both tinyint not null,
48 byte_not_null_none tinyint not null,
49 byte_null_hash tinyint,
50 byte_null_btree tinyint,
51 byte_null_both tinyint,
52 byte_null_none tinyint,
54 short_not_null_hash smallint not null,
55 short_not_null_btree smallint not null,
56 short_not_null_both smallint not null,
57 short_not_null_none smallint not null,
58 short_null_hash smallint,
59 short_null_btree smallint,
60 short_null_both smallint,
61 short_null_none smallint,
63 long_not_null_hash bigint not null,
64 long_not_null_btree bigint not null,
65 long_not_null_both bigint not null,
66 long_not_null_none bigint not null,
67 long_null_hash bigint,
68 long_null_btree bigint,
69 long_null_both bigint,
70 long_null_none bigint *
72 public class QueryExplainTest extends AbstractQueryTest {
75 public Class getInstanceType() {
76 return AllPrimitives.class;
80 void createInstances(int number) {
81 createAllPrimitivesInstances(10);
84 public void testExplainWithNoWhereClause() {
85 QueryBuilder builder = session.getQueryBuilder();
86 QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
87 Query<AllPrimitives> query = session.createQuery(dobj);
88 Map<String, Object> result = query.explain();
89 String indexUsed = result.get(Query.INDEX_USED).toString();
90 String scanType = result.get(Query.SCAN_TYPE).toString();
91 assertEquals("Query explain with no where clause should have index none", "none", indexUsed);
92 assertEquals("Query explain with no where clause should have scan type TABLE_SCAN", "TABLE_SCAN", scanType);
95 public void testExplainBeforeBindingParameters() {
96 QueryBuilder builder = session.getQueryBuilder();
97 QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
98 dobj.where(dobj.get("int_null_none").equal(dobj.param("equal")));
99 Query<AllPrimitives> query = session.createQuery(dobj);
102 fail("Explain before binding parameters should throw ClusterJUserException");
103 } catch (ClusterJUserException ex) {
104 // good catch; make sure message includes parameter name "equal"
105 assertTrue("Message should include parameter name \"equal\"", ex.getMessage().contains("equal"));
109 public void testExplainAfterBindingParametersNoIndexEqual() {
110 QueryBuilder builder = session.getQueryBuilder();
111 QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
112 dobj.where(dobj.get("int_null_none").equal(dobj.param("equal")));
113 Query<AllPrimitives> query = session.createQuery(dobj);
114 query.setParameter("equal", 1);
115 Map<String, Object> result = query.explain();
116 String indexUsed = result.get(Query.INDEX_USED).toString();
117 String scanType = result.get(Query.SCAN_TYPE).toString();
118 assertEquals("Query explain with no index should have index none", "none", indexUsed);
119 assertEquals("Query explain with no index should have scan type TABLE_SCAN", Query.SCAN_TYPE_TABLE_SCAN, scanType);
122 public void testExplainAfterBindingParametersUniqueEqual() {
123 QueryBuilder builder = session.getQueryBuilder();
124 QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
125 dobj.where(dobj.get("int_not_null_hash").equal(dobj.param("equal")));
126 Query<AllPrimitives> query = session.createQuery(dobj);
127 query.setParameter("equal", 1);
128 Map<String, Object> result = query.explain();
129 String indexUsed = result.get(Query.INDEX_USED).toString();
130 String scanType = result.get(Query.SCAN_TYPE).toString();
131 assertEquals("Query explain with PRIMARY key equal should have index int_not_null_hash", "idx_int_not_null_hash", indexUsed);
132 assertEquals("Query explain with PRIMARY key equal should have scan type UNIQUE_KEY", Query.SCAN_TYPE_UNIQUE_KEY, scanType);
135 public void testExplainAfterBindingParametersPrimaryEqual() {
136 QueryBuilder builder = session.getQueryBuilder();
137 QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
138 dobj.where(dobj.get("id").equal(dobj.param("equal")));
139 Query<AllPrimitives> query = session.createQuery(dobj);
140 query.setParameter("equal", 1);
141 Map<String, Object> result = query.explain();
142 String indexUsed = result.get(Query.INDEX_USED).toString();
143 String scanType = result.get(Query.SCAN_TYPE).toString();
144 assertEquals("Query explain with PRIMARY key equal should have index PRIMARY", "PRIMARY", indexUsed);
145 assertEquals("Query explain with PRIMARY key equal should have scan type PRIMARY_KEY", Query.SCAN_TYPE_PRIMARY_KEY, scanType);
148 public void testExplainAfterBindingParametersPrimaryLessThan() {
149 QueryBuilder builder = session.getQueryBuilder();
150 QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
151 dobj.where(dobj.get("id").lessThan(dobj.param("lessThan")));
152 Query<AllPrimitives> query = session.createQuery(dobj);
153 query.setParameter("lessThan", 1);
154 Map<String, Object> result = query.explain();
155 String indexUsed = result.get(Query.INDEX_USED).toString();
156 String scanType = result.get(Query.SCAN_TYPE).toString();
157 assertEquals("Query explain with PRIMARY key lessThan should have index PRIMARY", "PRIMARY", indexUsed);
158 assertEquals("Query explain with PRIMARY key lessThan should have scan type INDEX_SCAN", Query.SCAN_TYPE_INDEX_SCAN, scanType);
161 public void testExplainAfterBindingParametersPrimaryLessThanNull() {
162 QueryBuilder builder = session.getQueryBuilder();
163 QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
164 dobj.where(dobj.get("id").lessThan(dobj.param("lessThan")));
165 Query<AllPrimitives> query = session.createQuery(dobj);
166 query.setParameter("lessThan", null);
167 Map<String, Object> result = query.explain();
168 String indexUsed = result.get(Query.INDEX_USED).toString();
169 String scanType = result.get(Query.SCAN_TYPE).toString();
170 assertEquals("Query explain with PRIMARY key lessThan null should have index none", "none", indexUsed);
171 assertEquals("Query explain with PRIMARY key lessThan null should have scan type TABLE_SCAN", Query.SCAN_TYPE_TABLE_SCAN, scanType);