singhpk234 commented on code in PR #13400: URL: https://github.com/apache/iceberg/pull/13400#discussion_r2578159384
########## core/src/test/java/org/apache/iceberg/rest/TestRESTScanPlanning.java: ########## @@ -0,0 +1,878 @@ +/* + * 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.iceberg.rest; + +import static org.apache.iceberg.catalog.CatalogTests.FILE_A; +import static org.apache.iceberg.catalog.CatalogTests.FILE_A_DELETES; +import static org.apache.iceberg.catalog.CatalogTests.FILE_A_EQUALITY_DELETES; +import static org.apache.iceberg.catalog.CatalogTests.FILE_B; +import static org.apache.iceberg.catalog.CatalogTests.FILE_B_DELETES; +import static org.apache.iceberg.catalog.CatalogTests.FILE_B_EQUALITY_DELETES; +import static org.apache.iceberg.catalog.CatalogTests.FILE_C; +import static org.apache.iceberg.catalog.CatalogTests.FILE_C_EQUALITY_DELETES; +import static org.apache.iceberg.catalog.CatalogTests.SCHEMA; +import static org.apache.iceberg.catalog.CatalogTests.SPEC; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assumptions.assumeThat; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Function; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.MetadataTableType; +import org.apache.iceberg.MetadataTableUtils; +import org.apache.iceberg.Scan; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.SessionCatalog; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.inmemory.InMemoryCatalog; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.CloseableIterator; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.rest.responses.ErrorResponse; +import org.eclipse.jetty.server.Server; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.Mockito; + +public class TestRESTScanPlanning { + protected static final Namespace NS = Namespace.of("ns"); + + @TempDir protected Path temp; + + protected RESTCatalogTestInfrastructure infrastructure = new RESTCatalogTestInfrastructure(); + + // Expose infrastructure components as protected fields for backward compatibility + protected RESTCatalog restCatalog; + protected InMemoryCatalog backendCatalog; + protected Server httpServer; + protected RESTCatalogAdapter adapterForRESTServer; + protected ParserContext parserContext; + + // Scan-planning-specific fields + private RESTCatalog restCatalogWithScanPlanning; + + @BeforeEach + public void setupCatalogs() throws Exception { + infrastructure.before(temp); + this.backendCatalog = infrastructure.backendCatalog(); + this.httpServer = infrastructure.httpServer(); + this.adapterForRESTServer = infrastructure.adapter(); + + // Initialize catalog with scan planning enabled + this.restCatalogWithScanPlanning = + initCatalog( + "prod-with-scan-planning", + java.util.Map.of(RESTCatalogProperties.REST_SCAN_PLANNING_ENABLED, "true")); + } + + @AfterEach + public void teardownCatalogs() throws Exception { + if (restCatalogWithScanPlanning != null) { + restCatalogWithScanPlanning.close(); + } + infrastructure.after(); + } + + // ==================== Helper Methods ==================== + + private RESTCatalog initCatalog(String catalogName, Map<String, String> additionalProperties) { + return infrastructure.initCatalog(catalogName, additionalProperties); + } + + private boolean requiresNamespaceCreate() { + return true; + } + + private void setParserContext(org.apache.iceberg.Table table) { + infrastructure.setParserContext(table); + this.parserContext = infrastructure.parserContext(); + } + + private RESTCatalog scanPlanningCatalog() { + return restCatalogWithScanPlanning; + } + + private void configurePlanningBehavior( + Function<TestPlanningBehavior.Builder, TestPlanningBehavior.Builder> configurator) { + TestPlanningBehavior.Builder builder = TestPlanningBehavior.builder(); + adapterForRESTServer.setPlanningBehavior(configurator.apply(builder).build()); + } + + private Table createTableWithScanPlanning(RESTCatalog catalog, String tableName) { + return createTableWithScanPlanning(catalog, TableIdentifier.of(NS, tableName)); + } + + private Table createTableWithScanPlanning(RESTCatalog catalog, TableIdentifier identifier) { + if (requiresNamespaceCreate()) { + catalog.createNamespace(identifier.namespace()); + } + + return catalog.buildTable(identifier, SCHEMA).withPartitionSpec(SPEC).create(); + } + + private RESTTable restTableFor(RESTCatalog catalog, String tableName) { + Table table = createTableWithScanPlanning(catalog, tableName); + table.newAppend().appendFile(FILE_A).commit(); + assertThat(table).isInstanceOf(RESTTable.class); + return (RESTTable) table; + } + + private RESTTableScan restTableScanFor(Table table) { + assertThat(table).isInstanceOf(RESTTable.class); + RESTTable restTable = (RESTTable) table; + TableScan scan = restTable.newScan(); + assertThat(scan).isInstanceOf(RESTTableScan.class); + return (RESTTableScan) scan; + } + + // ==================== Test Planning Behavior ==================== + + /** Enum for parameterized tests to test both synchronous and asynchronous planning modes. */ + enum PlanningMode + implements Function<TestPlanningBehavior.Builder, TestPlanningBehavior.Builder> { + SYNCHRONOUS(TestPlanningBehavior.Builder::synchronous), + ASYNCHRONOUS(TestPlanningBehavior.Builder::asynchronous); + + private final Function<TestPlanningBehavior.Builder, TestPlanningBehavior.Builder> configurer; + + PlanningMode(Function<TestPlanningBehavior.Builder, TestPlanningBehavior.Builder> configurer) { + this.configurer = configurer; + } + + @Override + public TestPlanningBehavior.Builder apply(TestPlanningBehavior.Builder builder) { + return this.configurer.apply(builder); + } + } + + private static class TestPlanningBehavior implements RESTCatalogAdapter.PlanningBehavior { + private final boolean asyncPlanning; + private final int tasksPerPage; + + private TestPlanningBehavior(boolean asyncPlanning, int tasksPerPage) { + this.asyncPlanning = asyncPlanning; + this.tasksPerPage = tasksPerPage; + } + + static Builder builder() { + return new Builder(); + } + + @Override + public boolean shouldPlanTableScanAsync(Scan<?, FileScanTask, ?> scan) { + return asyncPlanning; + } + + @Override + public int numberFileScanTasksPerPlanTask() { + return tasksPerPage; + } + + protected static class Builder { + private boolean asyncPlanning; + private int tasksPerPage; + + Builder asyncPlanning(boolean async) { + asyncPlanning = async; + return this; + } + + Builder tasksPerPage(int tasks) { + tasksPerPage = tasks; + return this; + } + + // Convenience methods for common test scenarios + Builder synchronous() { + return asyncPlanning(false).tasksPerPage(100); + } + + Builder synchronousWithPagination() { + return asyncPlanning(false).tasksPerPage(1); + } + + Builder asynchronous() { + return asyncPlanning(true).tasksPerPage(100); + } + + TestPlanningBehavior build() { + return new TestPlanningBehavior(asyncPlanning, tasksPerPage); + } + } + } + + @ParameterizedTest + @EnumSource(PlanningMode.class) + public void scanPlanningWithAllTasksInSingleResponse() throws IOException { + configurePlanningBehavior(TestPlanningBehavior.Builder::synchronous); Review Comment: missed it :( -- 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]
