Skip to content

Commit

Permalink
workload: enhance querybench
Browse files Browse the repository at this point in the history
Release justification: non-production code changes.

`querybench` workload has been enhanced to support queries that consist
of multiple statements that are present on a single line. Previously,
the workload would fail on such queries because it attempts to `Prepare`
the whole line which would error out. Now we're still attempting to
prepare the whole line, but if that fails, we will store plain query and
will be executing it using `DB.Query` rather that `Stmt.Query`.

Release note: None
  • Loading branch information
yuzefovich committed Mar 27, 2020
1 parent 8f793a0 commit f9a536c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
49 changes: 35 additions & 14 deletions pkg/workload/querybench/query_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,17 @@ func (g *queryBench) Ops(urls []string, reg *histogram.Registry) (workload.Query

stmts := make([]namedStmt, len(g.queries))
for i, query := range g.queries {
stmt, err := db.Prepare(query)
if err != nil {
return workload.QueryLoad{}, errors.Wrapf(err, "failed to prepare query %q", query)
}
stmts[i] = namedStmt{
// TODO(solon): Allow specifying names in the query file rather than using
// the entire query as the name.
name: fmt.Sprintf("%2d: %s", i+1, query),
stmt: stmt,
}
stmt, err := db.Prepare(query)
if err != nil {
stmts[i].query = query
continue
}
stmts[i].preparedStmt = stmt
}

maxNumStmts := 0
Expand Down Expand Up @@ -192,7 +193,11 @@ func GetQueries(path string) ([]string, error) {

type namedStmt struct {
name string
stmt *gosql.Stmt
// We will try to Prepare the statement, and if that succeeds, the prepared
// statement will be stored in `preparedStmt', otherwise, we will store
// plain query in 'query'.
preparedStmt *gosql.Stmt
query string
}

type queryBenchWorker struct {
Expand Down Expand Up @@ -221,15 +226,31 @@ func (o *queryBenchWorker) run(ctx context.Context) error {
stmt := o.stmts[o.stmtIdx%len(o.stmts)]
o.stmtIdx++

rows, err := stmt.stmt.Query()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
exhaustRows := func(execFn func() (*gosql.Rows, error)) error {
rows, err := execFn()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
}
if err := rows.Err(); err != nil {
return err
}
return nil
}
if err := rows.Err(); err != nil {
return err
if stmt.preparedStmt != nil {
if err := exhaustRows(func() (*gosql.Rows, error) {
return stmt.preparedStmt.Query()
}); err != nil {
return err
}
} else {
if err := exhaustRows(func() (*gosql.Rows, error) {
return o.db.Query(stmt.query)
}); err != nil {
return err
}
}
elapsed := timeutil.Since(start)
if o.verbose {
Expand Down
4 changes: 1 addition & 3 deletions pkg/workload/querybench/tpch-queries
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ SELECT c_count, count(*) AS custdist FROM ( SELECT c_custkey, count(o_orderkey)
SELECT 100.00 * sum(CASE WHEN p_type LIKE 'PROMO%' THEN l_extendedprice * (1 - l_discount) ELSE 0 END) / sum(l_extendedprice * (1 - l_discount)) AS promo_revenue FROM lineitem, part WHERE l_partkey = p_partkey AND l_shipdate >= DATE '1995-09-01' AND l_shipdate < DATE '1995-09-01' + INTERVAL '1' MONTH

-- query 15
CREATE VIEW revenue0 (supplier_no, total_revenue) AS SELECT l_suppkey, sum(l_extendedprice * (1 - l_discount)) FROM lineitem WHERE l_shipdate >= DATE '1996-01-01' AND l_shipdate < DATE '1996-01-01' + INTERVAL '3' MONTH GROUP BY l_suppkey
SELECT s_suppkey, s_name, s_address, s_phone, total_revenue FROM supplier, revenue0 WHERE s_suppkey = supplier_no AND total_revenue = ( SELECT max(total_revenue) FROM revenue0) ORDER BY s_suppkey
DROP VIEW revenue0
CREATE VIEW revenue0 (supplier_no, total_revenue) AS SELECT l_suppkey, sum(l_extendedprice * (1 - l_discount)) FROM lineitem WHERE l_shipdate >= DATE '1996-01-01' AND l_shipdate < DATE '1996-01-01' + INTERVAL '3' MONTH GROUP BY l_suppkey; SELECT s_suppkey, s_name, s_address, s_phone, total_revenue FROM supplier, revenue0 WHERE s_suppkey = supplier_no AND total_revenue = ( SELECT max(total_revenue) FROM revenue0) ORDER BY s_suppkey; DROP VIEW revenue0

-- query 16
SELECT p_brand, p_type, p_size, count(distinct ps_suppkey) AS supplier_cnt FROM partsupp, part WHERE p_partkey = ps_partkey AND p_brand <> 'Brand#45' AND p_type NOT LIKE 'MEDIUM POLISHED%' AND p_size IN (49, 14, 23, 45, 19, 3, 36, 9) AND ps_suppkey NOT IN ( SELECT s_suppkey FROM supplier WHERE s_comment LIKE '%Customer%Complaints%') GROUP BY p_brand, p_type, p_size ORDER BY supplier_cnt DESC, p_brand, p_type, p_size
Expand Down

0 comments on commit f9a536c

Please sign in to comment.