This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-13406-3b78e3b8751bee4c9af5cd332f17a533d882e731 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 64cd8b5325617a2c8bfd9ac3682479f428b47e56 Author: niumy <[email protected]> AuthorDate: Thu Nov 26 13:43:59 2026 +0000 [Fix][Connector-V2][JDBC] Read the row count of PostgreSQL partitioned tables (#13406) Co-authored-by: muyue niu <[email protected]> --- .../internal/dialect/psql/PostgresDialect.java | 5 +- .../internal/dialect/psql/PostgresDialectTest.java | 33 ++++++++++ .../connectors/seatunnel/jdbc/JdbcPostgresIT.java | 73 ++++++++++++++++++++++ .../jdbc_postgres_partition_source_and_sink.conf | 44 +++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) diff --git a/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialect.java b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialect.java index 0572163d12..e27a828dc1 100644 --- a/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialect.java +++ b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialect.java @@ -281,9 +281,12 @@ public class PostgresDialect implements JdbcDialect { .getFullName() .equals(table.getTablePath().getFullName())); if (useTableStats) { + // Partitioned tables keep their reltuples entry on the parent relation, whose + // relkind is 'p' rather than 'r'. Include both kinds here, otherwise the lookup + // returns no row for a partitioned table and split generation fails. String rowCountQuery = String.format( - "SELECT reltuples FROM pg_class r WHERE relkind = 'r' AND relname = '%s';", + "SELECT reltuples FROM pg_class r WHERE relkind IN ('r', 'p') AND relname = '%s';", table.getTablePath().getTableName()); try (Statement stmt = connection.createStatement()) { log.info("Split Chunk, approximateRowCntStatement: {}", rowCountQuery); diff --git a/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialectTest.java b/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialectTest.java index 6d2648bb58..ea289c3747 100644 --- a/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialectTest.java +++ b/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialectTest.java @@ -17,8 +17,18 @@ package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.psql; +import org.apache.seatunnel.api.table.catalog.TablePath; +import org.apache.seatunnel.connectors.seatunnel.jdbc.source.JdbcSourceTable; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; public class PostgresDialectTest { @@ -54,4 +64,27 @@ public class PostgresDialectTest { doNothingSql, "INSERT INTO \"seatunnel\".\"role\" (\"id\", \"type\", \"role_name\", \"description\", \"create_time\", \"update_time\") VALUES (:id, :type, :role_name, :description, :create_time, :update_time) ON CONFLICT (\"id\", \"type\", \"role_name\", \"description\", \"create_time\", \"update_time\") DO NOTHING"); } + + @Test + void testApproximateRowCntStatementCoversPartitionedTables() throws SQLException { + PostgresDialect dialect = new PostgresDialect(); + JdbcSourceTable table = + JdbcSourceTable.builder().tablePath(TablePath.of("test.abc")).build(); + + Connection connection = Mockito.mock(Connection.class); + Statement statement = Mockito.mock(Statement.class); + ResultSet resultSet = Mockito.mock(ResultSet.class); + Mockito.when(connection.createStatement()).thenReturn(statement); + Mockito.when(statement.executeQuery(Mockito.anyString())).thenReturn(resultSet); + Mockito.when(resultSet.next()).thenReturn(true); + Mockito.when(resultSet.getLong(1)).thenReturn(42L); + + Assertions.assertEquals(42L, dialect.approximateRowCntStatement(connection, table)); + + ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class); + Mockito.verify(statement).executeQuery(sqlCaptor.capture()); + Assertions.assertTrue( + sqlCaptor.getValue().contains("relkind IN ('r', 'p')"), + "The row count query must include partitioned tables: " + sqlCaptor.getValue()); + } } diff --git a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-3/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcPostgresIT.java b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-3/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcPostgresIT.java index 66f218557c..b8e8545a72 100644 --- a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-3/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcPostgresIT.java +++ b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-3/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcPostgresIT.java @@ -82,6 +82,28 @@ public class JdbcPostgresIT extends TestSuiteBase implements TestResource { */ private static final String DROPPED_COLUMN_TABLE = "pg_catalog_dropped_column_test"; + /** Partitioned-table fixtures for the full batch read test. */ + private static final String PARTITION_SOURCE_TABLE = "pg_e2e_partition_source_table"; + + private static final String PARTITION_SINK_TABLE = "pg_e2e_partition_sink_table"; + + private static final String PARTITION_SOURCE_DDL = + "CREATE TABLE IF NOT EXISTS " + + PARTITION_SOURCE_TABLE + + " (\n" + + " gid INT NOT NULL,\n" + + " name VARCHAR(255),\n" + + " PRIMARY KEY (gid)\n" + + ") PARTITION BY RANGE (gid)"; + + private static final String PARTITION_SINK_DDL = + "CREATE TABLE IF NOT EXISTS " + + PARTITION_SINK_TABLE + + " (\n" + + " gid INT PRIMARY KEY,\n" + + " name VARCHAR(255)\n" + + ")"; + private static final List<String> PG_CONFIG_FILE_LIST = Lists.newArrayList( "/jdbc_postgres_source_and_sink.conf", @@ -1068,4 +1090,55 @@ public class JdbcPostgresIT extends TestSuiteBase implements TestResource { Assertions.assertFalse(postgresCatalog.tableExists(tablePathPgSink)); postgresCatalog.close(); } + + /** + * Verifies a full batch read of a PostgreSQL partitioned table. The parent table has a primary + * key, so the JDBC source splits it with the dynamic splitter and reads the approximate row + * count from pg_class; a partitioned parent is exposed there with relkind 'p' instead of 'r'. + * Before the fix the row count lookup returned no row for the parent table and the job failed + * during split generation. + */ + @TestTemplate + public void testPartitionedTableSource(TestContainer container) + throws IOException, InterruptedException { + preparePartitionedTables(); + try { + Container.ExecResult execResult = + container.executeJob("/jdbc_postgres_partition_source_and_sink.conf"); + Assertions.assertEquals( + 0, + execResult.getExitCode(), + "jdbc_postgres_partition_source_and_sink.conf job run failed."); + Assertions.assertIterableEquals( + querySql("select gid, name from " + PARTITION_SOURCE_TABLE + " order by gid"), + querySql("select gid, name from " + PARTITION_SINK_TABLE + " order by gid")); + } finally { + executeSQL("truncate table " + PARTITION_SINK_TABLE); + executeSQL("truncate table " + PARTITION_SOURCE_TABLE); + } + } + + private void preparePartitionedTables() { + executeSQL("DROP TABLE IF EXISTS " + PARTITION_SOURCE_TABLE + " CASCADE"); + executeSQL(PARTITION_SOURCE_DDL); + executeSQL( + "CREATE TABLE " + + PARTITION_SOURCE_TABLE + + "_p0 PARTITION OF " + + PARTITION_SOURCE_TABLE + + " FOR VALUES FROM (0) TO (1000)"); + executeSQL( + "CREATE TABLE " + + PARTITION_SOURCE_TABLE + + "_p1 PARTITION OF " + + PARTITION_SOURCE_TABLE + + " FOR VALUES FROM (1000) TO (2000)"); + executeSQL(PARTITION_SINK_DDL); + executeSQL("truncate table " + PARTITION_SINK_TABLE); + executeSQL("truncate table " + PARTITION_SOURCE_TABLE); + executeSQL( + "insert into " + + PARTITION_SOURCE_TABLE + + " (gid, name) values (1, 'a'), (2, 'b'), (3, 'c'), (1001, 'x'), (1002, 'y'), (1003, 'z')"); + } } diff --git a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-3/src/test/resources/jdbc_postgres_partition_source_and_sink.conf b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-3/src/test/resources/jdbc_postgres_partition_source_and_sink.conf new file mode 100644 index 0000000000..e95ec53cca --- /dev/null +++ b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-3/src/test/resources/jdbc_postgres_partition_source_and_sink.conf @@ -0,0 +1,44 @@ +# +# 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. +# + +env { + parallelism = 1 + job.mode = "BATCH" +} + +source { + Jdbc { + url = "jdbc:postgresql://postgresql:5432/test?loggerLevel=OFF" + driver = "org.postgresql.Driver" + username = "test" + password = "test" + table_path = "test.public.pg_e2e_partition_source_table" + } +} + +sink { + Jdbc { + driver = org.postgresql.Driver + url = "jdbc:postgresql://postgresql:5432/test?loggerLevel=OFF&stringtype=unspecified" + username = test + password = test + generate_sink_sql = true + database = test + table = public.pg_e2e_partition_sink_table + primary_keys = ["gid"] + } +} \ No newline at end of file
