Skip to content

Commit

Permalink
Fix bugs with TPCH implementation (cmu-db#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoVanderkooy authored Jul 8, 2022
1 parent 7c64657 commit bb8f25a
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 92 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/oltpbenchmark/benchmarks/tpch/TPCHUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,19 @@ public static String getRegionFromRegionKey(int regionKey) {
}
}

/**
* Generates a random brand string of the form 'Brand#MN' where M and N are
* two single character strings representing two numbers randomly and
* independently selected within [1 .. 5]
*
* @param rand Random generator to use
* @return A random brand conforming to the TPCH specification
*/
public static String randomBrand(RandomGenerator rand) {
int M = rand.number(1, 5);
int N = rand.number(1, 5);

return String.format("Brand#%d%d", M, N);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TPCHWorker(TPCHBenchmark benchmarkModule, int id) {
protected TransactionStatus executeWork(Connection conn, TransactionType nextTransaction) throws UserAbortException, SQLException {
try {
GenericQuery proc = (GenericQuery) this.getProcedure(nextTransaction.getProcedureClass());
proc.run(conn, rand);
proc.run(conn, rand, this.configuration.getScaleFactor());
} catch (ClassCastException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public abstract class GenericQuery extends Procedure {

protected static final Logger LOG = LoggerFactory.getLogger(GenericQuery.class);

protected abstract PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException;
protected abstract PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException;

public void run(Connection conn, RandomGenerator rand) throws SQLException {
public void run(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {

try (PreparedStatement stmt = getStatement(conn, rand); ResultSet rs = stmt.executeQuery()) {
try (PreparedStatement stmt = getStatement(conn, rand, scaleFactor); ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
//do nothing
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Q1 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
String delta = String.valueOf(rand.number(60, 120));

PreparedStatement stmt = this.getPreparedStatement(conn, query_stmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class Q10 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
c_custkey,
c_name,
Expand Down Expand Up @@ -63,7 +63,7 @@ public class Q10 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// DATE is the first day of a randomly selected month from the second month of 1993 to the first month of 1995
int year = rand.number(1993, 1995);
int month = rand.number(year == 1993 ? 2 : 1, year == 1995 ? 1 : 12);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class Q11 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
ps_partkey,
SUM(ps_supplycost * ps_availqty) AS VALUE
Expand All @@ -39,7 +39,7 @@ public class Q11 extends GenericQuery {
WHERE
ps_suppkey = s_suppkey
AND s_nationkey = n_nationkey
AND n_name = 'ETHIOPIA'
AND n_name = ?
GROUP BY
ps_partkey
HAVING
Expand All @@ -58,17 +58,17 @@ public class Q11 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// NATION is randomly selected within the list of values defined for N_NAME in Clause 4.2.3
String nation = TPCHUtil.choice(TPCHConstants.N_NAME, rand);

// FRACTION is chosen as 0.0001 / SF
// TODO: we should technically pass dbgen's SF down here somehow
double fraction = 0.0001;
double fraction = 0.0001 / scaleFactor;

PreparedStatement stmt = this.getPreparedStatement(conn, query_stmt);
stmt.setDouble(1, fraction);
stmt.setString(2, nation);
stmt.setString(1, nation);
stmt.setDouble(2, fraction);
stmt.setString(3, nation);
return stmt;
}
}
57 changes: 34 additions & 23 deletions src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q12.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,48 @@

public class Q12 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
ps_partkey,
SUM(ps_supplycost * ps_availqty) AS VALUE
l_shipmode,
SUM(
CASE
WHEN
o_orderpriority = '1-URGENT' OR o_orderpriority = '2-HIGH'
THEN
1
ELSE
0
END
) AS high_line_count,
SUM(
CASE
WHEN
o_orderpriority <> '1-URGENT' AND o_orderpriority <> '2-HIGH'
THEN
1
ELSE
0
END
) AS low_line_count
FROM
partsupp,
supplier,
nation
orders,
lineitem
WHERE
ps_suppkey = s_suppkey
AND s_nationkey = n_nationkey
AND n_name = 'ETHIOPIA'
o_orderkey = l_orderkey
AND l_shipmode IN (?, ?)
AND l_commitdate < l_receiptdate
AND l_shipdate < l_commitdate
AND l_receiptdate >= DATE ?
AND l_receiptdate < DATE ? + INTERVAL '1' YEAR
GROUP BY
ps_partkey
HAVING
SUM(ps_supplycost * ps_availqty) > (
SELECT
SUM(ps_supplycost * ps_availqty) * ?
FROM
partsupp, supplier, nation
WHERE
ps_suppkey = s_suppkey
AND s_nationkey = n_nationkey
AND n_name = ? )
ORDER BY
VALUE DESC
l_shipmode
ORDER BY
l_shipmode
"""
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// SHIPMODE1 is randomly selected within the list of values defined for Modes in Clause 4.2.2.13
String shipMode1 = TPCHUtil.choice(TPCHConstants.MODES, rand);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class Q13 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
c_count,
COUNT(*) AS custdist
Expand Down Expand Up @@ -55,7 +55,7 @@ public class Q13 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// WORD1 is randomly selected from 4 possible values: special, pending, unusual, express
String word1 = TPCHUtil.choice(new String[]{"special", "pending", "unusual", "express"}, rand);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class Q14 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
100.00 * SUM(
CASE
Expand All @@ -49,7 +49,7 @@ public class Q14 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// DATE is the first day of a month randomly selected from a random year within [1993 .. 1997]
int year = rand.number(1993, 1997);
int month = rand.number(1, 12);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CREATE view revenue0 (supplier_no, total_revenue) AS
"""
);

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
s_suppkey,
s_name,
Expand Down Expand Up @@ -71,7 +71,7 @@ CREATE view revenue0 (supplier_no, total_revenue) AS
);

@Override
public void run(Connection conn, RandomGenerator rand) throws SQLException {
public void run(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// With this query, we have to set up a view before we execute the
// query, then drop it once we're done.
try (Statement stmt = conn.createStatement()) {
Expand All @@ -85,7 +85,7 @@ public void run(Connection conn, RandomGenerator rand) throws SQLException {
String sql = createview_stmt.getSQL();
sql = sql.replace("?", String.format("'%s'", date));
stmt.execute(sql);
super.run(conn, rand);
super.run(conn, rand, scaleFactor);
} finally {
String sql = dropview_stmt.getSQL();
stmt.execute(sql);
Expand All @@ -95,7 +95,7 @@ public void run(Connection conn, RandomGenerator rand) throws SQLException {
}

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
return this.getPreparedStatement(conn, query_stmt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public class Q16 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
p_brand,
p_type,
Expand Down Expand Up @@ -66,12 +66,8 @@ AND p_size IN (?, ?, ?, ?, ?, ?, ?, ?)
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
// BRAND = Brand#MN where M and N are two single character strings representing two numbers randomly and
// independently selected within [1 .. 5];
int M = rand.number(1, 5);
int N = rand.number(1, 5);
String brand = String.format("BRAND#%d%d", M, N);
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
String brand = TPCHUtil.randomBrand(rand);

// TYPE is made of the first 2 syllables of a string randomly selected within the
// list of 3-syllable strings defined for Types in Clause 4.2.2.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class Q17 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
SUM(l_extendedprice) / 7.0 AS avg_yearly
FROM
Expand All @@ -49,12 +49,8 @@ public class Q17 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
// BRAND = 'Brand#MN' where MN is a two character string representing two numbers randomly and independently
// selected within [1 .. 5]
int M = rand.number(1, 5);
int N = rand.number(1, 5);
String brand = String.format("BRAND#%d%d", M, N);
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
String brand = TPCHUtil.randomBrand(rand);

// CONTAINER is randomly selected within the list of 2-syllable strings defined for Containers in Clause
// 4.2.2.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class Q18 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// QUANTITY is randomly selected within [312..315]
int quantity = rand.number(312, 315);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.oltpbenchmark.benchmarks.tpch.procedures;

import com.oltpbenchmark.api.SQLStmt;
import com.oltpbenchmark.benchmarks.tpch.TPCHUtil;
import com.oltpbenchmark.util.RandomGenerator;

import java.sql.Connection;
Expand Down Expand Up @@ -69,7 +70,7 @@ AND l_shipmode IN ('AIR', 'AIR REG')
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// QUANTITY1 is randomly selected within [1..10]
int quantity1 = rand.number(1, 10);

Expand All @@ -81,17 +82,9 @@ protected PreparedStatement getStatement(Connection conn, RandomGenerator rand)

// BRAND1, BRAND2, BRAND3 = 'Brand#MN' where each MN is a two character string representing two numbers
// randomly and independently selected within [1 .. 5]
int M;
int N;
M = rand.number(1, 5);
N = rand.number(1, 5);
String brand1 = String.format("BRAND#%d%d", M, N);
M = rand.number(1, 5);
N = rand.number(1, 5);
String brand2 = String.format("BRAND#%d%d", M, N);
M = rand.number(1, 5);
N = rand.number(1, 5);
String brand3 = String.format("BRAND#%d%d", M, N);
String brand1 = TPCHUtil.randomBrand(rand);
String brand2 = TPCHUtil.randomBrand(rand);
String brand3 = TPCHUtil.randomBrand(rand);

PreparedStatement stmt = this.getPreparedStatement(conn, query_stmt);
stmt.setString(1, brand1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class Q2 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
int size = rand.number(1, 50);
String type = TPCHUtil.choice(TPCHConstants.TYPE_S3, rand);
String region = TPCHUtil.choice(TPCHConstants.R_NAME, rand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class Q20 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
s_name,
s_address
Expand Down Expand Up @@ -72,7 +72,7 @@ public class Q20 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// COLOR is randomly selected within the list of values defined for the generation of P_NAME
String color = TPCHUtil.choice(TPCHConstants.P_NAME_GENERATOR, rand) + "%";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class Q21 extends GenericQuery {

public final SQLStmt query_stmt = new SQLStmt("""
public final SQLStmt query_stmt = new SQLStmt("""
SELECT
s_name,
COUNT(*) AS numwait
Expand Down Expand Up @@ -74,7 +74,7 @@ public class Q21 extends GenericQuery {
);

@Override
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
// NATION is randomly selected within the list of values defined for N_NAME in Clause 4.2.3
String nation = TPCHUtil.choice(TPCHConstants.N_NAME, rand);

Expand Down
Loading

0 comments on commit bb8f25a

Please sign in to comment.