reswqa commented on code in PR #21977: URL: https://github.com/apache/flink/pull/21977#discussion_r1112847462
########## flink-connectors/flink-connector-hive/src/test/java/org/apache/flink/connectors/hive/HiveTableCompactSinkTest.java: ########## @@ -0,0 +1,191 @@ +/* + * 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.flink.connectors.hive; + +import org.apache.flink.api.dag.Transformation; +import org.apache.flink.connector.file.table.batch.BatchSink; +import org.apache.flink.table.api.SqlDialect; +import org.apache.flink.table.api.TableEnvironment; +import org.apache.flink.table.api.internal.TableEnvironmentImpl; +import org.apache.flink.table.catalog.hive.HiveCatalog; +import org.apache.flink.table.catalog.hive.HiveTestUtils; +import org.apache.flink.table.operations.ModifyOperation; +import org.apache.flink.table.operations.Operation; +import org.apache.flink.table.planner.delegation.PlannerBase; +import org.apache.flink.util.TestLoggerExtension; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.util.Collections; +import java.util.List; + +import static org.apache.flink.connector.file.table.FileSystemConnectorOptions.COMPACTION_PARALLELISM; +import static org.apache.flink.connector.file.table.FileSystemConnectorOptions.SINK_PARALLELISM; +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link HiveTableSink} enable auto-compaction. */ +@ExtendWith(TestLoggerExtension.class) +class HiveTableCompactSinkTest { + /** + * Represents the parallelism need not check, it should follow the setting of planer or auto + * inference. + */ + public static final int NEED_NOT_CHECK_PARALLELISM = -1; + + private HiveCatalog catalog; + + private TableEnvironment tableEnv; + + @BeforeEach + void before() { + catalog = HiveTestUtils.createHiveCatalog(); + catalog.open(); + tableEnv = HiveTestUtils.createTableEnvInBatchMode(SqlDialect.HIVE); + tableEnv.registerCatalog(catalog.getName(), catalog); + tableEnv.useCatalog(catalog.getName()); + } + + @AfterEach + void after() { + if (catalog != null) { + catalog.close(); + } + } + + /** If only sink parallelism is set, compact operator should follow this setting. */ + @Test + void testOnlySetSinkParallelism() { + final int sinkParallelism = 4; + + tableEnv.executeSql( + String.format( + "CREATE TABLE src (" + + " key string," + + " value string" + + ") TBLPROPERTIES (" + + " 'auto-compaction' = 'true', " + + " '%s' = '%s' )", + SINK_PARALLELISM.key(), sinkParallelism)); + + assertSinkAndCompactOperatorParallelism(true, true, sinkParallelism, sinkParallelism); + } + + @Test + void testOnlySetCompactParallelism() { + final int compactParallelism = 4; + + tableEnv.executeSql( + String.format( + "CREATE TABLE src (" + + " key string," + + " value string" + + ") TBLPROPERTIES (" + + " 'auto-compaction' = 'true', " + + " '%s' = '%s' )", + COMPACTION_PARALLELISM.key(), compactParallelism)); + + assertSinkAndCompactOperatorParallelism( + false, true, NEED_NOT_CHECK_PARALLELISM, compactParallelism); + } + + @Test + void testSetBothSinkAndCompactParallelism() { + final int sinkParallelism = 8; + final int compactParallelism = 4; + + tableEnv.executeSql( + String.format( + "CREATE TABLE src (" + + " key string," + + " value string" + + ") TBLPROPERTIES (" + + " 'auto-compaction' = 'true', " + + " '%s' = '%s', " + + " '%s' = '%s' )", + SINK_PARALLELISM.key(), + sinkParallelism, + COMPACTION_PARALLELISM.key(), + compactParallelism)); + + assertSinkAndCompactOperatorParallelism(true, true, sinkParallelism, compactParallelism); + } + + @Test + void testSinkAndCompactAllNotSetParallelism() { + tableEnv.executeSql( + "CREATE TABLE src (" + + " key string," + + " value string" + + ") TBLPROPERTIES (" + + " 'auto-compaction' = 'true' )"); + assertSinkAndCompactOperatorParallelism( + false, false, NEED_NOT_CHECK_PARALLELISM, NEED_NOT_CHECK_PARALLELISM); + } + + private void assertSinkAndCompactOperatorParallelism( + boolean isSinkParallelismConfigured, + boolean isCompactParallelismConfigured, + int expectedSinkParallelism, + int expectedCompactParallelism) { + String statement = "insert into src values ('k1', 'v1'), ('k2', 'v2');"; + PlannerBase planner = (PlannerBase) ((TableEnvironmentImpl) tableEnv).getPlanner(); + planner.getExecEnv().setParallelism(10); + List<Operation> operations = planner.getParser().parse(statement); + List<Transformation<?>> transformations = + planner.translate(Collections.singletonList((ModifyOperation) (operations.get(0)))); + assertThat(transformations).hasSize(1); + Transformation<?> rootTransformation = transformations.get(0); + Transformation<?> compactTransformation = + findTransformationByName(rootTransformation, BatchSink.COMPACT_OP_NAME); + Transformation<?> hiveSinkTransformation = + findTransformationByName( + rootTransformation, HiveTableSink.BATCH_COMPACT_WRITER_OP_NAME); + assertThat(hiveSinkTransformation.isParallelismConfigured()) + .isEqualTo(isSinkParallelismConfigured); + assertThat(compactTransformation.isParallelismConfigured()) + .isEqualTo(isCompactParallelismConfigured); + if (isSinkParallelismConfigured) { Review Comment: Yes, it's just a placeholder. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org