From d91a696ca5f77d11f6cc14d48996cdda335aeb7a Mon Sep 17 00:00:00 2001 From: Jingsong Date: Wed, 25 Dec 2024 16:50:48 +0800 Subject: [PATCH] [core] SortLookupStoreWriter should support empty record --- .../apache/paimon/lookup/sort/BlockWriter.java | 5 ++++- .../sort/SortLookupStoreFactoryTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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 =