Commit f053fd59 by ashutoshm Committed by Madhan Neethiraj

ATLAS-1876: fix to handle large float/double values during ser-de, export/import

parent 242b5585
...@@ -283,7 +283,12 @@ public class AtlasBuiltInTypes { ...@@ -283,7 +283,12 @@ public class AtlasBuiltInTypes {
return ((Number) obj).floatValue(); return ((Number) obj).floatValue();
} else { } else {
try { try {
return Float.valueOf(obj.toString()); Float f = Float.valueOf(obj.toString());
if(!Float.isInfinite(f)) {
return f;
} else {
return null;
}
} catch (NumberFormatException excp) { } catch (NumberFormatException excp) {
// ignore // ignore
} }
...@@ -329,7 +334,12 @@ public class AtlasBuiltInTypes { ...@@ -329,7 +334,12 @@ public class AtlasBuiltInTypes {
return ((Number) obj).doubleValue(); return ((Number) obj).doubleValue();
} else { } else {
try { try {
return Double.valueOf(obj.toString()); Double d = Double.valueOf(obj.toString());
if(!Double.isInfinite(d)) {
return d;
} else {
return null;
}
} catch (NumberFormatException excp) { } catch (NumberFormatException excp) {
// ignore // ignore
} }
......
...@@ -21,9 +21,9 @@ package org.apache.atlas.type; ...@@ -21,9 +21,9 @@ package org.apache.atlas.type;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef; import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
...@@ -36,7 +36,8 @@ import java.util.List; ...@@ -36,7 +36,8 @@ import java.util.List;
public abstract class AtlasType { public abstract class AtlasType {
private static final ObjectMapper mapper = new ObjectMapper(); private static final ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationConfig.Feature.USE_BIG_DECIMAL_FOR_FLOATS, true);
private final String typeName; private final String typeName;
private final TypeCategory typeCategory; private final TypeCategory typeCategory;
......
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.type;
import org.testng.annotations.Test;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
public class TestAtlasBuiltInTypesFloatDouble {
@Test
public void floatRangeCheck() {
assertFloatChecks(String.valueOf("-1.E-45"), true);
assertFloatChecks(String.valueOf(Float.MAX_VALUE), true);
assertFloatChecks("3.4028235E32", true);
assertFloatChecks("-3.4028235E32", true);
assertFloatChecks(String.valueOf(Float.MIN_VALUE), true);
assertFloatChecks("4028235e+555", false);
assertFloatChecks("-4028235e+555", false);
}
@Test
public void doubleRangeCheck() {
assertDoubleChecks(String.valueOf(Double.MAX_VALUE), true);
assertDoubleChecks("3.4028235E32", true);
assertDoubleChecks(String.valueOf(Double.MIN_VALUE), true);
assertDoubleChecks("4028235e+55555", false);
assertDoubleChecks("-4028235e+55555", false);
}
private void assertFloatChecks(String v, boolean notNull) {
assertNullNotNull(notNull, new AtlasBuiltInTypes.AtlasFloatType().getNormalizedValue(v));
}
private void assertDoubleChecks(String v, boolean notNull) {
assertNullNotNull(notNull, new AtlasBuiltInTypes.AtlasDoubleType().getNormalizedValue(v));
}
private void assertNullNotNull(boolean notNull, Object f) {
if(notNull) {
assertNotNull(f);
} else {
assertNull(f);
}
}
}
...@@ -23,9 +23,8 @@ import org.apache.atlas.model.instance.AtlasEntity; ...@@ -23,9 +23,8 @@ import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
import org.apache.atlas.model.typedef.AtlasTypesDef; import org.apache.atlas.model.typedef.AtlasTypesDef;
import org.apache.atlas.repository.store.graph.v1.EntityImportStream; import org.apache.atlas.repository.store.graph.v1.EntityImportStream;
import org.apache.atlas.type.AtlasType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -39,8 +38,6 @@ import java.util.Map; ...@@ -39,8 +38,6 @@ import java.util.Map;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import static org.apache.atlas.AtlasErrorCode.JSON_ERROR_OBJECT_MAPPER_NULL_RETURNED;
public class ZipSource implements EntityImportStream { public class ZipSource implements EntityImportStream {
private static final Logger LOG = LoggerFactory.getLogger(ZipSource.class); private static final Logger LOG = LoggerFactory.getLogger(ZipSource.class);
...@@ -90,7 +87,7 @@ public class ZipSource implements EntityImportStream { ...@@ -90,7 +87,7 @@ public class ZipSource implements EntityImportStream {
try { try {
String s = getFromCache(fileName); String s = getFromCache(fileName);
this.creationOrder = convertFromJson(new TypeReference<List<String>>(){}, s); this.creationOrder = convertFromJson(List.class, s);
this.iterator = this.creationOrder.iterator(); this.iterator = this.creationOrder.iterator();
} catch (AtlasBaseException e) { } catch (AtlasBaseException e) {
LOG.error(String.format("Error retrieving '%s' from zip.", fileName), e); LOG.error(String.format("Error retrieving '%s' from zip.", fileName), e);
...@@ -137,26 +134,9 @@ public class ZipSource implements EntityImportStream { ...@@ -137,26 +134,9 @@ public class ZipSource implements EntityImportStream {
return entityWithExtInfo; return entityWithExtInfo;
} }
private <T> T convertFromJson(TypeReference clazz, String jsonData) throws AtlasBaseException {
try {
ObjectMapper mapper = new ObjectMapper();
T ret = mapper.readValue(jsonData, clazz);
if(ret == null) {
throw new AtlasBaseException(JSON_ERROR_OBJECT_MAPPER_NULL_RETURNED, clazz.toString());
}
return ret;
} catch (Exception e) {
throw new AtlasBaseException("Error converting file to JSON.", e);
}
}
private <T> T convertFromJson(Class<T> clazz, String jsonData) throws AtlasBaseException { private <T> T convertFromJson(Class<T> clazz, String jsonData) throws AtlasBaseException {
try { try {
ObjectMapper mapper = new ObjectMapper(); return AtlasType.fromJson(jsonData, clazz);
return mapper.readValue(jsonData, clazz);
} catch (Exception e) { } catch (Exception e) {
throw new AtlasBaseException("Error converting file to JSON.", e); throw new AtlasBaseException("Error converting file to JSON.", e);
......
...@@ -44,6 +44,13 @@ public class ZipSourceTest { ...@@ -44,6 +44,13 @@ public class ZipSourceTest {
return new Object[][] {{ new ZipSource(fs) }}; return new Object[][] {{ new ZipSource(fs) }};
} }
@DataProvider(name = "zipFileStocksFloat")
public static Object[][] getDataFromZipFileWithLongFloats() throws IOException {
FileInputStream fs = ZipFileResourceTestUtils.getFileInputStream("stocks-float.zip");
return new Object[][] {{ new ZipSource(fs) }};
}
@DataProvider(name = "sales") @DataProvider(name = "sales")
public static Object[][] getDataFromQuickStart_v1_Sales(ITestContext context) throws IOException { public static Object[][] getDataFromQuickStart_v1_Sales(ITestContext context) throws IOException {
return getZipSource("sales-v1-full.zip"); return getZipSource("sales-v1-full.zip");
...@@ -139,6 +146,21 @@ public class ZipSourceTest { ...@@ -139,6 +146,21 @@ public class ZipSourceTest {
assertTrue(zipSource.hasNext()); assertTrue(zipSource.hasNext());
} }
@Test(dataProvider = "zipFileStocksFloat")
public void attemptToSerializeLongFloats(ZipSource zipSource) throws IOException, AtlasBaseException {
Assert.assertTrue(zipSource.hasNext());
assertTrue(zipSource.hasNext());
assertTrue(zipSource.hasNext());
AtlasEntity.AtlasEntityWithExtInfo e = zipSource.getNextEntityWithExtInfo();
assertNotNull(e);
assertTrue(e.getEntity().getClassifications().size() > 0);
assertNotNull(e.getEntity().getClassifications().get(0).getAttribute("fv"));
assertEquals(e.getEntity().getClassifications().get(0).getAttribute("fv").toString(), "3.4028235E+38");
assertTrue(zipSource.hasNext());
}
@Test(dataProvider = "zipFileStocks") @Test(dataProvider = "zipFileStocks")
public void applyTransformation(ZipSource zipSource) throws IOException, AtlasBaseException { public void applyTransformation(ZipSource zipSource) throws IOException, AtlasBaseException {
ImportTransforms transforms = getTransformForHiveDB(); ImportTransforms transforms = getTransformForHiveDB();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment