public function assemble(AbstractSQLDataSourceQueryHandler $datasourceHandler, AbstractQueryRequest $request, DatasetMetaData $dataset, array $columnNames = NULL) {
        $engine = QueryEngineFactory::getInstance()->getHandler();

        $statement = $engine->newSelectStatement();

        // FIXME use the following code
/*      // the following code was commended out because functionality in Statement::findColumnTableByReferencePath() $table->dataset->name
        $tableName = assemble_database_entity_name($datasourceHandler, $dataset->datasourceName, $dataset->source);

        $table = $statement->newTable($tableName);
*/
        $table = new DatasetSection($dataset);
        $statement->tables[] = $table;

        if (isset($columnNames)) {
            $columnReferenceFactory = new CompositeColumnReferenceFactory(array(
                $dataset,
                new FormulaReferenceFactory($request->getFormulas())));

            $expressionAssembler = new FormulaExpressionAssembler($columnReferenceFactory);

            foreach ($columnNames as $columnName) {
                $column = $columnReferenceFactory->findColumn($columnName);

                if (isset($column) && ($column->persistence == FormulaMetaData::PERSISTENCE__CALCULATED)) {
                    $table->newCalculatedColumn($expressionAssembler->assemble($column), $columnName);
                }
                else {
                    $table->newColumn($columnName);
                }
            }
        }

        return $statement;
    }
    protected function collectTotalExpressions(AbstractQueryRequest $request) {
        // checking if the request has any formulas
        $formulas = $request->getFormulas();
        if (!isset($formulas)) {
            return NULL;
        }

        // collecting list of used formula names
        $usedFormulaExpressions = $this->assembleUsedFormulaExpressions($request);
        if (!isset($usedFormulaExpressions)) {
            return NULL;
        }

        $parser = new TotalInFormulaExpressionParser();
        $expressionCollector = new __TotalInFormulaExpressionRequestPreparer__ExpressionCollector();

        // collecting column names for which we need to calculate total
        $expressions = NULL;
        foreach ($usedFormulaExpressions as $formulaName => $formulaExpressions) {
            $totalExpressions = NULL;
            $parser->parse($formulaExpressions, array($expressionCollector, 'collectExpressionInTotalFunctionCall'), $totalExpressions);
            if (isset($totalExpressions)) {
                $expressions[$formulaName] = $totalExpressions;
            }
        }

        return $expressions;
    }