|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazon.opendistroforelasticsearch.sql.esintgtest; |
| 17 | + |
| 18 | +import org.json.JSONArray; |
| 19 | +import org.json.JSONObject; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import static com.amazon.opendistroforelasticsearch.sql.esintgtest.TestsConstants.TEST_INDEX_ACCOUNT; |
| 29 | +import static org.hamcrest.Matchers.equalTo; |
| 30 | + |
| 31 | +/** |
| 32 | + * Test new hash join algorithm by comparison with old implementation. |
| 33 | + */ |
| 34 | +public class HashJoinBasicIT extends SQLIntegTestCase { |
| 35 | + |
| 36 | + /** |
| 37 | + * Hint to use old join algorithm |
| 38 | + */ |
| 39 | + private static final String USE_OLD_JOIN_ALGORITHM = "/*! USE_NL*/"; |
| 40 | + |
| 41 | + /** |
| 42 | + * Set limit to 100% to bypass circuit break check |
| 43 | + */ |
| 44 | + private static final String BYPASS_CIRCUIT_BREAK = "/*! JOIN_CIRCUIT_BREAK_LIMIT(100)*/"; |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void init() throws Exception { |
| 48 | + loadIndex(Index.ACCOUNT); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void innerJoin() throws IOException { |
| 53 | + |
| 54 | + testJoin("INNER JOIN"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void leftJoin() throws IOException { |
| 59 | + |
| 60 | + testJoin("LEFT JOIN"); |
| 61 | + } |
| 62 | + |
| 63 | + public void testJoin(final String join) throws IOException { |
| 64 | + |
| 65 | + final String queryPrefix = "SELECT"; |
| 66 | + |
| 67 | + // TODO: reduce the balance threshold to 10000 when the memory circuit breaker issue |
| 68 | + // (https://github.com/opendistro-for-elasticsearch/sql/issues/73) is fixed. |
| 69 | + final String querySuffixTemplate = "a.firstname, a.lastname, b.city, b.state FROM %1$s a %2$s %1$s b " + |
| 70 | + "ON b.age = a.age WHERE a.balance > 45000 AND b.age > 25 LIMIT 1000000"; |
| 71 | + final String querySuffix = String.format(Locale.ROOT, querySuffixTemplate, TEST_INDEX_ACCOUNT, join); |
| 72 | + |
| 73 | + final String oldQuery = String.join(" ", queryPrefix, USE_OLD_JOIN_ALGORITHM, querySuffix); |
| 74 | + final String newQuery = String.join(" ", queryPrefix, BYPASS_CIRCUIT_BREAK, querySuffix); |
| 75 | + |
| 76 | + final JSONObject responseOld = executeQuery(oldQuery); |
| 77 | + final JSONObject responseNew = executeQuery(newQuery); |
| 78 | + |
| 79 | + Assert.assertThat(getTotalHits(responseOld), equalTo(getTotalHits(responseNew))); |
| 80 | + |
| 81 | + final JSONArray hitsOld = getHits(responseOld); |
| 82 | + final JSONArray hitsNew = getHits(responseNew); |
| 83 | + |
| 84 | + Assert.assertThat(hitsOld.length(), equalTo(hitsNew.length())); |
| 85 | + |
| 86 | + Set<String> idsOld = new HashSet<>(); |
| 87 | + |
| 88 | + hitsOld.forEach(hitObj -> { |
| 89 | + JSONObject hit = (JSONObject) hitObj; |
| 90 | + idsOld.add(hit.getString("_id")); |
| 91 | + }); |
| 92 | + |
| 93 | + hitsNew.forEach(hitObj -> { |
| 94 | + JSONObject hit = (JSONObject) hitObj; |
| 95 | + Assert.assertTrue(idsOld.contains(hit.getString("_id"))); |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
0 commit comments