Skip to content

Commit

Permalink
update list partition api: get partition type from table
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-024 committed Dec 30, 2024
1 parent 427f5a7 commit 4ec183c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 56 deletions.
13 changes: 1 addition & 12 deletions paimon-common/src/main/java/org/apache/paimon/types/RowType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
import org.apache.paimon.utils.Preconditions;
import org.apache.paimon.utils.StringUtils;

import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonCreator;
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonGetter;
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.JsonGenerator;

import java.io.IOException;
Expand All @@ -52,18 +48,13 @@
* @since 0.4.0
*/
@Public
@JsonIgnoreProperties(ignoreUnknown = true)
public final class RowType extends DataType {

private static final long serialVersionUID = 1L;

public static final String FILED_FIELDS = "fields";

public static final String FORMAT = "ROW<%s>";

@JsonProperty(FILED_FIELDS)
private final List<DataField> fields;

private InternalRow.FieldGetter[] fieldGetters;

public RowType(boolean isNullable, List<DataField> fields) {
Expand All @@ -76,16 +67,14 @@ public RowType(boolean isNullable, List<DataField> fields) {
validateFields(fields);
}

@JsonCreator
public RowType(@JsonProperty(FILED_FIELDS) List<DataField> fields) {
public RowType(List<DataField> fields) {
this(true, fields);
}

public RowType copy(List<DataField> newFields) {
return new RowType(isNullable(), newFields);
}

@JsonGetter(FILED_FIELDS)
public List<DataField> getFields() {
return fields;
}
Expand Down
17 changes: 10 additions & 7 deletions paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.apache.paimon.table.Table;
import org.apache.paimon.table.object.ObjectTable;
import org.apache.paimon.table.sink.BatchWriteBuilder;
import org.apache.paimon.types.RowType;
import org.apache.paimon.utils.Pair;
import org.apache.paimon.utils.Preconditions;

Expand Down Expand Up @@ -408,7 +409,9 @@ public List<PartitionEntry> listPartitions(Identifier identifier)
throws TableNotExistException {
boolean whetherSupportListPartitions = context.options().get(METASTORE_PARTITIONED);
if (whetherSupportListPartitions) {
return listPartitionsFromServer(identifier);
FileStoreTable table = (FileStoreTable) getTable(identifier);
RowType rowType = table.schema().logicalPartitionType();
return listPartitionsFromServer(identifier, rowType);
} else {
return getTable(identifier).newReadBuilder().newScan().listPartitionEntries();
}
Expand Down Expand Up @@ -462,7 +465,7 @@ Table getDataOrFormatTable(Identifier identifier) throws TableNotExistException
}

@VisibleForTesting
public List<PartitionEntry> listPartitionsFromServer(Identifier identifier)
public List<PartitionEntry> listPartitionsFromServer(Identifier identifier, RowType rowType)
throws TableNotExistException {
try {
ListPartitionsResponse response =
Expand All @@ -473,7 +476,7 @@ public List<PartitionEntry> listPartitionsFromServer(Identifier identifier)
headers());
if (response != null && response.getPartitions() != null) {
return response.getPartitions().stream()
.map(this::convertToPartitionEntry)
.map(p -> convertToPartitionEntry(p, rowType))
.collect(Collectors.toList());
} else {
return Collections.emptyList();
Expand All @@ -486,10 +489,10 @@ public List<PartitionEntry> listPartitionsFromServer(Identifier identifier)
}

@VisibleForTesting
PartitionEntry convertToPartitionEntry(ListPartitionsResponse.Partition partition) {
InternalRowSerializer serializer = new InternalRowSerializer(partition.getPartitionType());
GenericRow row =
convertSpecToInternalRow(partition.getSpec(), partition.getPartitionType(), null);
PartitionEntry convertToPartitionEntry(
ListPartitionsResponse.Partition partition, RowType rowType) {
InternalRowSerializer serializer = new InternalRowSerializer(rowType);
GenericRow row = convertSpecToInternalRow(partition.getSpec(), rowType, null);
return new PartitionEntry(
serializer.toBinaryRow(row).copy(),
partition.getRecordCount(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.paimon.rest.responses;

import org.apache.paimon.rest.RESTResponse;
import org.apache.paimon.types.RowType;

import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonCreator;
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonGetter;
Expand Down Expand Up @@ -53,7 +52,6 @@ public List<Partition> getPartitions() {
public static class Partition implements RESTResponse {

private static final String FIELD_SPEC = "spec";
public static final String FIELD_PARTITION_TYPE = "partitionType";
public static final String FIELD_RECORD_COUNT = "recordCount";
public static final String FIELD_FILE_SIZE_IN_BYTES = "fileSizeInBytes";
public static final String FIELD_FILE_COUNT = "fileCount";
Expand All @@ -62,9 +60,6 @@ public static class Partition implements RESTResponse {
@JsonProperty(FIELD_SPEC)
private final Map<String, String> spec;

@JsonProperty(FIELD_PARTITION_TYPE)
private final RowType partitionType;

@JsonProperty(FIELD_RECORD_COUNT)
private final long recordCount;

Expand All @@ -80,13 +75,11 @@ public static class Partition implements RESTResponse {
@JsonCreator
public Partition(
@JsonProperty(FIELD_SPEC) Map<String, String> spec,
@JsonProperty(FIELD_PARTITION_TYPE) RowType partitionType,
@JsonProperty(FIELD_RECORD_COUNT) long recordCount,
@JsonProperty(FIELD_FILE_SIZE_IN_BYTES) long fileSizeInBytes,
@JsonProperty(FIELD_FILE_COUNT) long fileCount,
@JsonProperty(FIELD_LAST_FILE_CREATION_TIME) long lastFileCreationTime) {
this.spec = spec;
this.partitionType = partitionType;
this.recordCount = recordCount;
this.fileSizeInBytes = fileSizeInBytes;
this.fileCount = fileCount;
Expand All @@ -98,11 +91,6 @@ public Map<String, String> getSpec() {
return spec;
}

@JsonGetter(FIELD_PARTITION_TYPE)
public RowType getPartitionType() {
return partitionType;
}

@JsonGetter(FIELD_RECORD_COUNT)
public long getRecordCount() {
return recordCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,9 @@ public static DropPartitionRequest dropPartitionRequest() {

public static ListPartitionsResponse listPartitionsResponse() {
Map<String, String> spec = new HashMap<>();
spec.put("a", "1");
spec.put("b", "2");
List<DataField> fields = new ArrayList<>();
fields.add(new DataField(0, "a", DataTypes.INT()));
fields.add(new DataField(1, "b", DataTypes.STRING()));
RowType partitionType = new RowType(false, fields);
spec.put("f0", "1");
ListPartitionsResponse.Partition partition =
new ListPartitionsResponse.Partition(spec, partitionType, 1, 1, 1, 1);
new ListPartitionsResponse.Partition(spec, 1, 1, 1, 1);
return new ListPartitionsResponse(ImmutableList.of(partition));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,13 @@ public void testListPartitionsWhenMetastorePartitionedIsTrue() throws Exception
RESTCatalog restCatalog = new RESTCatalog(CatalogContext.create(options));
RESTCatalog mockRestCatalog = spy(restCatalog);
String databaseName = MockRESTMessage.databaseName();
GetTableResponse getTableResponse = MockRESTMessage.getTableResponse();
mockResponse(mapper.writeValueAsString(getTableResponse), 200);
ListPartitionsResponse response = MockRESTMessage.listPartitionsResponse();
mockResponse(mapper.writeValueAsString(response), 200);
List<PartitionEntry> result =
mockRestCatalog.listPartitions(Identifier.create(databaseName, "table"));
verify(mockRestCatalog, times(1)).listPartitionsFromServer(any());
verify(mockRestCatalog, times(1)).listPartitionsFromServer(any(), any());
assertEquals(response.getPartitions().size(), result.size());
}

Expand All @@ -492,7 +494,7 @@ public void testListPartitionsFromFile() throws Exception {
mockResponse(mapper.writeValueAsString(response), 200);
mockRestCatalog.listPartitions(Identifier.create(databaseName, "table"));
verify(mockRestCatalog, times(1)).getTable(any());
verify(mockRestCatalog, times(0)).listPartitionsFromServer(any());
verify(mockRestCatalog, times(0)).listPartitionsFromServer(any(), any());
}

@Test
Expand All @@ -505,8 +507,9 @@ public void convertToPartitionEntryTest() {
fields.add(new DataField(1, "b", DataTypes.STRING()));
RowType partitionRowType = new RowType(false, fields);
ListPartitionsResponse.Partition partition =
new ListPartitionsResponse.Partition(spec, partitionRowType, 1, 1, 1, 1);
PartitionEntry partitionEntry = mockRestCatalog.convertToPartitionEntry(partition);
new ListPartitionsResponse.Partition(spec, 1, 1, 1, 1);
PartitionEntry partitionEntry =
mockRestCatalog.convertToPartitionEntry(partition, partitionRowType);
InternalRowPartitionComputer partitionComputer =
FileStorePathFactory.getPartitionComputer(partitionRowType, null, false);
Map<String, String> partValues =
Expand Down
2 changes: 0 additions & 2 deletions paimon-open-api/rest-catalog-open-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,6 @@ components:
properties:
spec:
type: object
partitionType:
$ref: '#/components/schemas/RowType'
recordCount:
type: integer
format: int64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
import org.apache.paimon.rest.responses.ListPartitionsResponse;
import org.apache.paimon.rest.responses.ListTablesResponse;
import org.apache.paimon.rest.responses.SuccessResponse;
import org.apache.paimon.types.DataField;
import org.apache.paimon.types.DataTypes;
import org.apache.paimon.types.RowType;

import org.apache.paimon.shade.guava30.com.google.common.collect.ImmutableList;
import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;
Expand All @@ -56,9 +53,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** RESTCatalog management APIs. */
Expand Down Expand Up @@ -379,14 +374,9 @@ public ListPartitionsResponse listPartitions(
@PathVariable String database,
@PathVariable String table) {
Map<String, String> spec = new HashMap<>();
spec.put("a", "1");
spec.put("b", "2");
List<DataField> fields = new ArrayList<>();
fields.add(new DataField(0, "a", DataTypes.INT()));
fields.add(new DataField(1, "b", DataTypes.STRING()));
RowType partitionType = new RowType(false, fields);
spec.put("f1", "1");
ListPartitionsResponse.Partition partition =
new ListPartitionsResponse.Partition(spec, partitionType, 1, 2, 3, 4);
new ListPartitionsResponse.Partition(spec, 1, 2, 3, 4);
return new ListPartitionsResponse(ImmutableList.of(partition));
}

Expand Down

0 comments on commit 4ec183c

Please sign in to comment.