Skip to content

Commit

Permalink
clean up ImportExportAnalyzer
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Broecheler <mbroecheler@users.noreply.github.com>
  • Loading branch information
mbroecheler committed Dec 26, 2024
1 parent f8800f9 commit 1a99e16
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.URI;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Optional;
import lombok.Getter;
import lombok.NonNull;

Expand Down Expand Up @@ -100,7 +101,11 @@ public ErrorCollector withLocation(ErrorLocation location) {
}

public ErrorCollector withScript(Path file, String scriptContent) {
return withScript(file.getFileName().toString(),scriptContent);
return withScript(Optional.of(file),scriptContent);
}

public ErrorCollector withScript(Optional<Path> file, String scriptContent) {
return withScript(file.map(p -> p.getFileName().toString()).orElse("undefined"),scriptContent);
}

public ErrorCollector withScript(String filename, String scriptContent) {
Expand Down
2 changes: 2 additions & 0 deletions sqrl-planner/src/main/java/com/datasqrl/MainScriptImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public Optional<Path> getPath() {
.map(NamePath::of)
.flatMap(resourceResolver::resolveFile);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public class SqlScriptPlanner {


public void plan(MainScript mainScript, SqrlEnvironment sqrlEnv) {
ErrorCollector scriptErrors = errorCollector.withScript(mainScript.getPath().orElse(Path.of("undefined")),
mainScript.getContent());
ErrorCollector scriptErrors = errorCollector.withScript(mainScript.getPath(), mainScript.getContent());

List<ParsedObject<SQLStatement>> statements = sqrlParser.parseScript(mainScript.getContent(), scriptErrors);

Expand Down Expand Up @@ -146,8 +145,7 @@ private void addImport(NamespaceObject table, Optional<String> alias, SqrlEnviro
Name tableName = alias.map(Name::system).orElse(flinkTable.getName());
String tableSql = flinkTable.getFlinkSQL();
//TODO: replace name and use SqlIdentifier for proper escaping
sqrlEnv.withErrors(errorCollector.withScript(flinkTable.getFlinkSqlFile(),
tableSql));
sqrlEnv.withErrors(errorCollector.withScript(flinkTable.getFlinkSqlFile(), tableSql));
tableSql = SqlScriptStatementSplitter.formatEndOfSqlFile(tableSql);
Preconditions.checkArgument(tableSql.endsWith(";\n"));
if (flinkTable.getSchema().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ public class ImportExportAnalyzer {
private final MainScriptImpl mainScript;

@SneakyThrows
public Set<NamePath> analyze(Path sqrlScript, ErrorCollector errors) {
ErrorCollector scriptErrors = errors.withScript(mainScript.getPath().orElse(Path.of("undefined")),
mainScript.getContent());

return sqrlParser.parseScript(mainScript.getContent(), scriptErrors).stream()
public Set<NamePath> analyze(String scriptContent, ErrorCollector errors) {
return sqrlParser.parseScript(scriptContent, errors).stream()
.map(ParsedObject::get)
.flatMap(stmt -> {
NamePath path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.datasqrl.error.ErrorCollector;
import com.datasqrl.packager.Preprocessors.PreprocessorsContext;
import com.datasqrl.packager.repository.Repository;
import com.datasqrl.plan.MainScript;
import com.datasqrl.util.FileUtil;
import com.datasqrl.util.ServiceLoaderDiscovery;
import com.datasqrl.util.SqrlObjectMapper;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class Packager {
private final BuildPath buildDir;
private final Preprocessors preprocessors;
private final ImportExportAnalyzer analyzer;
private final MainScript mainScript;

public void preprocess(ErrorCollector errors) {
errors.checkFatal(
Expand Down Expand Up @@ -100,15 +102,16 @@ private void inferDependencies(ErrorCollector errors) {
String mainScriptPath = config.getScriptConfig().getMainScript()
.orElseThrow(() -> new RuntimeException("No main script specified"));

Set<NamePath> unresolvedDeps = analyzer.analyze(rootDir.getRootDir().resolve(mainScriptPath), errors);
ErrorCollector scriptErrors = errors.withScript(mainScript.getPath(), mainScript.getContent());
Set<NamePath> unresolvedDeps = analyzer.analyze(mainScript.getContent(), scriptErrors);

List<Dependency> dependencies = unresolvedDeps.stream()
.flatMap(dep -> {
try {
return repository.resolveDependency(dep.toString())
.stream();
} catch (Exception e) {
//suppress any exception
//suppress any exception, those will be thrown when we actually plan the script
return Optional.<Dependency>empty()
.stream();
}
Expand Down

0 comments on commit 1a99e16

Please sign in to comment.