use of org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult in project legend-engine by finos.
the class RelationalExecutor method prepareForSQLExecution.
private void prepareForSQLExecution(ExecutionNode node, Connection connection, String databaseTimeZone, String databaseTypeName, List<String> tempTableList, MutableList<CommonProfile> profiles, ExecutionState executionState) {
String sqlQuery;
sqlQuery = node instanceof RelationalExecutionNode ? ((RelationalExecutionNode) node).sqlQuery() : ((SQLExecutionNode) node).sqlQuery();
DatabaseManager databaseManager = DatabaseManager.fromString(databaseTypeName);
for (Map.Entry<String, Result> var : executionState.getResults().entrySet()) {
if (var.getValue() instanceof StreamingResult && sqlQuery.contains("(${" + var.getKey() + "})")) {
String tableName = databaseManager.relationalDatabaseSupport().processTempTableName(var.getKey());
this.prepareTempTable(connection, (StreamingResult) var.getValue(), tableName, databaseTypeName, databaseTimeZone, tempTableList);
tempTableList.add(tableName);
sqlQuery = sqlQuery.replace("(${" + var.getKey() + "})", tableName);
} else if (var.getValue() instanceof PreparedTempTableResult && sqlQuery.contains("(${" + var.getKey() + "})")) {
sqlQuery = sqlQuery.replace("(${" + var.getKey() + "})", ((PreparedTempTableResult) var.getValue()).getTempTableName());
}
}
if (sqlQuery == null) {
throw new RuntimeException("Relational execution not supported on external server");
}
try {
sqlQuery = FreeMarkerExecutor.process(sqlQuery, executionState, databaseTypeName, databaseTimeZone);
Span span = GlobalTracer.get().activeSpan();
if (span != null) {
span.setTag("generatedSQL", sqlQuery);
}
} catch (Exception e) {
throw new IllegalStateException("Reprocessing sql failed with vars " + executionState.getResults().keySet(), e);
}
LOGGER.info(new LogInfo(profiles, LoggingEventType.EXECUTION_RELATIONAL_REPROCESS_SQL, "Reprocessing sql with vars " + executionState.getResults().keySet() + ": " + sqlQuery).toString());
executionState.activities.add(new RelationalExecutionActivity(sqlQuery));
}
use of org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult in project legend-engine by finos.
the class RelationalExecutionNodeExecutor method executeTempTableNodeChildren.
private void executeTempTableNodeChildren(RelationalTempTableGraphFetchExecutionNode node, RealizedRelationalResult realizedRelationalResult, DatabaseConnection databaseConnection, String databaseType, String databaseTimeZone, DoubleStrategyHashMap<Object, Object, SQLExecutionResult> nodeObjectsMap, List<Method> nodePrimaryKeyGetters, ExecutionState state) {
RelationalGraphObjectsBatch relationalGraphObjectsBatch = (RelationalGraphObjectsBatch) state.graphObjectsBatch;
if (realizedRelationalResult.resultSetRows.isEmpty()) {
node.children.forEach(x -> this.recursivelyPopulateEmptyResultsInGraphObjectsBatch(x, relationalGraphObjectsBatch));
} else {
String tempTableName = DatabaseManager.fromString(databaseType).relationalDatabaseSupport().processTempTableName(node.tempTableName);
RelationalExecutionNodeExecutor.this.createTempTableFromRealizedRelationalResultInBlockConnection(realizedRelationalResult, tempTableName, databaseConnection, databaseType, databaseTimeZone);
state.addResult(node.tempTableName, new PreparedTempTableResult(tempTableName));
relationalGraphObjectsBatch.setNodeObjectsHashMap(node.nodeIndex, nodeObjectsMap);
relationalGraphObjectsBatch.setNodePrimaryKeyGetters(node.nodeIndex, nodePrimaryKeyGetters);
node.children.forEach(x -> x.accept(new ExecutionNodeExecutor(this.profiles, state)));
}
}
use of org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult in project legend-engine by finos.
the class RelationalExecutionNodeExecutor method executeRelationalChildren.
private void executeRelationalChildren(RelationalGraphFetchExecutionNode node, String tempTableNameFromNode, RealizedRelationalResult realizedRelationalResult, DatabaseConnection databaseConnection, String databaseType, String databaseTimeZone, DoubleStrategyHashMap<Object, Object, SQLExecutionResult> parentMap, List<Method> parentKeyGetters) {
try (Scope ignored1 = GlobalTracer.get().buildSpan("Graph Query Relational: Execute Children").startActive(true)) {
RelationalStoreExecutionState relationalStoreExecutionState = (RelationalStoreExecutionState) this.executionState.getStoreExecutionState(StoreType.Relational);
String tempTableName = "temp_table_" + tempTableNameFromNode;
DatabaseManager databaseManager = DatabaseManager.fromString(databaseType);
try (Scope ignored2 = GlobalTracer.get().buildSpan("Graph Query Relational: Create Temp Table").startActive(true)) {
this.createTempTableFromRealizedRelationalResultInBlockConnection(realizedRelationalResult, tempTableName, databaseConnection, databaseType, databaseTimeZone);
}
this.executionState.addResult(tempTableNameFromNode, new PreparedTempTableResult(tempTableName));
for (RelationalGraphFetchExecutionNode childNode : node.children) {
try (Scope ignored3 = GlobalTracer.get().buildSpan("Graph Query Relational: Execute Child").startActive(true)) {
this.executeLocalRelationalGraphOperation(childNode, parentMap, parentKeyGetters);
}
}
}
}
Aggregations