use of org.finos.legend.engine.plan.execution.result.ConstantResult in project legend-engine by finos.
the class TestRelationalExecutor method testESTTimeZonePlanWithDateTimeAndMillisecondStringParameter.
@Test
public void testESTTimeZonePlanWithDateTimeAndMillisecondStringParameter() throws IOException {
String connectionTimeZone = "US/Arizona";
Assert.assertFalse("Expect " + connectionTimeZone + " never to be in DST", ZoneId.of(connectionTimeZone).getRules().isDaylightSavings(Instant.now()));
MutableMap<String, Result> vars = Maps.mutable.empty();
String gmtTestDateTimeParam = "2014-12-04T15:22:23.123";
vars.put("dt", new ConstantResult(gmtTestDateTimeParam));
String planWithTz = getTemplatePlanWithNoTz().replace("%timeZone%", ",\"timeZone\": \"" + connectionTimeZone + "\"").replace("%templateFunctions%", getFreeMarkerAlloyDateFunction()).replace("%dateTimeParam%", "testDateTime = '${GMTtoTZ( \\\"[" + connectionTimeZone + "]\\\" dt )}'");
SingleExecutionPlan executionPlan = objectMapper.readValue(planWithTz, SingleExecutionPlan.class);
RelationalResult result = (RelationalResult) executionPlan.rootExecutionNode.accept(new ExecutionNodeExecutor(null, new ExecutionState(vars, Lists.mutable.withAll(executionPlan.templateFunctions), Lists.mutable.with(new RelationalStoreExecutionState(new RelationalStoreState(serverPort))))));
Assert.assertEquals("{\"builder\": {\"_type\":\"classBuilder\",\"mapping\":\"meta::relational::tests::milestoning::milestoningmap\",\"classMappings\":[{\"setImplementationId\":\"meta_relational_tests_milestoning_ProductSynonym\",\"properties\":[{\"property\":\"synonym\",\"type\":\"String\"},{\"property\":\"type\",\"type\":\"String\"},{\"property\":\"testDate\",\"type\":\"Date\"},{\"property\":\"testDateTime\",\"type\":\"DateTime\"}],\"class\":\"meta::relational::tests::milestoning::ProductSynonym\"}],\"class\":\"meta::relational::tests::milestoning::ProductSynonym\"}, \"activities\": [{\"_type\":\"relational\",\"sql\":\"select \\\"root\\\".name as \\\"pk_0\\\", \\\"root\\\".synonym as \\\"pk_1\\\", \\\"root\\\".type as \\\"pk_2\\\", \\\"root\\\".synonym as \\\"synonym\\\", \\\"root\\\".type as \\\"type\\\", \\\"root\\\".testDate as \\\"testDate\\\", \\\"root\\\".testDateTime as \\\"testDateTime\\\", \\\"root\\\".from_z as \\\"k_businessDate\\\" from ProductSynonymTable as \\\"root\\\" where \\\"root\\\".testDateTime = '2014-12-04T08:22:23.123'\"}], \"result\" : {\"columns\" : [\"pk_0\",\"pk_1\",\"pk_2\",\"synonym\",\"type\",\"testDate\",\"testDateTime\",\"k_businessDate\"], \"rows\" : [{\"values\": [\"GS-ModD\",\"GS-Mod-S1\",\"STOCK\",\"GS-Mod-S1\",\"STOCK\",\"2015-09-26\",\"2014-12-04T15:22:23.123000000+0000\",\"2015-08-27T06:00:00.000000000+0000\"]}]}}", result.flush(new RelationalResultToJsonDefaultSerializer(result)));
}
use of org.finos.legend.engine.plan.execution.result.ConstantResult in project legend-engine by finos.
the class TestRelationalExecutor method testErrorFromSqlTemplateFreeMarkerProcessingBubblesUp.
@Test
public void testErrorFromSqlTemplateFreeMarkerProcessingBubblesUp() throws IOException {
String planWithTz = getTemplatePlanWithNoTz().replace("%timeZone%", ",\"timeZone\": \"GMT\"").replace("%templateFunctions%", "\"templateFunctions\": [],");
String plan = planWithTz.replace("%dateTimeParam%", "testDateTime = '${GMTtoTZ( \\\"[US/Arizona]\\\" dt )}'");
SingleExecutionPlan executionPlan = objectMapper.readValue(plan, SingleExecutionPlan.class);
MutableMap<String, Result> vars = Maps.mutable.with("dt", new ConstantResult("2014-12-04T15:22:23"));
IllegalStateException exception = Assert.assertThrows(IllegalStateException.class, () -> executionPlan.rootExecutionNode.accept(new ExecutionNodeExecutor(null, new ExecutionState(vars, Lists.mutable.withAll(executionPlan.templateFunctions), Lists.mutable.with(new RelationalStoreExecutionState(new RelationalStoreState(serverPort)))))));
assertThat(exception, hasMessage(startsWith("Reprocessing sql failed with vars [dt]")));
assertThat(exception, hasCause(hasMessage(startsWith("Issue processing freemarker function. Template with error: select \"root\".name as \"pk_0\", \"root\".synonym as \"pk_1\", \"root\".type as \"pk_2\", \"root\".synonym as \"synonym\", \"root\".type as \"type\", \"root\".testDate as \"testDate\", \"root\".testDateTime as \"testDateTime\", \"root\".from_z as \"k_businessDate\" from ProductSynonymTable as \"root\" where \"root\".testDateTime = 'FreeMarker template error "))));
}
use of org.finos.legend.engine.plan.execution.result.ConstantResult in project legend-engine by finos.
the class TestRelationalExecutor method testNonDSTTTimeZonePlanWithDateTimeStringParameterPlan.
@Test
public void testNonDSTTTimeZonePlanWithDateTimeStringParameterPlan() throws IOException {
String connectionTimeZone = "US/Arizona";
Assert.assertFalse("Expect " + connectionTimeZone + " never to be in DST", ZoneId.of(connectionTimeZone).getRules().isDaylightSavings(Instant.now()));
MutableMap<String, Result> vars = Maps.mutable.empty();
String gmtTestDateTimeParam = "2014-12-04T15:22:23";
vars.put("dt", new ConstantResult(gmtTestDateTimeParam));
String planWithTz = getTemplatePlanWithNoTz().replace("%timeZone%", ",\"timeZone\": \"" + connectionTimeZone + "\"").replace("%templateFunctions%", getFreeMarkerAlloyDateFunction()).replace("%dateTimeParam%", "testDateTime = '${GMTtoTZ( \\\"[" + connectionTimeZone + "]\\\" dt )}'");
SingleExecutionPlan executionPlan = objectMapper.readValue(planWithTz, SingleExecutionPlan.class);
RelationalResult result = (RelationalResult) executionPlan.rootExecutionNode.accept(new ExecutionNodeExecutor(null, new ExecutionState(vars, Lists.mutable.withAll(executionPlan.templateFunctions), Lists.mutable.with(new RelationalStoreExecutionState(new RelationalStoreState(serverPort))))));
Assert.assertEquals("{\"builder\": {\"_type\":\"classBuilder\",\"mapping\":\"meta::relational::tests::milestoning::milestoningmap\",\"classMappings\":[{\"setImplementationId\":\"meta_relational_tests_milestoning_ProductSynonym\",\"properties\":[{\"property\":\"synonym\",\"type\":\"String\"},{\"property\":\"type\",\"type\":\"String\"},{\"property\":\"testDate\",\"type\":\"Date\"},{\"property\":\"testDateTime\",\"type\":\"DateTime\"}],\"class\":\"meta::relational::tests::milestoning::ProductSynonym\"}],\"class\":\"meta::relational::tests::milestoning::ProductSynonym\"}, \"activities\": [{\"_type\":\"relational\",\"sql\":\"select \\\"root\\\".name as \\\"pk_0\\\", \\\"root\\\".synonym as \\\"pk_1\\\", \\\"root\\\".type as \\\"pk_2\\\", \\\"root\\\".synonym as \\\"synonym\\\", \\\"root\\\".type as \\\"type\\\", \\\"root\\\".testDate as \\\"testDate\\\", \\\"root\\\".testDateTime as \\\"testDateTime\\\", \\\"root\\\".from_z as \\\"k_businessDate\\\" from ProductSynonymTable as \\\"root\\\" where \\\"root\\\".testDateTime = '2014-12-04T08:22:23'\"}], \"result\" : {\"columns\" : [\"pk_0\",\"pk_1\",\"pk_2\",\"synonym\",\"type\",\"testDate\",\"testDateTime\",\"k_businessDate\"], \"rows\" : [{\"values\": [\"GS-ModC\",\"GS-Mod-S1\",\"STOCK\",\"GS-Mod-S1\",\"STOCK\",\"2015-08-26\",\"2014-12-04T15:22:23.000000000+0000\",\"2015-08-27T06:00:00.000000000+0000\"]}]}}", result.flush(new RelationalResultToJsonDefaultSerializer(result)));
}
use of org.finos.legend.engine.plan.execution.result.ConstantResult in project legend-engine by finos.
the class TestRelationalExecutor method testGMTTimeZonePlanWithDateTimeStringParameter.
@Test
public void testGMTTimeZonePlanWithDateTimeStringParameter() throws IOException {
MutableMap<String, Result> vars = Maps.mutable.empty();
vars.put("dt", new ConstantResult("2014-12-04T15:22:23.123456789"));
String planWithTz = getTemplatePlanWithNoTz().replace("%timeZone%", ",\"timeZone\": \"GMT\"").replace("%templateFunctions%", getFreeMarkerAlloyDateFunction()).replace("%dateTimeParam%", "testDateTime = '${dt}'");
SingleExecutionPlan executionPlan = objectMapper.readValue(planWithTz, SingleExecutionPlan.class);
RelationalResult result = (RelationalResult) executionPlan.rootExecutionNode.accept(new ExecutionNodeExecutor(null, new ExecutionState(vars, Lists.mutable.withAll(executionPlan.templateFunctions), Lists.mutable.with(new RelationalStoreExecutionState(new RelationalStoreState(serverPort))))));
Assert.assertEquals("{\"builder\": {\"_type\":\"classBuilder\",\"mapping\":\"meta::relational::tests::milestoning::milestoningmap\",\"classMappings\":[{\"setImplementationId\":\"meta_relational_tests_milestoning_ProductSynonym\",\"properties\":[{\"property\":\"synonym\",\"type\":\"String\"},{\"property\":\"type\",\"type\":\"String\"},{\"property\":\"testDate\",\"type\":\"Date\"},{\"property\":\"testDateTime\",\"type\":\"DateTime\"}],\"class\":\"meta::relational::tests::milestoning::ProductSynonym\"}],\"class\":\"meta::relational::tests::milestoning::ProductSynonym\"}, \"activities\": [{\"_type\":\"relational\",\"sql\":\"select \\\"root\\\".name as \\\"pk_0\\\", \\\"root\\\".synonym as \\\"pk_1\\\", \\\"root\\\".type as \\\"pk_2\\\", \\\"root\\\".synonym as \\\"synonym\\\", \\\"root\\\".type as \\\"type\\\", \\\"root\\\".testDate as \\\"testDate\\\", \\\"root\\\".testDateTime as \\\"testDateTime\\\", \\\"root\\\".from_z as \\\"k_businessDate\\\" from ProductSynonymTable as \\\"root\\\" where \\\"root\\\".testDateTime = '2014-12-04T15:22:23.123456789'\"}], \"result\" : {\"columns\" : [\"pk_0\",\"pk_1\",\"pk_2\",\"synonym\",\"type\",\"testDate\",\"testDateTime\",\"k_businessDate\"], \"rows\" : [{\"values\": [\"GS-ModA\",\"GS-Mod-S1\",\"STOCK\",\"GS-Mod-S1\",\"STOCK\",\"2015-06-26\",\"2014-12-04T15:22:23.123456789+0000\",\"2015-08-26T00:00:00.000000000+0000\"]}]}}", result.flush(new RelationalResultToJsonDefaultSerializer(result)));
}
use of org.finos.legend.engine.plan.execution.result.ConstantResult in project legend-engine by finos.
the class ExecutionNodeExecutor method visit.
@Override
public Result visit(GlobalGraphFetchExecutionNode globalGraphFetchExecutionNode) {
final Span topSpan = GlobalTracer.get().activeSpan();
final boolean isGraphRoot = globalGraphFetchExecutionNode.parentIndex == null;
if (isGraphRoot) {
final boolean enableConstraints = globalGraphFetchExecutionNode.enableConstraints == null ? false : globalGraphFetchExecutionNode.enableConstraints;
final boolean checked = globalGraphFetchExecutionNode.checked == null ? false : globalGraphFetchExecutionNode.checked;
// Handle batching at root level
final AtomicLong rowCount = new AtomicLong(0L);
final AtomicLong objectCount = new AtomicLong(0L);
final DoubleSummaryStatistics memoryStatistics = new DoubleSummaryStatistics();
GraphFetchResult graphFetchResult = (GraphFetchResult) globalGraphFetchExecutionNode.localGraphFetchExecutionNode.accept(new ExecutionNodeExecutor(this.profiles, this.executionState));
Stream<?> objectStream = graphFetchResult.getGraphObjectsBatchStream().map(batch -> {
List<?> parentObjects = batch.getObjectsForNodeIndex(0);
boolean nonEmptyObjectList = !parentObjects.isEmpty();
ExecutionState newState = new ExecutionState(this.executionState).setGraphObjectsBatch(batch);
if (globalGraphFetchExecutionNode.children != null && !globalGraphFetchExecutionNode.children.isEmpty() && nonEmptyObjectList) {
globalGraphFetchExecutionNode.children.forEach(c -> c.accept(new ExecutionNodeExecutor(this.profiles, newState)));
}
rowCount.addAndGet(batch.getRowCount());
if (nonEmptyObjectList) {
long currentObjectCount = objectCount.addAndGet(parentObjects.size());
memoryStatistics.accept(batch.getTotalObjectMemoryUtilization() / (parentObjects.size() * 1.0));
if (graphFetchResult.getGraphFetchSpan() != null) {
Span graphFetchSpan = graphFetchResult.getGraphFetchSpan();
graphFetchSpan.setTag("batchCount", memoryStatistics.getCount());
graphFetchSpan.setTag("objectCount", currentObjectCount);
graphFetchSpan.setTag("avgMemoryUtilizationInBytesPerObject", memoryStatistics.getAverage());
}
}
if (!nonEmptyObjectList) {
if (topSpan != null && rowCount.get() > 0) {
topSpan.setTag("lastQueryRowCount", rowCount);
}
}
if (checked) {
return parentObjects.stream().map(x -> (IChecked<?>) x).map(x -> x.getValue() instanceof Constrained ? ((Constrained<?>) x.getValue()).toChecked(x.getSource(), enableConstraints) : x).collect(Collectors.toList());
}
if (enableConstraints) {
return parentObjects.stream().map(x -> x instanceof Constrained ? ((Constrained<?>) x).withConstraintsApplied() : x).collect(Collectors.toList());
}
return parentObjects;
}).flatMap(Collection::stream);
boolean realizeAsConstant = this.executionState.inAllocation && ExecutionNodeResultHelper.isResultSizeRangeSet(globalGraphFetchExecutionNode) && ExecutionNodeResultHelper.isSingleRecordResult(globalGraphFetchExecutionNode);
if (realizeAsConstant) {
return new ConstantResult(objectStream.findFirst().orElseThrow(() -> new RuntimeException("Constant value not found")));
}
return new StreamingObjectResult<>(objectStream, new PartialClassBuilder(globalGraphFetchExecutionNode), graphFetchResult);
} else {
GraphObjectsBatch graphObjectsBatch = this.executionState.graphObjectsBatch;
List<?> parentObjects = graphObjectsBatch.getObjectsForNodeIndex(globalGraphFetchExecutionNode.parentIndex);
if ((parentObjects != null) && !parentObjects.isEmpty()) {
if (globalGraphFetchExecutionNode.xStorePropertyFetchDetails != null && globalGraphFetchExecutionNode.xStorePropertyFetchDetails.supportsCaching && this.executionState.graphFetchCaches != null) {
graphObjectsBatch.setXStorePropertyCachesForNodeIndex(globalGraphFetchExecutionNode.localGraphFetchExecutionNode.nodeIndex, findGraphFetchCacheByTargetCrossKeys(globalGraphFetchExecutionNode));
}
globalGraphFetchExecutionNode.localGraphFetchExecutionNode.accept(new ExecutionNodeExecutor(this.profiles, this.executionState));
if (globalGraphFetchExecutionNode.children != null && !globalGraphFetchExecutionNode.children.isEmpty()) {
globalGraphFetchExecutionNode.children.forEach(c -> c.accept(new ExecutionNodeExecutor(this.profiles, this.executionState)));
}
}
return new ConstantResult(parentObjects);
}
}
Aggregations