diff --git a/paimon-common/src/main/java/org/apache/paimon/lookup/sort/BlockWriter.java b/paimon-common/src/main/java/org/apache/paimon/lookup/sort/BlockWriter.java index d4f48e9c79d5..340abac8620c 100644 --- a/paimon-common/src/main/java/org/apache/paimon/lookup/sort/BlockWriter.java +++ b/paimon-common/src/main/java/org/apache/paimon/lookup/sort/BlockWriter.java @@ -83,8 +83,11 @@ public int memory() { public MemorySlice finish() throws IOException { if (positions.isEmpty()) { - throw new IllegalStateException(); + // Do not use alignment mode, as it is impossible to calculate how many records are + // inside when reading + aligned = false; } + if (aligned) { block.writeInt(alignedSize); } else { diff --git a/paimon-common/src/test/java/org/apache/paimon/lookup/sort/SortLookupStoreFactoryTest.java b/paimon-common/src/test/java/org/apache/paimon/lookup/sort/SortLookupStoreFactoryTest.java index 7ba3f8283aea..354283a1d495 100644 --- a/paimon-common/src/test/java/org/apache/paimon/lookup/sort/SortLookupStoreFactoryTest.java +++ b/paimon-common/src/test/java/org/apache/paimon/lookup/sort/SortLookupStoreFactoryTest.java @@ -114,6 +114,24 @@ public void testNormal() throws IOException { assertThat(cacheManager.indexCache().asMap()).isEmpty(); } + @TestTemplate + public void testEmpty() throws IOException { + CacheManager cacheManager = new CacheManager(MemorySize.ofMebiBytes(1)); + SortLookupStoreFactory factory = + new SortLookupStoreFactory(Comparator.naturalOrder(), cacheManager, 1024, compress); + + SortLookupStoreWriter writer = + factory.createWriter(file, createBloomFiler(bloomFilterEnabled)); + Context context = writer.close(); + + SortLookupStoreReader reader = factory.createReader(file, context); + byte[] bytes = toBytes(rnd.nextInt(VALUE_COUNT)); + assertThat(reader.lookup(bytes)).isNull(); + reader.close(); + assertThat(cacheManager.dataCache().asMap()).isEmpty(); + assertThat(cacheManager.indexCache().asMap()).isEmpty(); + } + @TestTemplate public void testIntKey() throws IOException { RowCompactedSerializer keySerializer =