Skip to content

Commit c36815a

Browse files
takezoeraunaqmorarka
authored andcommitted
Fix memory size calculation when memory table is truncated
1 parent 58103a7 commit c36815a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Diff for: plugin/trino-memory/src/main/java/io/trino/plugin/memory/MemoryPagesStore.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public synchronized boolean contains(Long tableId)
136136

137137
public synchronized void purge(long tableId)
138138
{
139-
tables.remove(tableId);
139+
TableData tableData = tables.remove(tableId);
140+
currentBytes = currentBytes - tableData.getPages().stream().mapToLong(Page::getRetainedSizeInBytes).sum();
140141
}
141142

142143
public synchronized void cleanUp(Set<Long> activeTableIds)

Diff for: plugin/trino-memory/src/test/java/io/trino/plugin/memory/TestMemoryPagesStore.java

+24
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ public void testMemoryLimitExceeded()
133133
.hasMessageMatching("Memory limit.*");
134134
}
135135

136+
@Test
137+
public void testTruncate()
138+
{
139+
createTable(0L, 0L);
140+
insertToTable(0L, createOneMegaBytePage(), 0L);
141+
truncateTable(0L, 0L);
142+
insertToTable(0L, createOneMegaBytePage(), 0L);
143+
}
144+
136145
private void insertToTable(long tableId, Long... activeTableIds)
137146
{
138147
insertToTable(tableId, createPage(), activeTableIds);
@@ -159,6 +168,16 @@ private void createTable(long tableId, Long... activeTableIds)
159168
pageSink.finish();
160169
}
161170

171+
private void truncateTable(long tableId, Long... activeTableIds)
172+
{
173+
ConnectorPageSink pageSink = pageSinkProvider.createPageSink(
174+
MemoryTransactionHandle.INSTANCE,
175+
SESSION,
176+
createOverwriteMemoryInsertTableHandle(tableId, activeTableIds),
177+
TESTING_PAGE_SINK_ID);
178+
pageSink.finish();
179+
}
180+
162181
private static ConnectorOutputTableHandle createMemoryOutputTableHandle(long tableId, Long... activeTableIds)
163182
{
164183
return new MemoryOutputTableHandle(tableId, ImmutableSet.copyOf(activeTableIds));
@@ -169,6 +188,11 @@ private static ConnectorInsertTableHandle createMemoryInsertTableHandle(long tab
169188
return new MemoryInsertTableHandle(tableId, InsertMode.APPEND, ImmutableSet.copyOf(activeTableIds));
170189
}
171190

191+
private static ConnectorInsertTableHandle createOverwriteMemoryInsertTableHandle(long tableId, Long[] activeTableIds)
192+
{
193+
return new MemoryInsertTableHandle(tableId, InsertMode.OVERWRITE, ImmutableSet.copyOf(activeTableIds));
194+
}
195+
172196
private static Page createPage()
173197
{
174198
BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(POSITIONS_PER_PAGE);

0 commit comments

Comments
 (0)