6
6
import org .enso .table .data .column .storage .ColumnDoubleStorage ;
7
7
import org .enso .table .data .column .storage .ColumnLongStorage ;
8
8
import org .enso .table .data .column .storage .ColumnStorage ;
9
- import org .enso .table .data .column .storage .ColumnStorageFacade ;
10
- import org .enso .table .data .column .storage .PreciseTypeOptions ;
11
- import org .enso .table .data .column .storage .numeric .DoubleStorageFacade ;
12
9
import org .enso .table .data .column .storage .type .BigDecimalType ;
13
10
import org .enso .table .data .column .storage .type .BigIntegerType ;
14
11
import org .enso .table .data .column .storage .type .FloatType ;
15
12
import org .enso .table .data .column .storage .type .IntegerType ;
16
- import org .enso .table .data .column .storage .type .NullType ;
17
13
import org .enso .table .data .column .storage .type .StorageType ;
18
- import org .enso .table .data .table .Column ;
19
14
20
15
/**
21
16
* A binary coalescing operation for numeric types. This class is used to perform operations on two
26
21
*
27
22
* @param <T> the type of the elements in the column
28
23
*/
29
- public abstract class BinaryCoalescingOperationNumeric <T > implements BinaryOperation < T > {
24
+ public abstract class BinaryCoalescingOperationNumeric <T > extends BinaryOperationNumeric < T , T > {
30
25
/**
31
26
* An abstract class representing a numeric operation. This class defines the methods that must be
32
27
* implemented by any numeric operation.
@@ -103,112 +98,24 @@ public static BinaryOperation<?> create(
103
98
}
104
99
}
105
100
106
- private static StorageType <?> storageTypeForObject (Object right ) {
107
- if (right == null ) {
108
- return NullType .INSTANCE ;
109
- }
110
-
111
- if (right instanceof Column rightColumn ) {
112
- return rightColumn .getStorage ().getType ();
113
- }
114
-
115
- return StorageType .forBoxedItem (right , PreciseTypeOptions .DEFAULT );
116
- }
117
-
118
- protected final StorageType <T > validType ;
119
101
protected final NumericOperation operation ;
120
- protected final StorageType <?>[] validInputs ;
121
102
122
103
protected BinaryCoalescingOperationNumeric (
123
- StorageType <T > validType , NumericOperation operation , StorageType <?>... validInputs ) {
124
- this . validType = validType ;
104
+ NumericColumnAdapter <T > adapter , StorageType <T > returnType , NumericOperation operation ) {
105
+ super ( adapter , false , returnType ) ;
125
106
this .operation = operation ;
126
- this .validInputs = validInputs ;
127
107
}
128
108
129
109
@ Override
130
- public boolean canApplyMap (ColumnStorage <?> left , Object rightValue ) {
131
- var leftType = left .getType ();
132
- if (validType .isOfType (leftType )) {
133
- return true ;
134
- }
135
-
136
- for (var validInput : validInputs ) {
137
- if (validInput .isOfType (leftType )) {
138
- return true ;
139
- }
140
- }
141
-
142
- return false ;
143
- }
144
-
145
- @ Override
146
- public boolean canApplyZip (ColumnStorage <?> left , ColumnStorage <?> right ) {
147
- return canApplyMap (left , null ) && canApplyMap (right , null );
110
+ protected ColumnStorage <T > applyNullMap (
111
+ ColumnStorage <?> left , MapOperationProblemAggregator problemAggregator ) {
112
+ return adapter .asTypedStorage (left );
148
113
}
149
114
150
- @ Override
151
- public ColumnStorage <T > applyMap (
152
- ColumnStorage <?> left , Object rightValue , MapOperationProblemAggregator problemAggregator ) {
153
- if (rightValue == null ) {
154
- return asTypedStorage (left );
155
- }
156
-
157
- T rightValueTyped = validType .valueAsType (rightValue );
158
- if (rightValueTyped == null ) {
159
- throw new IllegalArgumentException (
160
- "Unsupported right value type " + rightValue .getClass () + "." );
161
- }
162
-
163
- return innerApplyMap (asTypedStorage (left ), rightValueTyped , problemAggregator );
164
- }
165
-
166
- @ Override
167
- public ColumnStorage <T > applyZip (
168
- ColumnStorage <?> left ,
169
- ColumnStorage <?> right ,
170
- MapOperationProblemAggregator problemAggregator ) {
171
- if (NullType .INSTANCE .isOfType (right .getType ())) {
172
- return validType .asTypedStorage (left );
173
- }
174
-
175
- return innerApplyZip (asTypedStorage (left ), asTypedStorage (right ), problemAggregator );
176
- }
177
-
178
- protected abstract ColumnStorage <T > asTypedStorage (ColumnStorage <?> storage );
179
-
180
- protected abstract ColumnStorage <T > innerApplyMap (
181
- ColumnStorage <T > left , T right , MapOperationProblemAggregator problemAggregator );
182
-
183
- protected abstract ColumnStorage <T > innerApplyZip (
184
- ColumnStorage <T > left ,
185
- ColumnStorage <T > right ,
186
- MapOperationProblemAggregator problemAggregator );
187
-
188
115
private static class BinaryCoalescingOperationDouble
189
116
extends BinaryCoalescingOperationNumeric <Double > {
190
117
public BinaryCoalescingOperationDouble (NumericOperation operation ) {
191
- super (
192
- FloatType .FLOAT_64 ,
193
- operation ,
194
- BigDecimalType .INSTANCE ,
195
- BigIntegerType .INSTANCE ,
196
- IntegerType .INT_64 );
197
- }
198
-
199
- @ Override
200
- protected ColumnDoubleStorage asTypedStorage (ColumnStorage <?> storage ) {
201
- return switch (storage .getType ()) {
202
- case FloatType floatType -> floatType .asTypedStorage (storage );
203
- case BigDecimalType bigDecimalType -> DoubleStorageFacade .forBigDecimal (
204
- bigDecimalType .asTypedStorage (storage ));
205
- case BigIntegerType bigIntegerType -> DoubleStorageFacade .forBigInteger (
206
- bigIntegerType .asTypedStorage (storage ));
207
- case IntegerType integerType -> DoubleStorageFacade .forLong (
208
- integerType .asTypedStorage (storage ));
209
- default -> throw new IllegalArgumentException (
210
- "Unsupported storage type: " + storage .getType ());
211
- };
118
+ super (DoubleColumnAdapter .INSTANCE , FloatType .FLOAT_64 , operation );
212
119
}
213
120
214
121
@ Override
@@ -245,107 +152,60 @@ protected ColumnStorage<Double> innerApplyZip(
245
152
}
246
153
});
247
154
}
248
- }
249
-
250
- private abstract static class BinaryCoalescingOperationBigNumber <T >
251
- extends BinaryCoalescingOperationNumeric <T > {
252
- protected BinaryCoalescingOperationBigNumber (
253
- StorageType <T > validType , NumericOperation operation , StorageType <?>... validInputs ) {
254
- super (validType , operation , validInputs );
255
- }
256
-
257
- @ Override
258
- protected ColumnStorage <T > innerApplyMap (
259
- ColumnStorage <T > left , T right , MapOperationProblemAggregator problemAggregator ) {
260
- return StorageIterators .mapOverStorage (
261
- left ,
262
- false ,
263
- validType .makeBuilder (left .getSize (), problemAggregator ),
264
- (index , value ) -> value == null ? right : doSingle (value , right , index ));
265
- }
266
155
267
156
@ Override
268
- protected ColumnStorage <T > innerApplyZip (
269
- ColumnStorage <T > left ,
270
- ColumnStorage <T > right ,
271
- MapOperationProblemAggregator problemAggregator ) {
272
- return StorageIterators .zipOverStorages (
273
- left ,
274
- right ,
275
- size -> validType .makeBuilder (size , problemAggregator ),
276
- false ,
277
- (index , x , y ) -> {
278
- if (x == null ) {
279
- return y ;
280
- } else {
281
- return y == null ? x : doSingle (x , y , index );
282
- }
283
- });
157
+ protected Double doSingle (
158
+ Double left , Double right , long index , MapOperationProblemAggregator problemAggregator ) {
159
+ if (left == null ) {
160
+ return right ;
161
+ } else if (right == null ) {
162
+ return left ;
163
+ } else {
164
+ return operation .doDouble (left , right , index );
165
+ }
284
166
}
285
-
286
- protected abstract T doSingle (T left , T right , long index );
287
167
}
288
168
289
169
private static class BinaryCoalescingOperationBigDecimal
290
- extends BinaryCoalescingOperationBigNumber <BigDecimal > {
170
+ extends BinaryCoalescingOperationNumeric <BigDecimal > {
291
171
public BinaryCoalescingOperationBigDecimal (NumericOperation operation ) {
292
- super (BigDecimalType .INSTANCE , operation , BigIntegerType .INSTANCE , IntegerType .INT_64 );
293
- }
294
-
295
- @ Override
296
- protected ColumnStorage <BigDecimal > asTypedStorage (ColumnStorage <?> storage ) {
297
- return switch (storage .getType ()) {
298
- case BigDecimalType bigDecimalType -> bigDecimalType .asTypedStorage (storage );
299
- case BigIntegerType bigIntegerType -> new ColumnStorageFacade <>(
300
- bigIntegerType .asTypedStorage (storage ), BigDecimal ::new );
301
- case IntegerType integerType -> new ColumnStorageFacade <>(
302
- integerType .asTypedStorage (storage ), BigDecimal ::valueOf );
303
- default -> throw new IllegalArgumentException (
304
- "Unsupported storage type: " + storage .getType ());
305
- };
172
+ super (BigDecimalColumnAdapter .INSTANCE , BigDecimalType .INSTANCE , operation );
306
173
}
307
174
308
175
@ Override
309
- protected BigDecimal doSingle (BigDecimal left , BigDecimal right , long index ) {
310
- return operation .doBigDecimal (left , right , index );
176
+ protected BigDecimal doSingle (
177
+ BigDecimal left ,
178
+ BigDecimal right ,
179
+ long index ,
180
+ MapOperationProblemAggregator problemAggregator ) {
181
+ return left == null
182
+ ? right
183
+ : (right == null ? left : operation .doBigDecimal (left , right , index ));
311
184
}
312
185
}
313
186
314
187
private static class BinaryCoalescingOperationBigInteger
315
- extends BinaryCoalescingOperationBigNumber <BigInteger > {
188
+ extends BinaryCoalescingOperationNumeric <BigInteger > {
316
189
public BinaryCoalescingOperationBigInteger (NumericOperation operation ) {
317
- super (BigIntegerType .INSTANCE , operation , IntegerType .INT_64 );
318
- }
319
-
320
- @ Override
321
- protected ColumnStorage <BigInteger > asTypedStorage (ColumnStorage <?> storage ) {
322
- return switch (storage .getType ()) {
323
- case BigIntegerType bigIntegerType -> bigIntegerType .asTypedStorage (storage );
324
- case IntegerType integerType -> new ColumnStorageFacade <>(
325
- integerType .asTypedStorage (storage ), BigInteger ::valueOf );
326
- default -> throw new IllegalArgumentException (
327
- "Unsupported storage type: " + storage .getType ());
328
- };
190
+ super (BigIntegerColumnAdapter .INSTANCE , BigIntegerType .INSTANCE , operation );
329
191
}
330
192
331
193
@ Override
332
- protected BigInteger doSingle (BigInteger left , BigInteger right , long index ) {
333
- return operation .doBigInteger (left , right , index );
194
+ protected BigInteger doSingle (
195
+ BigInteger left ,
196
+ BigInteger right ,
197
+ long index ,
198
+ MapOperationProblemAggregator problemAggregator ) {
199
+ return left == null
200
+ ? right
201
+ : (right == null ? left : operation .doBigInteger (left , right , index ));
334
202
}
335
203
}
336
204
337
205
private static class BinaryCoalescingOperationLong
338
206
extends BinaryCoalescingOperationNumeric <Long > {
339
207
public BinaryCoalescingOperationLong (NumericOperation operation ) {
340
- super (IntegerType .INT_64 , operation );
341
- }
342
-
343
- @ Override
344
- protected ColumnLongStorage asTypedStorage (ColumnStorage <?> storage ) {
345
- if (storage .getType () instanceof IntegerType integerType ) {
346
- return integerType .asTypedStorage (storage );
347
- }
348
- throw new IllegalArgumentException ("Unsupported storage type: " + storage .getType ());
208
+ super (LongColumnAdapter .INSTANCE , IntegerType .INT_64 , operation );
349
209
}
350
210
351
211
@ Override
@@ -368,7 +228,7 @@ protected ColumnStorage<Long> innerApplyZip(
368
228
return StorageIterators .zipOverLongStorages (
369
229
(ColumnLongStorage ) left ,
370
230
(ColumnLongStorage ) right ,
371
- s -> validType .makeBuilder (s , problemAggregator ),
231
+ s -> IntegerType . INT_64 .makeBuilder (s , problemAggregator ),
372
232
true ,
373
233
(index , value1 , isNothing1 , value2 , isNothing2 ) -> {
374
234
if (isNothing1 && isNothing2 ) {
@@ -382,5 +242,17 @@ protected ColumnStorage<Long> innerApplyZip(
382
242
}
383
243
});
384
244
}
245
+
246
+ @ Override
247
+ protected Long doSingle (
248
+ Long left , Long right , long index , MapOperationProblemAggregator problemAggregator ) {
249
+ if (left == null ) {
250
+ return right ;
251
+ } else if (right == null ) {
252
+ return left ;
253
+ } else {
254
+ return operation .doLong (left , right , index );
255
+ }
256
+ }
385
257
}
386
258
}
0 commit comments