You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by ru...@apache.org on 2023/04/21 08:55:30 UTC
[calcite] branch main updated: [CALCITE-5651] Inferred scale for decimal should not exceed maximum allowed scale
This is an automated email from the ASF dual-hosted git repository.
rubenql pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 1fe1ce8845 [CALCITE-5651] Inferred scale for decimal should not exceed maximum allowed scale
1fe1ce8845 is described below
commit 1fe1ce88455f3bbae9ba9c032298695d9c6ffb2b
Author: Mihai Budiu <mb...@vmware.com>
AuthorDate: Wed Apr 19 09:32:42 2023 -0700
[CALCITE-5651] Inferred scale for decimal should not exceed maximum allowed scale
---
.../org/apache/calcite/sql/type/SqlTypeUtil.java | 3 ++-
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 27 ++++++++++++++++++++++
2 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
index 9895c989f9..aef3fd0725 100644
--- a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
+++ b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
@@ -1773,8 +1773,9 @@ public abstract class SqlTypeUtil {
* 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);
}
diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 18644c5672..8cae0d8deb 100644
--- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -4487,6 +4487,19 @@ class RelToSqlConverterTest {
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"
@@ -6944,6 +6957,20 @@ class RelToSqlConverterTest {
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 =