Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think that the choice of maxPrecision/2 is rather arbitrary, but I didn't want to change too many things at once. This used to be maxScale, and that should always be smaller than maxPrecision.

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