Search in sources :

Example 1 with DatabaseManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager in project legend-engine by finos.

the class RelationalExecutionNodeExecutor method createTempTableFromRealizedRelationalResultInBlockConnection.

private void createTempTableFromRealizedRelationalResultInBlockConnection(RealizedRelationalResult realizedRelationalResult, String tempTableName, DatabaseConnection databaseConnection, String databaseType, String databaseTimeZone) {
    try (Scope ignored = GlobalTracer.get().buildSpan("create temp table").withTag("tempTableName", tempTableName).withTag("databaseType", databaseType).startActive(true)) {
        RelationalStoreExecutionState relationalStoreExecutionState = (RelationalStoreExecutionState) this.executionState.getStoreExecutionState(StoreType.Relational);
        DatabaseManager databaseManager = DatabaseManager.fromString(databaseType);
        BlockConnection blockConnection = relationalStoreExecutionState.getBlockConnectionContext().getBlockConnection(relationalStoreExecutionState, databaseConnection, this.profiles);
        databaseManager.relationalDatabaseSupport().accept(RelationalDatabaseCommandsVisitorBuilder.getStreamResultToTempTableVisitor(relationalStoreExecutionState.getRelationalExecutor().getRelationalExecutionConfiguration(), blockConnection, realizedRelationalResult, tempTableName, databaseTimeZone));
        blockConnection.addCommitQuery(databaseManager.relationalDatabaseSupport().dropTempTable(tempTableName));
        blockConnection.addRollbackQuery(databaseManager.relationalDatabaseSupport().dropTempTable(tempTableName));
        blockConnection.close();
    }
}
Also used : Scope(io.opentracing.Scope) DatabaseManager(org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager) BlockConnection(org.finos.legend.engine.plan.execution.stores.relational.blockConnection.BlockConnection)

Example 2 with DatabaseManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager in project legend-engine by finos.

the class SQLExecutionResult method close.

@Override
public void close() {
    DatabaseManager databaseManager = DatabaseManager.fromString(this.SQLExecutionNode.getDatabaseTypeName());
    if (this.temporaryTables != null && this.statement != null) {
        this.temporaryTables.forEach((Consumer<? super String>) table -> {
            try {
                statement.execute(databaseManager.relationalDatabaseSupport().dropTempTable(table));
            } catch (Exception ignored) {
            }
        });
    }
    Consumer<AutoCloseable> closingFunction = (AutoCloseable c) -> {
        if (c != null) {
            try {
                c.close();
            } catch (Exception e) {
            }
        }
    };
    FastList.newListWith(this.resultSet, this.statement, this.connection).forEach((Procedure<AutoCloseable>) closingFunction::accept);
}
Also used : LoggingEventType(org.finos.legend.engine.shared.core.operational.logs.LoggingEventType) DatabaseManager(org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager) DatabaseType(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.DatabaseType) CommonProfile(org.pac4j.core.profile.CommonProfile) Connection(java.sql.Connection) ExecutionActivity(org.finos.legend.engine.plan.execution.result.ExecutionActivity) Procedure(org.eclipse.collections.api.block.procedure.Procedure) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) Calendar(java.util.Calendar) ResultSet(java.sql.ResultSet) RelationalExecutionActivity(org.finos.legend.engine.plan.execution.stores.relational.activity.RelationalExecutionActivity) Result(org.finos.legend.engine.plan.execution.result.Result) ResultVisitor(org.finos.legend.engine.plan.execution.result.ResultVisitor) SQLResultColumn(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.result.SQLResultColumn) Logger(org.slf4j.Logger) GregorianCalendar(java.util.GregorianCalendar) TimeZone(java.util.TimeZone) SQLExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.SQLExecutionNode) LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) Consumer(java.util.function.Consumer) List(java.util.List) Statement(java.sql.Statement) Span(io.opentracing.Span) ResultSetMetaData(java.sql.ResultSetMetaData) DatabaseManager(org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager)

Example 3 with DatabaseManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager 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)

Example 4 with DatabaseManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager 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);
            }
        }
    }
}
Also used : Scope(io.opentracing.Scope) DatabaseManager(org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager) PreparedTempTableResult(org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult)

Aggregations

DatabaseManager (org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager)4 Scope (io.opentracing.Scope)2 Span (io.opentracing.Span)2 Result (org.finos.legend.engine.plan.execution.result.Result)2 RelationalExecutionActivity (org.finos.legend.engine.plan.execution.stores.relational.activity.RelationalExecutionActivity)2 PreparedTempTableResult (org.finos.legend.engine.plan.execution.stores.relational.result.PreparedTempTableResult)2 SQLExecutionNode (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.SQLExecutionNode)2 LogInfo (org.finos.legend.engine.shared.core.operational.logs.LogInfo)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Map (java.util.Map)1 TimeZone (java.util.TimeZone)1 Consumer (java.util.function.Consumer)1 Procedure (org.eclipse.collections.api.block.procedure.Procedure)1