xiangfu0 commented on code in PR #18549: URL: https://github.com/apache/pinot/pull/18549#discussion_r3293223899
########## pinot-controller/src/test/java/org/apache/pinot/controller/api/upload/SegmentValidationUtilsTest.java: ########## @@ -0,0 +1,155 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.controller.api.upload; + +import java.util.Map; +import java.util.Set; +import javax.ws.rs.core.Response; +import org.apache.pinot.controller.api.exception.ControllerApplicationException; +import org.apache.pinot.segment.spi.ColumnMetadata; +import org.apache.pinot.segment.spi.SegmentMetadata; +import org.apache.pinot.spi.config.table.ColumnPartitionConfig; +import org.apache.pinot.spi.config.table.ReplicaGroupStrategyConfig; +import org.apache.pinot.spi.config.table.SegmentPartitionConfig; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.config.table.UpsertConfig; +import org.apache.pinot.spi.config.table.assignment.InstanceAssignmentConfig; +import org.apache.pinot.spi.config.table.assignment.InstancePartitionsType; +import org.apache.pinot.spi.config.table.assignment.InstanceReplicaGroupPartitionConfig; +import org.apache.pinot.spi.config.table.assignment.InstanceTagPoolConfig; +import org.apache.pinot.spi.utils.builder.TableConfigBuilder; +import org.testng.annotations.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.expectThrows; + + +public class SegmentValidationUtilsTest { + private static final String RAW_TABLE_NAME = "testTable"; + private static final String SEGMENT_NAME = "testSegment"; + private static final String PARTITION_COLUMN = "pk"; + private static final int NUM_PARTITIONS = 4; + + @Test + public void testValidateUpsertSegmentPartitionMetadataAcceptsSinglePartitionId() { + SegmentValidationUtils.validateUpsertSegmentPartitionMetadata(mockSegmentMetadata(Set.of(2)), + getOfflineUpsertTableConfig()); + } + + @Test + public void testValidateUpsertSegmentPartitionMetadataAcceptsInstanceAssignmentPartitionColumn() { + InstanceAssignmentConfig instanceAssignmentConfig = + new InstanceAssignmentConfig(new InstanceTagPoolConfig("DefaultTenant", false, 0, null), null, + new InstanceReplicaGroupPartitionConfig(true, 0, 0, 0, 1, 0, false, PARTITION_COLUMN), null, false); + TableConfig tableConfig = new TableConfigBuilder(TableType.OFFLINE) + .setTableName(RAW_TABLE_NAME) + .setUpsertConfig(new UpsertConfig(UpsertConfig.Mode.FULL)) + .setInstanceAssignmentConfigMap(Map.of(InstancePartitionsType.OFFLINE.name(), instanceAssignmentConfig)) + .build(); + + SegmentValidationUtils.validateUpsertSegmentPartitionMetadata(mockSegmentMetadata(Set.of(2)), tableConfig); + } + + @Test + public void testValidateUpsertSegmentPartitionMetadataUsesSingleSegmentPartitionConfigColumn() { + ControllerApplicationException exception = expectThrows(ControllerApplicationException.class, + () -> SegmentValidationUtils.validateUpsertSegmentPartitionMetadata(mockSegmentMetadata(Set.of(1, 2)), + getOfflineUpsertTableConfigWithSegmentPartitionConfig())); + assertEquals(exception.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); + } + + @Test + public void testValidateUpsertSegmentPartitionMetadataRejectsMultiplePartitionIds() { + ControllerApplicationException exception = expectThrows(ControllerApplicationException.class, + () -> SegmentValidationUtils.validateUpsertSegmentPartitionMetadata(mockSegmentMetadata(Set.of(1, 2)), + getOfflineUpsertTableConfig())); + assertEquals(exception.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); + } + + @Test + public void testValidateUpsertSegmentPartitionMetadataRejectsMissingPartitionId() { + ControllerApplicationException exception = expectThrows(ControllerApplicationException.class, + () -> SegmentValidationUtils.validateUpsertSegmentPartitionMetadata(mockSegmentMetadata(null), + getOfflineUpsertTableConfig())); + assertEquals(exception.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Review Comment: Added focused tests for both branches: the single-column segmentPartitionConfig happy path and the missing partition-column metadata rejection path. ########## pinot-controller/src/main/java/org/apache/pinot/controller/api/upload/SegmentValidationUtils.java: ########## @@ -57,6 +70,66 @@ public static void validateTimeInterval(SegmentMetadata segmentMetadata, TableCo } } + public static void validateUpsertSegmentPartitionMetadata(SegmentMetadata segmentMetadata, TableConfig tableConfig) { + if (tableConfig.getTableType() != TableType.OFFLINE || !tableConfig.isUpsertEnabled()) { Review Comment: Kept this intentionally scoped to offline upsert for this PR and added a code comment explaining why realtime needs separate CONSUMING/COMPLETED partition-column resolution instead of reusing the OFFLINE lookup. ########## pinot-controller/src/main/java/org/apache/pinot/controller/api/upload/SegmentValidationUtils.java: ########## @@ -57,6 +70,66 @@ public static void validateTimeInterval(SegmentMetadata segmentMetadata, TableCo } } + public static void validateUpsertSegmentPartitionMetadata(SegmentMetadata segmentMetadata, TableConfig tableConfig) { + if (tableConfig.getTableType() != TableType.OFFLINE || !tableConfig.isUpsertEnabled()) { + return; + } + + String partitionColumn = getPartitionColumn(tableConfig); + if (StringUtils.isEmpty(partitionColumn)) { + return; + } + + ColumnMetadata columnMetadata = segmentMetadata.getColumnMetadataFor(partitionColumn); + Set<Integer> partitions = columnMetadata != null ? columnMetadata.getPartitions() : null; + if (partitions == null || partitions.size() != 1) { + throw new ControllerApplicationException(LOGGER, + String.format("Uploaded segment: %s for offline upsert table: %s must contain exactly one partition id for " Review Comment: Switched this exception message from String.format to plain concatenation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
