Search in sources :

Example 1 with RelationalExecutionActivity

use of org.finos.legend.engine.plan.execution.stores.relational.activity.RelationalExecutionActivity 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));
}
Also used : LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) DatabaseManager(org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager) PreparedTempTableResult(org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult) RelationalExecutionActivity(org.finos.legend.engine.plan.execution.stores.relational.activity.RelationalExecutionActivity) SQLExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.SQLExecutionNode) Span(io.opentracing.Span) SQLException(java.sql.SQLException) ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) PreparedTempTableResult(org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult) SQLExecutionResult(org.finos.legend.engine.plan.execution.stores.relational.result.SQLExecutionResult) VoidRelationalResult(org.finos.legend.engine.plan.execution.stores.relational.result.VoidRelationalResult) RelationalResult(org.finos.legend.engine.plan.execution.stores.relational.result.RelationalResult) RealizedRelationalResult(org.finos.legend.engine.plan.execution.stores.relational.result.RealizedRelationalResult) Result(org.finos.legend.engine.plan.execution.result.Result) RelationalExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.RelationalExecutionNode) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) Map(java.util.Map) MutableMap(org.eclipse.collections.api.map.MutableMap)

Aggregations

Span (io.opentracing.Span)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1 ConstantResult (org.finos.legend.engine.plan.execution.result.ConstantResult)1 Result (org.finos.legend.engine.plan.execution.result.Result)1 StreamingResult (org.finos.legend.engine.plan.execution.result.StreamingResult)1 RelationalExecutionActivity (org.finos.legend.engine.plan.execution.stores.relational.activity.RelationalExecutionActivity)1 DatabaseManager (org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager)1 PreparedTempTableResult (org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult)1 RealizedRelationalResult (org.finos.legend.engine.plan.execution.stores.relational.result.RealizedRelationalResult)1 RelationalResult (org.finos.legend.engine.plan.execution.stores.relational.result.RelationalResult)1 SQLExecutionResult (org.finos.legend.engine.plan.execution.stores.relational.result.SQLExecutionResult)1 VoidRelationalResult (org.finos.legend.engine.plan.execution.stores.relational.result.VoidRelationalResult)1 RelationalExecutionNode (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.RelationalExecutionNode)1 SQLExecutionNode (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.SQLExecutionNode)1 LogInfo (org.finos.legend.engine.shared.core.operational.logs.LogInfo)1