Skip to content

Commit

Permalink
DBPE-14965: Don't mix indexing static and non-static rows
Browse files Browse the repository at this point in the history
An index on static column indexes static rows only,
an index on non-static column indexes regular rows only.

Because the partition key is always present in static rows,
before this patch,
an index on partition key received both static and non-static
rows which could lead to duplicates under some circumstances.
  • Loading branch information
pkolaczk committed Jan 29, 2025
1 parent 1d7c4cb commit 2e52cab
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public void addRow(PrimaryKey key, Row row, long sstableRowId) throws IOExceptio
if (maybeAbort())
return;

// This is to avoid duplicates (and also reduce space taken by indexes on static columns).
// An index on a static column indexes static rows only.
// An index on a non-static column indexes regular rows only.
if (indexContext.getDefinition().isStatic() != row.isStatic())
return;

if (indexContext.isNonFrozenCollection())
{
Iterator<ByteBuffer> valueIterator = indexContext.getValuesOf(row, nowInSec);
Expand Down
46 changes: 46 additions & 0 deletions test/unit/org/apache/cassandra/index/sai/cql/DBPE14965Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright DataStax, Inc.
*
* Licensed 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 org.junit.Test;

import org.apache.cassandra.index.sai.SAITester;
import org.assertj.core.api.Assertions;

public class DBPE14965Test extends SAITester
{
@Test
public void testDBPE14965()
{
createTable("CREATE TABLE %s (" +
" pk1 int," +
" pk2 int," +
" ck int," +
" r int," +
" s int static," +
" PRIMARY KEY ((pk1, pk2), ck))");

createIndex("CREATE CUSTOM INDEX ON %s (pk1) USING 'StorageAttachedIndex'");

disableCompaction();
execute("INSERT INTO %s (pk1, pk2, ck, r, s) VALUES (0, ?, ?, ?, ?)", 1, 1, 1, 1);
flush();
compact();

Assertions.assertThat(execute("SELECT * FROM %s WHERE pk1=0").size()).isEqualTo(1); // finds 2!!
}
}

0 comments on commit 2e52cab

Please sign in to comment.