2 Copyright (c) 2011, 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;
20 import com.mysql.clusterj.ClusterJUserException;
22 import java.util.Arrays;
24 import testsuite.clusterj.model.LongIntStringIndex;
26 /** Query for columns compared via IN.
27 * Predicates using IN cannot use indexes, although indexes can
28 * be used for AND predicates where some of the predicates are IN
30 * This test is based on AbstractQueryTest.
32 public class QueryMultiColumnIndexInTest extends AbstractQueryTest {
34 drop table if exists longintstringix;
35 create table longintstringix (
37 longix bigint(20) not null,
38 stringix varchar(10) not null,
39 intix int(11) not null,
40 stringvalue varchar(10) default null,
42 KEY idx_long_int_string (longix, intix, stringix)
43 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
48 public Class<?> getInstanceType() {
49 return LongIntStringIndex.class;
52 protected int PK_MODULUS = 3;
53 protected long PRETTY_BIG_NUMBER = 1000000000000000L;
56 protected boolean getCleanupAfterTest() {
60 public void testInAndBetween() {
61 inAndBetweenQuery("longix", new Object[] {1000000000000000L, 0L}, "intix", 1, 2, "idx_long_int_string", 3, 4, 5, 6, 7, 8);
62 inAndBetweenQuery("longix", Arrays.asList(new Object[] {1000000000000000L, 0L}), "stringix", "1", "4", "idx_long_int_string", 1, 2, 4, 5, 7, 8);
66 public void testBetweenAndIn() {
67 betweenAndInQuery("longix", 0L, 3000000000000000L, "intix", new Object[] {2, 0}, "idx_long_int_string", 0, 1, 2, 6, 7, 8, 9);
68 betweenAndInQuery("longix", 0L, 1000000000000000L, "intix", Arrays.asList(new Object[] {2, 1}), "idx_long_int_string", 3, 4, 5, 6, 7, 8);
72 public void testPartialBoundsAndEqual() {
73 greaterThanAnd1ExtraQuery("longix", 0, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 9);
74 greaterEqualAnd1ExtraQuery("longix", 0, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 0, 1, 2, 9);
75 lessThanAnd1ExtraQuery("longix", 1000000000000000L, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 0, 1, 2);
76 lessEqualAnd1ExtraQuery("longix", 1000000000000000L, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 0, 1, 2, 9);
80 public void testGapBoundsAndEqual() {
81 greaterThanAnd1ExtraQuery("longix", 0, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 9);
82 greaterEqualAnd1ExtraQuery("longix", 0, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 0, 3, 6, 9);
83 lessThanAnd1ExtraQuery("longix", 1000000000000000L, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 0, 3, 6);
84 lessEqualAnd1ExtraQuery("longix", 1000000000000000L, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 0, 3, 6, 9);
88 public void testNegativeInParameter() {
89 int keys[] = new int[0];
91 inQuery("id", keys, "PRIMARY", keys);
92 error("Query with ''in'' parameter of int[] type should fail.");
93 } catch (ClusterJUserException e) {
97 String message = e.getMessage();
98 if (!message.contains("id")) {
99 error("Query with ''in'' parameter of int[] type should fail.");
105 public void testPrettyBigIn() {
106 int arraySize = 4096;
107 Integer[] keys = new Integer[arraySize];
108 for (int i = 0; i < arraySize; ++i) {
111 int[] expectedKeys = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
112 inQuery("id", keys, "PRIMARY", expectedKeys);
116 public void testNegativeInTooBig() {
117 int arraySize = 4097;
118 Integer[] keys = new Integer[arraySize];
119 for (int i = 0; i < arraySize; ++i) {
122 int[] expectedKeys = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
124 inQuery("id", keys, "PRIMARY", expectedKeys);
125 error("Query with more than 4096 elements should fail.");
126 } catch (ClusterJUserException e) {
130 String message = e.getMessage();
131 if (!message.contains("4097")) {
132 error("Query with more than 4096 elements should fail.");
138 /** The strategy for instances is for the "instance number" to create
139 * the three keys, such that the value of the instance is:
140 * pk1 * PK_MODULUS^2 + pk2 * PK_MODULUS + pk3
143 protected void createInstances(int number) {
144 for (int i = 0; i < number; ++i) {
145 LongIntStringIndex instance = createInstance(i);
146 //System.out.println(toString(instance));
147 instances.add(instance);
151 /** Create an instance of LongIntStringPK.
152 * @param index the index to use to generate data
153 * @return the instance
155 protected LongIntStringIndex createInstance(int index) {
156 LongIntStringIndex instance = session.newInstance(LongIntStringIndex.class);
157 instance.setId(index);
158 instance.setLongix(getPK1(index));
159 instance.setIntix(getPK2(index));
160 instance.setStringix(getPK3(index));
161 instance.setStringvalue(getValue(index));
165 protected long getPK1(int index) {
166 return PRETTY_BIG_NUMBER * ((index / PK_MODULUS / PK_MODULUS) % PK_MODULUS);
169 protected int getPK2(int index) {
170 return ((index / PK_MODULUS) % PK_MODULUS);
173 protected String getPK3(int index) {
174 return "" + (index % PK_MODULUS);
177 protected String getValue(int index) {
178 return "Value " + index;
181 protected String toString(LongIntStringIndex instance) {
182 StringBuffer result = new StringBuffer();
183 result.append("LongIntStringIndex[");
184 result.append(instance.getId());
185 result.append("]: ");
186 result.append(instance.getLongix());
188 result.append(instance.getIntix());
189 result.append(", \"");
190 result.append(instance.getStringix());
191 result.append("\", \"");
192 result.append(instance.getStringvalue());
193 result.append("\".");
194 return result.toString();