|
| 1 | +package com.linkedin.hoptimator.jdbc; |
| 2 | + |
| 3 | +import com.linkedin.hoptimator.jdbc.HoptimatorDriver; |
| 4 | +import com.linkedin.hoptimator.util.ConnectionService; |
| 5 | +import com.linkedin.hoptimator.util.DeploymentService; |
| 6 | +import com.linkedin.hoptimator.util.Sink; |
| 7 | +import com.linkedin.hoptimator.util.planner.PipelineRel; |
| 8 | + |
| 9 | +import net.hydromatic.quidem.AbstractCommand; |
| 10 | +import net.hydromatic.quidem.Command; |
| 11 | +import net.hydromatic.quidem.CommandHandler; |
| 12 | +import net.hydromatic.quidem.Quidem; |
| 13 | + |
| 14 | +import org.apache.calcite.rel.RelNode; |
| 15 | +import org.apache.calcite.jdbc.CalciteConnection; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.Assertions; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileReader; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.PrintWriter; |
| 25 | +import java.io.Reader; |
| 26 | +import java.io.Writer; |
| 27 | +import java.net.URI; |
| 28 | +import java.net.URISyntaxException; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.stream.Collectors; |
| 37 | +import java.sql.Connection; |
| 38 | +import java.sql.DriverManager; |
| 39 | + |
| 40 | +public abstract class QuidemTestBase { |
| 41 | + |
| 42 | + protected void run(String resourceName) throws IOException, URISyntaxException { |
| 43 | + run(Thread.currentThread().getContextClassLoader().getResource(resourceName).toURI()); |
| 44 | + } |
| 45 | + |
| 46 | + protected void run(URI resource) throws IOException { |
| 47 | + File in = new File(resource); |
| 48 | + File out = File.createTempFile(in.getName(), ".out"); |
| 49 | + try (Reader r = new FileReader(in); |
| 50 | + Writer w = new PrintWriter(out)) { |
| 51 | + Quidem.Config config = Quidem.configBuilder() |
| 52 | + .withReader(r) |
| 53 | + .withWriter(w) |
| 54 | + .withConnectionFactory((x, y) -> DriverManager.getConnection("jdbc:hoptimator://" + x)) |
| 55 | + .withCommandHandler(new CustomCommandHandler()) |
| 56 | + .build(); |
| 57 | + new Quidem(config).execute(); |
| 58 | + } |
| 59 | + List<String> input = Files.readAllLines(in.toPath(), StandardCharsets.UTF_8); |
| 60 | + List<String> output = Files.readAllLines(out.toPath(), StandardCharsets.UTF_8); |
| 61 | + Assertions.assertTrue(!input.isEmpty(), "input script is empty"); |
| 62 | + Assertions.assertTrue(!output.isEmpty(), "script output is empty"); |
| 63 | + for (String line : output) { |
| 64 | + System.out.println(line); |
| 65 | + } |
| 66 | + Assertions.assertIterableEquals(input, output); |
| 67 | + } |
| 68 | + |
| 69 | + private static class CustomCommandHandler implements CommandHandler { |
| 70 | + @Override |
| 71 | + public Command parseCommand(List<String> lines, List<String> content, final String line) { |
| 72 | + List<String> copy = new ArrayList<>(); |
| 73 | + copy.addAll(lines); |
| 74 | + if (line.startsWith("spec")) { |
| 75 | + return new AbstractCommand() { |
| 76 | + @Override |
| 77 | + public void execute(Context context, boolean execute) throws Exception { |
| 78 | + if (execute) { |
| 79 | + if (!(context.connection() instanceof CalciteConnection)) { |
| 80 | + throw new IllegalArgumentException("This connection doesn't support `!specify`."); |
| 81 | + } |
| 82 | + String sql = context.previousSqlCommand().sql; |
| 83 | + CalciteConnection conn = (CalciteConnection) context.connection(); |
| 84 | + RelNode rel = HoptimatorDriver.convert(conn.createPrepareContext(), sql).root.rel; |
| 85 | + String specs = DeploymentService.plan(rel).pipeline().specify().stream() |
| 86 | + .collect(Collectors.joining("\n---\n")); |
| 87 | + context.echo(Collections.singletonList(specs)); |
| 88 | + } else { |
| 89 | + context.echo(content); |
| 90 | + } |
| 91 | + context.echo(copy); |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + return null; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments