Skip to content

Commit d454452

Browse files
committed
Fix
1 parent 7d0faa6 commit d454452

File tree

1 file changed

+10
-131
lines changed

1 file changed

+10
-131
lines changed

common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java

Lines changed: 10 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,152 +1295,31 @@ public boolean toByte(IntWrapper intWrapper) {
12951295
}
12961296

12971297
/**
1298-
* Parses this UTF8String(trimmed if needed) to long.
1298+
* Parses UTF8String(trimmed if needed) to long. This method is used when ANSI is enabled.
12991299
*
1300-
* This method is almost similar to `toLong` defined above. It is used for parsing the UTF8String
1301-
* when ANSI mode is enabled.
13021300
* @return If string contains valid numeric value then it returns the long value otherwise a
13031301
* NumberFormatException is thrown.
13041302
*/
1305-
13061303
public long toLongExact() {
1307-
int offset = 0;
1308-
while (offset < this.numBytes && getByte(offset) <= ' ') offset++;
1309-
if (offset == this.numBytes) {
1310-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1311-
}
1312-
1313-
int end = this.numBytes - 1;
1314-
while (end > offset && getByte(end) <= ' ') end--;
1315-
1316-
byte b = getByte(offset);
1317-
final boolean negative = b == '-';
1318-
if (negative || b == '+') {
1319-
if (end - offset == 0) {
1320-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1321-
}
1322-
offset++;
1304+
LongWrapper result = new LongWrapper();
1305+
if (toLong(result)) {
1306+
return result.value;
13231307
}
1324-
1325-
final byte separator = '.';
1326-
final int radix = 10;
1327-
final long stopValue = Long.MIN_VALUE / radix;
1328-
long result = 0;
1329-
1330-
while (offset <= end) {
1331-
b = getByte(offset);
1332-
offset++;
1333-
if (b == separator) {
1334-
break;
1335-
}
1336-
1337-
int digit;
1338-
if (b >= '0' && b <= '9') {
1339-
digit = b - '0';
1340-
} else {
1341-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1342-
}
1343-
1344-
if (result < stopValue) {
1345-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1346-
}
1347-
1348-
result = result * radix - digit;
1349-
if (result > 0) {
1350-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1351-
}
1352-
}
1353-
1354-
while (offset <= end) {
1355-
byte currentByte = getByte(offset);
1356-
if (currentByte < '0' || currentByte > '9') {
1357-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1358-
}
1359-
offset++;
1360-
}
1361-
1362-
if (!negative) {
1363-
result = -result;
1364-
if (result < 0) {
1365-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1366-
}
1367-
}
1368-
1369-
return result;
1308+
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
13701309
}
13711310

13721311
/**
1373-
* Parses this UTF8String(trimmed if needed) to Int.
1312+
* Parses UTF8String(trimmed if needed) to int. This method is used when ANSI is enabled.
13741313
*
1375-
* This method is almost similar to `toInt` defined above. It is used for parsing the UTF8String
1376-
* when ANSI mode is enabled.
13771314
* @return If string contains valid numeric value then it returns the int value otherwise a
13781315
* NumberFormatException is thrown.
13791316
*/
1380-
13811317
public int toIntExact() {
1382-
int offset = 0;
1383-
while (offset < this.numBytes && getByte(offset) <= ' ') offset++;
1384-
if (offset == this.numBytes) {
1385-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1386-
}
1387-
1388-
int end = this.numBytes - 1;
1389-
while (end > offset && getByte(end) <= ' ') end--;
1390-
1391-
byte b = getByte(offset);
1392-
final boolean negative = b == '-';
1393-
if (negative || b == '+') {
1394-
if (end - offset == 0) {
1395-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1396-
}
1397-
offset++;
1318+
IntWrapper result = new IntWrapper();
1319+
if (toInt(result)) {
1320+
return result.value;
13981321
}
1399-
1400-
final byte separator = '.';
1401-
final int radix = 10;
1402-
final int stopValue = Integer.MIN_VALUE / radix;
1403-
int result = 0;
1404-
1405-
while (offset <= end) {
1406-
b = getByte(offset);
1407-
offset++;
1408-
if (b == separator) {
1409-
break;
1410-
}
1411-
1412-
int digit;
1413-
if (b >= '0' && b <= '9') {
1414-
digit = b - '0';
1415-
} else {
1416-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1417-
}
1418-
1419-
if (result < stopValue) {
1420-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1421-
}
1422-
1423-
result = result * radix - digit;
1424-
if (result > 0) {
1425-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1426-
}
1427-
}
1428-
1429-
while (offset <= end) {
1430-
byte currentByte = getByte(offset);
1431-
if (currentByte < '0' || currentByte > '9') {
1432-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1433-
}
1434-
offset++;
1435-
}
1436-
1437-
if (!negative) {
1438-
result = -result;
1439-
if (result < 0) {
1440-
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
1441-
}
1442-
}
1443-
return result;
1322+
throw new NumberFormatException("invalid input syntax for type numeric: " + this);
14441323
}
14451324

14461325
public short toShortExact() {

0 commit comments

Comments
 (0)