use of org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport in project legend-pure by finos.
the class Test_PureTestSuite method getClassLoaderExecutionSupport.
public static CompiledExecutionSupport getClassLoaderExecutionSupport() {
MutableList<CodeRepository> codeRepos = Lists.mutable.of(CodeRepository.newPlatformCodeRepository()).withAll(CodeRepositoryProviderHelper.findCodeRepositories());
ClassLoader classLoader = Test_PureTestSuite.class.getClassLoader();
return new CompiledExecutionSupport(new JavaCompilerState(null, classLoader), new CompiledProcessorSupport(classLoader, MetadataLazy.fromClassLoader(classLoader), Sets.mutable.empty()), null, new PureCodeStorage(null, new ClassLoaderCodeStorage(classLoader, codeRepos)), null, null, new ConsoleCompiled(), new FunctionCache(), new ClassCache(classLoader), null, Sets.mutable.empty());
}
use of org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport in project legend-pure by finos.
the class CompiledSupport method dynamicallyEvaluateValueSpecification.
public static Object dynamicallyEvaluateValueSpecification(final CoreInstance valueSpecification, PureMap lambdaOpenVariablesMap, ExecutionSupport es) {
MemoryFileManager fileManager = ((CompiledExecutionSupport) es).getMemoryFileManager();
ClassLoader globalClassLoader = ((CompiledExecutionSupport) es).getClassLoader();
final CompiledProcessorSupport compiledSupport = new CompiledProcessorSupport(globalClassLoader, ((CompiledExecutionSupport) es).getMetadata(), ((CompiledExecutionSupport) es).getExtraSupportedTypes());
final ProcessorContext processorContext = new ProcessorContext(compiledSupport);
// Don't do anything if the ValueSpecification is already resolved ----------------
if (Instance.instanceOf(valueSpecification, M3Paths.InstanceValue, processorContext.getSupport())) {
ListIterable<? extends CoreInstance> l = valueSpecification.getValueForMetaPropertyToMany(M3Properties.values);
if (l.noneSatisfy(instance -> Instance.instanceOf(instance, M3Paths.ValueSpecification, processorContext.getSupport()) || Instance.instanceOf(instance, M3Paths.LambdaFunction, processorContext.getSupport()))) {
ListIterable<Object> result = l.collect(instance -> instance instanceof ValCoreInstance ? ((ValCoreInstance) instance).getValue() : instance);
return result.size() == 1 ? result.get(0) : result;
}
}
// ---------------------------------------------------------------------------------
processorContext.setInLineAllLambda(true);
String processed = ValueSpecificationProcessor.processValueSpecification(valueSpecification, true, processorContext);
String returnType = TypeProcessor.typeToJavaObjectWithMul(valueSpecification.getValueForMetaPropertyToOne(M3Properties.genericType), valueSpecification.getValueForMetaPropertyToOne(M3Properties.multiplicity), false, compiledSupport);
String name = "DynaClass";
RichIterable<Pair<String, CoreInstance>> values = lambdaOpenVariablesMap.getMap().keyValuesView();
final MutableMap<String, Object> openVars = Maps.mutable.of();
String _class = JavaSourceCodeGenerator.imports + "\npublic class " + name + "{\n" + " public static " + returnType + " doProcess(final MapIterable<String, Object> vars, final MutableMap<String, Object> valMap, final IntObjectMap<CoreInstance> localLambdas, final ExecutionSupport es){\n" + values.collect(new Function<Pair<String, CoreInstance>, String>() {
@Override
public String valueOf(Pair<String, CoreInstance> pair) {
final String name = pair.getOne();
CoreInstance valuesCoreInstance = pair.getTwo();
ListIterable<? extends CoreInstance> values = valuesCoreInstance.getValueForMetaPropertyToMany(M3Properties.values).select(coreInstance -> !Instance.instanceOf(coreInstance, "meta::pure::executionPlan::PlanVarPlaceHolder", compiledSupport) && !Instance.instanceOf(coreInstance, "meta::pure::executionPlan::PlanVariablePlaceHolder", compiledSupport));
String type = null;
openVars.put(name, valuesCoreInstance);
if (values.isEmpty()) {
MutableList<CoreInstance> vars = FastList.newList();
collectVars(valueSpecification, vars, compiledSupport);
CoreInstance found = vars.detect(coreInstance -> coreInstance.getValueForMetaPropertyToOne("name").getName().equals(name));
if (found != null) {
type = TypeProcessor.typeToJavaObjectSingle(found.getValueForMetaPropertyToOne(M3Properties.genericType), false, compiledSupport);
return " final " + type + " _" + name + " = null;";
}
return "";
} else {
type = TypeProcessor.pureRawTypeToJava(compiledSupport.getClassifier(values.getFirst()), false, compiledSupport);
final String listImpl = JavaPackageAndImportBuilder.buildImplClassReferenceFromUserPath(M3Paths.List);
return (values.size() == 1) ? (" final " + type + " _" + name + " = (" + type + ")((" + listImpl + ")vars.get(\"" + name + "\"))._values.getFirst();") : (" final RichIterable<" + type + "> _" + name + " = ((" + listImpl + ")vars.get(\"" + name + "\"))._values;");
}
}
}).makeString("\n") + " return " + processed + ";\n" + " }\n" + "}\n";
String javaPackage = JavaPackageAndImportBuilder.buildPackageForPackageableElement(valueSpecification);
ListIterable<StringJavaSource> javaClasses = Lists.immutable.with(StringJavaSource.newStringJavaSource(javaPackage, name, _class));
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
MemoryFileManager manager = new MemoryFileManager(compiler, fileManager, null);
try {
PureJavaCompiler.compile(compiler, javaClasses, manager);
} catch (Exception e) {
StringBuilder message = new StringBuilder("Error dynamically evaluating value specification");
SourceInformation valueSpecSourceInfo = valueSpecification.getSourceInformation();
if (valueSpecSourceInfo != null) {
valueSpecSourceInfo.appendMessage(message.append(" (from ")).append(')');
}
message.append("; error compiling generated Java code:\n").append(_class);
throw new RuntimeException(message.toString(), e);
}
ClassLoader cl = new MemoryClassLoader(manager, globalClassLoader);
try {
Class<?> realClass = cl.loadClass(javaPackage + "." + name);
return realClass.getMethod("doProcess", MapIterable.class, MutableMap.class, IntObjectMap.class, ExecutionSupport.class).invoke(null, openVars, processorContext.getObjectToPassToDynamicallyGeneratedCode(), processorContext.getLocalLambdas(), es);
} catch (Exception e) {
StringBuilder message = new StringBuilder("Error dynamically evaluating value specification");
SourceInformation valueSpecSourceInfo = valueSpecification.getSourceInformation();
if (valueSpecSourceInfo != null) {
valueSpecSourceInfo.appendMessage(message.append(" (from ")).append(')');
}
String errorMessage = e.getMessage();
if (errorMessage != null) {
message.append(": ").append(errorMessage);
}
throw new RuntimeException(message.toString(), e);
}
}
use of org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport in project legend-pure by finos.
the class CompiledSupport method dynamicallyBuildLambdaFunction.
public static Object dynamicallyBuildLambdaFunction(CoreInstance lambdaFunction, ExecutionSupport es) {
ClassLoader globalClassLoader = ((CompiledExecutionSupport) es).getClassLoader();
CompiledProcessorSupport compiledSupport = new CompiledProcessorSupport(globalClassLoader, ((CompiledExecutionSupport) es).getMetadata(), ((CompiledExecutionSupport) es).getExtraSupportedTypes());
ProcessorContext processorContext = new ProcessorContext(compiledSupport);
processorContext.setInLineAllLambda(true);
String name = "DynamicLambdaGeneration";
String _class = JavaSourceCodeGenerator.imports + "\nimport " + JavaPackageAndImportBuilder.rootPackage() + ".*;\npublic class " + name + "{" + " public static PureCompiledLambda build(final MutableMap<String, Object> valMap, final IntObjectMap<CoreInstance> localLambdas){\n" + "return " + ValueSpecificationProcessor.processLambda(null, lambdaFunction, compiledSupport, processorContext) + ";" + "}" + "}";
MemoryFileManager fileManager = ((CompiledExecutionSupport) es).getMemoryFileManager();
MutableList<StringJavaSource> javaClasses = FastList.newList();
javaClasses.add(StringJavaSource.newStringJavaSource("temp", name, _class));
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
MemoryFileManager manager = new MemoryFileManager(compiler, fileManager, null);
try {
PureJavaCompiler.compile(compiler, javaClasses, manager);
} catch (PureJavaCompileException e) {
throw new RuntimeException(e);
}
ClassLoader cl = new MemoryClassLoader(manager, globalClassLoader);
try {
Class<?> realClass = cl.loadClass("temp" + "." + name);
return realClass.getMethod("build", MutableMap.class, IntObjectMap.class).invoke(null, processorContext.getObjectToPassToDynamicallyGeneratedCode(), processorContext.getLocalLambdas());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport in project legend-pure by finos.
the class RelationalNativeImplementation method executeInDb.
public static ResultSet executeInDb(String sql, DatabaseConnection pureConnection, long queryTimeoutInSeconds, long fetchSize, SourceInformation si, Function0<ResultSet> resultSetBuilder, Function0<SQLNull> sqlNullBuilder, Function0<Row> rowBuilder, Function0<DataSource> datasourceBuilder, ExecutionSupport es) {
// new Root_meta_relational_metamodel_execute_ResultSet_Impl("OK");
ResultSet pureResult = resultSetBuilder.value();
Connection connection = null;
ConnectionWithDataSourceInfo connectionWithDataSourceInfo = null;
try {
long startRequestConnection = System.nanoTime();
connectionWithDataSourceInfo = connectionManagerHandler.getConnectionWithDataSourceInfo(pureConnection, ((CompiledExecutionSupport) es).getProcessorSupport());
connection = connectionWithDataSourceInfo.getConnection();
if (!PureConnectionUtils.isPureConnectionType(pureConnection, "Hive")) {
connection.setAutoCommit(true);
}
pureResult._connectionAcquisitionTimeInNanoSecond(System.nanoTime() - startRequestConnection);
// new Root_meta_relational_metamodel_SQLNull_Impl("SQLNull");
SQLNull sqlNull = sqlNullBuilder.value();
String tz = pureConnection._timeZone() == null ? "GMT" : pureConnection._timeZone();
String URL = connectionManagerHandler.getPotentialDebug(pureConnection, connection);
if (URL != null) {
pureResult = pureResult._executionPlanInformation(URL);
}
ResultSetRowIterableProvider.ResultSetIterableContainer resultContainer = ResultSetRowIterableProvider.createResultSetIterator(pureConnection, connection, sql, RelationalExecutionProperties.getMaxRows(), RelationalExecutionProperties.shouldThrowIfMaxRowsExceeded(), (int) queryTimeoutInSeconds, (int) fetchSize, new CreateRowFunction(pureResult, rowBuilder), sqlNull, tz, si, (CompiledExecutionSupport) es, connectionWithDataSourceInfo);
pureResult._columnNamesAddAll(resultContainer.columnNames);
pureResult._executionTimeInNanoSecond(resultContainer.queryTimeInNanos);
pureResult._rows(resultContainer.rowIterable);
String dbHost = connectionWithDataSourceInfo.getDataSource().getHost();
Integer dbPort = connectionWithDataSourceInfo.getDataSource().getPort();
String dbName = connectionWithDataSourceInfo.getDataSource().getDataSourceName();
String serverPrincipal = connectionWithDataSourceInfo.getDataSource().getServerPrincipal();
if (pureConnection._type() != null && dbHost != null && dbPort != null && dbName != null) {
// new Root_meta_relational_runtime_DataSource_Impl("ID");
DataSource ds = datasourceBuilder.value();
ds._type(pureConnection._type());
ds._port(dbPort.longValue());
ds._host(dbHost);
ds._name(dbName);
if (serverPrincipal != null)
ds._serverPrincipal(serverPrincipal);
pureResult._dataSource(ds);
}
return pureResult;
} catch (SQLException e) {
throw new PureExecutionException(si, SQLExceptionHandler.buildExceptionString(e, connection), e);
}
}
use of org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport in project legend-engine by finos.
the class Test_ExternalFormat_UsingPureClient method suite.
public static Test suite() {
return PureTestHelper.wrapSuite(() -> PureTestHelper.initClientVersionIfNotAlreadySet("vX_X_X"), () -> {
CompiledExecutionSupport executionSupport = PureTestHelper.getClassLoaderExecutionSupport();
TestSuite suite = new TestSuite();
// NOTE: temporarily ignore these tests until we bring extensions back into Legend
suite.addTest(PureTestHelper.buildSuite(TestCollection.collectTests("meta::external::format", executionSupport.getProcessorSupport(), ci -> !ci.getName().equals("transform_testClassWithMapToProtoBuf__Boolean_1_") && PureTestHelper.satisfiesConditions(ci, executionSupport.getProcessorSupport())), executionSupport));
suite.addTest(PureTestHelper.buildSuite(TestCollection.collectTests("meta::external::language", executionSupport.getProcessorSupport(), ci -> PureTestHelper.satisfiesConditions(ci, executionSupport.getProcessorSupport())), executionSupport));
suite.addTest(PureTestHelper.buildSuite(TestCollection.collectTests("meta::external::shared", executionSupport.getProcessorSupport(), ci -> PureTestHelper.satisfiesConditions(ci, executionSupport.getProcessorSupport())), executionSupport));
suite.addTest(PureTestHelper.buildSuite(TestCollection.collectTests("meta::external::store", executionSupport.getProcessorSupport(), ci -> PureTestHelper.satisfiesConditions(ci, executionSupport.getProcessorSupport())), executionSupport));
return suite;
});
}
Aggregations