3131import org .apache .paimon .fs .FileIO ;
3232import org .apache .paimon .fs .Path ;
3333import org .apache .paimon .manifest .PartitionEntry ;
34- import org .apache .paimon .operation .FileStoreCommit ;
3534import org .apache .paimon .operation .Lock ;
3635import org .apache .paimon .options .CatalogOptions ;
3736import org .apache .paimon .options .Options ;
6564import org .apache .paimon .table .FileStoreTableFactory ;
6665import org .apache .paimon .table .Table ;
6766import org .apache .paimon .table .object .ObjectTable ;
68- import org .apache .paimon .table .sink .BatchWriteBuilder ;
67+ import org .apache .paimon .table .sink .BatchTableCommit ;
6968import org .apache .paimon .types .RowType ;
7069import org .apache .paimon .utils .Pair ;
7170import org .apache .paimon .utils .Preconditions ;
8584import java .util .Optional ;
8685import java .util .Set ;
8786import java .util .concurrent .ScheduledExecutorService ;
88- import java .util .stream .Collectors ;
8987
90- import static org .apache .paimon .CoreOptions .createCommitUser ;
88+ import static org .apache .paimon .CoreOptions .METASTORE_PARTITIONED_TABLE ;
89+ import static org .apache .paimon .CoreOptions .PARTITION_DEFAULT_NAME ;
9190import static org .apache .paimon .catalog .CatalogUtils .checkNotSystemDatabase ;
9291import static org .apache .paimon .catalog .CatalogUtils .checkNotSystemTable ;
9392import static org .apache .paimon .catalog .CatalogUtils .isSystemDatabase ;
@@ -360,6 +359,12 @@ public void dropTable(Identifier identifier, boolean ignoreIfNotExists)
360359 @ Override
361360 public void createPartition (Identifier identifier , Map <String , String > partitionSpec )
362361 throws TableNotExistException {
362+ Table table = getTable (identifier );
363+ Options options = Options .fromMap (table .options ());
364+ if (!options .get (METASTORE_PARTITIONED_TABLE )) {
365+ return ;
366+ }
367+
363368 try {
364369 CreatePartitionRequest request = new CreatePartitionRequest (identifier , partitionSpec );
365370 client .post (
@@ -376,27 +381,77 @@ public void createPartition(Identifier identifier, Map<String, String> partition
376381 }
377382
378383 @ Override
379- public void dropPartition (Identifier identifier , Map <String , String > partitions )
384+ public void dropPartition (Identifier identifier , Map <String , String > partition )
380385 throws TableNotExistException , PartitionNotExistException {
381386 checkNotSystemTable (identifier , "dropPartition" );
382- dropPartitionMetadata ( identifier , partitions );
387+
383388 Table table = getTable (identifier );
384- cleanPartitionsInFileSystem (table , partitions );
389+ Options options = Options .fromMap (table .options ());
390+ if (options .get (METASTORE_PARTITIONED_TABLE )) {
391+ try {
392+ client .delete (
393+ resourcePaths .partitions (
394+ identifier .getDatabaseName (), identifier .getTableName ()),
395+ new DropPartitionRequest (partition ),
396+ headers ());
397+ } catch (NoSuchResourceException ignore ) {
398+ throw new PartitionNotExistException (identifier , partition );
399+ } catch (ForbiddenException e ) {
400+ throw new TableNoPermissionException (identifier , e );
401+ }
402+ }
403+
404+ try (BatchTableCommit commit =
405+ table .newBatchWriteBuilder ().withOverwrite (partition ).newCommit ()) {
406+ commit .commit (Collections .emptyList ());
407+ } catch (Exception e ) {
408+ throw new RuntimeException (e );
409+ }
385410 }
386411
387412 @ Override
388413 public List <PartitionEntry > listPartitions (Identifier identifier )
389414 throws TableNotExistException {
390- FileStoreTable table = (FileStoreTable ) getTable (identifier );
391- boolean whetherSupportListPartitions =
392- Boolean .parseBoolean (
393- table .options ().get (CoreOptions .METASTORE_PARTITIONED_TABLE .key ()));
394- if (whetherSupportListPartitions ) {
395- RowType rowType = table .schema ().logicalPartitionType ();
396- return listPartitionsFromServer (identifier , rowType );
397- } else {
398- return getTable (identifier ).newReadBuilder ().newScan ().listPartitionEntries ();
415+ Table table = getTable (identifier );
416+ Options options = Options .fromMap (table .options ());
417+ if (!options .get (METASTORE_PARTITIONED_TABLE )) {
418+ return table .newReadBuilder ().newScan ().listPartitionEntries ();
399419 }
420+
421+ ListPartitionsResponse response ;
422+ try {
423+ response =
424+ client .get (
425+ resourcePaths .partitions (
426+ identifier .getDatabaseName (), identifier .getTableName ()),
427+ ListPartitionsResponse .class ,
428+ headers ());
429+ } catch (NoSuchResourceException e ) {
430+ throw new TableNotExistException (identifier );
431+ } catch (ForbiddenException e ) {
432+ throw new TableNoPermissionException (identifier , e );
433+ }
434+
435+ if (response == null || response .getPartitions () == null ) {
436+ return Collections .emptyList ();
437+ }
438+
439+ RowType partitionType = table .rowType ().project (table .partitionKeys ());
440+ InternalRowSerializer serializer = new InternalRowSerializer (partitionType );
441+ String defaultName = options .get (PARTITION_DEFAULT_NAME );
442+ List <PartitionEntry > result = new ArrayList <>();
443+ for (PartitionResponse partition : response .getPartitions ()) {
444+ GenericRow row =
445+ convertSpecToInternalRow (partition .getSpec (), partitionType , defaultName );
446+ result .add (
447+ new PartitionEntry (
448+ serializer .toBinaryRow (row ).copy (),
449+ partition .getRecordCount (),
450+ partition .getFileSizeInBytes (),
451+ partition .getFileCount (),
452+ partition .getLastFileCreationTime ()));
453+ }
454+ return result ;
400455 }
401456
402457 @ Override
@@ -444,41 +499,6 @@ private Table getDataOrFormatTable(Identifier identifier) throws TableNotExistEx
444499 return table ;
445500 }
446501
447- private List <PartitionEntry > listPartitionsFromServer (Identifier identifier , RowType rowType )
448- throws TableNotExistException {
449- try {
450- ListPartitionsResponse response =
451- client .get (
452- resourcePaths .partitions (
453- identifier .getDatabaseName (), identifier .getTableName ()),
454- ListPartitionsResponse .class ,
455- headers ());
456- if (response != null && response .getPartitions () != null ) {
457- return response .getPartitions ().stream ()
458- .map (p -> convertToPartitionEntry (p , rowType ))
459- .collect (Collectors .toList ());
460- } else {
461- return Collections .emptyList ();
462- }
463- } catch (NoSuchResourceException e ) {
464- throw new TableNotExistException (identifier );
465- } catch (ForbiddenException e ) {
466- throw new TableNoPermissionException (identifier , e );
467- }
468- }
469-
470- private void cleanPartitionsInFileSystem (Table table , Map <String , String > partitions ) {
471- FileStoreTable fileStoreTable = (FileStoreTable ) table ;
472- try (FileStoreCommit commit =
473- fileStoreTable
474- .store ()
475- .newCommit (
476- createCommitUser (fileStoreTable .coreOptions ().toConfiguration ()))) {
477- commit .dropPartitions (
478- Collections .singletonList (partitions ), BatchWriteBuilder .COMMIT_IDENTIFIER );
479- }
480- }
481-
482502 private GetTableResponse getTableResponse (Identifier identifier ) throws TableNotExistException {
483503 try {
484504 return client .get (
@@ -492,23 +512,6 @@ private GetTableResponse getTableResponse(Identifier identifier) throws TableNot
492512 }
493513 }
494514
495- private boolean dropPartitionMetadata (Identifier identifier , Map <String , String > partitions )
496- throws TableNoPermissionException , PartitionNotExistException {
497- try {
498- DropPartitionRequest request = new DropPartitionRequest (partitions );
499- client .delete (
500- resourcePaths .partitions (
501- identifier .getDatabaseName (), identifier .getTableName ()),
502- request ,
503- headers ());
504- return true ;
505- } catch (NoSuchResourceException ignore ) {
506- throw new PartitionNotExistException (identifier , partitions );
507- } catch (ForbiddenException e ) {
508- throw new TableNoPermissionException (identifier , e );
509- }
510- }
511-
512515 private static Map <String , String > configHeaders (Map <String , String > properties ) {
513516 return RESTUtil .extractPrefixMap (properties , "header." );
514517 }
@@ -540,17 +543,6 @@ private ScheduledExecutorService tokenRefreshExecutor() {
540543 return refreshExecutor ;
541544 }
542545
543- private PartitionEntry convertToPartitionEntry (PartitionResponse partition , RowType rowType ) {
544- InternalRowSerializer serializer = new InternalRowSerializer (rowType );
545- GenericRow row = convertSpecToInternalRow (partition .getSpec (), rowType , null );
546- return new PartitionEntry (
547- serializer .toBinaryRow (row ).copy (),
548- partition .getRecordCount (),
549- partition .getFileSizeInBytes (),
550- partition .getFileCount (),
551- partition .getLastFileCreationTime ());
552- }
553-
554546 private static FileIO getFileIOFromOptions (CatalogContext context ) {
555547 try {
556548 Options options = context .options ();
0 commit comments