nacz0 opened a new issue, #41520:
URL: https://github.com/apache/beam/issues/41520

   ### What happened?
   
   JdbcIO schema inference discards declared precision and scale for columns 
reported as JDBC `DECIMAL`, even when both are available in JDBC metadata. 
Fixed-precision JDBC `NUMERIC` preserves them.
   
   In [SchemaUtil.java in Beam 
2.77.0](https://github.com/apache/beam/blob/v2.77.0/sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/SchemaUtil.java#L125),
 `DECIMAL` maps directly to `FieldType.DECIMAL`, whereas `NUMERIC` uses 
`beamLogicalNumericField()` and creates `FixedPrecisionNumeric(p, s)` when 
precision is specified. The same mapping is present in [master at 
52d59b7417d0](https://github.com/apache/beam/blob/52d59b7417d0971cfe9e6d4875be194aa0a6c8bd/sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/SchemaUtil.java#L125).
   
   ### Environment
   
   - Apache Beam Python SDK: **2.77.0**
   - Python: **3.12.16**
   - Expansion service: **`apache/beam_java_expansion_service:2.77.0`**, Java 
**17.0.20.1**
   - SQL Server: **2022 (16.0.4305.5)**, Microsoft JDBC driver **12.2.0**
   - MySQL: **8.0.46**, Connector/J **8.0.22**
   - Both JDBC drivers are bundled in the expansion service.
   
   ### Observed behavior
   
   I reproduced this using Python `ReadFromJdbc` with automatic schema 
inference during expansion:
   
   | Source column or expression | Inferred field type |
   |---|---|
   | SQL Server `DECIMAL(12,2)` | `beam:logical_type:decimal:v1`, no 
precision/scale arguments |
   | SQL Server `NUMERIC(12,2)` | `beam:logical_type:fixed_decimal:v1`, with 
`(12,2)` |
   | SQL Server `CAST(col AS NUMERIC(12,2))` | `fixed_decimal:v1`, with 
`(12,2)` |
   | MySQL `DECIMAL(12,2)`, `NUMERIC(12,2)`, or `CAST(... AS DECIMAL(12,2))` | 
`decimal:v1`, no precision/scale arguments |
   
   The SQL Server cases use the same database and driver. Connector/J reports 
both MySQL `DECIMAL` and `NUMERIC` columns as JDBC `DECIMAL`, so changing 
between those column declarations does not avoid this mapping.
   
   JdbcIO reads the values correctly. The loss concerns the column's declared 
precision and scale.
   
   ### Minimal reproduction without a database
   
   Both metadata objects below have precision 12 and scale 2; only the JDBC 
type code changes. Save as `JdbcDecimalRepro.java`:
   
   ```java
   import java.sql.JDBCType;
   import java.sql.Types;
   import javax.sql.rowset.RowSetMetaDataImpl;
   import org.apache.beam.sdk.io.jdbc.SchemaUtil;
   import org.apache.beam.sdk.schemas.SchemaTranslation;
   import org.apache.beam.sdk.values.Row;
   
   public class JdbcDecimalRepro {
     public static void main(String[] args) throws Exception {
       for (int type : new int[] {Types.DECIMAL, Types.NUMERIC}) {
         var md = new RowSetMetaDataImpl();
         md.setColumnCount(1);
         md.setColumnLabel(1, "amount");
         md.setColumnType(1, type);
         md.setPrecision(1, 12);
         md.setScale(1, 2);
   
         var schema = SchemaUtil.toBeamSchema(md);
         var fieldType = schema.getField(0).getType();
         var logical = SchemaTranslation.schemaToProto(schema, false)
             .getFields(0).getType().getLogicalType();
         String parameters = "none";
         if (logical.hasArgument()) {
           Row argument = (Row) fieldType.getLogicalType().getArgument();
           parameters = argument.getInt32("precision") + "," + 
argument.getInt32("scale");
         }
         System.out.printf("%s (%d), metadata=(%d,%d) -> %s, parameters=%s%n",
             JDBCType.valueOf(type), type, md.getPrecision(1), md.getScale(1),
             logical.getUrn(), parameters);
       }
     }
   }
   ```
   
   Download the [official Beam 2.77.0 schemaio expansion-service 
JAR](https://repo.maven.apache.org/maven2/org/apache/beam/beam-sdks-java-extensions-schemaio-expansion-service/2.77.0/beam-sdks-java-extensions-schemaio-expansion-service-2.77.0.jar),
 save it as `beam-jdbc.jar`, and run with Java 17:
   
   ```text
   java -cp beam-jdbc.jar JdbcDecimalRepro.java
   ```
   
   Actual output from the isolated check with Temurin Java 17.0.19+10:
   
   ```text
   DECIMAL (3), metadata=(12,2) -> beam:logical_type:decimal:v1, parameters=none
   NUMERIC (2), metadata=(12,2) -> beam:logical_type:fixed_decimal:v1, 
parameters=12,2
   ```
   
   ### Expected behavior and possible fix
   
   For fixed-precision columns, both JDBC `DECIMAL` and `NUMERIC` should 
preserve precision and scale supplied by the metadata.
   
   Routing `DECIMAL` through `beamLogicalNumericField()` would address this, 
retaining an unconstrained decimal fallback when precision is unspecified. It 
changes inferred schemas for existing fixed-precision `DECIMAL` columns, so 
compatibility needs consideration.
   
   Those parameters are needed to reconstruct the declared decimal type in 
downstream schemas such as Parquet, Iceberg, and Avro. This does not establish 
support in every downstream converter: in particular, Beam 2.77.0 `AvroUtils` 
also lacks support for `FixedPrecisionNumeric`, which is a separate conversion 
issue.
   
   ### Related work
   
   #27380 covers the broader JdbcIO-to-Avro compatibility problem, with earlier 
work in #27333 and #27406. This issue specifically tracks preserving precision 
and scale during JDBC schema inference, independently of Avro conversion 
support.
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [ ] Component: Python SDK
   - [x] Component: Java SDK
   - [ ] Component: Go SDK
   - [ ] Component: Typescript SDK
   - [x] Component: IO connector
   - [ ] Component: Beam YAML
   - [ ] Component: Beam examples
   - [ ] Component: Beam playground
   - [ ] Component: Beam katas
   - [ ] Component: Website
   - [ ] Component: Infrastructure
   - [ ] Component: Spark Runner
   - [ ] Component: Flink Runner
   - [ ] Component: Prism Runner
   - [ ] Component: Hazelcast Jet Runner
   - [ ] Component: Google Cloud Dataflow Runner


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