2222import com .google .errorprone .annotations .concurrent .GuardedBy ;
2323import com .google .inject .Inject ;
2424import io .airlift .slice .Slice ;
25+ import io .trino .plugin .memory .MemoryInsertTableHandle .InsertMode ;
2526import io .trino .spi .HostAddress ;
2627import io .trino .spi .Node ;
2728import io .trino .spi .NodeManager ;
@@ -277,7 +278,7 @@ public synchronized void renameTable(ConnectorSession session, ConnectorTableHan
277278 long tableId = handle .id ();
278279
279280 TableInfo oldInfo = tables .get (tableId );
280- tables .put (tableId , new TableInfo (tableId , newTableName .getSchemaName (), newTableName .getTableName (), oldInfo .columns (), oldInfo .dataFragments (), oldInfo .comment ()));
281+ tables .put (tableId , new TableInfo (tableId , newTableName .getSchemaName (), newTableName .getTableName (), oldInfo .columns (), oldInfo .truncated (), oldInfo . dataFragments (), oldInfo .comment ()));
281282
282283 tableIds .remove (oldInfo .getSchemaTableName ());
283284 tableIds .put (newTableName , tableId );
@@ -311,6 +312,7 @@ public synchronized MemoryOutputTableHandle beginCreateTable(ConnectorSession se
311312 tableMetadata .getTable ().getSchemaName (),
312313 tableMetadata .getTable ().getTableName (),
313314 columns .build (),
315+ false ,
314316 new HashMap <>(),
315317 tableMetadata .getComment ()));
316318
@@ -350,7 +352,10 @@ public synchronized Optional<ConnectorOutputMetadata> finishCreateTable(Connecto
350352 public synchronized MemoryInsertTableHandle beginInsert (ConnectorSession session , ConnectorTableHandle tableHandle , List <ColumnHandle > columns , RetryMode retryMode )
351353 {
352354 MemoryTableHandle memoryTableHandle = (MemoryTableHandle ) tableHandle ;
353- return new MemoryInsertTableHandle (memoryTableHandle .id (), ImmutableSet .copyOf (tableIds .values ()));
355+ TableInfo tableInfo = tables .get (memoryTableHandle .id ());
356+ InsertMode mode = tableInfo .truncated () ? InsertMode .OVERWRITE : InsertMode .APPEND ;
357+ tables .put (tableInfo .id (), new TableInfo (tableInfo .id (), tableInfo .schemaName (), tableInfo .tableName (), tableInfo .columns (), false , tableInfo .dataFragments (), tableInfo .comment ()));
358+ return new MemoryInsertTableHandle (memoryTableHandle .id (), mode , ImmutableSet .copyOf (tableIds .values ()));
354359 }
355360
356361 @ Override
@@ -374,7 +379,7 @@ public synchronized void truncateTable(ConnectorSession session, ConnectorTableH
374379 MemoryTableHandle handle = (MemoryTableHandle ) tableHandle ;
375380 long tableId = handle .id ();
376381 TableInfo info = tables .get (handle .id ());
377- tables .put (tableId , new TableInfo (tableId , info .schemaName (), info .tableName (), info .columns (), ImmutableMap .of (), info .comment ()));
382+ tables .put (tableId , new TableInfo (tableId , info .schemaName (), info .tableName (), info .columns (), true , ImmutableMap .of (), info .comment ()));
378383 }
379384
380385 @ Override
@@ -393,7 +398,7 @@ public synchronized void addColumn(ConnectorSession session, ConnectorTableHandl
393398 .add (new ColumnInfo (new MemoryColumnHandle (table .columns ().size (), column .getType ()), column .getName (), column .getType (), column .isNullable (), Optional .ofNullable (column .getComment ())))
394399 .build ();
395400
396- tables .put (tableId , new TableInfo (tableId , table .schemaName (), table .tableName (), columns , table .dataFragments (), table .comment ()));
401+ tables .put (tableId , new TableInfo (tableId , table .schemaName (), table .tableName (), columns , table .truncated (), table . dataFragments (), table .comment ()));
397402 }
398403
399404 @ Override
@@ -408,7 +413,7 @@ public synchronized void renameColumn(ConnectorSession session, ConnectorTableHa
408413 ColumnInfo columnInfo = columns .get (column .columnIndex ());
409414 columns .set (column .columnIndex (), new ColumnInfo (columnInfo .handle (), target , columnInfo .type (), columnInfo .nullable (), columnInfo .comment ()));
410415
411- tables .put (tableId , new TableInfo (tableId , table .schemaName (), table .tableName (), ImmutableList .copyOf (columns ), table .dataFragments (), table .comment ()));
416+ tables .put (tableId , new TableInfo (tableId , table .schemaName (), table .tableName (), ImmutableList .copyOf (columns ), table .truncated (), table . dataFragments (), table .comment ()));
412417 }
413418
414419 @ Override
@@ -423,7 +428,7 @@ public synchronized void dropNotNullConstraint(ConnectorSession session, Connect
423428 ColumnInfo columnInfo = columns .get (column .columnIndex ());
424429 columns .set (column .columnIndex (), new ColumnInfo (columnInfo .handle (), columnInfo .name (), columnInfo .type (), true , columnInfo .comment ()));
425430
426- tables .put (tableId , new TableInfo (tableId , table .schemaName (), table .tableName (), ImmutableList .copyOf (columns ), table .dataFragments (), table .comment ()));
431+ tables .put (tableId , new TableInfo (tableId , table .schemaName (), table .tableName (), ImmutableList .copyOf (columns ), table .truncated (), table . dataFragments (), table .comment ()));
427432 }
428433
429434 @ Override
@@ -538,7 +543,7 @@ private void updateRowsOnHosts(long tableId, Collection<Slice> fragments)
538543 dataFragments .merge (memoryDataFragment .hostAddress (), memoryDataFragment , MemoryDataFragment ::merge );
539544 }
540545
541- tables .put (tableId , new TableInfo (tableId , info .schemaName (), info .tableName (), info .columns (), dataFragments , info .comment ()));
546+ tables .put (tableId , new TableInfo (tableId , info .schemaName (), info .tableName (), info .columns (), info . truncated (), dataFragments , info .comment ()));
542547 }
543548
544549 public synchronized List <MemoryDataFragment > getDataFragments (long tableId )
@@ -599,7 +604,7 @@ public synchronized void setTableComment(ConnectorSession session, ConnectorTabl
599604 MemoryTableHandle table = (MemoryTableHandle ) tableHandle ;
600605 TableInfo info = tables .get (table .id ());
601606 checkArgument (info != null , "Table not found" );
602- tables .put (table .id (), new TableInfo (table .id (), info .schemaName (), info .tableName (), info .columns (), info .dataFragments (), comment ));
607+ tables .put (table .id (), new TableInfo (table .id (), info .schemaName (), info .tableName (), info .columns (), info .truncated (), info . dataFragments (), comment ));
603608 }
604609
605610 @ Override
@@ -617,6 +622,7 @@ public synchronized void setColumnComment(ConnectorSession session, ConnectorTab
617622 info .columns ().stream ()
618623 .map (tableColumn -> Objects .equals (tableColumn .handle (), columnHandle ) ? new ColumnInfo (tableColumn .handle (), tableColumn .name (), tableColumn .getMetadata ().getType (), tableColumn .nullable (), comment ) : tableColumn )
619624 .collect (toImmutableList ()),
625+ info .truncated (),
620626 info .dataFragments (),
621627 info .comment ()));
622628 }
0 commit comments