Skip to content

Commit

Permalink
Add DISTINCT to Jobs realm JobDataset query (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpalmer authored Jun 9, 2020
1 parent 292cd50 commit 6397018
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions classes/DataWarehouse/Query/Jobs/JobDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,55 @@ public function getColumnDocumentation()
{
return $this->documentation;
}

/**
* The query differs from the base class query because the same fact table
* row may correspond to multiple rows in the aggregate table (e.g. a job
* that runs over two days). Therefore the DISTINCT keyword is added to
* deduplicate.
*
* @see \DataWarehouse\Query\Query::getQueryString()
*/
public function getQueryString($limit = null, $offset = null, $extraHavingClause = null)
{
$wheres = $this->getWhereConditions();
$groups = $this->getGroups();

$select_tables = $this->getSelectTables();
$select_fields = $this->getSelectFields();

if ( 0 == count($select_fields) ) {
$this->logAndThrowException("Cannot generate query string with no select fields");
}

$select_order_by = $this->getSelectOrderBy();

$format = <<<SQL
SELECT DISTINCT
%s
FROM
%s%s
WHERE
%s
%s%s%s%s
SQL;

$data_query = sprintf(
$format,
implode(",\n ", $select_fields),
implode(",\n ", $select_tables),
( "" == $this->getLeftJoinSql() ? "" : "\n" . $this->getLeftJoinSql() ),
implode("\n AND ", $wheres),
( count($groups) > 0 ? "GROUP BY " . implode(",\n ", $groups) : "" ),
( null !== $extraHavingClause ? "\nHAVING $extraHavingClause" : "" ),
( count($select_order_by) > 0 ? "\nORDER BY " . implode(",\n ", $select_order_by) : "" ),
( null !== $limit && null !== $offset ? "\nLIMIT $limit OFFSET $offset" : "" )
);

$this->logger->debug(
sprintf("%s %s()\n%s", $this, __FUNCTION__, $data_query)
);

return $data_query;
}
}

0 comments on commit 6397018

Please sign in to comment.