Search in sources :

Example 1 with StreamingResult

use of org.finos.legend.engine.plan.execution.result.StreamingResult in project legend-sdlc by finos.

the class LegendPureV1TestCase method getResultAsJson.

private JsonNode getResultAsJson(Result result) {
    if (result instanceof StreamingResult) {
        try {
            Serializer serializer = ((StreamingResult) result).getSerializer(SerializationFormat.DEFAULT);
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
            serializer.stream(byteStream);
            return objectMapper.readTree(byteStream.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (result instanceof ConstantResult) {
        Object value = ((ConstantResult) result).getValue();
        return objectMapper.valueToTree(value);
    }
    throw new RuntimeException("Unhandled result type: " + result.getClass().getSimpleName());
}
Also used : ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Serializer(org.finos.legend.engine.plan.execution.result.serialization.Serializer)

Example 2 with StreamingResult

use of org.finos.legend.engine.plan.execution.result.StreamingResult in project legend-engine by finos.

the class MappingTestRunner method getResultAsJson.

private JsonNode getResultAsJson(Result result) {
    if (result instanceof StreamingResult) {
        try {
            Serializer serializer = ((StreamingResult) result).getSerializer(SerializationFormat.DEFAULT);
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
            serializer.stream(byteStream);
            return objectMapper.readTree(byteStream.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (result instanceof ConstantResult) {
        Object value = ((ConstantResult) result).getValue();
        return objectMapper.valueToTree(value);
    }
    throw new RuntimeException("Unhandled result type: " + result.getClass().getSimpleName());
}
Also used : ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Serializer(org.finos.legend.engine.plan.execution.result.serialization.Serializer)

Example 3 with StreamingResult

use of org.finos.legend.engine.plan.execution.result.StreamingResult 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 StreamingResult

use of org.finos.legend.engine.plan.execution.result.StreamingResult in project legend-engine by finos.

the class TestExternalFormatQueries method runTest.

protected String runTest(PureModelContextData generated, String grammar, String query, String mappingPath, String runtimePath, InputStream input) {
    try {
        PureModelContextData parsed = PureGrammarParser.newInstance().parseModel(grammar);
        PureModelContextData modelData = generated == null ? parsed : parsed.combine(generated);
        ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
        String json = objectMapper.writeValueAsString(modelData);
        modelData = objectMapper.readValue(json, PureModelContextData.class);
        PureModel model = Compiler.compile(modelData, DeploymentMode.TEST, null);
        PureGrammarParser parser = PureGrammarParser.newInstance();
        Lambda lambdaProtocol = parser.parseLambda(query, "query", true);
        LambdaFunction<?> lambda = HelperValueSpecificationBuilder.buildLambda(lambdaProtocol.body, Lists.fixedSize.<Variable>empty(), model.getContext());
        ExecutionContext context = new Root_meta_pure_runtime_ExecutionContext_Impl(" ")._enableConstraints(true);
        RichIterable<Root_meta_external_shared_format_ExternalFormatExtension> planGenerationExtensions = LazyIterate.collect(ExternalFormatPlanGenerationExtensionLoader.extensions().values(), ext -> ext.getPureExtension(model.getExecutionSupport()));
        RichIterable<? extends Root_meta_pure_router_extension_RouterExtension> extensions = core_external_shared_extension.Root_meta_external_shared_format_routerExtensions_String_1__ExternalFormatExtension_MANY__RouterExtension_MANY_("externalFormat", planGenerationExtensions, model.getExecutionSupport());
        Mapping mapping = model.getMapping(mappingPath);
        Runtime runtime = model.getRuntime(runtimePath);
        String plan = PlanGenerator.generateExecutionPlanAsString(lambda, mapping, runtime, context, model, "vX_X_X", PlanPlatform.JAVA, "test", extensions, LegendPlanTransformers.transformers);
        PlanExecutor executor = PlanExecutor.newPlanExecutorWithAvailableStoreExecutors(true);
        Result result = executor.execute(plan, input);
        StreamingResult streamingResult = (StreamingResult) result;
        return streamingResult.flush(streamingResult.getSerializer(SerializationFormat.DEFAULT));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Root_meta_external_shared_format_ExternalFormatExtension(org.finos.legend.pure.generated.Root_meta_external_shared_format_ExternalFormatExtension) PlanExecutor(org.finos.legend.engine.plan.execution.PlanExecutor) Mapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) PureGrammarParser(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser) Result(org.finos.legend.engine.plan.execution.result.Result) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) Runtime(org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.Runtime) ExecutionContext(org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.ExecutionContext) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) Root_meta_pure_runtime_ExecutionContext_Impl(org.finos.legend.pure.generated.Root_meta_pure_runtime_ExecutionContext_Impl) Lambda(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

StreamingResult (org.finos.legend.engine.plan.execution.result.StreamingResult)4 ConstantResult (org.finos.legend.engine.plan.execution.result.ConstantResult)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Result (org.finos.legend.engine.plan.execution.result.Result)2 Serializer (org.finos.legend.engine.plan.execution.result.serialization.Serializer)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Span (io.opentracing.Span)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)1 PureGrammarParser (org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser)1 PlanExecutor (org.finos.legend.engine.plan.execution.PlanExecutor)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