Skip to content

Commit bb8f25a

Browse files
Fix bugs with TPCH implementation (#204)
1 parent 7c64657 commit bb8f25a

File tree

26 files changed

+103
-92
lines changed

26 files changed

+103
-92
lines changed

src/main/java/com/oltpbenchmark/benchmarks/tpch/TPCHUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,19 @@ public static String getRegionFromRegionKey(int regionKey) {
9999
}
100100
}
101101

102+
/**
103+
* Generates a random brand string of the form 'Brand#MN' where M and N are
104+
* two single character strings representing two numbers randomly and
105+
* independently selected within [1 .. 5]
106+
*
107+
* @param rand Random generator to use
108+
* @return A random brand conforming to the TPCH specification
109+
*/
110+
public static String randomBrand(RandomGenerator rand) {
111+
int M = rand.number(1, 5);
112+
int N = rand.number(1, 5);
113+
114+
return String.format("Brand#%d%d", M, N);
115+
}
116+
102117
}

src/main/java/com/oltpbenchmark/benchmarks/tpch/TPCHWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public TPCHWorker(TPCHBenchmark benchmarkModule, int id) {
4141
protected TransactionStatus executeWork(Connection conn, TransactionType nextTransaction) throws UserAbortException, SQLException {
4242
try {
4343
GenericQuery proc = (GenericQuery) this.getProcedure(nextTransaction.getProcedureClass());
44-
proc.run(conn, rand);
44+
proc.run(conn, rand, this.configuration.getScaleFactor());
4545
} catch (ClassCastException e) {
4646
throw new RuntimeException(e);
4747
}

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/GenericQuery.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public abstract class GenericQuery extends Procedure {
3131

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

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

36-
public void run(Connection conn, RandomGenerator rand) throws SQLException {
36+
public void run(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
3737

38-
try (PreparedStatement stmt = getStatement(conn, rand); ResultSet rs = stmt.executeQuery()) {
38+
try (PreparedStatement stmt = getStatement(conn, rand, scaleFactor); ResultSet rs = stmt.executeQuery()) {
3939
while (rs.next()) {
4040
//do nothing
4141
}

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class Q1 extends GenericQuery {
5252
);
5353

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

5858
PreparedStatement stmt = this.getPreparedStatement(conn, query_stmt);

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q10.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
public class Q10 extends GenericQuery {
2929

30-
public final SQLStmt query_stmt = new SQLStmt("""
30+
public final SQLStmt query_stmt = new SQLStmt("""
3131
SELECT
3232
c_custkey,
3333
c_name,
@@ -63,7 +63,7 @@ public class Q10 extends GenericQuery {
6363
);
6464

6565
@Override
66-
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
66+
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
6767
// DATE is the first day of a randomly selected month from the second month of 1993 to the first month of 1995
6868
int year = rand.number(1993, 1995);
6969
int month = rand.number(year == 1993 ? 2 : 1, year == 1995 ? 1 : 12);

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q11.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public class Q11 extends GenericQuery {
3030

31-
public final SQLStmt query_stmt = new SQLStmt("""
31+
public final SQLStmt query_stmt = new SQLStmt("""
3232
SELECT
3333
ps_partkey,
3434
SUM(ps_supplycost * ps_availqty) AS VALUE
@@ -39,7 +39,7 @@ public class Q11 extends GenericQuery {
3939
WHERE
4040
ps_suppkey = s_suppkey
4141
AND s_nationkey = n_nationkey
42-
AND n_name = 'ETHIOPIA'
42+
AND n_name = ?
4343
GROUP BY
4444
ps_partkey
4545
HAVING
@@ -58,17 +58,17 @@ public class Q11 extends GenericQuery {
5858
);
5959

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

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

6968
PreparedStatement stmt = this.getPreparedStatement(conn, query_stmt);
70-
stmt.setDouble(1, fraction);
71-
stmt.setString(2, nation);
69+
stmt.setString(1, nation);
70+
stmt.setDouble(2, fraction);
71+
stmt.setString(3, nation);
7272
return stmt;
7373
}
7474
}

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q12.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,48 @@
2929

3030
public class Q12 extends GenericQuery {
3131

32-
public final SQLStmt query_stmt = new SQLStmt("""
32+
public final SQLStmt query_stmt = new SQLStmt("""
3333
SELECT
34-
ps_partkey,
35-
SUM(ps_supplycost * ps_availqty) AS VALUE
34+
l_shipmode,
35+
SUM(
36+
CASE
37+
WHEN
38+
o_orderpriority = '1-URGENT' OR o_orderpriority = '2-HIGH'
39+
THEN
40+
1
41+
ELSE
42+
0
43+
END
44+
) AS high_line_count,
45+
SUM(
46+
CASE
47+
WHEN
48+
o_orderpriority <> '1-URGENT' AND o_orderpriority <> '2-HIGH'
49+
THEN
50+
1
51+
ELSE
52+
0
53+
END
54+
) AS low_line_count
3655
FROM
37-
partsupp,
38-
supplier,
39-
nation
56+
orders,
57+
lineitem
4058
WHERE
41-
ps_suppkey = s_suppkey
42-
AND s_nationkey = n_nationkey
43-
AND n_name = 'ETHIOPIA'
59+
o_orderkey = l_orderkey
60+
AND l_shipmode IN (?, ?)
61+
AND l_commitdate < l_receiptdate
62+
AND l_shipdate < l_commitdate
63+
AND l_receiptdate >= DATE ?
64+
AND l_receiptdate < DATE ? + INTERVAL '1' YEAR
4465
GROUP BY
45-
ps_partkey
46-
HAVING
47-
SUM(ps_supplycost * ps_availqty) > (
48-
SELECT
49-
SUM(ps_supplycost * ps_availqty) * ?
50-
FROM
51-
partsupp, supplier, nation
52-
WHERE
53-
ps_suppkey = s_suppkey
54-
AND s_nationkey = n_nationkey
55-
AND n_name = ? )
56-
ORDER BY
57-
VALUE DESC
66+
l_shipmode
67+
ORDER BY
68+
l_shipmode
5869
"""
5970
);
6071

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

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q13.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
public class Q13 extends GenericQuery {
2929

30-
public final SQLStmt query_stmt = new SQLStmt("""
30+
public final SQLStmt query_stmt = new SQLStmt("""
3131
SELECT
3232
c_count,
3333
COUNT(*) AS custdist
@@ -55,7 +55,7 @@ public class Q13 extends GenericQuery {
5555
);
5656

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

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q14.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
public class Q14 extends GenericQuery {
2929

30-
public final SQLStmt query_stmt = new SQLStmt("""
30+
public final SQLStmt query_stmt = new SQLStmt("""
3131
SELECT
3232
100.00 * SUM(
3333
CASE
@@ -49,7 +49,7 @@ public class Q14 extends GenericQuery {
4949
);
5050

5151
@Override
52-
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
52+
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
5353
// DATE is the first day of a month randomly selected from a random year within [1993 .. 1997]
5454
int year = rand.number(1993, 1997);
5555
int month = rand.number(1, 12);

src/main/java/com/oltpbenchmark/benchmarks/tpch/procedures/Q15.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ CREATE view revenue0 (supplier_no, total_revenue) AS
4242
"""
4343
);
4444

45-
public final SQLStmt query_stmt = new SQLStmt("""
45+
public final SQLStmt query_stmt = new SQLStmt("""
4646
SELECT
4747
s_suppkey,
4848
s_name,
@@ -71,7 +71,7 @@ CREATE view revenue0 (supplier_no, total_revenue) AS
7171
);
7272

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

9797
@Override
98-
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand) throws SQLException {
98+
protected PreparedStatement getStatement(Connection conn, RandomGenerator rand, double scaleFactor) throws SQLException {
9999
return this.getPreparedStatement(conn, query_stmt);
100100
}
101101
}

0 commit comments

Comments
 (0)