Skip to content

Commit 511cf97

Browse files
committed
safe cast
1 parent 3fd3c1c commit 511cf97

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/main/java/org/apache/sysds/runtime/frame/data/columns/StringArray.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ public double getAsNaNDouble(int i) {
607607

608608
private static double getAsDouble(String s) {
609609
try {
610-
611610
return DoubleArray.parseDouble(s);
612611
}
613612
catch(Exception e) {

src/main/java/org/apache/sysds/runtime/frame/data/lib/MatrixBlockFromFrame.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,19 @@ else if(ret.getNumRows() != m || ret.getNumColumns() != n || ret.isInSparseForma
9494
}
9595

9696
private static long convert(FrameBlock frame, MatrixBlock mb, int n, int rl, int ru) {
97-
if(mb.getDenseBlock().isContiguous())
98-
return convertContiguous(frame, mb, n, rl, ru);
99-
else
100-
return convertGeneric(frame, mb, n, rl, ru);
97+
try {
98+
99+
if(mb.getDenseBlock().isContiguous())
100+
return convertContiguous(frame, mb, n, rl, ru);
101+
else
102+
return convertGeneric(frame, mb, n, rl, ru);
103+
}
104+
catch(NumberFormatException e) {
105+
LOG.error(
106+
"Failed to convert to Matrix because of number format errors, falling back to NaN on incompatible cells",
107+
e);
108+
return convertSafeCast(frame, mb, n, rl, ru);
109+
}
101110
}
102111

103112
private static long convertParallel(FrameBlock frame, MatrixBlock mb, int m, int n, int k) throws Exception {
@@ -169,4 +178,39 @@ private static long convertBlockGeneric(final FrameBlock frame, long lnnz, final
169178
}
170179
return lnnz;
171180
}
181+
182+
183+
184+
private static long convertSafeCast(final FrameBlock frame, final MatrixBlock mb, final int n, final int rl, final int ru){
185+
final DenseBlock c = mb.getDenseBlock();
186+
long lnnz = 0;
187+
for(int bi = rl; bi < ru; bi += blocksizeIJ) {
188+
for(int bj = 0; bj < n; bj += blocksizeIJ) {
189+
int bimin = Math.min(bi + blocksizeIJ, ru);
190+
int bjmin = Math.min(bj + blocksizeIJ, n);
191+
lnnz = convertBlockSafeCast(frame, lnnz, c, bi, bj, bimin, bjmin);
192+
}
193+
}
194+
return lnnz;
195+
}
196+
197+
198+
private static long convertBlockSafeCast(final FrameBlock frame, long lnnz, final DenseBlock c, final int rl,
199+
final int cl, final int ru, final int cu) {
200+
for(int i = rl; i < ru; i++) {
201+
final double[] cvals = c.values(i);
202+
final int cpos = c.pos(i);
203+
for(int j = cl; j < cu; j++){
204+
try{
205+
lnnz += (cvals[cpos + j] = frame.getDoubleNaN(i, j)) != 0 ? 1 : 0;
206+
}
207+
catch(NumberFormatException e){
208+
lnnz += 1;
209+
cvals[cpos + j] = Double.NaN;
210+
}
211+
}
212+
}
213+
return lnnz;
214+
}
215+
172216
}

0 commit comments

Comments
 (0)