mattcasters commented on code in PR #8548: URL: https://github.com/apache/hop/pull/8548#discussion_r4083680566
########## plugins/databases/sqlite/src/main/java/org/apache/hop/databases/sqlite/SqliteDeclaredTypes.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.hop.databases.sqlite; + +import java.sql.Types; +import java.util.Locale; +import org.apache.hop.core.database.DatabaseMeta; +import org.apache.hop.core.database.types.DatabaseColumn; +import org.apache.hop.core.database.types.IDatabaseTypeRule; +import org.apache.hop.core.util.Utils; +import org.apache.hop.core.variables.IVariables; + +/** + * The JDBC type of a SQLite table column, taken from its declared type alone. + * + * <p>The xerial driver answers {@code getColumnType()} from the declared type and the storage class + * of the value on the current row together. Before a query runs there is no row and the declared + * type decides; once it runs, a value the declared type does not expect wins: a column declared + * NUMERIC reads as INTEGER when the first row holds 42, as NUMERIC when it holds null, and as + * VARCHAR when it holds text. One column, three types, depending on the data. + * + * <p>Hop describes a query twice, once from the prepared statement to show the fields and once from + * the result to read the rows, and the two have to agree. A transform that takes its fields from + * the first and its values from the second otherwise gets a Long in a Number field. See issue + * #3633. + * + * <p>So the type a table column is read as only depends on its declared type: see {@link + * #jdbcType}. An expression column is left alone: it has no declared type to go by, only its data. + */ +final class SqliteDeclaredTypes { + + private SqliteDeclaredTypes() { + // Utility class. + } + + /** Puts the declared type's JDBC type back on a table column the driver typed from its data. */ + static final IDatabaseTypeRule RULE = + new IDatabaseTypeRule() { + @Override + public DatabaseColumn correctColumn( + IVariables variables, DatabaseMeta databaseMeta, DatabaseColumn column) { + if (Utils.isEmpty(column.getTableName()) || Utils.isEmpty(column.getNativeTypeName())) { + return null; + } + int declared = jdbcType(column.getNativeTypeName()); + return declared == column.getSqlType() ? null : column.withSqlType(declared); Review Comment: **[bug]** Replacing the driver's JDBC type with the declared type sends SQLite `NUMERIC` / `DECIMAL` / `NUMBER` columns through `StandardJdbcTypeMapper`, which treats declared precision and scale as real SQL precision. That does not match SQLite, and it regresses reads that already worked. On an executed query the xerial driver reports an unsized `NUMERIC` column (the #3633 shape: precision 0, scale 0) as `Types.INTEGER` or `Types.BIGINT` when the value is an integer, and Hop read it with `getLong`. After this correction the column is `Types.NUMERIC` with precision 0, so the mapper produces Hop Number and `ValueMetaBase` calls `ResultSet.getDouble`. Integers outside the 53-bit mantissa are silently rounded (`(1L << 53) + 3` comes back as `(1L << 53) + 4`). Table Input and any other executed-metadata read of those ids loses precision. Database Join only looked inconsistent because prepared-statement metadata was already Number; making the value a double fixes the cast by discarding exactness. The same correction turns `NUMERIC(p)` / `DECIMAL(p)` with `p > 18` and scale 0 into BigNumber. `ResultSet.getBigDecimal` then throws `SQLException: Bad value for type BigDecimal` on non-numeric text, which SQLite stores legally in a numeric-affinity column. Previously the driver reported `VARCHAR` and Hop returned the text. A non-numeric value in an unsized `NUMERIC` column is worse in a different way: `getDouble` returns `0.0` and `wasNull()` is false, so the text becomes zero. Integer values in a sized column also change from `Long` to `BigDecimal`, so pipelines that already read them as Integer can hit the same metadata/value mismatch this PR is fixing. `SqliteDeclaredTypesTest` only compares `IValueMeta` types. `aNumericColumnHoldingIntegersIsANumberThroughARecursiveQuery` asserts `TYPE_NUMBER`, which locks this mapping in, and never reads a 64-bit integer or a non-numeric string. **Suggestion:** Keep prepared-statement and result-set metadata identical, but do not use a double for exact SQLite numerics. Read declared `NUMERIC` / `DECIMAL` / `NUMBER` as BigNumber via a SQLite binding that uses `getBigDecimal` when it succeeds and falls back to the stored text otherwise, and assert that `Long.MAX_VALUE` and a non-numeric string round-trip. If the only goal is the #3633 cast, map unsized integral `NUMERIC` to Integer on both paths so the field and the value stay `Long`. -- 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]
