Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions isthmus/src/test/java/io/substrait/isthmus/PlanTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import io.substrait.type.Type;
import io.substrait.type.TypeCreator;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelRoot;
import org.apache.calcite.rel.type.RelDataType;
Expand All @@ -45,11 +47,28 @@ public static String asString(String resource) throws IOException {
return Resources.toString(Resources.getResource(resource), Charsets.UTF_8);
}

public static List<String> tpchSchemaCreateStatements() throws IOException {
String[] values = asString("tpch/schema.sql").split(";");
return Arrays.stream(values)
.filter(t -> !t.trim().isBlank())
.collect(java.util.stream.Collectors.toList());
/** Holder class to load TPC-H create statements only once on first access. */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea ✨

private static final class TpchCreateStatementsHolder {
static final List<String> createStatements;

static {
final String[] values;
try {
values = asString("tpch/schema.sql").split(";");
} catch (IOException e) {
throw new UncheckedIOException(e);
}

createStatements =
Arrays.stream(values)
.map(String::trim)
.filter(s -> !s.isBlank())
.collect(Collectors.toList());
}
}

public static List<String> tpchSchemaCreateStatements() {
return TpchCreateStatementsHolder.createStatements;
}

protected Plan assertProtoPlanRoundrip(String query) throws IOException, SqlParseException {
Expand Down

This file was deleted.

61 changes: 61 additions & 0 deletions isthmus/src/test/java/io/substrait/isthmus/TpcdsQueryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.substrait.isthmus;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import io.substrait.plan.Plan.Root;
import io.substrait.plan.ProtoPlanConverter;
import io.substrait.proto.Plan;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import org.apache.calcite.adapter.tpcds.TpcdsSchema;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelRoot;
import org.apache.calcite.sql.parser.SqlParseException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/** TPC-DS test to convert SQL to Substrait and then convert those plans back to SQL. */
public class TpcdsQueryTest extends PlanTestBase {
private static final Set<Integer> toSubstraitExclusions = Set.of(9, 27, 36, 70, 86, 98);
private static final Set<Integer> fromSubstraitExclusions = Set.of(6, 8, 67);

private final TpcdsSchema schema = new TpcdsSchema(1.0);
private final ProtoPlanConverter planConverter = new ProtoPlanConverter();

static IntStream testCases() {
return IntStream.rangeClosed(1, 99).filter(n -> !toSubstraitExclusions.contains(n));
}

/**
* Note that this test does not currently validate the correctness of the Substrait plan; just
* that the SQL can be converted to Substrait and back to SQL without error.
*/
@ParameterizedTest
@MethodSource("testCases")
public void testQuery(int query) throws IOException {
String inputSql = asString(String.format("tpcds/queries/%02d.sql", query));

Plan plan = assertDoesNotThrow(() -> toSubstraitPlan(inputSql), "SQL to Substrait");

if (!fromSubstraitExclusions.contains(query)) {
assertDoesNotThrow(() -> toSql(plan), "Substrait to SQL");
}
}

private Plan toSubstraitPlan(String sql) throws SqlParseException, IOException {
return new SqlToSubstrait().execute(sql, "tpcds", schema);
}

private String toSql(Plan plan) {
List<Root> roots = planConverter.from(plan).getRoots();
assertEquals(1, roots.size(), "number of roots");

Root root = roots.get(0);
RelRoot relRoot = new SubstraitToCalcite(extensions, typeFactory).convert(root);
RelNode project = relRoot.project(true);
return SubstraitToSql.toSql(project);
}
}

This file was deleted.

59 changes: 59 additions & 0 deletions isthmus/src/test/java/io/substrait/isthmus/TpchQueryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.substrait.isthmus;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import io.substrait.plan.Plan.Root;
import io.substrait.plan.ProtoPlanConverter;
import io.substrait.proto.Plan;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelRoot;
import org.apache.calcite.sql.parser.SqlParseException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/** TPC-H test to convert SQL to Substrait and then convert those plans back to SQL. */
public class TpchQueryTest extends PlanTestBase {
private static final Set<Integer> fromSubstraitExclusions = Set.of(17);

private final ProtoPlanConverter planConverter = new ProtoPlanConverter();

static IntStream testCases() {
return IntStream.rangeClosed(1, 22);
}

/**
* Note that this test does not currently validate the correctness of the Substrait plan; just
* that the SQL can be converted to Substrait and back to SQL without error.
*/
@ParameterizedTest
@MethodSource("testCases")
public void testQuery(int query) throws IOException {
String inputSql = asString(String.format("tpch/queries/%02d.sql", query));

Plan plan = assertDoesNotThrow(() -> toSubstraitPlan(inputSql), "SQL to Substrait");

if (!fromSubstraitExclusions.contains(query)) {
assertDoesNotThrow(() -> toSql(plan), "Substrait to SQL");
}
}

private Plan toSubstraitPlan(String sql) throws SqlParseException {
List<String> createStatements = tpchSchemaCreateStatements();
return new SqlToSubstrait().execute(sql, createStatements);
}

private String toSql(Plan plan) {
List<Root> roots = planConverter.from(plan).getRoots();
assertEquals(1, roots.size(), "number of roots");

Root root = roots.get(0);
RelRoot relRoot = new SubstraitToCalcite(extensions, typeFactory).convert(root);
RelNode project = relRoot.project(true);
return SubstraitToSql.toSql(project);
}
}
Loading