Skip to content

Commit dd725c5

Browse files
committed
Ensure inferPreciseType returns a local StorageType.
Add a temporary makeLocalStorage method until JavaProblems is rewritten.
1 parent 78ed9fc commit dd725c5

File tree

4 files changed

+37
-57
lines changed

4 files changed

+37
-57
lines changed

distribution/lib/Standard/Table/0.0.0-dev/src/Internal/In_Memory_Column_Implementation.enso

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ type In_Memory_Column_Implementation
632632
Vector.from_polyglot_array column_as_list . map java_to_enso
633633

634634
value_type (this_column : Column & In_Memory_Column) =
635-
storage_type = this_column.java_column.getType
635+
storage_type = CastOperation.makeLocalType this_column.java_column.getType
636636
Storage.to_value_type storage_type
637637

638638
inferred_precise_value_type (this_column : Column & In_Memory_Column) =

distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Java_Problems.enso

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from project.Internal.Storage import java_to_enso
1111

1212
polyglot java import org.enso.table.data.column.builder.LossOfBigDecimalPrecision
1313
polyglot java import org.enso.table.data.column.builder.LossOfIntegerPrecision
14+
polyglot java import org.enso.table.data.column.operation.cast.CastOperation
1415
polyglot java import org.enso.table.data.column.operation.cast.ConversionFailure
1516
polyglot java import org.enso.table.data.column.operation.cast.ConversionFailureType
1617
polyglot java import org.enso.table.data.table.problems.ArithmeticError
@@ -56,14 +57,16 @@ translate_problem p = case p.problemType of
5657
"LossOfBigDecimalPrecision" ->
5758
Loss_Of_Decimal_Precision.Warning p.getAffectedRowsCount (java_to_enso p.getExampleValue) p.getExampleValueConverted
5859
"ArithmeticOverflow" ->
59-
target_type = Storage.to_value_type p.targetType
60+
storage_type = CastOperation.makeLocalType p.target_type
61+
target_type = Storage.to_value_type storage_type
6062
example_operands = case p.exampleOperands of
6163
Nothing -> Nothing
6264
array -> Vector.from_polyglot_array array
6365
Arithmetic_Overflow.Warning target_type p.affectedRowCount example_operands
6466
"ConversionFailure" ->
6567
examples = Vector.from_polyglot_array p.examples
66-
target_type = Storage.to_value_type p.targetType
68+
storage_type = CastOperation.makeLocalType p.target_type
69+
target_type = Storage.to_value_type storage_type
6770
related_column = p.relatedColumn
6871
affected_rows_count = p.affectedRowCount
6972
constructor = if p.errorType == ConversionFailureType.NUMBER_OUT_OF_RANGE then Conversion_Failure.Out_Of_Range else

distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Storage.enso

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ polyglot java import org.enso.table.problems.ProblemAggregator
3030
---
3131
Gets the value type represented by this Java Storage.
3232
to_value_type : StorageType -> Value_Type
33-
to_value_type storage_type = case (StorageType.makeLocal storage_type) of
33+
to_value_type storage_type = case storage_type of
3434
i : IntegerType -> case i.size of
3535
8 -> Value_Type.Byte
3636
b -> Value_Type.Integer (Bits.from_integer b)

std-bits/table/src/main/java/org/enso/table/data/column/operation/cast/CastOperation.java

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,28 @@ public static Column apply(
4040
/** Construct a StorageConverter for the given target type. */
4141
private static StorageConverter<?> fromStorageType(StorageType<?> storageType) {
4242
return switch (StorageType.makeLocal(storageType)) {
43-
case AnyObjectType anyObjectType -> new ToMixedStorageConverter();
44-
case BooleanType booleanType -> new ToBooleanStorageConverter();
45-
case DateType dateType -> new ToDateStorageConverter();
46-
case DateTimeType dateTimeType -> new ToDateTimeStorageConverter();
43+
case AnyObjectType _ -> new ToMixedStorageConverter();
44+
case BooleanType _ -> new ToBooleanStorageConverter();
45+
case DateType _ -> new ToDateStorageConverter();
46+
case DateTimeType _ -> new ToDateTimeStorageConverter();
4747
case FloatType floatType -> new ToFloatStorageConverter(floatType);
4848
case IntegerType integerType -> new ToIntegerStorageConverter(integerType);
4949
case TextType textType -> new ToTextStorageConverter(textType);
50-
case TimeOfDayType timeOfDayType -> new ToTimeOfDayStorageConverter();
51-
case BigIntegerType bigIntegerType -> new ToBigIntegerConverter();
52-
case BigDecimalType bigDecimalType -> new ToBigDecimalConverter();
53-
case NullType nullType -> throw new IllegalArgumentException("Cannot cast to Null type.");
50+
case TimeOfDayType _ -> new ToTimeOfDayStorageConverter();
51+
case BigIntegerType _ -> new ToBigIntegerConverter();
52+
case BigDecimalType _ -> new ToBigDecimalConverter();
53+
case NullType _ -> throw new IllegalArgumentException("Cannot cast to Null type.");
5454
default ->
5555
throw new IllegalStateException(
5656
"Unsupported type: " + storageType + " - this is a bug in the Table library.");
5757
};
5858
}
5959

60+
/** Helper method until Java Problems code in Enso is re-visited. */
61+
public static StorageType<?> makeLocalType(StorageType<?> type) {
62+
return StorageType.makeLocal(type);
63+
}
64+
6065
public static StorageType<?> inferPreciseType(Column column) {
6166
return inferPreciseType(column, PreciseTypeOptions.DEFAULT);
6267
}
@@ -65,13 +70,14 @@ public static StorageType<?> inferPreciseType(Column column, PreciseTypeOptions
6570
var columnStorage = column.getStorage();
6671
var storage = ColumnStorageWithInferredStorage.resolveStorage(columnStorage);
6772

68-
return switch (storage.getType()) {
69-
case TextType textType -> inferTextType(storage, options);
70-
case IntegerType integerType -> inferIntegerType(storage, options);
71-
case FloatType floatType -> inferFloatType(storage, options);
72-
case BigIntegerType bigIntegerType -> inferBigIntegerType(storage, options);
73-
case BigDecimalType bigDecimalType -> inferBigDecimalType(storage, options);
74-
default -> storage.getType();
73+
var storageType = StorageType.makeLocal(storage.getType());
74+
return switch (storageType) {
75+
case TextType textType -> inferTextType(storage, textType, options);
76+
case IntegerType integerType -> inferIntegerType(storage, integerType, options);
77+
case FloatType floatType -> inferFloatType(storage, floatType, options);
78+
case BigIntegerType bigIntegerType -> inferBigIntegerType(storage, bigIntegerType, options);
79+
case BigDecimalType bigDecimalType -> inferBigDecimalType(storage, bigDecimalType, options);
80+
default -> storageType;
7581
};
7682
}
7783

@@ -104,17 +110,8 @@ public long getMinLength() {
104110
}
105111

106112
private static StorageType<?> inferTextType(
107-
ColumnStorage<?> columnStorage, PreciseTypeOptions options) {
108-
if (!options.shrinkText()) {
109-
return columnStorage.getType();
110-
}
111-
112-
if (!(columnStorage.getType() instanceof TextType textType)) {
113-
throw new IllegalArgumentException(
114-
"Cannot infer text type from non-text storage: " + columnStorage.getType());
115-
}
116-
117-
if (textType.fixedLength()) {
113+
ColumnStorage<?> columnStorage, TextType textType, PreciseTypeOptions options) {
114+
if (!options.shrinkText() || textType.fixedLength()) {
118115
return textType;
119116
}
120117

@@ -180,14 +177,9 @@ public IntegerType resolveType() {
180177
}
181178

182179
private static StorageType<?> inferIntegerType(
183-
ColumnStorage<?> columnStorage, PreciseTypeOptions options) {
180+
ColumnStorage<?> columnStorage, IntegerType integerType, PreciseTypeOptions options) {
184181
if (!options.shrinkIntegers()) {
185-
return columnStorage.getType();
186-
}
187-
188-
if (!(columnStorage.getType() instanceof IntegerType integerType)) {
189-
throw new IllegalArgumentException(
190-
"Cannot infer integer type from non-integer storage: " + columnStorage.getType());
182+
return integerType;
191183
}
192184

193185
if (integerType.size() <= 16) {
@@ -206,12 +198,7 @@ private static StorageType<?> inferIntegerType(
206198
}
207199

208200
private static StorageType<?> inferBigIntegerType(
209-
ColumnStorage<?> columnStorage, PreciseTypeOptions options) {
210-
if (!(columnStorage.getType() instanceof BigIntegerType bigIntegerType)) {
211-
throw new IllegalArgumentException(
212-
"Cannot infer integer type from non-integer storage: " + columnStorage.getType());
213-
}
214-
201+
ColumnStorage<?> columnStorage, BigIntegerType bigIntegerType, PreciseTypeOptions options) {
215202
// Build the min and max of values in the column.
216203
var accumulator = new LongAccumulator();
217204
var endedEarly =
@@ -239,14 +226,9 @@ private static StorageType<?> inferBigIntegerType(
239226
}
240227

241228
private static StorageType<?> inferFloatType(
242-
ColumnStorage<?> columnStorage, PreciseTypeOptions options) {
229+
ColumnStorage<?> columnStorage, FloatType floatType, PreciseTypeOptions options) {
243230
if (!options.wholeFloatsBecomeIntegers()) {
244-
return columnStorage.getType();
245-
}
246-
247-
if (!(columnStorage.getType() instanceof FloatType floatType)) {
248-
throw new IllegalArgumentException(
249-
"Cannot infer float type from non-integer storage: " + columnStorage.getType());
231+
return floatType;
250232
}
251233

252234
// Build the min and max of values in the column.
@@ -301,14 +283,9 @@ public boolean getOverflowed() {
301283
}
302284

303285
private static StorageType<?> inferBigDecimalType(
304-
ColumnStorage<?> columnStorage, PreciseTypeOptions options) {
286+
ColumnStorage<?> columnStorage, BigDecimalType bigDecimalType, PreciseTypeOptions options) {
305287
if (!options.wholeFloatsBecomeIntegers()) {
306-
return columnStorage.getType();
307-
}
308-
309-
if (!(columnStorage.getType() instanceof BigDecimalType bigDecimalType)) {
310-
throw new IllegalArgumentException(
311-
"Cannot infer decimal type from non-decimal storage: " + columnStorage.getType());
288+
return bigDecimalType;
312289
}
313290

314291
// Build the min and max of values in the column.

0 commit comments

Comments
 (0)