use of org.finos.legend.engine.plan.dependencies.domain.date.PureDate in project legend-engine by finos.
the class ResultNormalizer method normalizeToSql.
public static Object normalizeToSql(Object o, String databaseTimeZone) {
if (o == null) {
return "null";
}
if (o instanceof Map) {
return ((Map<?, ?>) o).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> normalizeToSql(e.getValue(), databaseTimeZone)));
}
if (o instanceof Iterable) {
return StreamSupport.stream(((Iterable<?>) o).spliterator(), false).map(v -> normalizeToSql(v, databaseTimeZone)).collect(Collectors.toList());
}
if (o instanceof PureDate) {
PureDate pureDate = (PureDate) o;
if (pureDate.hasSubsecond()) {
return pureDate.format("[" + databaseTimeZone + "]yyyy-MM-dd HH:mm:ss.SSSSSS");
}
if (pureDate.hasSecond()) {
return pureDate.format("[" + databaseTimeZone + "]yyyy-MM-dd HH:mm:ss");
}
return pureDate.format("[" + databaseTimeZone + "]yyyy-MM-dd");
}
if (o instanceof EngineDate) {
return ((EngineDate) o).formatToSql();
}
if (o instanceof String) {
return ((String) o).replace("'", "\'").replace("\\", "\\\\");
}
if (o instanceof IReferencedObject) {
Field[] fields = o.getClass().getDeclaredFields();
Map<String, Object> fieldToNormalizedValueMap = new HashMap<>();
for (Field f : fields) {
try {
f.setAccessible(true);
fieldToNormalizedValueMap.put(f.getName(), normalizeToSql(f.get(o), databaseTimeZone));
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
}
}
return fieldToNormalizedValueMap;
}
return o.toString();
}
use of org.finos.legend.engine.plan.dependencies.domain.date.PureDate in project legend-engine by finos.
the class TestExecutionPlan method testPlatform.
@Test
public void testPlatform() throws Exception {
String plan = "{\n" + " \"serializer\": {\n" + " \"name\": \"pure\",\n" + " \"version\": \"vX_X_X\"\n" + " },\n" + " \"rootExecutionNode\": {\n" + " \"varName\": \"businessDate\",\n" + " \"_type\": \"allocation\",\n" + " \"resultType\": {\n" + " \"dataType\": \"DateTime\",\n" + " \"_type\": \"dataType\"\n" + " },\n" + " \"executionNodes\": [\n" + " {\n" + " \"pure\": {\n" + " \"function\": \"now\",\n" + " \"fControl\": \"now__DateTime_1_\",\n" + " \"_type\": \"func\"\n" + " },\n" + " \"implementation\": {\n" + " \"executionClassFullName\": \"org.finos.legend.engine.plan.dependencies.store.platform.PredefinedExpressions\",\n" + " \"executionMethodName\": \"now\",\n" + " \"_type\": \"java\"\n" + " },\n" + " \"_type\": \"platform\",\n" + " \"resultType\": {\n" + " \"dataType\": \"DateTime\",\n" + " \"_type\": \"dataType\"\n" + " }\n" + " }\n" + " ]\n" + " }\n" + "}";
SingleExecutionPlan executionPlan = objectMapper.readValue(plan, SingleExecutionPlan.class);
ConstantResult result = runJavaExecutionPlan(executionPlan);
Assert.assertTrue(result.getValue() instanceof PureDate);
}
use of org.finos.legend.engine.plan.dependencies.domain.date.PureDate in project legend-engine by finos.
the class TestPureDate method testSubtractSubseconds.
@Test
public void testSubtractSubseconds() {
PureDate date = PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "780013429");
Assert.assertSame(date, date.subtractSubseconds("0"));
Assert.assertSame(date, date.subtractSubseconds("00000000000000000000000000000000000"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "680013429"), date.subtractSubseconds("1"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "680013429"), date.subtractSubseconds("1000000"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 32, "980013429"), date.subtractSubseconds("8"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "770013429"), date.subtractSubseconds("01"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "760013429"), date.subtractSubseconds("02"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 32, "781013429"), date.subtractSubseconds("999"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "777013429"), date.subtractSubseconds("003"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 32, "977813358"), date.subtractSubseconds("802200071"));
}
use of org.finos.legend.engine.plan.dependencies.domain.date.PureDate in project legend-engine by finos.
the class TestPureDate method testAddSubseconds.
@Test
public void testAddSubseconds() {
PureDate date = PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "780013429");
Assert.assertSame(date, date.addSubseconds("0"));
Assert.assertSame(date, date.addSubseconds("00000000000000000000000000000000000"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "880013429"), date.addSubseconds("1"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "880013429"), date.addSubseconds("1000000"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 34, "580013429"), date.addSubseconds("8"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "790013429"), date.addSubseconds("01"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "800013429"), date.addSubseconds("02"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 34, "779013429"), date.addSubseconds("999"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 33, "783013429"), date.addSubseconds("003"));
Assert.assertEquals(PureDate.newPureDate(2016, 5, 17, 10, 26, 34, "582213500"), date.addSubseconds("802200071"));
}
use of org.finos.legend.engine.plan.dependencies.domain.date.PureDate in project legend-engine by finos.
the class TestResultNormalizer method testDateTimeWithSubSecondNormalization.
@Test
public void testDateTimeWithSubSecondNormalization() {
PureDate date = PureDate.parsePureDate("2020-01-01T12:00:00.345678+0530");
String normalized = (String) ResultNormalizer.normalizeToSql(date);
Assert.assertEquals("2020-01-01 06:30:00.345678", normalized);
}
Aggregations