Skip to content

Commit

Permalink
[CALCITE-6566] Code Review about the javadoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
NobiGo committed Sep 21, 2024
1 parent 85e8373 commit c4eff23
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private ImmutableList<MetaTypeInfo> getAllDefaultType() {
false,
false,
typeSystem.isAutoincrement(sqlTypeName),
(short) sqlTypeName.getMinScale(),
(short) typeSystem.getMinScale(sqlTypeName),
(short) typeSystem.getMaxScale(sqlTypeName),
typeSystem.getNumTypeRadix(sqlTypeName)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ protected DelegatingTypeSystem(RelDataTypeSystem typeSystem) {
return typeSystem.getMaxScale(typeName);
}

@Override public int getMinScale(SqlTypeName typeName) {
return typeSystem.getMinScale(typeName);
}

@Override public int getDefaultPrecision(SqlTypeName typeName) {
return typeSystem.getDefaultPrecision(typeName);
}

@Override public int getDefaultScale(SqlTypeName typeName) {
return typeSystem.getDefaultScale(typeName);
}

@Override public int getMaxPrecision(SqlTypeName typeName) {
return typeSystem.getMaxPrecision(typeName);
}

@Override public int getMinPrecision(SqlTypeName typeName) {
return typeSystem.getMinPrecision(typeName);
}

@Override public int getMaxNumericScale() {
return typeSystem.getMaxNumericScale();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,24 @@ public interface RelDataTypeSystem {
/** Default type system. */
RelDataTypeSystem DEFAULT = new RelDataTypeSystemImpl() { };

/** Returns the maximum scale of a given type. */
/**
* Returns the maximum scale allowed for this type, or
* {@link RelDataType#SCALE_NOT_SPECIFIED}
* if scale is not applicable for this type.
*
* @return Maximum allowed scale
*/
int getMaxScale(SqlTypeName typeName);

/**
* Returns the minimum scale allowed for this type, or
* {@link RelDataType#SCALE_NOT_SPECIFIED}
* if scale are not applicable for this type.
*
* @return Minimum allowed scale
*/
int getMinScale(SqlTypeName typeName);

/**
* Returns default precision for this type if supported, otherwise
* {@link RelDataType#PRECISION_NOT_SPECIFIED}
Expand All @@ -49,6 +64,15 @@ public interface RelDataTypeSystem {
*/
int getDefaultPrecision(SqlTypeName typeName);

/**
* Returns default scale for this type if supported, otherwise
* {@link RelDataType#SCALE_NOT_SPECIFIED}
* if scale is either unsupported or must be specified explicitly.
*
* @return Default scale
*/
int getDefaultScale(SqlTypeName typeName);

/**
* Returns the maximum precision (or length) allowed for this type, or
* {@link RelDataType#PRECISION_NOT_SPECIFIED}
Expand All @@ -58,20 +82,24 @@ public interface RelDataTypeSystem {
*/
int getMaxPrecision(SqlTypeName typeName);

/** Returns the maximum scale of a NUMERIC or DECIMAL type. */
/**
* Returns the minimum precision (or length) allowed for this type, or
* {@link RelDataType#PRECISION_NOT_SPECIFIED}
* if precision/length are not applicable for this type.
*
* @return Minimum allowed precision
*/
int getMinPrecision(SqlTypeName typeName);

/** Returns the maximum scale of a NUMERIC or DECIMAL type. And the default value is 19. */
int getMaxNumericScale();

/** Returns the maximum precision of a NUMERIC or DECIMAL type. */
/** Returns the maximum precision of a NUMERIC or DECIMAL type. And the default value is 19. */
int getMaxNumericPrecision();

/** Returns the minimum scale of a NUMERIC or DECIMAL type. */
/** Returns the minimum scale of a NUMERIC or DECIMAL type. And the default value is 0. */
int getMinNumericScale();

/** Returns whether the scale of a NUMERIC or DECIMAL type can be negative. */
default boolean supportsNegativeScale() {
return getMinNumericScale() < 0;
}

/** Returns the rounding behavior for numerical operations capable of discarding precision. */
RoundingMode roundingMode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

import java.math.RoundingMode;

import static org.apache.calcite.sql.type.SqlTypeName.DEFAULT_INTERVAL_FRACTIONAL_SECOND_PRECISION;
import static org.apache.calcite.sql.type.SqlTypeName.MIN_INTERVAL_FRACTIONAL_SECOND_PRECISION;
import static org.apache.calcite.sql.type.SqlTypeName.MIN_INTERVAL_START_PRECISION;

/** Default implementation of
* {@link org.apache.calcite.rel.type.RelDataTypeSystem},
* providing parameters from the SQL standard.
Expand Down Expand Up @@ -63,6 +67,36 @@ public abstract class RelDataTypeSystemImpl implements RelDataTypeSystem {
}
}

/**
* Returns the minimum scale (or fractional second precision in the case of
* intervals) allowed for this type, or {@link RelDataType#SCALE_NOT_SPECIFIED}
* if precision/length are not applicable for this type.
*
* @return Minimum allowed scale
*/
@Override public int getMinScale(SqlTypeName typeName) {
switch (typeName) {
case DECIMAL:
return getMinNumericScale();
case INTERVAL_YEAR:
case INTERVAL_YEAR_MONTH:
case INTERVAL_MONTH:
case INTERVAL_DAY:
case INTERVAL_DAY_HOUR:
case INTERVAL_DAY_MINUTE:
case INTERVAL_DAY_SECOND:
case INTERVAL_HOUR:
case INTERVAL_HOUR_MINUTE:
case INTERVAL_HOUR_SECOND:
case INTERVAL_MINUTE:
case INTERVAL_MINUTE_SECOND:
case INTERVAL_SECOND:
return MIN_INTERVAL_FRACTIONAL_SECOND_PRECISION;
default:
return RelDataType.SCALE_NOT_SPECIFIED;
}
}

@Override public int getDefaultPrecision(SqlTypeName typeName) {
// Following BasicSqlType precision as the default
switch (typeName) {
Expand Down Expand Up @@ -120,6 +154,31 @@ public abstract class RelDataTypeSystemImpl implements RelDataTypeSystem {
}
}

/** Returns the default scale for this type if supported, otherwise {@link RelDataType#SCALE_NOT_SPECIFIED}
* if scale is either unsupported or must be specified explicitly. */
@Override public int getDefaultScale(SqlTypeName typeName) {
switch (typeName) {
case DECIMAL:
return 0;
case INTERVAL_YEAR:
case INTERVAL_YEAR_MONTH:
case INTERVAL_MONTH:
case INTERVAL_DAY:
case INTERVAL_DAY_HOUR:
case INTERVAL_DAY_MINUTE:
case INTERVAL_DAY_SECOND:
case INTERVAL_HOUR:
case INTERVAL_HOUR_MINUTE:
case INTERVAL_HOUR_SECOND:
case INTERVAL_MINUTE:
case INTERVAL_MINUTE_SECOND:
case INTERVAL_SECOND:
return DEFAULT_INTERVAL_FRACTIONAL_SECOND_PRECISION;
default:
return RelDataType.SCALE_NOT_SPECIFIED;
}
}

@Override public int getMaxPrecision(SqlTypeName typeName) {
switch (typeName) {
case DECIMAL:
Expand Down Expand Up @@ -156,6 +215,46 @@ public abstract class RelDataTypeSystemImpl implements RelDataTypeSystem {
}
}

/**
* Returns the minimum precision (or length) allowed for this type,
* or {@link RelDataType#PRECISION_NOT_SPECIFIED}
* if precision/length are not applicable for this type.
*
* @return Minimum allowed precision
*/
@Override public int getMinPrecision(SqlTypeName typeName) {
switch (typeName) {
case DECIMAL:
case VARCHAR:
case CHAR:
case VARBINARY:
case BINARY:
case TIME:
case TIME_WITH_LOCAL_TIME_ZONE:
case TIME_TZ:
case TIMESTAMP:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
case TIMESTAMP_TZ:
return 1;
case INTERVAL_YEAR:
case INTERVAL_YEAR_MONTH:
case INTERVAL_MONTH:
case INTERVAL_DAY:
case INTERVAL_DAY_HOUR:
case INTERVAL_DAY_MINUTE:
case INTERVAL_DAY_SECOND:
case INTERVAL_HOUR:
case INTERVAL_HOUR_MINUTE:
case INTERVAL_HOUR_SECOND:
case INTERVAL_MINUTE:
case INTERVAL_MINUTE_SECOND:
case INTERVAL_SECOND:
return MIN_INTERVAL_START_PRECISION;
default:
return RelDataType.PRECISION_NOT_SPECIFIED;
}
}

@Override public int getMaxNumericScale() {
return 19;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public static int combineStartPrecisionPreservingDefault(

public int getFractionalSecondPrecision(RelDataTypeSystem typeSystem) {
if (fractionalSecondPrecision == RelDataType.PRECISION_NOT_SPECIFIED) {
return typeName().getDefaultScale();
return typeSystem.getDefaultScale(typeName());
} else {
return fractionalSecondPrecision;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public SqlTypeFactoryImpl(RelDataTypeSystem typeSystem) {
SqlTypeName typeName,
int precision) {
if (typeName.allowsScale()) {
return createSqlType(typeName, precision, typeName.getDefaultScale());
return createSqlType(typeName, precision, typeSystem.getDefaultScale(typeName));
}
final int maxPrecision = typeSystem.getMaxPrecision(typeName);
if (maxPrecision >= 0 && precision > maxPrecision) {
Expand Down Expand Up @@ -90,14 +90,14 @@ public SqlTypeFactoryImpl(RelDataTypeSystem typeSystem) {
if (maxPrecision >= 0 && precision > maxPrecision) {
precision = maxPrecision;
}
if (typeName == SqlTypeName.DECIMAL) {
if (precision != RelDataType.PRECISION_NOT_SPECIFIED && precision <= 0) {
throw RESOURCE.invalidPrecisionForDecimalType(precision, maxPrecision).ex();
}
if (scale != RelDataType.SCALE_NOT_SPECIFIED && scale < typeSystem.getMinNumericScale()) {
throw RESOURCE.invalidScaleForDecimalType(scale,
typeSystem.getMinNumericScale(), typeSystem.getMaxNumericScale()).ex();
}
if (precision != RelDataType.PRECISION_NOT_SPECIFIED
&& precision < typeSystem.getMinPrecision(typeName)) {
throw RESOURCE.invalidPrecisionForDecimalType(precision, maxPrecision).ex();
}
if (scale != RelDataType.SCALE_NOT_SPECIFIED
&& scale < typeSystem.getMinScale(typeName)) {
throw RESOURCE.invalidScaleForDecimalType(scale,
typeSystem.getMinScale(typeName), typeSystem.getMaxNumericScale()).ex();
}
RelDataType newType =
new BasicSqlType(typeSystem, typeName, precision, scale);
Expand Down
19 changes: 17 additions & 2 deletions core/src/main/java/org/apache/calcite/sql/type/SqlTypeName.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,13 @@ private static List<SqlTypeName> combine(
}

/** Returns the default scale for this type if supported, otherwise -1 if
* scale is either unsupported or must be specified explicitly. */
* scale is either unsupported or must be specified explicitly.
*
* @deprecated
* Use {@link org.apache.calcite.rel.type.RelDataTypeSystem#getDefaultScale(SqlTypeName)}
* but return Integer.MIN_VALUE if scale is unsupported.
*/
@Deprecated
public int getDefaultScale() {
switch (this) {
case DECIMAL:
Expand Down Expand Up @@ -781,7 +787,11 @@ public int getDefaultScale() {
* precision/length are not applicable for this type.
*
* @return Minimum allowed precision
*
* @deprecated
* Use {@link org.apache.calcite.rel.type.RelDataTypeSystem#getMinPrecision(SqlTypeName)}.
*/
@Deprecated
public int getMinPrecision() {
switch (this) {
case DECIMAL:
Expand Down Expand Up @@ -817,11 +827,16 @@ public int getMinPrecision() {

/**
* Returns the minimum scale (or fractional second precision in the case of
* intervals) allowed for this type, or -1 if precision/length are not
* intervals) allowed for this type, or -1 if scale are not
* applicable for this type.
*
* @return Minimum allowed scale
*
* @deprecated
* Use {@link org.apache.calcite.rel.type.RelDataTypeSystem#getMinScale(SqlTypeName)}
* but return Integer.MIN_VALUE if scale is unsupported.
*/
@Deprecated
public int getMinScale() {
switch (this) {
// TODO: Minimum numeric scale for decimal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3526,8 +3526,8 @@ private void validateLiteralAsDouble(SqlLiteral literal) {
final int fracPrecision =
qualifier.getFractionalSecondPrecision(typeSystem);
final int maxPrecision = typeSystem.getMaxPrecision(qualifier.typeName());
final int minPrecision = qualifier.typeName().getMinPrecision();
final int minScale = qualifier.typeName().getMinScale();
final int minPrecision = typeSystem.getMinPrecision(qualifier.typeName());
final int minScale = typeSystem.getMinScale(qualifier.typeName());
final int maxScale = typeSystem.getMaxScale(qualifier.typeName());
if (startPrecision < minPrecision || startPrecision > maxPrecision) {
startPrecisionOutOfRange = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2308,12 +2308,12 @@ void testLikeAndSimilarFails() {
fixture().factory.getTypeFactory().getTypeSystem();
final RelDataTypeSystem defTypeSystem = RelDataTypeSystem.DEFAULT;
for (SqlTypeName typeName : SqlTypeName.INTERVAL_TYPES) {
assertThat(typeName.getMinPrecision(), is(1));
assertThat(typeSystem.getMinPrecision(typeName), is(1));
assertThat(typeSystem.getMaxPrecision(typeName), is(10));
assertThat(typeSystem.getDefaultPrecision(typeName), is(2));
assertThat(typeName.getMinScale(), is(0));
assertThat(typeSystem.getMinScale(typeName), is(0));
assertThat(typeSystem.getMaxScale(typeName), is(9));
assertThat(typeName.getDefaultScale(), is(6));
assertThat(typeSystem.getDefaultScale(typeName), is(6));
}

final SqlValidatorFixture f = fixture();
Expand Down

0 comments on commit c4eff23

Please sign in to comment.