-
Notifications
You must be signed in to change notification settings - Fork 94
test: add PostgreSQL TPC-H integration tests #855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bestbeforetoday
wants to merge
5
commits into
substrait-io:main
Choose a base branch
from
bestbeforetoday:tpch-reference-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a288923
feat(isthmus): add PostgreSQL TPC-H integration testing
nielspardon 3f317ba
feat: use tpchgen-cli to generate test data
bestbeforetoday 3f36dca
fix: typo on SuccessfulExitCheckStrategy JavaDoc
bestbeforetoday 73eddfd
test: better PostgreSqlIntegrationTest failure reporting
bestbeforetoday 1ea681b
test: assert expected TPC-H integration test fails
bestbeforetoday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
isthmus/src/test/java/io/substrait/isthmus/integration/PostgreSqlIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| package io.substrait.isthmus.integration; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.substrait.isthmus.ConverterProvider; | ||
| import io.substrait.isthmus.PlanTestBase; | ||
| import io.substrait.isthmus.SqlToSubstrait; | ||
| import io.substrait.isthmus.SubstraitToSql; | ||
| import io.substrait.plan.Plan; | ||
| import java.io.IOException; | ||
| import java.sql.Connection; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import java.util.stream.Stream; | ||
| import org.apache.calcite.sql.dialect.PostgresqlSqlDialect; | ||
| import org.apache.calcite.sql.parser.SqlParseException; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.testcontainers.containers.BindMode; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.JdbcDatabaseContainer.NoDriverFoundException; | ||
| import org.testcontainers.junit.jupiter.Container; | ||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||
| import org.testcontainers.postgresql.PostgreSQLContainer; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| @Tag("integration") | ||
| @Testcontainers | ||
| class PostgreSqlIntegrationTest extends PlanTestBase { | ||
| private static final Logger LOG = LoggerFactory.getLogger(PostgreSqlIntegrationTest.class); | ||
|
|
||
| // TODO: These queries produce different results when generated from Substrait | ||
| private static final List<Integer> EXPECTED_FAILURES = List.of(8, 14); | ||
|
|
||
| private static final DockerImageName UV_IMAGE = | ||
| DockerImageName.parse("ghcr.io/astral-sh/uv:python3.14-trixie-slim"); | ||
| private static final DockerImageName POSTGRES_IMAGE = DockerImageName.parse("postgres:18"); | ||
|
|
||
| private static final String TPCH_DATA_HOST_PATH = "tpch/data"; | ||
| private static final String TPCH_DATA_CONTAINER_PATH = "/tmp/tpc-h"; | ||
| private static final String TPCH_INIT_HOST_PATH = "tpch/postgresql/tpch_init.sql"; | ||
|
|
||
| private static final List<String> TPCHGEN_ARGS = | ||
| List.of( | ||
| "--scale-factor", "0.01", "--format", "csv", "--output-dir", TPCH_DATA_CONTAINER_PATH); | ||
|
|
||
| private static final List<String> TPCHGEN_CMD = | ||
| Stream.concat( | ||
| Stream.of("uvx", "--from", "tpchgen-cli == 2.*", "tpchgen-cli"), | ||
| TPCHGEN_ARGS.stream()) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| /** Create TPC-H test data. */ | ||
| @Container | ||
| @SuppressWarnings("resource") | ||
| private static final GenericContainer<?> tpchgen = | ||
| new GenericContainer<>(UV_IMAGE) | ||
| .withClasspathResourceMapping( | ||
| TPCH_DATA_HOST_PATH, TPCH_DATA_CONTAINER_PATH, BindMode.READ_WRITE) | ||
| .withCommand(TPCHGEN_CMD.toArray(new String[0])) | ||
| .withStartupCheckStrategy(new SuccessfulExitCheckStrategy()); | ||
|
|
||
| /** PostgreSQL instance shared across all test methods in this class. */ | ||
| @Container | ||
| @SuppressWarnings("resource") | ||
| private static final PostgreSQLContainer postgres = | ||
| new PostgreSQLContainer(POSTGRES_IMAGE) | ||
| .dependsOn(tpchgen) | ||
| .withClasspathResourceMapping( | ||
| TPCH_DATA_HOST_PATH, TPCH_DATA_CONTAINER_PATH, BindMode.READ_ONLY) | ||
| .withClasspathResourceMapping( | ||
| TPCH_INIT_HOST_PATH, "/docker-entrypoint-initdb.d/tpch_init.sql", BindMode.READ_ONLY); | ||
|
|
||
| private static final String COMPARE_RESULTS_SQL_TEMPLATE = | ||
| """ | ||
| WITH expected AS (%s), | ||
| actual AS (%s) | ||
| SELECT count(*) FROM ( | ||
| SELECT * FROM | ||
| (SELECT * FROM expected EXCEPT SELECT * FROM actual) | ||
| UNION (SELECT * FROM actual EXCEPT SELECT * FROM expected) | ||
| ) | ||
| """; | ||
|
|
||
| static IntStream tpcHTestCases() { | ||
| return IntStream.rangeClosed(1, 22); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("tpcHTestCases") | ||
| void testTpcH(final int queryNo) | ||
| throws NoDriverFoundException, SQLException, IOException, SqlParseException { | ||
|
|
||
| final String inputSql = asString(String.format("tpch/queries/%02d.sql", queryNo)); | ||
| final SqlToSubstrait sqlToSubstrait = new SqlToSubstrait(); | ||
| final Plan plan = sqlToSubstrait.convert(inputSql, TPCH_CATALOG); | ||
|
|
||
| final ConverterProvider provider = new ConverterProvider(extensions); | ||
| final SubstraitToSql substraitToSql = new SubstraitToSql(provider); | ||
|
|
||
| final String generatedSql = substraitToSql.convert(plan, PostgresqlSqlDialect.DEFAULT).get(0); | ||
|
|
||
| final String referenceSql = asString(String.format("tpch/postgresql/%02d.sql", queryNo)); | ||
|
|
||
| LOG.atDebug().log("Reference SQL:\n%s", referenceSql); | ||
| LOG.atDebug().log("Generated SQL:\n%s", generatedSql); | ||
|
|
||
| final String compareSql = | ||
| String.format(COMPARE_RESULTS_SQL_TEMPLATE, referenceSql, generatedSql); | ||
|
|
||
| try (Connection conn = postgres.createConnection(""); | ||
| Statement stmt = conn.createStatement(); | ||
| ResultSet result = stmt.executeQuery(compareSql); ) { | ||
| // we expect exactly one row | ||
| assertTrue(result.next()); | ||
|
|
||
| // the count should be zero if both the reference and generated SQL produce the same results | ||
| int differenceCount = result.getInt(1); | ||
| if (EXPECTED_FAILURES.contains(queryNo)) { | ||
| assertNotEquals( | ||
| 0, | ||
| differenceCount, | ||
| String.format( | ||
| "Expected query %d to fail but it matched the reference results", queryNo)); | ||
| } else { | ||
| assertEquals( | ||
| 0, | ||
| differenceCount, | ||
| String.format( | ||
| "Reference and generated SQL produce %d different results.\n\nReference SQL:\n%s\n\nGenerated SQL:\n%s", | ||
| differenceCount, referenceSql, generatedSql)); | ||
| } | ||
|
|
||
| // we expect exactly one row | ||
| assertFalse(result.next()); | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
isthmus/src/test/java/io/substrait/isthmus/integration/SuccessfulExitCheckStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package io.substrait.isthmus.integration; | ||
|
|
||
| import com.github.dockerjava.api.DockerClient; | ||
| import com.github.dockerjava.api.command.InspectContainerResponse; | ||
| import org.testcontainers.containers.startupcheck.StartupCheckStrategy; | ||
| import org.testcontainers.utility.DockerStatus; | ||
|
|
||
| /** | ||
| * A {@link StartupCheckStrategy} that checks if the container has exited with a successful exit | ||
| * code. This allows use of a container that is launched purely to execute a command prior to | ||
| * startup of dependent containers. | ||
| */ | ||
| public class SuccessfulExitCheckStrategy extends StartupCheckStrategy { | ||
| @Override | ||
| public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { | ||
| InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId); | ||
|
|
||
| if (!DockerStatus.isContainerStopped(state)) { | ||
| return StartupStatus.NOT_YET_KNOWN; | ||
| } | ||
|
|
||
| if (!DockerStatus.isContainerExitCodeSuccess(state)) { | ||
| return StartupStatus.FAILED; | ||
| } | ||
|
|
||
| return StartupStatus.SUCCESSFUL; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Root logger level - set to INFO for test visibility | ||
| .level=INFO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # TPC-H test data is generated to this directory. | ||
|
|
||
| *.tbl | ||
| *.csv | ||
| *.parquet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| select | ||
| "L_RETURNFLAG", | ||
| "L_LINESTATUS", | ||
| sum("L_QUANTITY") as "SUM_QTY", | ||
| sum("L_EXTENDEDPRICE") as "SUM_BASE_PRICE", | ||
| sum("L_EXTENDEDPRICE" * (1 - "L_DISCOUNT")) as "SUM_DISC_PRICE", | ||
| sum("L_EXTENDEDPRICE" * (1 - "L_DISCOUNT") * (1 + "L_TAX")) as "SUM_CHARGE", | ||
| avg("L_QUANTITY") as "AVG_QTY", | ||
| avg("L_EXTENDEDPRICE") as "AVG_PRICE", | ||
| avg("L_DISCOUNT") as "AVG_DISC", | ||
| count(*) as "COUNT_ORDER" | ||
| from | ||
| "LINEITEM" | ||
| where | ||
| "L_SHIPDATE" <= date '1998-12-01' - interval '120 days' | ||
| group by | ||
| "L_RETURNFLAG", | ||
| "L_LINESTATUS" | ||
|
|
||
| order by | ||
| "L_RETURNFLAG", | ||
| "L_LINESTATUS" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| select | ||
| "S"."S_ACCTBAL", | ||
| "S"."S_NAME", | ||
| "N"."N_NAME", | ||
| "P"."P_PARTKEY", | ||
| "P"."P_MFGR", | ||
| "S"."S_ADDRESS", | ||
| "S"."S_PHONE", | ||
| "S"."S_COMMENT" | ||
| from | ||
| "PART" "P", | ||
| "SUPPLIER" "S", | ||
| "PARTSUPP" "PS", | ||
| "NATION" "N", | ||
| "REGION" "R" | ||
| where | ||
| "P"."P_PARTKEY" = "PS"."PS_PARTKEY" | ||
| and "S"."S_SUPPKEY" = "PS"."PS_SUPPKEY" | ||
| and "P"."P_SIZE" = 41 | ||
| and "P"."P_TYPE" like '%NICKEL' | ||
| and "S"."S_NATIONKEY" = "N"."N_NATIONKEY" | ||
| and "N"."N_REGIONKEY" = "R"."R_REGIONKEY" | ||
| and "R"."R_NAME" = 'EUROPE' | ||
| and "PS"."PS_SUPPLYCOST" = ( | ||
|
|
||
| select | ||
| min("PS"."PS_SUPPLYCOST") | ||
|
|
||
| from | ||
| "PARTSUPP" "PS", | ||
| "SUPPLIER" "S", | ||
| "NATION" "N", | ||
| "REGION" "R" | ||
| where | ||
| "P"."P_PARTKEY" = "PS"."PS_PARTKEY" | ||
| and "S"."S_SUPPKEY" = "PS"."PS_SUPPKEY" | ||
| and "S"."S_NATIONKEY" = "N"."N_NATIONKEY" | ||
| and "N"."N_REGIONKEY" = "R"."R_REGIONKEY" | ||
| and "R"."R_NAME" = 'EUROPE' | ||
| ) | ||
|
|
||
| order by | ||
| "S"."S_ACCTBAL" desc, | ||
| "N"."N_NAME", | ||
| "S"."S_NAME", | ||
| "P"."P_PARTKEY" | ||
| limit 100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| select | ||
| "L"."L_ORDERKEY", | ||
| sum("L"."L_EXTENDEDPRICE" * (1 - "L"."L_DISCOUNT")) as "REVENUE", | ||
| "O"."O_ORDERDATE", | ||
| "O"."O_SHIPPRIORITY" | ||
|
|
||
| from | ||
| "CUSTOMER" "C", | ||
| "ORDERS" "O", | ||
| "LINEITEM" "L" | ||
|
|
||
| where | ||
| "C"."C_MKTSEGMENT" = 'HOUSEHOLD' | ||
| and "C"."C_CUSTKEY" = "O"."O_CUSTKEY" | ||
| and "L"."L_ORDERKEY" = "O"."O_ORDERKEY" | ||
| and "O"."O_ORDERDATE" < date '1995-03-25' | ||
| and "L"."L_SHIPDATE" > date '1995-03-25' | ||
|
|
||
| group by | ||
| "L"."L_ORDERKEY", | ||
| "O"."O_ORDERDATE", | ||
| "O"."O_SHIPPRIORITY" | ||
| order by | ||
| "REVENUE" desc, | ||
| "O"."O_ORDERDATE" | ||
| limit 10 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super familiar with PostgreSQL TPC-H, but where do these test files actually come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe Niels created them (for PR #700) by modifying the existing TPC-H queries so that the SQL syntax was acceptable to PostgreSQL. He can talk to that better than me though.
I would personally prefer for exactly the same input SQL used to generate the Substrait plan also be used as the reference SQL. I will look at that when I get a chance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are modifications of the original set of TPC-H SQL queries we already have in: https://github.com/substrait-io/substrait-java/tree/main/isthmus/src/test/resources/tpch/queries
There are small syntactic differences that PostgreSQL requires vs the copy we already have in the repo.
Most modifications are upper casing all table and column names and quoting them since the isthmus configuration for parsing SQL in
ConverterProvideris configured to upper-case unquoted identifiers:substrait-java/isthmus/src/main/java/io/substrait/isthmus/ConverterProvider.java
Line 136 in b38ca00
And then there are other small syntactic differences, e.g. PostgreSQL does not like the
days(3)suffix in this line:substrait-java/isthmus/src/test/resources/tpch/queries/01.sql
Line 15 in b38ca00
So I removed it to make the expression work below in Q1: