Search in sources :

Example 1 with PureDate

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();
}
Also used : EngineDate(org.finos.legend.engine.plan.execution.result.date.EngineDate) PureDate(org.finos.legend.engine.plan.dependencies.domain.date.PureDate) Map(java.util.Map) HashMap(java.util.HashMap) StreamSupport(java.util.stream.StreamSupport) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) IReferencedObject(org.finos.legend.engine.plan.dependencies.store.shared.IReferencedObject) HashMap(java.util.HashMap) PureDate(org.finos.legend.engine.plan.dependencies.domain.date.PureDate) EngineDate(org.finos.legend.engine.plan.execution.result.date.EngineDate) IReferencedObject(org.finos.legend.engine.plan.dependencies.store.shared.IReferencedObject) Field(java.lang.reflect.Field) IReferencedObject(org.finos.legend.engine.plan.dependencies.store.shared.IReferencedObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with PureDate

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);
}
Also used : ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) PureDate(org.finos.legend.engine.plan.dependencies.domain.date.PureDate) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) Test(org.junit.Test)

Example 3 with 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"));
}
Also used : PureDate(org.finos.legend.engine.plan.dependencies.domain.date.PureDate) Test(org.junit.Test)

Example 4 with PureDate

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"));
}
Also used : PureDate(org.finos.legend.engine.plan.dependencies.domain.date.PureDate) Test(org.junit.Test)

Example 5 with PureDate

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);
}
Also used : PureDate(org.finos.legend.engine.plan.dependencies.domain.date.PureDate) Test(org.junit.Test)

Aggregations

PureDate (org.finos.legend.engine.plan.dependencies.domain.date.PureDate)20 Test (org.junit.Test)17 ConstantResult (org.finos.legend.engine.plan.execution.result.ConstantResult)3 SingleExecutionPlan (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan)3 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 IReferencedObject (org.finos.legend.engine.plan.dependencies.store.shared.IReferencedObject)1 EngineDate (org.finos.legend.engine.plan.execution.result.date.EngineDate)1