Skip to content

Commit a21deb8

Browse files
committed
feat: improve DECIMAL type handling (#655)
1 parent aebd1bc commit a21deb8

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

isthmus/src/main/java/io/substrait/isthmus/SubstraitTypeSystem.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,29 @@ public int getMaxPrecision(final SqlTypeName typeName) {
3535
case TIMESTAMP:
3636
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
3737
return 6;
38+
case DECIMAL:
39+
return 38;
3840
}
3941
return super.getMaxPrecision(typeName);
4042
}
4143

4244
@Override
43-
public int getMaxNumericScale() {
44-
return 38;
45+
public int getDefaultPrecision(final SqlTypeName typeName) {
46+
switch (typeName) {
47+
case DECIMAL:
48+
return getMaxPrecision(typeName);
49+
default:
50+
return super.getDefaultPrecision(typeName);
51+
}
4552
}
4653

4754
@Override
48-
public int getMaxNumericPrecision() {
49-
return 38;
55+
public int getMaxScale(final SqlTypeName typeName) {
56+
switch (typeName) {
57+
case DECIMAL:
58+
return 38;
59+
}
60+
return super.getMaxScale(typeName);
5061
}
5162

5263
@Override
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.substrait.isthmus;
2+
3+
import static io.substrait.isthmus.SubstraitTypeSystem.TYPE_FACTORY;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.apache.calcite.rel.type.RelDataType;
7+
import org.apache.calcite.rel.type.RelDataTypeSystem;
8+
import org.apache.calcite.sql.type.SqlTypeName;
9+
import org.junit.jupiter.api.Test;
10+
11+
class SubstraitTypeSystemTest {
12+
13+
private final RelDataTypeSystem typeSystem = SubstraitTypeSystem.TYPE_SYSTEM;
14+
15+
@Test
16+
void decimalMaxPrecision() {
17+
assertEquals(38, typeSystem.getMaxPrecision(SqlTypeName.DECIMAL));
18+
}
19+
20+
@Test
21+
void decimalMaxScale() {
22+
assertEquals(38, typeSystem.getMaxScale(SqlTypeName.DECIMAL));
23+
}
24+
25+
@Test
26+
void decimalDefaultPrecision() {
27+
assertEquals(38, typeSystem.getDefaultPrecision(SqlTypeName.DECIMAL));
28+
}
29+
30+
@Test
31+
void decimalDefaultScale() {
32+
assertEquals(0, typeSystem.getDefaultScale(SqlTypeName.DECIMAL));
33+
}
34+
35+
@Test
36+
void timestampMaxPrecision() {
37+
assertEquals(6, typeSystem.getMaxPrecision(SqlTypeName.TIMESTAMP));
38+
}
39+
40+
@Test
41+
void timeMaxPrecision() {
42+
assertEquals(6, typeSystem.getMaxPrecision(SqlTypeName.TIME));
43+
}
44+
45+
@Test
46+
void canCreateDecimalWithMaxPrecision() {
47+
RelDataType decimalType = TYPE_FACTORY.createSqlType(SqlTypeName.DECIMAL, 38, 10);
48+
assertEquals(38, decimalType.getPrecision());
49+
assertEquals(10, decimalType.getScale());
50+
}
51+
52+
@Test
53+
void decimalMaxPrecisionAndScaleDifferentFromDefaultTypeSystem() {
54+
RelDataTypeSystem defaultTypeSystem = RelDataTypeSystem.DEFAULT;
55+
int defaultMaxPrecision = defaultTypeSystem.getMaxPrecision(SqlTypeName.DECIMAL);
56+
int defaultMaxScale = defaultTypeSystem.getMaxScale(SqlTypeName.DECIMAL);
57+
58+
assertEquals(19, defaultMaxPrecision);
59+
assertEquals(19, defaultMaxScale);
60+
assertEquals(38, typeSystem.getMaxPrecision(SqlTypeName.DECIMAL));
61+
assertEquals(38, typeSystem.getMaxScale(SqlTypeName.DECIMAL));
62+
}
63+
}

0 commit comments

Comments
 (0)