Skip to content

Commit 9bac9ed

Browse files
committed
Fix expected values in SingleRestrictionEstimatedRowCountTest
Fixes riptano/cndb#12243 main-5.0 uses different default for memetable SAI structure than 5.0. This commits adjusts the expected row count values. Also removes assert on the total row count, since it varies in this branch for different column types.
1 parent 2c7649f commit 9bac9ed

File tree

1 file changed

+62
-67
lines changed

1 file changed

+62
-67
lines changed

test/unit/org/apache/cassandra/index/sai/plan/SingleRestrictionEstimatedRowCountTest.java

+62-67
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
23+
import java.util.AbstractMap;
24+
import java.util.HashMap;
25+
import java.util.Map;
2326

24-
import org.junit.After;
25-
import org.junit.Before;
2627
import org.junit.Test;
2728

2829
import org.apache.cassandra.Util;
@@ -44,7 +45,9 @@
4445

4546
public class SingleRestrictionEstimatedRowCountTest extends SAITester
4647
{
47-
private int queryOptLevel;
48+
static protected Map<Map.Entry<Version, CQL3Type.Native>, ColumnFamilyStore> tables = new HashMap<>();
49+
static Version[] versions = new Version[]{ Version.DB, Version.EB };
50+
static CQL3Type.Native[] types = new CQL3Type.Native[]{ INT, DECIMAL, VARINT };
4851

4952
static protected Object getFilterValue(CQL3Type.Native type, int value)
5053
{
@@ -61,69 +64,75 @@ static protected Object getFilterValue(CQL3Type.Native type, int value)
6164
return null;
6265
}
6366

64-
@Before
65-
public void setup()
67+
static Map.Entry<Version, CQL3Type.Native> tablesEntryKey(Version version, CQL3Type.Native type)
6668
{
67-
queryOptLevel = QueryController.QUERY_OPT_LEVEL;
68-
QueryController.QUERY_OPT_LEVEL = 0;
69-
}
70-
71-
@After
72-
public void teardown()
73-
{
74-
QueryController.QUERY_OPT_LEVEL = queryOptLevel;
69+
return new AbstractMap.SimpleEntry<>(version, type);
7570
}
7671

7772
@Test
78-
public void testInequality()
73+
public void testMemtablesSAI()
7974
{
80-
var test = new RowCountTest(Operator.NEQ, 25);
81-
test.doTest(Version.DB, INT, 97.0);
82-
test.doTest(Version.EB, INT, 97.0);
75+
createTables();
76+
77+
RowCountTest test = new RowCountTest(Operator.NEQ, 25);
78+
test.doTest(Version.DB, INT, 83.1);
79+
test.doTest(Version.EB, INT, 82.4);
8380
// Truncated numeric types planned differently
84-
test.doTest(Version.DB, DECIMAL, 97.0);
85-
test.doTest(Version.EB, DECIMAL, 97.0);
86-
test.doTest(Version.EB, VARINT, 97.0);
81+
test.doTest(Version.DB, DECIMAL, 117);
82+
test.doTest(Version.EB, DECIMAL, 117);
83+
test.doTest(Version.EB, VARINT, 119);
84+
85+
test = new RowCountTest(Operator.LT, 50);
86+
test.doTest(Version.DB, INT, 50);
87+
test.doTest(Version.EB, INT, 50);
88+
test.doTest(Version.DB, DECIMAL, 51);
89+
test.doTest(Version.EB, DECIMAL, 51);
90+
91+
test = new RowCountTest(Operator.LT, 150);
92+
test.doTest(Version.DB, INT, 100);
93+
test.doTest(Version.EB, INT, 99);
94+
test.doTest(Version.DB, DECIMAL, 100);
95+
test.doTest(Version.EB, DECIMAL, 99);
96+
97+
test = new RowCountTest(Operator.EQ, 31);
98+
test.doTest(Version.DB, INT, 1);
99+
test.doTest(Version.EB, INT, 1);
100+
test.doTest(Version.DB, DECIMAL, 1);
101+
test.doTest(Version.EB, DECIMAL, 1);
87102
}
88103

89-
@Test
90-
public void testHalfRangeMiddle()
91-
{
92-
var test = new RowCountTest(Operator.LT, 50);
93-
test.doTest(Version.DB, INT, 48);
94-
test.doTest(Version.EB, INT, 48);
95-
test.doTest(Version.DB, DECIMAL, 48);
96-
test.doTest(Version.EB, DECIMAL, 48);
97-
}
98104

99-
@Test
100-
public void testHalfRangeEverything()
105+
void createTables()
101106
{
102-
var test = new RowCountTest(Operator.LT, 150);
103-
test.doTest(Version.DB, INT, 97);
104-
test.doTest(Version.EB, INT, 97);
105-
test.doTest(Version.DB, DECIMAL, 97);
106-
test.doTest(Version.EB, DECIMAL, 97);
107-
}
108-
109-
@Test
110-
public void testEquality()
111-
{
112-
var test = new RowCountTest(Operator.EQ, 31);
113-
test.doTest(Version.DB, INT, 15);
114-
test.doTest(Version.EB, INT, 0);
115-
test.doTest(Version.DB, DECIMAL, 15);
116-
test.doTest(Version.EB, DECIMAL, 0);
107+
for (Version version : versions)
108+
{
109+
SAIUtil.setLatestVersion(version);
110+
for (CQL3Type.Native type : types)
111+
{
112+
createTable("CREATE TABLE %s (pk text PRIMARY KEY, age " + type + ')');
113+
createIndex("CREATE CUSTOM INDEX ON %s(age) USING 'StorageAttachedIndex'");
114+
tables.put(tablesEntryKey(version, type), getCurrentColumnFamilyStore());
115+
}
116+
}
117+
flush();
118+
for (ColumnFamilyStore cfs : tables.values())
119+
populateTable(cfs);
117120
}
118121

119-
protected ColumnFamilyStore prepareTable(CQL3Type.Native type)
122+
void populateTable(ColumnFamilyStore cfs)
120123
{
121-
createTable("CREATE TABLE %s (pk text PRIMARY KEY, age " + type + ')');
122-
createIndex("CREATE CUSTOM INDEX ON %s(age) USING 'StorageAttachedIndex'");
123-
return getCurrentColumnFamilyStore();
124+
// Avoid race condition of starting before flushing completed
125+
cfs.unsafeRunWithoutFlushing(() -> {
126+
for (int i = 0; i < 100; i++)
127+
{
128+
String query = String.format("INSERT INTO %s (pk, age) VALUES (?, " + i + ')',
129+
cfs.keyspace.getName() + '.' + cfs.name);
130+
executeFormattedQuery(query, "key" + i);
131+
}
132+
});
124133
}
125134

126-
class RowCountTest
135+
static class RowCountTest
127136
{
128137
final Operator op;
129138
final int filterValue;
@@ -136,20 +145,8 @@ class RowCountTest
136145

137146
void doTest(Version version, CQL3Type.Native type, double expectedRows)
138147
{
139-
Version latest = Version.latest();
140-
SAIUtil.setLatestVersion(version);
141-
142-
ColumnFamilyStore cfs = prepareTable(type);
143-
// Avoid race condition of flushing after the index creation
144-
cfs.unsafeRunWithoutFlushing(() -> {
145-
for (int i = 0; i < 100; i++)
146-
{
147-
execute("INSERT INTO %s (pk, age) VALUES (?," + i + ')', "key" + i);
148-
}
149-
});
150-
148+
ColumnFamilyStore cfs = tables.get(new AbstractMap.SimpleEntry<>(version, type));
151149
Object filter = getFilterValue(type, filterValue);
152-
153150
ReadCommand rc = Util.cmd(cfs)
154151
.columns("age")
155152
.filterOn("age", op, filter)
@@ -158,9 +155,9 @@ void doTest(Version version, CQL3Type.Native type, double expectedRows)
158155
rc,
159156
version.onDiskFormat().indexFeatureSet(),
160157
new QueryContext());
158+
161159
long totalRows = controller.planFactory.tableMetrics.rows;
162160
assertEquals(0, cfs.metrics().liveSSTableCount.getValue().intValue());
163-
assertEquals(97, totalRows);
164161

165162
Plan plan = controller.buildPlan();
166163
assert plan instanceof Plan.RowsIteration;
@@ -171,8 +168,6 @@ void doTest(Version version, CQL3Type.Native type, double expectedRows)
171168
assertEquals(expectedRows, root.expectedRows(), 0.1);
172169
assertEquals(expectedRows, planNode.expectedKeys(), 0.1);
173170
assertEquals(expectedRows / totalRows, planNode.selectivity(), 0.001);
174-
175-
SAIUtil.setLatestVersion(latest);
176171
}
177172
}
178173
}

0 commit comments

Comments
 (0)