@@ -595,32 +595,76 @@ void DerivedTable::importJoinsIntoFirstDt(const DerivedTable* firstDt) {
595595}
596596
597597void DerivedTable::flattenDt (const DerivedTable* dt) {
598- // TODO: std::move(...)?
599598 VELOX_DCHECK_NOT_NULL (dt);
600599 VELOX_DCHECK_EQ (tables.size (), 1 );
601600 VELOX_DCHECK (tables[0 ] == dt);
602- VELOX_DCHECK_EQ (cardinality, dt->cardinality );
601+ VELOX_DCHECK_NULL (dt->write );
602+ if (hasAggregation () && dt->hasAggregation ()) {
603+ // Can't have two aggregations in single dt.
604+ return ;
605+ }
606+ if (hasLimit () || dt->hasLimit ()) {
607+ // TODO: Can we combine limits?
608+ // But we need to account cardinality, aggregation, order in such case.
609+ return ;
610+ }
611+ if (write) {
612+ // We shouldn't flatten into a dt that writes.
613+ // TODO: We can avoid this with putting write in the end of addPostprocess.
614+ return ;
615+ }
616+ // TODO: Use std::move(...)?
617+
618+ cardinality = dt->cardinality ;
603619 cname = dt->cname ;
604620 columns = dt->columns ;
605621 exprs = dt->exprs ;
606- VELOX_DCHECK ( joinedBy == dt->joinedBy ) ;
622+ joinedBy = dt->joinedBy ;
607623 tables = dt->tables ;
608624 tableSet = dt->tableSet ;
625+
626+ // TODO: Is setop possible here at all?
609627 VELOX_DCHECK (setOp == dt->setOp );
610628 VELOX_DCHECK (children == dt->children );
629+
630+ // TODO: Is it true?
611631 VELOX_DCHECK (singleRowDts == dt->singleRowDts );
612- VELOX_DCHECK (startTables == dt->startTables );
632+
633+ startTables = dt->startTables ;
613634 joins = dt->joins ;
635+
636+ // TODO: Is it true?
614637 VELOX_DCHECK (conjuncts == dt->conjuncts );
638+
615639 // TODO: Why unionSet here?
616640 importedExistences.unionSet (dt->importedExistences );
641+
617642 fullyImported = dt->fullyImported ;
618- VELOX_DCHECK_EQ (noImportOfExists, dt->noImportOfExists );
643+
644+ // TODO: Is this correct?
645+ // I made it like this for consistency with unionSet above.
646+ noImportOfExists = noImportOfExists && dt->noImportOfExists ;
647+
619648 joinOrder = dt->joinOrder ;
620- aggregation = dt->aggregation ;
621- having = dt->having ;
622- VELOX_DCHECK (orderKeys == dt->orderKeys );
623- VELOX_DCHECK (orderTypes == dt->orderTypes );
649+
650+ if (!aggregation) {
651+ aggregation = dt->aggregation ;
652+ VELOX_DCHECK (having.empty ());
653+ having = dt->having ;
654+ } else {
655+ VELOX_DCHECK_NULL (dt->aggregation );
656+ VELOX_DCHECK (dt->having .empty ());
657+ }
658+
659+ if (orderKeys.empty ()) {
660+ orderKeys = dt->orderKeys ;
661+ VELOX_DCHECK (orderTypes.empty ());
662+ orderTypes = dt->orderTypes ;
663+ } else {
664+ VELOX_DCHECK (dt->orderKeys .empty ());
665+ VELOX_DCHECK (dt->orderTypes .empty ());
666+ }
667+
624668 VELOX_DCHECK (limit == dt->limit );
625669 VELOX_DCHECK (offset == dt->offset );
626670 VELOX_DCHECK (write == dt->write );
0 commit comments