Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache short decimal type instances #14523

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 @@ -41,7 +41,7 @@ public static DecimalType createDecimalType(int precision, int scale)
}

if (precision <= MAX_SHORT_PRECISION) {
return new ShortDecimalType(precision, scale);
return ShortDecimalType.getInstance(precision, scale);
}
return new LongDecimalType(precision, scale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static io.trino.spi.function.OperatorType.LESS_THAN;
import static io.trino.spi.function.OperatorType.LESS_THAN_OR_EQUAL;
import static io.trino.spi.function.OperatorType.XX_HASH_64;
import static io.trino.spi.type.Decimals.MAX_SHORT_PRECISION;
import static io.trino.spi.type.TypeOperatorDeclaration.extractOperatorDeclaration;
import static java.lang.invoke.MethodHandles.lookup;

Expand All @@ -42,7 +43,23 @@ final class ShortDecimalType
{
private static final TypeOperatorDeclaration TYPE_OPERATOR_DECLARATION = extractOperatorDeclaration(ShortDecimalType.class, lookup(), long.class);

ShortDecimalType(int precision, int scale)
private static final ShortDecimalType[][] INSTANCES;

static {
INSTANCES = new ShortDecimalType[MAX_SHORT_PRECISION][MAX_SHORT_PRECISION + 1];
for (int precision = 1; precision <= MAX_SHORT_PRECISION; precision++) {
for (int scale = 0; scale <= precision; scale++) {
INSTANCES[precision - 1][scale] = new ShortDecimalType(precision, scale);
}
}
}

static ShortDecimalType getInstance(int precision, int scale)
{
return INSTANCES[precision - 1][scale];
}

private ShortDecimalType(int precision, int scale)
{
super(precision, scale, long.class);
checkArgument(0 < precision && precision <= Decimals.MAX_SHORT_PRECISION, "Invalid precision: %s", precision);
Expand Down