stankiewicz commented on code in PR #41848:
URL: https://github.com/apache/beam/pull/41848#discussion_r4784822111


##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java:
##########
@@ -1710,14 +1730,34 @@ public KV<Long, KV<PartitionColumnT, PartitionColumnT>> 
apply(
               .withOutputParallelization(false)
               .withDisableAutoCommit(getDisableAutoCommit());
 
-      if (getUseBeamSchema()) {
-        checkStateNotNull(schema);
-        readAll = readAll.withCoder((Coder<T>) RowCoder.of(schema));
-      } else if (getCoder() != null) {
-        readAll = readAll.withCoder(getCoder());
+      @Nullable Coder<T> coder =
+          getUseBeamSchema() ? (Coder<T>) 
RowCoder.of(checkStateNotNull(schema)) : getCoder();
+      if (coder != null) {
+        readAll = readAll.withCoder(coder);
+      }
+
+      PCollection<T> rangeRows = ranges.apply("Read ranges", readAll);
+      if (!inferBounds) {
+        return rangeRows;
+      }
+
+      // NULL never matches the range condition, so when reading the whole 
table, rows with a NULL
+      // partition column are read with a separate query.
+      JdbcIO.Read<T> readNulls =
+          JdbcIO.<T>read()
+              .withDataSourceProviderFn(dataSourceProviderFn)
+              .withQuery(
+                  String.format("select * from %1$s where %2$s is null", 
table, partitionColumn))
+              .withRowMapper(rowMapper)
+              .withFetchSize(getFetchSize())
+              .withDisableAutoCommit(getDisableAutoCommit());
+      if (coder != null) {
+        readNulls = readNulls.withCoder(coder);

Review Comment:
   this will cause a full table scan on jobs that:
   1. don't set bounds
   2. do not index nulls. 
   
   including nulls should be behind options, not unconditional. 
   



##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java:
##########
@@ -1618,15 +1634,19 @@ && getLowerBound() instanceof Comparable<?>) {
 
       PCollection<KV<Long, KV<PartitionColumnT, PartitionColumnT>>> params;
 
-      if (getLowerBound() == null && getUpperBound() == null) {
+      boolean inferBounds = getLowerBound() == null && getUpperBound() == null;
+      if (inferBounds) {
+        // min and max are NULL for an empty or all-NULL column. The HAVING 
clause skips range
+        // generation in that case, as there is no range to read.
         String query =
             String.format(
-                "SELECT min(%s), max(%s) FROM %s", partitionColumn, 
partitionColumn, table);
+                "SELECT min(%1$s), max(%1$s) FROM %2$s HAVING min(%1$s) IS NOT 
NULL",
+                partitionColumn, table);
         if (getNumPartitions() == null) {
           query =
               String.format(
-                  "SELECT min(%s), max(%s), count(*) FROM %s",
-                  partitionColumn, partitionColumn, table);
+                  "SELECT min(%1$s), max(%1$s), count(*) FROM %2$s HAVING 
min(%1$s) IS NOT NULL",
+                  partitionColumn, table);

Review Comment:
   `having` is not fully supported in various engines
   ```
   SELECT min_val, max_val
   FROM (SELECT min(%1$s) AS min_val, max(%1$s) AS max_val FROM %2$s) 
beam_bounds
   WHERE min_val IS NOT NULL
   ```
   is safer



##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java:
##########
@@ -1710,14 +1730,34 @@ public KV<Long, KV<PartitionColumnT, PartitionColumnT>> 
apply(
               .withOutputParallelization(false)
               .withDisableAutoCommit(getDisableAutoCommit());
 
-      if (getUseBeamSchema()) {
-        checkStateNotNull(schema);
-        readAll = readAll.withCoder((Coder<T>) RowCoder.of(schema));
-      } else if (getCoder() != null) {
-        readAll = readAll.withCoder(getCoder());
+      @Nullable Coder<T> coder =
+          getUseBeamSchema() ? (Coder<T>) 
RowCoder.of(checkStateNotNull(schema)) : getCoder();
+      if (coder != null) {
+        readAll = readAll.withCoder(coder);
+      }
+
+      PCollection<T> rangeRows = ranges.apply("Read ranges", readAll);
+      if (!inferBounds) {
+        return rangeRows;
+      }
+
+      // NULL never matches the range condition, so when reading the whole 
table, rows with a NULL
+      // partition column are read with a separate query.
+      JdbcIO.Read<T> readNulls =
+          JdbcIO.<T>read()
+              .withDataSourceProviderFn(dataSourceProviderFn)
+              .withQuery(
+                  String.format("select * from %1$s where %2$s is null", 
table, partitionColumn))
+              .withRowMapper(rowMapper)
+              .withFetchSize(getFetchSize())
+              .withDisableAutoCommit(getDisableAutoCommit());

Review Comment:
    .withOutputParallelization(false) is missing to have consistent behaviour. 
between partition and null branch



##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java:
##########
@@ -239,6 +241,11 @@
  *       provided, but not upper or lower bounds.
  * </ul>
  *
+ * <p>When the bounds are auto-inferred, rows where the partitioning column is 
{@code NULL} are read
+ * with one additional query ({@code select * from table where column is 
null}), so the whole table
+ * is read. When the bounds are provided, only rows with a partitioning column 
value between the

Review Comment:
   Due to performance reasons this behavior should be opt-in.



-- 
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]

Reply via email to