Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace NUMERIC without parameters with DECFLOAT in PostgreSQL compatibility mode #3540

Merged
merged 2 commits into from Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion h2/pom.xml
Expand Up @@ -44,7 +44,7 @@
<junit.version>5.6.2</junit.version>
<lucene.version>8.5.2</lucene.version>
<osgi.version>5.0.0</osgi.version>
<pgjdbc.version>42.3.2</pgjdbc.version>
<pgjdbc.version>42.4.0</pgjdbc.version>
<javax.servlet.version>4.0.1</javax.servlet.version>
<jakarta.servlet.version>5.0.0</jakarta.servlet.version>
<slf4j.version>1.7.30</slf4j.version>
Expand Down
2 changes: 2 additions & 0 deletions h2/src/docsrc/html/changelog.html
Expand Up @@ -21,6 +21,8 @@ <h1>Change Log</h1>

<h2>Next Version (unreleased)</h2>
<ul>
<li>Issue #3538: In Postgres compatibility mode the NUMERIC type w/o scale should not default to 0
</li>
<li>Issue #3534: Subquery has incorrect empty parameters since 2.1.210 that breaks sameResultAsLast()
</li>
<li>Issue #3390: "ROW" cannot be set as a non keyword in 2.x
Expand Down
1 change: 1 addition & 0 deletions h2/src/docsrc/html/features.html
Expand Up @@ -1089,6 +1089,7 @@ <h3>PostgreSQL Compatibility Mode</h3>
</li><li>ON CONFLICT DO NOTHING is supported in INSERT statements.
</li><li>Spaces are trimmed from the right side of CHAR values, but CHAR values in result sets are right-padded with
spaces to the declared length.
</li><li>NUMERIC and DECIMAL/DEC data types without parameters are treated like DECFLOAT data type.
</li><li>MONEY data type is treated like NUMERIC(19, 2) data type.
</li><li>Datetime value functions return the same value within a transaction.
</li><li>ARRAY_SLICE() out of bounds parameters are silently corrected.
Expand Down
5 changes: 5 additions & 0 deletions h2/src/main/org/h2/command/Parser.java
Expand Up @@ -6164,6 +6164,9 @@ private TypeInfo readIfDataType1() {
original = "CHARACTER LARGE OBJECT";
}
break;
case "DATE":
return database.getMode().dateIsTimestamp0 ? TypeInfo.getTypeInfo(Value.TIMESTAMP, -1L, 0, null)
: TypeInfo.TYPE_DATE;
case "DATETIME":
case "DATETIME2":
return parseDateTimeType(false);
Expand Down Expand Up @@ -6370,6 +6373,8 @@ private TypeInfo parseNumericType(boolean decimal) {
}
}
read(CLOSE_PAREN);
} else if (database.getMode().numericIsDecfloat) {
return TypeInfo.TYPE_DECFLOAT;
}
return TypeInfo.getTypeInfo(Value.NUMERIC, precision, scale, decimal ? ExtTypeInfoNumeric.DECIMAL : null);
}
Expand Down
17 changes: 12 additions & 5 deletions h2/src/main/org/h2/engine/Mode.java
Expand Up @@ -423,6 +423,16 @@ public enum CharPadding {
*/
public boolean autoIncrementClause;

/**
* Whether DATE data type is parsed as TIMESTAMP(0).
*/
public boolean dateIsTimestamp0;

/**
* Whether NUMERIC and DECIMAL/DEC without parameters are parsed as DECFLOAT.
*/
public boolean numericIsDecfloat;

/**
* An optional Set of hidden/disallowed column types.
* Certain DBMSs don't support all column types provided by H2, such as
Expand Down Expand Up @@ -646,13 +656,9 @@ public enum CharPadding {
mode.minusIsExcept = true;
mode.expressionNames = ExpressionNames.ORIGINAL_SQL;
mode.viewExpressionNames = ViewExpressionNames.EXCEPTION;
mode.dateIsTimestamp0 = true;
mode.typeByNameMap.put("BINARY_FLOAT", DataType.getDataType(Value.REAL));
mode.typeByNameMap.put("BINARY_DOUBLE", DataType.getDataType(Value.DOUBLE));
dt = DataType.createDate(/* 2001-01-01 23:59:59 */ 19, 19, "DATE", false, 0, 0);
dt.type = Value.TIMESTAMP;
dt.sqlType = Types.TIMESTAMP;
dt.specialPrecisionScale = true;
mode.typeByNameMap.put("DATE", dt);
add(mode);

mode = new Mode(ModeEnum.PostgreSQL);
Expand All @@ -673,6 +679,7 @@ public enum CharPadding {
mode.allowUsingFromClauseInUpdateStatement = true;
mode.limit = true;
mode.serialDataTypes = true;
mode.numericIsDecfloat = true;
// Enumerate all H2 types NOT supported by PostgreSQL:
Set<String> disallowedTypes = new java.util.HashSet<>();
disallowedTypes.add("NUMBER");
Expand Down
20 changes: 20 additions & 0 deletions h2/src/test/org/h2/test/scripts/datatypes/numeric.sql
Expand Up @@ -184,5 +184,25 @@ INSERT INTO TEST VALUES CAST(20 AS NUMERIC(2));
DROP TABLE TEST;
> ok

SET MODE PostgreSQL;
> ok

CREATE TABLE TEST(A NUMERIC, B DECIMAL, C DEC, D NUMERIC(1));
> ok

SELECT COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, NUMERIC_SCALE,
DECLARED_DATA_TYPE, DECLARED_NUMERIC_PRECISION, DECLARED_NUMERIC_SCALE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TEST' ORDER BY ORDINAL_POSITION;
> COLUMN_NAME DATA_TYPE NUMERIC_PRECISION NUMERIC_PRECISION_RADIX NUMERIC_SCALE DECLARED_DATA_TYPE DECLARED_NUMERIC_PRECISION DECLARED_NUMERIC_SCALE
> ----------- --------- ----------------- ----------------------- ------------- ------------------ -------------------------- ----------------------
> A DECFLOAT 100000 10 null DECFLOAT null null
> B DECFLOAT 100000 10 null DECFLOAT null null
> C DECFLOAT 100000 10 null DECFLOAT null null
> D NUMERIC 1 10 0 NUMERIC 1 null
> rows (ordered): 4

DROP TABLE TEST;
> ok

SET MODE Regular;
> ok
4 changes: 2 additions & 2 deletions h2/src/tools/org/h2/build/Build.java
Expand Up @@ -53,9 +53,9 @@ public class Build extends BuildBase {

private static final String OSGI_VERSION = "5.0.0";

private static final String PGJDBC_VERSION = "42.3.2";
private static final String PGJDBC_VERSION = "42.4.0";

private static final String PGJDBC_HASH = "8fd7a20f008a58b97b26ba5c5084ee61602203aa";
private static final String PGJDBC_HASH = "21ff952426bbfe4a041c175407333d4a07c70931";

private static final String JAVAX_SERVLET_VERSION = "4.0.1";

Expand Down