Skip to content

Commit

Permalink
[CALCITE-5651] Inferred scale for decimal should not exceed maximum a…
Browse files Browse the repository at this point in the history
…llowed scale
  • Loading branch information
Mihai Budiu authored and rubenada committed Apr 21, 2023
1 parent 74524cf commit 1fe1ce8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1773,8 +1773,9 @@ public static boolean isAtomic(RelDataType type) {
* type system. */
public static RelDataType getMaxPrecisionScaleDecimal(RelDataTypeFactory factory) {
int maxPrecision = factory.getTypeSystem().getMaxNumericPrecision();
int maxScale = factory.getTypeSystem().getMaxNumericScale();
// scale should not greater than precision.
int scale = maxPrecision / 2;
int scale = Math.min(maxPrecision / 2, maxScale);
return factory.createSqlType(SqlTypeName.DECIMAL, maxPrecision, scale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4487,6 +4487,19 @@ private void checkLiteral2(String expression, String expected) {
sql(sql).ok(expected);
}

// Test for [CALCITE-5651] Inferred scale for decimal should
// not exceed maximum allowed scale
@Test void testNumericScale() {
final String sql = "WITH v(x) AS (VALUES('4.2')) "
+ " SELECT x1 + x2 FROM v AS v1(x1), v AS V2(x2)";
final String expected = "SELECT CAST(\"t\".\"EXPR$0\" AS "
+ "DECIMAL(39, 10)) + CAST(\"t0\".\"EXPR$0\" AS "
+ "DECIMAL(39, 10))\nFROM (VALUES ('4.2')) AS "
+ "\"t\" (\"EXPR$0\"),\n(VALUES ('4.2')) AS \"t0\" (\"EXPR$0\")";
sql(sql).withPostgresqlModifiedDecimalTypeSystem()
.ok(expected);
}

@Test void testMatchRecognizePatternExpression2() {
final String sql = "select *\n"
+ " from \"product\" match_recognize\n"
Expand Down Expand Up @@ -6944,6 +6957,20 @@ Sql withPostgresqlModifiedTypeSystem() {
return dialect(postgresqlSqlDialect);
}

Sql withPostgresqlModifiedDecimalTypeSystem() {
final PostgresqlSqlDialect postgresqlSqlDialect =
new PostgresqlSqlDialect(PostgresqlSqlDialect.DEFAULT_CONTEXT
.withDataTypeSystem(new RelDataTypeSystemImpl() {
@Override public int getMaxNumericScale() {
return 10;
}
@Override public int getMaxNumericPrecision() {
return 39;
}
}));
return dialect(postgresqlSqlDialect);
}

Sql withOracleModifiedTypeSystem() {
// Oracle dialect with max length for varchar set to 512
final OracleSqlDialect oracleSqlDialect =
Expand Down

0 comments on commit 1fe1ce8

Please sign in to comment.