mike-tr-adamson commented on code in PR #2943: URL: https://github.com/apache/cassandra/pull/2943#discussion_r1410795228
########## test/unit/org/apache/cassandra/index/sai/cql/RandomItersectionTest.java: ########## @@ -0,0 +1,209 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.index.sai.cql; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.cassandra.index.sai.utils.SAIRandomizedTester; + +public class RandomItersectionTest extends SAIRandomizedTester +{ + private int numRows; + + @Before + public void createTableAndIndexes() + { + createTable("CREATE TABLE %s (pk int, ck int, v1 int, v2 int, PRIMARY KEY(pk, ck))"); + createIndex("CREATE INDEX ON %s(v1) USING 'sai'"); + createIndex("CREATE INDEX ON %s(v2) USING 'sai'"); + + numRows = nextInt(50000, 200000); + } + + @Test + public void smallPartitionPartitionRestrictedTest() + { + Map<Integer, List<TestRow>> testRowMap = buildAndLoadTestRows(false); + + for (int queryCount = 0; queryCount < nextInt(10, 100); queryCount++) + { + int pk = testRowMap.keySet().stream().skip(nextInt(0, testRowMap.size())).findFirst().get(); + int v1 = nextInt(10, numRows/10); + int v2 = nextInt(10, numRows/50); + + List<Object[]> expected = testRowMap.get(pk) + .stream() + .sorted(Comparator.comparingInt(o -> o.ck)) + .filter(row -> row.v1 > v1 && row.v2 > v2) + .map(row -> row(row.ck)) + .collect(Collectors.toList()); + + assertRowsIgnoringOrder(execute("SELECT ck FROM %s WHERE pk = ? AND v1 > ? AND v2 > ?", pk, v1, v2), expected.toArray(new Object[][]{})); + } + } + + @Test + public void smallPartitionUnrestrictedTest() + { + Map<Integer, List<TestRow>> testRowMap = buildAndLoadTestRows(false); + + for (int queryCount = 0; queryCount < nextInt(10, 100); queryCount++) + { + int v1 = nextInt(10, numRows/10); + int v2 = nextInt(10, numRows/50); + + List<Object[]> expected = testRowMap.values() + .stream() + .flatMap(list -> list.stream()) + .filter(row -> row.v1 == v1 && row.v2 == v2) + .map(row -> row(row.ck)) + .collect(Collectors.toList()); + + assertRowsIgnoringOrder(execute("SELECT ck FROM %s WHERE v1 = ? AND v2 = ?", v1, v2), expected.toArray(new Object[][]{})); + } + } + + @Test + public void largePartitionPartitionRestrictedTest() + { + Map<Integer, List<TestRow>> testRowMap = buildAndLoadTestRows(true); + + for (int queryCount = 0; queryCount < nextInt(10, 100); queryCount++) + { + int pk = testRowMap.keySet().stream().skip(nextInt(0, testRowMap.size())).findFirst().get(); + int v1 = nextInt(10, numRows/10); + int v2 = nextInt(10, numRows/50); + + List<Object[]> expected = testRowMap.get(pk) + .stream() + .sorted(Comparator.comparingInt(o -> o.ck)) + .filter(row -> row.v1 > v1 && row.v2 > v2) + .map(row -> row(row.ck)) + .collect(Collectors.toList()); + + assertRowsIgnoringOrder(execute("SELECT ck FROM %s WHERE pk = ? AND v1 > ? AND v2 > ?", pk, v1, v2), expected.toArray(new Object[][]{})); + } + } + + @Test + public void largePartitionUnrestrictedTest() + { + Map<Integer, List<TestRow>> testRowMap = buildAndLoadTestRows(true); + + for (int queryCount = 0; queryCount < nextInt(10, 100); queryCount++) + { + int v1 = nextInt(10, numRows/10); + int v2 = nextInt(10, numRows/50); + + List<Object[]> expected = testRowMap.values() + .stream() + .flatMap(list -> list.stream()) + .filter(row -> row.v1 == v1 && row.v2 == v2) + .map(row -> row(row.ck)) + .collect(Collectors.toList()); + + assertRowsIgnoringOrder(execute("SELECT ck FROM %s WHERE v1 = ? AND v2 = ?", v1, v2), expected.toArray(new Object[][]{})); + } + } + + private Map<Integer, List<TestRow>> buildAndLoadTestRows(boolean largeClusters) + { + Map<Integer, List<TestRow>> testRowMap = new HashMap<>(); + + int clusterSize = largeClusters ? nextInt(500, 5000) : nextInt(10, 100); + int partition = nextInt(0, numRows); + while (testRowMap.containsKey(partition)) Review Comment: Good point, that was duplication from inside the loop. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

