|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.cdc.connectors.base.source.assigner.splitter; |
| 19 | + |
| 20 | +import org.apache.flink.table.api.DataTypes; |
| 21 | + |
| 22 | +import io.debezium.jdbc.JdbcConnection; |
| 23 | +import io.debezium.relational.Column; |
| 24 | +import io.debezium.relational.TableId; |
| 25 | +import org.assertj.core.api.Assertions; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import javax.annotation.Nullable; |
| 29 | + |
| 30 | +import java.sql.SQLException; |
| 31 | + |
| 32 | +/** Tests for {@link JdbcSourceChunkSplitter}. */ |
| 33 | +class JdbcSourceChunkSplitterTest { |
| 34 | + |
| 35 | + /** |
| 36 | + * Guards against regressions where {@code queryNextChunkMax} may return {@code null} (e.g. when |
| 37 | + * the max row was removed after MIN/MAX was determined). {@code nextChunkEnd} must handle this |
| 38 | + * gracefully and return null without throwing exceptions. |
| 39 | + */ |
| 40 | + @Test |
| 41 | + void testNextChunkEndReturnsNullWhenMaxRowRemoved() throws Exception { |
| 42 | + // given a splitter whose queryNextChunkMax always returns null |
| 43 | + JdbcSourceChunkSplitter splitter = new TestingJdbcSourceChunkSplitter(null); |
| 44 | + |
| 45 | + TableId tableId = TableId.parse("catalog.db.table"); |
| 46 | + Column splitColumn = |
| 47 | + Column.editor().name("id").type("INT").jdbcType(java.sql.Types.INTEGER).create(); |
| 48 | + |
| 49 | + Object previousChunkEnd = 10; |
| 50 | + Object max = 100; |
| 51 | + int chunkSize = 5; |
| 52 | + |
| 53 | + // when queryNextChunkMax returns null, nextChunkEnd should also return null |
| 54 | + Object result = |
| 55 | + splitter.nextChunkEnd(null, previousChunkEnd, tableId, splitColumn, max, chunkSize); |
| 56 | + |
| 57 | + Assertions.assertThat(result).isNull(); |
| 58 | + } |
| 59 | + |
| 60 | + /** Minimal testing implementation that stubs out JDBC interactions. */ |
| 61 | + private static class TestingJdbcSourceChunkSplitter extends JdbcSourceChunkSplitter { |
| 62 | + |
| 63 | + @Nullable private final Object nextChunkMaxResult; |
| 64 | + |
| 65 | + TestingJdbcSourceChunkSplitter(@Nullable Object nextChunkMaxResult) { |
| 66 | + super(null, null, null, null, null); |
| 67 | + this.nextChunkMaxResult = nextChunkMaxResult; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected Object queryNextChunkMax( |
| 72 | + JdbcConnection jdbc, |
| 73 | + TableId tableId, |
| 74 | + Column splitColumn, |
| 75 | + int chunkSize, |
| 76 | + Object includedLowerBound) |
| 77 | + throws SQLException { |
| 78 | + return nextChunkMaxResult; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected Long queryApproximateRowCnt(JdbcConnection jdbc, TableId tableId) |
| 83 | + throws SQLException { |
| 84 | + return 0L; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected Object queryMin( |
| 89 | + JdbcConnection jdbc, TableId tableId, Column splitColumn, Object excludedLowerBound) |
| 90 | + throws SQLException { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected org.apache.flink.table.types.DataType fromDbzColumn(Column splitColumn) { |
| 96 | + // The concrete type is irrelevant for this test; just return a simple numeric type. |
| 97 | + return DataTypes.BIGINT(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments