use of org.finos.legend.engine.plan.dependencies.domain.dataQuality.Constrained 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