You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudberry.apache.org by "yjhjstz (via GitHub)" <gi...@apache.org> on 2025/10/16 16:52:30 UTC

[PR] orca: implement intra-segment parallel table scan support [cloudberry]

yjhjstz opened a new pull request, #1398:
URL: https://github.com/apache/cloudberry/pull/1398

   Add comprehensive parallel table scan capability to GPORCA optimizer, enabling worker-level parallelism within segments for improved query performance on large table scans.
   
   Key components:
   - New CPhysicalParallelTableScan operator and CDistributionSpecWorkerRandom distribution specification for worker-level data distribution
   - CXformGet2ParallelTableScan transformation with parallel safety checks (excludes CTEs, dynamic scans, foreign tables, replicated tables, etc.)
   - Cost model integration with parallel_setup_cost and efficiency degradation scaling (logarithmic based on worker count)
   - DXL serialization/deserialization for CDXLPhysicalParallelTableScan
   - Plan translation to PostgreSQL SeqScan nodes with parallel_aware=true
   - Rewindability constraints (parallel scans are non-rewindable)
   - GUC integration: max_parallel_workers_per_gather controls worker count
   
   Impl https://github.com/apache/cloudberry/discussions/1316
   
   ### Bench TPCH 10GB
   | Query ID | Parallel Duration (s) | Non-parallel Duration (s) | Performance Improvement (s) | Performance Improvement Rate |
   |----------|----------------------|--------------------------|----------------------------|-----------------------------|
   | 01       | 16.000000            | 30.000000                | 14.00                      | 46.67%                      |
   | 02       | 3.000000             | 4.000000                 | 1.00                       | 25.00%                      |
   | 03       | 11.000000            | 20.000000                | 9.00                       | 45.00%                      |
   | 04       | 7.000000             | 15.000000                | 8.00                       | 53.33%                      |
   | 05       | 8.000000             | 8.000000                 | 0.00                       | 0.00%                       |
   | 06       | 3.000000             | 5.000000                 | 2.00                       | 40.00%                      |
   | 07       | 5.000000             | 8.000000                 | 3.00                       | 37.50%                      |
   | 08       | 6.000000             | 8.000000                 | 2.00                       | 25.00%                      |
   | 09       | 10.000000            | 15.000000                | 5.00                       | 33.33%                      |
   | 10       | 6.000000             | 7.000000                 | 1.00                       | 14.29%                      |
   | 11       | 1.000000             | 2.000000                 | 1.00                       | 50.00%                      |
   | 12       | 5.000000             | 7.000000                 | 2.00                       | 28.57%                      |
   | 13       | 4.000000             | 6.000000                 | 2.00                       | 33.33%                      |
   | 14       | 3.000000             | 5.000000                 | 2.00                       | 40.00%                      |
   | 15       | 5.000000             | 5.000000                 | 0.00                       | 0.00%                       |
   | 16       | 2.000000             | 1.000000                 | -1.00                      | --------                    |
   | 17       | 34.000000            | 62.000000                | 28.00                      | 45.16%                      |
   | 18       | 22.000000            | 28.000000                | 6.00                       | 21.43%                      |
   | 19       | 3.000000             | 5.000000                 | 2.00                       | 40.00%                      |
   | 20       | 6.000000             | 11.000000                | 5.00                       | 45.45%                      |
   | 21       | 22.000000            | 25.000000                | 3.00                       | 12.00%                      |
   | 22       | 4.000000             | 6.000000                 | 2.00                       | 33.33%                      |
   
   
   Conclusion:
   With parallel execution, the TPCH queries' total execution time decreased from 284 seconds to 186 seconds, saving 98 seconds in total, with a performance improvement of 34.51%.
   
   
   ### What does this PR do?
   <!-- Brief overview of the changes, including any major features or fixes -->
   
   ### Type of Change
   - [ ] Bug fix (non-breaking change)
   - [ ] New feature (non-breaking change)
   - [ ] Breaking change (fix or feature with breaking changes)
   - [ ] Documentation update
   
   ### Breaking Changes
   <!-- Remove if not applicable. If yes, explain impact and migration path -->
   
   ### Test Plan
   <!-- How did you test these changes? -->
   - [ ] Unit tests added/updated
   - [ ] Integration tests added/updated
   - [ ] Passed `make installcheck`
   - [ ] Passed `make -C src/test installcheck-cbdb-parallel`
   
   ### Impact
   <!-- Remove sections that don't apply -->
   **Performance:**
   <!-- Any performance implications? -->
   
   **User-facing changes:**
   <!-- Any changes visible to users? -->
   
   **Dependencies:**
   <!-- New dependencies or version changes? -->
   
   ### Checklist
   - [ ] Followed [contribution guide](https://cloudberry.apache.org/contribute/code)
   - [ ] Added/updated documentation
   - [ ] Reviewed code for security implications
   - [ ] Requested review from [cloudberry committers](https://github.com/orgs/apache/teams/cloudberry-committers)
   
   ### Additional Context
   <!-- Any other information that would help reviewers? Remove if none -->
   
   ### CI Skip Instructions
   <!--
   To skip CI builds, add the appropriate CI skip identifier to your PR title.
   The identifier must:
   - Be in square brackets []
   - Include the word "ci" and either "skip" or "no"
   - Only use for documentation-only changes or when absolutely necessary
   -->
   
   ---
   <!-- Join our community:
   - Mailing list: [dev@cloudberry.apache.org](https://lists.apache.org/list.html?dev@cloudberry.apache.org) (subscribe: dev-subscribe@cloudberry.apache.org)
   - Discussions: https://github.com/apache/cloudberry/discussions -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448614374


##########
src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp:
##########
@@ -2415,15 +2527,34 @@ CTranslatorDXLToPlStmt::TranslateDXLMotion(
 	sendslice->directDispatch.contentIds = NIL;
 	sendslice->directDispatch.haveProcessedAnyCalculations = false;
 
+	// set parallel workers if needed
+	ULONG child_index = motion_dxlop->GetRelationChildIdx();
+	CDXLNode *child_dxlnode = (*motion_dxlnode)[child_index];
+	ULONG child_parallel_workers = ExtractParallelWorkersFromDXL(child_dxlnode);
+	if (child_parallel_workers > 1)
+	{
+		// Determine parallel workers based on enable_parallel and gang type
+		bool supports_parallel = (sendslice->gangType == GANGTYPE_PRIMARY_READER ||
+		                          sendslice->gangType == GANGTYPE_PRIMARY_WRITER);
+
+		if (enable_parallel && supports_parallel)

Review Comment:
   yes. no need enable_parallel.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448545397


##########
src/backend/gpopt/gpdbwrappers.cpp:
##########
@@ -2706,4 +2726,36 @@ gpdb::TestexprIsHashable(Node *testexpr, List *param_ids)
 	return false;
 }
 
+// check if parallel mode is OK (comprehensive check)
+bool
+gpdb::IsParallelModeOK(void)
+{
+	GP_WRAP_START;
+	{
+		if (!enable_parallel)
+			return false;
+
+		if (IS_SINGLENODE())

Review Comment:
   orca not support single node mode yet.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448579348


##########
src/backend/gporca/libgpopt/include/gpopt/base/CRewindabilitySpec.h:
##########
@@ -108,6 +108,13 @@ class CRewindabilitySpec : public CPropSpec
 		EmhtSentinel
 	};
 
+	enum EBarrierHazardType

Review Comment:
   to remove.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2447308871


##########
src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp:
##########
@@ -6156,9 +6297,11 @@ CTranslatorDXLToPlStmt::TranslatePlanCosts(const CDXLNode *dxlnode, Plan *plan)
 	// process, whereas the row estimates in GPORCA are global, across all
 	// processes. Divide the row count estimate by the number of segments
 	// executing it.
-	plan->plan_rows =
-		ceil(CostFromStr(costs->GetRowsOutStr()) /
-			 m_dxl_to_plstmt_context->GetCurrentSlice()->numsegments);
+	PlanSlice *current_slice = m_dxl_to_plstmt_context->GetCurrentSlice();
+	double total_rows = CostFromStr(costs->GetRowsOutStr());
+	double rows_per_segment = total_rows / current_slice->numsegments;
+	
+	plan->plan_rows = ceil(rows_per_segment);

Review Comment:
   Why change codes here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2450654076


##########
src/test/regress/GNUmakefile:
##########
@@ -223,6 +223,12 @@ installcheck-cbdb-parallel: all tablespace-setup twophase_pqexecparams
 	$(pg_regress_installcheck) $(REGRESS_OPTS) --schedule=$(srcdir)/parallel_schedule --schedule=$(srcdir)/greenplum_schedule --max-connections=5 $(EXTRA_TESTS) --exclude-tests explain \
 	)
 
+installcheck-orca-parallel: all tablespace-setup twophase_pqexecparams
+	( \
+	export PGOPTIONS='-c optimizer=on -c enable_parallel=true -c min_parallel_table_scan_size=0 -c min_parallel_index_scan_size=0 -c parallel_setup_cost=0'; \
+	$(pg_regress_installcheck) $(REGRESS_OPTS) --exclude-file=$(srcdir)/excluded_tests.conf --schedule=$(srcdir)/parallel_schedule --schedule=$(srcdir)/greenplum_schedule --max-connections=4 --ignore-plans $(EXTRA_TESTS) \

Review Comment:
   why ignore-plans here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448577972


##########
src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp:
##########
@@ -0,0 +1,284 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CPhysicalParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/CDistributionSpec.h"
+#include "gpopt/base/CDistributionSpecHashed.h"
+#include "gpopt/base/CDistributionSpecRandom.h"
+#include "gpopt/base/CDistributionSpecWorkerRandom.h"
+#include "gpopt/base/CDistributionSpecSingleton.h"
+#include "gpopt/base/CUtils.h"
+#include "gpopt/base/CEnfdDistribution.h"
+#include "gpopt/base/CEnfdRewindability.h"
+#include "gpopt/base/COptimizationContext.h"
+#include "gpopt/base/CRewindabilitySpec.h"
+#include "gpopt/base/CDrvdPropPlan.h"
+#include "gpopt/metadata/CName.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp)
+	: CPhysicalTableScan(mp, GPOS_NEW(mp) CName(GPOS_NEW(mp) CWStringConst(GPOS_WSZ_LIT("parallel_table"))), nullptr, nullptr),
+	  m_ulParallelWorkers(1),
+	  m_pdsWorkerDistribution(nullptr)
+{
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp,
+													   const CName *pnameAlias,
+													   CTableDescriptor *ptabdesc,
+													   CColRefArray *pdrgpcrOutput,
+													   ULONG ulParallelWorkers)
+	: CPhysicalTableScan(mp, pnameAlias, ptabdesc, pdrgpcrOutput),
+	  m_ulParallelWorkers(ulParallelWorkers),
+	  m_pdsWorkerDistribution(nullptr)
+{
+	GPOS_ASSERT(ulParallelWorkers > 0);
+	GPOS_ASSERT(nullptr != m_pds);
+	// Create worker-level distribution based on table's segment distribution
+	if (ulParallelWorkers > 0 && nullptr != m_pds)
+	{
+		// Create worker-level random distribution using the table's distribution as base
+		// The base CPhysicalScan already sets up m_pds from the table descriptor
+		m_pdsWorkerDistribution = CDistributionSpecWorkerRandom::PdsCreateWorkerRandom(mp, ulParallelWorkers, m_pds);

Review Comment:
   anyway,  it is random.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448613167


##########
src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp:
##########
@@ -6156,9 +6297,11 @@ CTranslatorDXLToPlStmt::TranslatePlanCosts(const CDXLNode *dxlnode, Plan *plan)
 	// process, whereas the row estimates in GPORCA are global, across all
 	// processes. Divide the row count estimate by the number of segments
 	// executing it.
-	plan->plan_rows =
-		ceil(CostFromStr(costs->GetRowsOutStr()) /
-			 m_dxl_to_plstmt_context->GetCurrentSlice()->numsegments);
+	PlanSlice *current_slice = m_dxl_to_plstmt_context->GetCurrentSlice();
+	double total_rows = CostFromStr(costs->GetRowsOutStr());
+	double rows_per_segment = total_rows / current_slice->numsegments;
+	
+	plan->plan_rows = ceil(rows_per_segment);

Review Comment:
   to revert.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2447332155


##########
src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp:
##########
@@ -7282,4 +7425,75 @@ CTranslatorDXLToPlStmt::IsIndexForOrderBy(
 	}
 	return false;
 }
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CTranslatorDXLToPlStmt::ExtractParallelWorkersFromDXL
+//
+//	@doc:
+//		Extract parallel workers count from DXL node tree recursively.
+//		Since parallel degree is uniform across all parallel scans in a query,
+//		returns the first parallel degree found from any CDXLPhysicalParallelTableScan,
+//		or 1 if no parallel scan exists.
+//
+//---------------------------------------------------------------------------
+ULONG
+CTranslatorDXLToPlStmt::ExtractParallelWorkersFromDXL(const CDXLNode *dxlnode)
+{
+	if (nullptr == dxlnode)
+	{
+		return 1;
+	}
+
+	CDXLOperator *dxlop = dxlnode->GetOperator();
+	if (EdxlopPhysicalParallelTableScan == dxlop->GetDXLOperator())
+	{
+		// Return parallel workers from the parallel table scan operator
+		// All parallel scans in the query share the same parallel degree
+		CDXLPhysicalParallelTableScan *parallel_scan_dxlop =
+			CDXLPhysicalParallelTableScan::Cast(dxlop);
+		return parallel_scan_dxlop->UlParallelWorkers();
+	}
+	else if (EdxlopPhysicalTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalIndexScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalIndexOnlyScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalBitmapTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicBitmapTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalForeignScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicForeignScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicIndexScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicIndexOnlyScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalValuesScan == dxlop->GetDXLOperator())
+	{
+		// Non-parallel scans (table, index, bitmap, foreign, values)
+		// These are leaf nodes in terms of parallel worker extraction
+		// Return 1 to indicate no parallel workers
+		return 1;
+	}
+	else if (EdxlopPhysicalMotionGather == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionBroadcast == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionRedistribute == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionRandom == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionRoutedDistribute == dxlop->GetDXLOperator())
+	{
+		// Motion node creates a slice boundary - do not recurse into child
+		// The child's parallel workers belong to the sending slice, not receiving slice
+		// Return 0 to indicate the receiving slice (current slice) has no parallel workers
+		return 1;
+	}
+
+	// Recursively check child nodes, return early when first parallel scan is found
+	for (ULONG ul = 0; ul < dxlnode->Arity(); ul++)
+	{
+		ULONG child_parallel_workers = ExtractParallelWorkersFromDXL((*dxlnode)[ul]);

Review Comment:
   For  HashJoin, should we return 3 or 6?
   
   ```
   HashJoin
   |─ Motion
          |─ Parallel Scan on A (parallel_workers = 3)
   |─ Motion
          |─ Parallel Scan on B (parallel_workers = 6)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2450658497


##########
src/test/regress/excluded_tests.conf:
##########
@@ -0,0 +1,14 @@
+brin_bloom

Review Comment:
   Why exclude those tests, because of failures?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#issuecomment-3440719932

   > Add some cases to test the plan?
   
   maybe after impl parallel hash join .


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2452198119


##########
src/test/regress/excluded_tests.conf:
##########
@@ -0,0 +1,14 @@
+brin_bloom

Review Comment:
   have not support parallel bitmap scan, some direct dispatch ...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#issuecomment-3425398922

   Please add more test cases


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448769677


##########
src/backend/gpopt/translate/CTranslatorRelcacheToDXL.cpp:
##########
@@ -618,14 +617,31 @@ CTranslatorRelcacheToDXL::RetrieveRel(CMemoryPool *mp, CMDAccessor *md_accessor,
 			CMDIdGPDB(IMDId::EmdidGeneral, gpdb::GetForeignServerId(oid));
 	}
 
-	md_rel = GPOS_NEW(mp) CMDRelationGPDB(
+	CMDRelationGPDB *md_rel_gpdb = GPOS_NEW(mp) CMDRelationGPDB(
 		mp, mdid, mdname, is_temporary, rel_storage_type, dist,
 		mdcol_array, distr_cols, distr_op_families, part_keys, part_types,
 		partition_oids, convert_hash_to_random, keyset_array,
 		md_index_info_array, check_constraint_mdids, mdpart_constraint,
 		foreign_server_mdid, rel->rd_rel->reltuples);
 
-	return md_rel;
+	// Set segment file count for AO/AOCO tables
+	// Skip partitioned tables as they don't have physical storage (only leaf partitions do)
+	if ((rel_storage_type == IMDRelation::ErelstorageAppendOnlyRows ||
+		 rel_storage_type == IMDRelation::ErelstorageAppendOnlyCols) &&
+		rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
+	{
+		INT seg_file_count = gpdb::GetAppendOnlySegmentFilesCount(rel.get());

Review Comment:
   default is -1,  and with rel_storage_type judge



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r3231164678


##########
src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp:
##########
@@ -0,0 +1,265 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CXformGet2ParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/xforms/CXformGet2ParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/COptCtxt.h"
+#include "gpopt/hints/CHintUtils.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+#include "gpopt/operators/CLogicalGet.h"
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+#include "gpopt/optimizer/COptimizerConfig.h"
+#include "naucrates/md/IMDRelation.h"
+#include "gpopt/search/CGroupProxy.h"
+#include "gpopt/search/CMemo.h"
+
+
+// Use gpdbwrappers for parallel checks
+extern int max_parallel_workers_per_gather;
+
+// Forward declarations for gpdbwrappers functions
+namespace gpdb {
+	bool IsParallelModeOK(void);
+}
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::FHasParallelIncompatibleOps
+//
+//	@doc:
+//		Check if memo contains logical operators that are incompatible
+//		with parallel execution (CTE, Dynamic scans, Foreign scans, etc.)
+//
+//---------------------------------------------------------------------------
+BOOL
+CXformGet2ParallelTableScan::FHasParallelIncompatibleOps(CExpressionHandle &exprhdl)
+{
+	CGroupExpression *pgexprHandle = exprhdl.Pgexpr();
+	if (nullptr == pgexprHandle)
+	{
+		return false;
+	}
+
+	CGroup *pgroup = pgexprHandle->Pgroup();
+	if (nullptr == pgroup)
+	{
+		return false;
+	}
+
+	CMemo *pmemo = pgroup->Pmemo();
+	if (nullptr == pmemo)
+	{
+		return false;
+	}
+
+	// Iterate through all groups in memo to check for parallel-incompatible operations
+	const ULONG_PTR ulGroups = pmemo->UlpGroups();
+	for (ULONG_PTR ul = 0; ul < ulGroups; ul++)
+	{
+		CGroup *pgroupCurrent = pmemo->Pgroup(ul);
+		if (nullptr == pgroupCurrent)
+		{
+			continue;
+		}
+
+		// Check all group expressions in this group using CGroupProxy
+		CGroupProxy gp(pgroupCurrent);
+		CGroupExpression *pgexpr = gp.PgexprFirst();
+		while (nullptr != pgexpr)
+		{
+			COperator::EOperatorId eopid = pgexpr->Pop()->Eopid();
+
+			// Check for CTE-related operators (incompatible with parallel execution)
+			if (COperator::EopLogicalCTEProducer == eopid ||
+				COperator::EopLogicalCTEConsumer == eopid ||
+				COperator::EopLogicalSequence == eopid ||
+				COperator::EopLogicalSequenceProject == eopid)
+			{
+				return true;
+			}
+
+			if (COperator::EopLogicalUnion == eopid ||
+				COperator::EopLogicalUnionAll == eopid ||
+				COperator::EopLogicalIntersect == eopid ||
+				COperator::EopLogicalIntersectAll == eopid ||
+				COperator::EopLogicalDifference == eopid ||
+				COperator::EopLogicalDifferenceAll == eopid)
+			{
+				// Set operations are not supported in parallel plans
+				return true;
+			}
+
+			pgexpr = gp.PgexprNext(pgexpr);
+		}
+	}
+
+	return false;
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::CXformGet2ParallelTableScan
+//
+//	@doc:
+//		Ctor
+//
+//---------------------------------------------------------------------------
+CXformGet2ParallelTableScan::CXformGet2ParallelTableScan(CMemoryPool *mp)
+	: CXformImplementation(
+		  // pattern
+		  GPOS_NEW(mp) CExpression(mp, GPOS_NEW(mp) CLogicalGet(mp)))
+{
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::Exfp
+//
+//	@doc:
+//		Compute promise of xform based on GUC enable_parallel
+//		Uses unified parallel degree from max_parallel_workers_per_gather
+//
+//---------------------------------------------------------------------------
+CXform::EXformPromise
+CXformGet2ParallelTableScan::Exfp(CExpressionHandle &exprhdl) const
+{
+	// Check if parallel plans are enabled in context and parallel processing is safe
+	if (!gpdb::IsParallelModeOK())
+	{
+		return CXform::ExfpNone;
+	}
+
+	// Check for parallel-incompatible operations that would conflict with parallel scans
+	if (FHasParallelIncompatibleOps(exprhdl))
+	{
+		return CXform::ExfpNone;
+	}
+
+	CLogicalGet *popGet = CLogicalGet::PopConvert(exprhdl.Pop());
+	CTableDescriptor *ptabdesc = popGet->Ptabdesc();
+
+	// Don't use parallel scan for replicated tables
+	if (ptabdesc->GetRelDistribution() == IMDRelation::EreldistrReplicated ||
+		ptabdesc->GetRelDistribution() == IMDRelation::EreldistrMasterOnly ||
+		COptCtxt::PoctxtFromTLS()->HasReplicatedTables())
+	{
+		//FIXME: Should we consider replicated tables.
+		return CXform::ExfpNone;
+	}
+
+	// For AO/AOCO tables, check segfilecount early to avoid useless transformation
+	CMDAccessor *md_accessor = COptCtxt::PoctxtFromTLS()->Pmda();
+	const IMDRelation *pmdrel = md_accessor->RetrieveRel(ptabdesc->MDId());
+	IMDRelation::Erelstoragetype storage_type = pmdrel->RetrieveRelStorageType();
+
+	// Check if this is an AO/AOCO table
+	if (storage_type == IMDRelation::ErelstorageAppendOnlyRows ||
+		storage_type == IMDRelation::ErelstorageAppendOnlyCols)
+	{
+		INT seg_file_count = pmdrel->SegFileCount();
+		// Only reject if segfilecount is explicitly known to be 0 or 1
+		// -1 means unknown (e.g., from DXL deserialization), so allow parallel in that case
+		if (seg_file_count >= 0 && seg_file_count <= 1)
+		{
+			// If segfilecount is 0 or 1, parallel execution is pointless
+			// Reject parallel scan early in promise phase
+			GPOS_TRACE_FORMAT("CXformGet2ParallelTableScan rejected for table %ls: AO/AOCO table has segfilecount=%d (needs >1 for parallel scan)",
+							ptabdesc->Name().Pstr()->GetBuffer(), seg_file_count);
+			return CXform::ExfpNone;
+		}
+	}
+
+	// High promise for parallel scan when enabled
+	// All tables will use the same parallel degree from max_parallel_workers_per_gather
+	return CXform::ExfpHigh;
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::Transform
+//
+//	@doc:
+//		Actual transformation
+//
+//---------------------------------------------------------------------------
+void
+CXformGet2ParallelTableScan::Transform(CXformContext *pxfctxt, CXformResult *pxfres,
+									   CExpression *pexpr) const
+{
+	GPOS_ASSERT(nullptr != pxfctxt);
+	GPOS_ASSERT(FPromising(pxfctxt->Pmp(), this, pexpr));
+	GPOS_ASSERT(FCheckPattern(pexpr));
+
+	CLogicalGet *popGet = CLogicalGet::PopConvert(pexpr->Pop());
+
+	CMemoryPool *mp = pxfctxt->Pmp();
+
+	// create/extract components for alternative
+	CName *pname = GPOS_NEW(mp) CName(mp, popGet->Name());
+
+	CTableDescriptor *ptabdesc = popGet->Ptabdesc();
+	ptabdesc->AddRef();
+
+	CColRefArray *pdrgpcrOutput = popGet->PdrgpcrOutput();
+	GPOS_ASSERT(nullptr != pdrgpcrOutput);
+	pdrgpcrOutput->AddRef();
+
+	// Determine parallel workers degree
+	// Priority: table-level parallel_workers setting > GUC max_parallel_workers_per_gather > default
+	ULONG ulParallelWorkers = 2;  // default

Review Comment:
   add a macros.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2447257475


##########
src/backend/gpopt/gpdbwrappers.cpp:
##########
@@ -2548,6 +2555,19 @@ gpdb::GetForeignServerId(Oid reloid)
 	return 0;
 }
 
+int16
+gpdb::GetAppendOnlySegmentFilesCount(Relation rel)
+{
+	GP_WRAP_START;

Review Comment:
   Need to support pax also.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448540212


##########
src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp:
##########
@@ -7282,4 +7425,75 @@ CTranslatorDXLToPlStmt::IsIndexForOrderBy(
 	}
 	return false;
 }
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CTranslatorDXLToPlStmt::ExtractParallelWorkersFromDXL
+//
+//	@doc:
+//		Extract parallel workers count from DXL node tree recursively.
+//		Since parallel degree is uniform across all parallel scans in a query,
+//		returns the first parallel degree found from any CDXLPhysicalParallelTableScan,
+//		or 1 if no parallel scan exists.
+//
+//---------------------------------------------------------------------------
+ULONG
+CTranslatorDXLToPlStmt::ExtractParallelWorkersFromDXL(const CDXLNode *dxlnode)
+{
+	if (nullptr == dxlnode)
+	{
+		return 1;
+	}
+
+	CDXLOperator *dxlop = dxlnode->GetOperator();
+	if (EdxlopPhysicalParallelTableScan == dxlop->GetDXLOperator())
+	{
+		// Return parallel workers from the parallel table scan operator
+		// All parallel scans in the query share the same parallel degree
+		CDXLPhysicalParallelTableScan *parallel_scan_dxlop =
+			CDXLPhysicalParallelTableScan::Cast(dxlop);
+		return parallel_scan_dxlop->UlParallelWorkers();
+	}
+	else if (EdxlopPhysicalTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalIndexScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalIndexOnlyScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalBitmapTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicBitmapTableScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalForeignScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicForeignScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicIndexScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalDynamicIndexOnlyScan == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalValuesScan == dxlop->GetDXLOperator())
+	{
+		// Non-parallel scans (table, index, bitmap, foreign, values)
+		// These are leaf nodes in terms of parallel worker extraction
+		// Return 1 to indicate no parallel workers
+		return 1;
+	}
+	else if (EdxlopPhysicalMotionGather == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionBroadcast == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionRedistribute == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionRandom == dxlop->GetDXLOperator() ||
+			 EdxlopPhysicalMotionRoutedDistribute == dxlop->GetDXLOperator())
+	{
+		// Motion node creates a slice boundary - do not recurse into child
+		// The child's parallel workers belong to the sending slice, not receiving slice
+		// Return 0 to indicate the receiving slice (current slice) has no parallel workers
+		return 1;
+	}
+
+	// Recursively check child nodes, return early when first parallel scan is found
+	for (ULONG ul = 0; ul < dxlnode->Arity(); ul++)
+	{
+		ULONG child_parallel_workers = ExtractParallelWorkersFromDXL((*dxlnode)[ul]);

Review Comment:
   this is called by `TranslateDXLMotion`, 
   
   HashJoin
   |─ Motion A
          |─ Parallel Scan on A (parallel_workers = 3)
   |─ Motion B
          |─ Parallel Scan on B (parallel_workers = 6)
   
   if called by Motion A, so parallel_workers=3,  if called by Motion B, parallel_workers=6.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r3231141360


##########
src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp:
##########
@@ -2416,6 +2422,120 @@ CCostModelGPDB::CostScan(CMemoryPool *,	 // mp
 }
 
 
+//---------------------------------------------------------------------------
+//	@function:
+//		CCostModelGPDB::CostParallelTableScan
+//
+//	@doc:
+//		Cost of parallel table scan
+//
+//---------------------------------------------------------------------------
+CCost
+CCostModelGPDB::CostParallelTableScan(CMemoryPool *mp,
+									  CExpressionHandle &exprhdl,
+									  const CCostModelGPDB *pcmgpdb,
+									  const SCostingInfo *pci)
+{
+	GPOS_ASSERT(nullptr != pcmgpdb);
+	GPOS_ASSERT(nullptr != pci);
+
+	COperator *pop = exprhdl.Pop();
+	GPOS_ASSERT(COperator::EopPhysicalParallelTableScan == pop->Eopid());
+
+	// Get the parallel table scan operator
+	CPhysicalParallelTableScan *popParallelScan =
+		CPhysicalParallelTableScan::PopConvert(pop);
+	ULONG ulWorkers = popParallelScan->UlParallelWorkers();
+
+	// If only 1 worker, use regular scan cost
+	if (ulWorkers <= 1)
+	{
+		return CostScan(mp, exprhdl, pcmgpdb, pci);
+	}
+
+	// Get base scan parameters
+	const CDouble dInitScan =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpInitScanFactor)
+			->Get();
+	const CDouble dTableWidth =
+		CPhysicalScan::PopConvert(pop)->PstatsBaseTable()->Width();
+	const CDouble dTableScanCostUnit =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpTableScanCostUnit)
+			->Get();
+
+	// Calculate base scan cost
+	CDouble dBaseScanCost = dInitScan + pci->Rows() * dTableWidth * dTableScanCostUnit;
+
+	// Calculate parallel efficiency (decreases with more workers)
+	CDouble dParallelEfficiency = CalculateParallelEfficiency(ulWorkers);
+
+	// Parallel scan cost = base cost / (workers * efficiency)
+	CDouble dParallelScanCost = dBaseScanCost / (ulWorkers * dParallelEfficiency);
+
+	// Add worker startup cost
+	CDouble dWorkerStartupCost = GetWorkerStartupCost(pcmgpdb, ulWorkers);
+
+	// Total cost
+	return CCost(pci->NumRebinds() * (dParallelScanCost + dWorkerStartupCost));

Review Comment:
   fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz merged PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2447353768


##########
src/backend/gpopt/translate/CTranslatorRelcacheToDXL.cpp:
##########
@@ -618,14 +617,31 @@ CTranslatorRelcacheToDXL::RetrieveRel(CMemoryPool *mp, CMDAccessor *md_accessor,
 			CMDIdGPDB(IMDId::EmdidGeneral, gpdb::GetForeignServerId(oid));
 	}
 
-	md_rel = GPOS_NEW(mp) CMDRelationGPDB(
+	CMDRelationGPDB *md_rel_gpdb = GPOS_NEW(mp) CMDRelationGPDB(
 		mp, mdid, mdname, is_temporary, rel_storage_type, dist,
 		mdcol_array, distr_cols, distr_op_families, part_keys, part_types,
 		partition_oids, convert_hash_to_random, keyset_array,
 		md_index_info_array, check_constraint_mdids, mdpart_constraint,
 		foreign_server_mdid, rel->rd_rel->reltuples);
 
-	return md_rel;
+	// Set segment file count for AO/AOCO tables
+	// Skip partitioned tables as they don't have physical storage (only leaf partitions do)
+	if ((rel_storage_type == IMDRelation::ErelstorageAppendOnlyRows ||
+		 rel_storage_type == IMDRelation::ErelstorageAppendOnlyCols) &&
+		rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
+	{
+		INT seg_file_count = gpdb::GetAppendOnlySegmentFilesCount(rel.get());

Review Comment:
   Good to set SetSegFileCount directly here? Since SetSegFileCount is only the concept of AO/AOCS table instead of generic concept.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2447392437


##########
src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp:
##########
@@ -0,0 +1,284 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CPhysicalParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/CDistributionSpec.h"
+#include "gpopt/base/CDistributionSpecHashed.h"
+#include "gpopt/base/CDistributionSpecRandom.h"
+#include "gpopt/base/CDistributionSpecWorkerRandom.h"
+#include "gpopt/base/CDistributionSpecSingleton.h"
+#include "gpopt/base/CUtils.h"
+#include "gpopt/base/CEnfdDistribution.h"
+#include "gpopt/base/CEnfdRewindability.h"
+#include "gpopt/base/COptimizationContext.h"
+#include "gpopt/base/CRewindabilitySpec.h"
+#include "gpopt/base/CDrvdPropPlan.h"
+#include "gpopt/metadata/CName.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp)
+	: CPhysicalTableScan(mp, GPOS_NEW(mp) CName(GPOS_NEW(mp) CWStringConst(GPOS_WSZ_LIT("parallel_table"))), nullptr, nullptr),
+	  m_ulParallelWorkers(1),
+	  m_pdsWorkerDistribution(nullptr)
+{
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp,
+													   const CName *pnameAlias,
+													   CTableDescriptor *ptabdesc,
+													   CColRefArray *pdrgpcrOutput,
+													   ULONG ulParallelWorkers)
+	: CPhysicalTableScan(mp, pnameAlias, ptabdesc, pdrgpcrOutput),
+	  m_ulParallelWorkers(ulParallelWorkers),
+	  m_pdsWorkerDistribution(nullptr)
+{
+	GPOS_ASSERT(ulParallelWorkers > 0);
+	GPOS_ASSERT(nullptr != m_pds);
+	// Create worker-level distribution based on table's segment distribution
+	if (ulParallelWorkers > 0 && nullptr != m_pds)
+	{
+		// Create worker-level random distribution using the table's distribution as base
+		// The base CPhysicalScan already sets up m_pds from the table descriptor
+		m_pdsWorkerDistribution = CDistributionSpecWorkerRandom::PdsCreateWorkerRandom(mp, ulParallelWorkers, m_pds);
+	}
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::~CPhysicalParallelTableScan
+//
+//	@doc:
+//		dtor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::~CPhysicalParallelTableScan()
+{
+	CRefCount::SafeRelease(m_pdsWorkerDistribution);
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::HashValue
+//
+//	@doc:
+//		Combine pointer for table descriptor, parallel workers and Eop
+//
+//---------------------------------------------------------------------------
+ULONG
+CPhysicalParallelTableScan::HashValue() const
+{
+	ULONG ulHash = gpos::CombineHashes(CPhysicalTableScan::HashValue(),
+									   gpos::HashValue<ULONG>(&m_ulParallelWorkers));
+	return ulHash;
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::Matches
+//
+//	@doc:
+//		match operator
+//
+//---------------------------------------------------------------------------
+BOOL
+CPhysicalParallelTableScan::Matches(COperator *pop) const
+{
+	if (Eopid() != pop->Eopid())
+	{
+		return false;
+	}
+
+	CPhysicalParallelTableScan *popParallelTableScan = 
+		CPhysicalParallelTableScan::PopConvert(pop);
+	
+	return CPhysicalTableScan::Matches(pop) && 
+		   m_ulParallelWorkers == popParallelTableScan->UlParallelWorkers();
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::OsPrint
+//
+//	@doc:
+//		debug print
+//
+//---------------------------------------------------------------------------
+IOstream &
+CPhysicalParallelTableScan::OsPrint(IOstream &os) const
+{
+	os << SzId() << " ";
+
+	// alias of table as referenced in the query
+	m_pnameAlias->OsPrint(os);
+
+	// actual name of table in catalog and columns
+	os << " (";
+	m_ptabdesc->Name().OsPrint(os);
+	os << "), Columns: [";
+
+	CUtils::OsPrintDrgPcr(os, m_pdrgpcrOutput);
+	os << "], Workers: " << m_ulParallelWorkers;
+
+	return os;
+}
+
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::PdsDerive
+//
+//	@doc:
+//		Derive distribution for parallel table scan
+//
+//---------------------------------------------------------------------------
+CDistributionSpec *
+CPhysicalParallelTableScan::PdsDerive(CMemoryPool *mp, CExpressionHandle &exprhdl) const
+{
+	// If we have a pre-computed worker distribution, use it
+	if (nullptr != m_pdsWorkerDistribution)
+	{
+		m_pdsWorkerDistribution->AddRef();
+		return m_pdsWorkerDistribution;
+	}
+
+	// Otherwise, derive from the base physical scan
+	// This uses the m_pds member from CPhysicalScan
+	return CPhysicalScan::PdsDerive(mp, exprhdl);

Review Comment:
   When m_pdsWorkerDistribution == nullptr?



##########
src/backend/gporca/libgpopt/src/search/CGroup.cpp:
##########
@@ -534,6 +535,25 @@ CGroup::SetState(EState estNewState)
 }
 
 
+//---------------------------------------------------------------------------
+//	@function:
+//		CGroup::SetMemo
+//
+//	@doc:
+//		Set containing memo reference;
+//
+//---------------------------------------------------------------------------
+void
+CGroup::SetMemo(CMemo *pmemo)

Review Comment:
   Why we need to change container memo?



##########
src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp:
##########
@@ -2410,6 +2416,120 @@ CCostModelGPDB::CostScan(CMemoryPool *,	 // mp
 }
 
 
+//---------------------------------------------------------------------------
+//	@function:
+//		CCostModelGPDB::CostParallelTableScan
+//
+//	@doc:
+//		Cost of parallel table scan
+//
+//---------------------------------------------------------------------------
+CCost
+CCostModelGPDB::CostParallelTableScan(CMemoryPool *mp,
+									  CExpressionHandle &exprhdl,
+									  const CCostModelGPDB *pcmgpdb,
+									  const SCostingInfo *pci)
+{
+	GPOS_ASSERT(nullptr != pcmgpdb);
+	GPOS_ASSERT(nullptr != pci);
+
+	COperator *pop = exprhdl.Pop();
+	GPOS_ASSERT(COperator::EopPhysicalParallelTableScan == pop->Eopid());
+
+	// Get the parallel table scan operator
+	CPhysicalParallelTableScan *popParallelScan =
+		CPhysicalParallelTableScan::PopConvert(pop);
+	ULONG ulWorkers = popParallelScan->UlParallelWorkers();
+
+	// If only 1 worker, use regular scan cost
+	if (ulWorkers <= 1)
+	{
+		return CostScan(mp, exprhdl, pcmgpdb, pci);
+	}
+
+	// Get base scan parameters
+	const CDouble dInitScan =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpInitScanFactor)
+			->Get();
+	const CDouble dTableWidth =
+		CPhysicalScan::PopConvert(pop)->PstatsBaseTable()->Width();
+	const CDouble dTableScanCostUnit =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpTableScanCostUnit)
+			->Get();
+
+	// Calculate base scan cost
+	CDouble dBaseScanCost = dInitScan + pci->Rows() * dTableWidth * dTableScanCostUnit;
+
+	// Calculate parallel efficiency (decreases with more workers)
+	CDouble dParallelEfficiency = CalculateParallelEfficiency(ulWorkers);
+
+	// Parallel scan cost = base cost / (workers * efficiency)
+	CDouble dParallelScanCost = dBaseScanCost / (ulWorkers * dParallelEfficiency);
+
+	// Add worker startup cost
+	CDouble dWorkerStartupCost = GetWorkerStartupCost(pcmgpdb, ulWorkers);
+
+	// Total cost
+	return CCost(pci->NumRebinds() * (dParallelScanCost + dWorkerStartupCost));
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CCostModelGPDB::CalculateParallelEfficiency
+//
+//	@doc:
+//		Calculate parallel efficiency factor (0-1) based on worker count
+//
+//---------------------------------------------------------------------------
+CDouble
+CCostModelGPDB::CalculateParallelEfficiency(ULONG ulWorkers)
+{
+	if (ulWorkers <= 1)
+	{
+		return 1.0;
+	}
+
+	// Efficiency decreases logarithmically with more workers
+	// Formula: efficiency = 1 / (1 + 0.1 * log2(workers))
+	// This gives: 2 workers = 0.91, 4 workers = 0.83, 8 workers = 0.77

Review Comment:
   Where is the formula from?



##########
src/backend/gporca/libgpopt/include/gpopt/base/CRewindabilitySpec.h:
##########
@@ -108,6 +108,13 @@ class CRewindabilitySpec : public CPropSpec
 		EmhtSentinel
 	};
 
+	enum EBarrierHazardType

Review Comment:
   Why we declare enum in RewindabilitySpec?



##########
src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp:
##########
@@ -0,0 +1,284 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CPhysicalParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/CDistributionSpec.h"
+#include "gpopt/base/CDistributionSpecHashed.h"
+#include "gpopt/base/CDistributionSpecRandom.h"
+#include "gpopt/base/CDistributionSpecWorkerRandom.h"
+#include "gpopt/base/CDistributionSpecSingleton.h"
+#include "gpopt/base/CUtils.h"
+#include "gpopt/base/CEnfdDistribution.h"
+#include "gpopt/base/CEnfdRewindability.h"
+#include "gpopt/base/COptimizationContext.h"
+#include "gpopt/base/CRewindabilitySpec.h"
+#include "gpopt/base/CDrvdPropPlan.h"
+#include "gpopt/metadata/CName.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp)
+	: CPhysicalTableScan(mp, GPOS_NEW(mp) CName(GPOS_NEW(mp) CWStringConst(GPOS_WSZ_LIT("parallel_table"))), nullptr, nullptr),
+	  m_ulParallelWorkers(1),
+	  m_pdsWorkerDistribution(nullptr)
+{
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp,
+													   const CName *pnameAlias,
+													   CTableDescriptor *ptabdesc,
+													   CColRefArray *pdrgpcrOutput,
+													   ULONG ulParallelWorkers)
+	: CPhysicalTableScan(mp, pnameAlias, ptabdesc, pdrgpcrOutput),
+	  m_ulParallelWorkers(ulParallelWorkers),
+	  m_pdsWorkerDistribution(nullptr)
+{
+	GPOS_ASSERT(ulParallelWorkers > 0);
+	GPOS_ASSERT(nullptr != m_pds);
+	// Create worker-level distribution based on table's segment distribution
+	if (ulParallelWorkers > 0 && nullptr != m_pds)
+	{
+		// Create worker-level random distribution using the table's distribution as base
+		// The base CPhysicalScan already sets up m_pds from the table descriptor
+		m_pdsWorkerDistribution = CDistributionSpecWorkerRandom::PdsCreateWorkerRandom(mp, ulParallelWorkers, m_pds);

Review Comment:
   It looks like a composite spec, e.g, original spec + parallel workers. Is WorkerRandom accurate here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "zhangwenchao-123 (via GitHub)" <gi...@apache.org>.
zhangwenchao-123 commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r3231086692


##########
src/backend/gporca/libgpopt/src/base/CCostContext.cpp:
##########
@@ -505,6 +505,7 @@ CCostContext::FBetterThan(const CCostContext *pcc) const
 	if (CDistributionSpec::EdtHashed == Pdpplan()->Pds()->Edt() &&
 		CDistributionSpec::EdtRandom == pcc->Pdpplan()->Pds()->Edt())
 	{
+		// FIXME:

Review Comment:
   Here forget or just mark?



##########
src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp:
##########
@@ -2489,11 +2609,15 @@ CCostModelGPDB::Cost(
 		case COperator::EopPhysicalDynamicTableScan:
 		case COperator::EopPhysicalForeignScan:
 		case COperator::EopPhysicalDynamicForeignScan:
-
 		{
 			return CostScan(m_mp, exprhdl, this, pci);
 		}
 
+		case COperator::EopPhysicalParallelTableScan:
+		{
+			return CostParallelTableScan(m_mp, exprhdl, this, pci);

Review Comment:
   How about we move the CostParallelTableScan logic into CostScan?



##########
src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp:
##########
@@ -2416,6 +2422,120 @@ CCostModelGPDB::CostScan(CMemoryPool *,	 // mp
 }
 
 
+//---------------------------------------------------------------------------
+//	@function:
+//		CCostModelGPDB::CostParallelTableScan
+//
+//	@doc:
+//		Cost of parallel table scan
+//
+//---------------------------------------------------------------------------
+CCost
+CCostModelGPDB::CostParallelTableScan(CMemoryPool *mp,
+									  CExpressionHandle &exprhdl,
+									  const CCostModelGPDB *pcmgpdb,
+									  const SCostingInfo *pci)
+{
+	GPOS_ASSERT(nullptr != pcmgpdb);
+	GPOS_ASSERT(nullptr != pci);
+
+	COperator *pop = exprhdl.Pop();
+	GPOS_ASSERT(COperator::EopPhysicalParallelTableScan == pop->Eopid());
+
+	// Get the parallel table scan operator
+	CPhysicalParallelTableScan *popParallelScan =
+		CPhysicalParallelTableScan::PopConvert(pop);
+	ULONG ulWorkers = popParallelScan->UlParallelWorkers();
+
+	// If only 1 worker, use regular scan cost
+	if (ulWorkers <= 1)
+	{
+		return CostScan(mp, exprhdl, pcmgpdb, pci);
+	}
+
+	// Get base scan parameters
+	const CDouble dInitScan =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpInitScanFactor)
+			->Get();
+	const CDouble dTableWidth =
+		CPhysicalScan::PopConvert(pop)->PstatsBaseTable()->Width();
+	const CDouble dTableScanCostUnit =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpTableScanCostUnit)
+			->Get();
+
+	// Calculate base scan cost
+	CDouble dBaseScanCost = dInitScan + pci->Rows() * dTableWidth * dTableScanCostUnit;
+
+	// Calculate parallel efficiency (decreases with more workers)
+	CDouble dParallelEfficiency = CalculateParallelEfficiency(ulWorkers);
+
+	// Parallel scan cost = base cost / (workers * efficiency)
+	CDouble dParallelScanCost = dBaseScanCost / (ulWorkers * dParallelEfficiency);
+
+	// Add worker startup cost
+	CDouble dWorkerStartupCost = GetWorkerStartupCost(pcmgpdb, ulWorkers);
+
+	// Total cost
+	return CCost(pci->NumRebinds() * (dParallelScanCost + dWorkerStartupCost));

Review Comment:
   Whether this CCost(pci->NumRebinds()) * dParallelScanCost + dWorkerStartupCost;?
   



##########
src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp:
##########
@@ -0,0 +1,265 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CXformGet2ParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/xforms/CXformGet2ParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/COptCtxt.h"
+#include "gpopt/hints/CHintUtils.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+#include "gpopt/operators/CLogicalGet.h"
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+#include "gpopt/optimizer/COptimizerConfig.h"
+#include "naucrates/md/IMDRelation.h"
+#include "gpopt/search/CGroupProxy.h"
+#include "gpopt/search/CMemo.h"
+
+
+// Use gpdbwrappers for parallel checks
+extern int max_parallel_workers_per_gather;
+
+// Forward declarations for gpdbwrappers functions
+namespace gpdb {
+	bool IsParallelModeOK(void);
+}
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::FHasParallelIncompatibleOps
+//
+//	@doc:
+//		Check if memo contains logical operators that are incompatible
+//		with parallel execution (CTE, Dynamic scans, Foreign scans, etc.)
+//
+//---------------------------------------------------------------------------
+BOOL
+CXformGet2ParallelTableScan::FHasParallelIncompatibleOps(CExpressionHandle &exprhdl)
+{
+	CGroupExpression *pgexprHandle = exprhdl.Pgexpr();
+	if (nullptr == pgexprHandle)
+	{
+		return false;
+	}
+
+	CGroup *pgroup = pgexprHandle->Pgroup();
+	if (nullptr == pgroup)
+	{
+		return false;
+	}
+
+	CMemo *pmemo = pgroup->Pmemo();
+	if (nullptr == pmemo)
+	{
+		return false;
+	}
+
+	// Iterate through all groups in memo to check for parallel-incompatible operations
+	const ULONG_PTR ulGroups = pmemo->UlpGroups();
+	for (ULONG_PTR ul = 0; ul < ulGroups; ul++)
+	{
+		CGroup *pgroupCurrent = pmemo->Pgroup(ul);
+		if (nullptr == pgroupCurrent)
+		{
+			continue;
+		}
+
+		// Check all group expressions in this group using CGroupProxy
+		CGroupProxy gp(pgroupCurrent);
+		CGroupExpression *pgexpr = gp.PgexprFirst();
+		while (nullptr != pgexpr)
+		{
+			COperator::EOperatorId eopid = pgexpr->Pop()->Eopid();
+
+			// Check for CTE-related operators (incompatible with parallel execution)
+			if (COperator::EopLogicalCTEProducer == eopid ||
+				COperator::EopLogicalCTEConsumer == eopid ||
+				COperator::EopLogicalSequence == eopid ||
+				COperator::EopLogicalSequenceProject == eopid)
+			{
+				return true;
+			}
+
+			if (COperator::EopLogicalUnion == eopid ||
+				COperator::EopLogicalUnionAll == eopid ||
+				COperator::EopLogicalIntersect == eopid ||
+				COperator::EopLogicalIntersectAll == eopid ||
+				COperator::EopLogicalDifference == eopid ||
+				COperator::EopLogicalDifferenceAll == eopid)
+			{
+				// Set operations are not supported in parallel plans
+				return true;
+			}
+
+			pgexpr = gp.PgexprNext(pgexpr);
+		}
+	}
+
+	return false;
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::CXformGet2ParallelTableScan
+//
+//	@doc:
+//		Ctor
+//
+//---------------------------------------------------------------------------
+CXformGet2ParallelTableScan::CXformGet2ParallelTableScan(CMemoryPool *mp)
+	: CXformImplementation(
+		  // pattern
+		  GPOS_NEW(mp) CExpression(mp, GPOS_NEW(mp) CLogicalGet(mp)))
+{
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::Exfp
+//
+//	@doc:
+//		Compute promise of xform based on GUC enable_parallel
+//		Uses unified parallel degree from max_parallel_workers_per_gather
+//
+//---------------------------------------------------------------------------
+CXform::EXformPromise
+CXformGet2ParallelTableScan::Exfp(CExpressionHandle &exprhdl) const
+{
+	// Check if parallel plans are enabled in context and parallel processing is safe
+	if (!gpdb::IsParallelModeOK())
+	{
+		return CXform::ExfpNone;
+	}
+
+	// Check for parallel-incompatible operations that would conflict with parallel scans
+	if (FHasParallelIncompatibleOps(exprhdl))
+	{
+		return CXform::ExfpNone;
+	}
+
+	CLogicalGet *popGet = CLogicalGet::PopConvert(exprhdl.Pop());
+	CTableDescriptor *ptabdesc = popGet->Ptabdesc();
+
+	// Don't use parallel scan for replicated tables
+	if (ptabdesc->GetRelDistribution() == IMDRelation::EreldistrReplicated ||
+		ptabdesc->GetRelDistribution() == IMDRelation::EreldistrMasterOnly ||
+		COptCtxt::PoctxtFromTLS()->HasReplicatedTables())
+	{
+		//FIXME: Should we consider replicated tables.
+		return CXform::ExfpNone;
+	}
+
+	// For AO/AOCO tables, check segfilecount early to avoid useless transformation
+	CMDAccessor *md_accessor = COptCtxt::PoctxtFromTLS()->Pmda();
+	const IMDRelation *pmdrel = md_accessor->RetrieveRel(ptabdesc->MDId());
+	IMDRelation::Erelstoragetype storage_type = pmdrel->RetrieveRelStorageType();
+
+	// Check if this is an AO/AOCO table
+	if (storage_type == IMDRelation::ErelstorageAppendOnlyRows ||
+		storage_type == IMDRelation::ErelstorageAppendOnlyCols)
+	{
+		INT seg_file_count = pmdrel->SegFileCount();
+		// Only reject if segfilecount is explicitly known to be 0 or 1
+		// -1 means unknown (e.g., from DXL deserialization), so allow parallel in that case
+		if (seg_file_count >= 0 && seg_file_count <= 1)
+		{
+			// If segfilecount is 0 or 1, parallel execution is pointless
+			// Reject parallel scan early in promise phase
+			GPOS_TRACE_FORMAT("CXformGet2ParallelTableScan rejected for table %ls: AO/AOCO table has segfilecount=%d (needs >1 for parallel scan)",
+							ptabdesc->Name().Pstr()->GetBuffer(), seg_file_count);
+			return CXform::ExfpNone;
+		}
+	}
+
+	// High promise for parallel scan when enabled
+	// All tables will use the same parallel degree from max_parallel_workers_per_gather
+	return CXform::ExfpHigh;
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::Transform
+//
+//	@doc:
+//		Actual transformation
+//
+//---------------------------------------------------------------------------
+void
+CXformGet2ParallelTableScan::Transform(CXformContext *pxfctxt, CXformResult *pxfres,
+									   CExpression *pexpr) const
+{
+	GPOS_ASSERT(nullptr != pxfctxt);
+	GPOS_ASSERT(FPromising(pxfctxt->Pmp(), this, pexpr));
+	GPOS_ASSERT(FCheckPattern(pexpr));
+
+	CLogicalGet *popGet = CLogicalGet::PopConvert(pexpr->Pop());
+
+	CMemoryPool *mp = pxfctxt->Pmp();
+
+	// create/extract components for alternative
+	CName *pname = GPOS_NEW(mp) CName(mp, popGet->Name());
+
+	CTableDescriptor *ptabdesc = popGet->Ptabdesc();
+	ptabdesc->AddRef();
+
+	CColRefArray *pdrgpcrOutput = popGet->PdrgpcrOutput();
+	GPOS_ASSERT(nullptr != pdrgpcrOutput);
+	pdrgpcrOutput->AddRef();
+
+	// Determine parallel workers degree
+	// Priority: table-level parallel_workers setting > GUC max_parallel_workers_per_gather > default
+	ULONG ulParallelWorkers = 2;  // default

Review Comment:
   Add a macros for parallel workers 2?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r3231133001


##########
src/backend/gporca/libgpopt/src/base/CCostContext.cpp:
##########
@@ -505,6 +505,7 @@ CCostContext::FBetterThan(const CCostContext *pcc) const
 	if (CDistributionSpec::EdtHashed == Pdpplan()->Pds()->Edt() &&
 		CDistributionSpec::EdtRandom == pcc->Pdpplan()->Pds()->Edt())
 	{
+		// FIXME:

Review Comment:
   removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r3231127229


##########
src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp:
##########
@@ -2489,11 +2609,15 @@ CCostModelGPDB::Cost(
 		case COperator::EopPhysicalDynamicTableScan:
 		case COperator::EopPhysicalForeignScan:
 		case COperator::EopPhysicalDynamicForeignScan:
-
 		{
 			return CostScan(m_mp, exprhdl, this, pci);
 		}
 
+		case COperator::EopPhysicalParallelTableScan:
+		{
+			return CostParallelTableScan(m_mp, exprhdl, this, pci);

Review Comment:
   better not.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448567650


##########
src/backend/gporca/libgpopt/src/search/CGroup.cpp:
##########
@@ -534,6 +535,25 @@ CGroup::SetState(EState estNewState)
 }
 
 
+//---------------------------------------------------------------------------
+//	@function:
+//		CGroup::SetMemo
+//
+//	@doc:
+//		Set containing memo reference;
+//
+//---------------------------------------------------------------------------
+void
+CGroup::SetMemo(CMemo *pmemo)

Review Comment:
   not change, just link with CMemo.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448562298


##########
src/backend/gpopt/gpdbwrappers.cpp:
##########
@@ -2548,6 +2555,19 @@ gpdb::GetForeignServerId(Oid reloid)
 	return 0;
 }
 
+int16
+gpdb::GetAppendOnlySegmentFilesCount(Relation rel)
+{
+	GP_WRAP_START;

Review Comment:
   supported.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448764672


##########
src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp:
##########
@@ -0,0 +1,284 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CPhysicalParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/operators/CPhysicalParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/CDistributionSpec.h"
+#include "gpopt/base/CDistributionSpecHashed.h"
+#include "gpopt/base/CDistributionSpecRandom.h"
+#include "gpopt/base/CDistributionSpecWorkerRandom.h"
+#include "gpopt/base/CDistributionSpecSingleton.h"
+#include "gpopt/base/CUtils.h"
+#include "gpopt/base/CEnfdDistribution.h"
+#include "gpopt/base/CEnfdRewindability.h"
+#include "gpopt/base/COptimizationContext.h"
+#include "gpopt/base/CRewindabilitySpec.h"
+#include "gpopt/base/CDrvdPropPlan.h"
+#include "gpopt/metadata/CName.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp)
+	: CPhysicalTableScan(mp, GPOS_NEW(mp) CName(GPOS_NEW(mp) CWStringConst(GPOS_WSZ_LIT("parallel_table"))), nullptr, nullptr),
+	  m_ulParallelWorkers(1),
+	  m_pdsWorkerDistribution(nullptr)
+{
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::CPhysicalParallelTableScan
+//
+//	@doc:
+//		ctor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::CPhysicalParallelTableScan(CMemoryPool *mp,
+													   const CName *pnameAlias,
+													   CTableDescriptor *ptabdesc,
+													   CColRefArray *pdrgpcrOutput,
+													   ULONG ulParallelWorkers)
+	: CPhysicalTableScan(mp, pnameAlias, ptabdesc, pdrgpcrOutput),
+	  m_ulParallelWorkers(ulParallelWorkers),
+	  m_pdsWorkerDistribution(nullptr)
+{
+	GPOS_ASSERT(ulParallelWorkers > 0);
+	GPOS_ASSERT(nullptr != m_pds);
+	// Create worker-level distribution based on table's segment distribution
+	if (ulParallelWorkers > 0 && nullptr != m_pds)
+	{
+		// Create worker-level random distribution using the table's distribution as base
+		// The base CPhysicalScan already sets up m_pds from the table descriptor
+		m_pdsWorkerDistribution = CDistributionSpecWorkerRandom::PdsCreateWorkerRandom(mp, ulParallelWorkers, m_pds);
+	}
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::~CPhysicalParallelTableScan
+//
+//	@doc:
+//		dtor
+//
+//---------------------------------------------------------------------------
+CPhysicalParallelTableScan::~CPhysicalParallelTableScan()
+{
+	CRefCount::SafeRelease(m_pdsWorkerDistribution);
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::HashValue
+//
+//	@doc:
+//		Combine pointer for table descriptor, parallel workers and Eop
+//
+//---------------------------------------------------------------------------
+ULONG
+CPhysicalParallelTableScan::HashValue() const
+{
+	ULONG ulHash = gpos::CombineHashes(CPhysicalTableScan::HashValue(),
+									   gpos::HashValue<ULONG>(&m_ulParallelWorkers));
+	return ulHash;
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::Matches
+//
+//	@doc:
+//		match operator
+//
+//---------------------------------------------------------------------------
+BOOL
+CPhysicalParallelTableScan::Matches(COperator *pop) const
+{
+	if (Eopid() != pop->Eopid())
+	{
+		return false;
+	}
+
+	CPhysicalParallelTableScan *popParallelTableScan = 
+		CPhysicalParallelTableScan::PopConvert(pop);
+	
+	return CPhysicalTableScan::Matches(pop) && 
+		   m_ulParallelWorkers == popParallelTableScan->UlParallelWorkers();
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::OsPrint
+//
+//	@doc:
+//		debug print
+//
+//---------------------------------------------------------------------------
+IOstream &
+CPhysicalParallelTableScan::OsPrint(IOstream &os) const
+{
+	os << SzId() << " ";
+
+	// alias of table as referenced in the query
+	m_pnameAlias->OsPrint(os);
+
+	// actual name of table in catalog and columns
+	os << " (";
+	m_ptabdesc->Name().OsPrint(os);
+	os << "), Columns: [";
+
+	CUtils::OsPrintDrgPcr(os, m_pdrgpcrOutput);
+	os << "], Workers: " << m_ulParallelWorkers;
+
+	return os;
+}
+
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CPhysicalParallelTableScan::PdsDerive
+//
+//	@doc:
+//		Derive distribution for parallel table scan
+//
+//---------------------------------------------------------------------------
+CDistributionSpec *
+CPhysicalParallelTableScan::PdsDerive(CMemoryPool *mp, CExpressionHandle &exprhdl) const
+{
+	// If we have a pre-computed worker distribution, use it
+	if (nullptr != m_pdsWorkerDistribution)
+	{
+		m_pdsWorkerDistribution->AddRef();
+		return m_pdsWorkerDistribution;
+	}
+
+	// Otherwise, derive from the base physical scan
+	// This uses the m_pds member from CPhysicalScan
+	return CPhysicalScan::PdsDerive(mp, exprhdl);

Review Comment:
   will not happen



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2452196255


##########
src/test/regress/GNUmakefile:
##########
@@ -223,6 +223,12 @@ installcheck-cbdb-parallel: all tablespace-setup twophase_pqexecparams
 	$(pg_regress_installcheck) $(REGRESS_OPTS) --schedule=$(srcdir)/parallel_schedule --schedule=$(srcdir)/greenplum_schedule --max-connections=5 $(EXTRA_TESTS) --exclude-tests explain \
 	)
 
+installcheck-orca-parallel: all tablespace-setup twophase_pqexecparams
+	( \
+	export PGOPTIONS='-c optimizer=on -c enable_parallel=true -c min_parallel_table_scan_size=0 -c min_parallel_index_scan_size=0 -c parallel_setup_cost=0'; \
+	$(pg_regress_installcheck) $(REGRESS_OPTS) --exclude-file=$(srcdir)/excluded_tests.conf --schedule=$(srcdir)/parallel_schedule --schedule=$(srcdir)/greenplum_schedule --max-connections=4 --ignore-plans $(EXTRA_TESTS) \

Review Comment:
   like installcheck-cbdb-parallel,  it use force_parallel_mode to ignore-plans .



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz closed pull request #1398: orca: implement intra-segment parallel table scan support
URL: https://github.com/apache/cloudberry/pull/1398


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "avamingli (via GitHub)" <gi...@apache.org>.
avamingli commented on PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#issuecomment-3440518729

   Add some cases to  test the plan?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2452193740


##########
src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp:
##########
@@ -0,0 +1,265 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CXformGet2ParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/xforms/CXformGet2ParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/COptCtxt.h"
+#include "gpopt/hints/CHintUtils.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+#include "gpopt/operators/CLogicalGet.h"
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+#include "gpopt/optimizer/COptimizerConfig.h"
+#include "naucrates/md/IMDRelation.h"
+#include "gpopt/search/CGroupProxy.h"
+#include "gpopt/search/CMemo.h"
+
+
+// Use gpdbwrappers for parallel checks
+extern int max_parallel_workers_per_gather;
+
+// Forward declarations for gpdbwrappers functions
+namespace gpdb {
+	bool IsParallelModeOK(void);
+}
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::FHasParallelIncompatibleOps
+//
+//	@doc:
+//		Check if memo contains logical operators that are incompatible
+//		with parallel execution (CTE, Dynamic scans, Foreign scans, etc.)
+//
+//---------------------------------------------------------------------------
+BOOL
+CXformGet2ParallelTableScan::FHasParallelIncompatibleOps(CExpressionHandle &exprhdl)
+{
+	CGroupExpression *pgexprHandle = exprhdl.Pgexpr();
+	if (nullptr == pgexprHandle)
+	{
+		return false;
+	}
+
+	CGroup *pgroup = pgexprHandle->Pgroup();
+	if (nullptr == pgroup)
+	{
+		return false;
+	}
+
+	CMemo *pmemo = pgroup->Pmemo();
+	if (nullptr == pmemo)
+	{
+		return false;
+	}
+
+	// Iterate through all groups in memo to check for parallel-incompatible operations
+	const ULONG_PTR ulGroups = pmemo->UlpGroups();
+	for (ULONG_PTR ul = 0; ul < ulGroups; ul++)
+	{
+		CGroup *pgroupCurrent = pmemo->Pgroup(ul);
+		if (nullptr == pgroupCurrent)
+		{
+			continue;
+		}
+
+		// Check all group expressions in this group using CGroupProxy
+		CGroupProxy gp(pgroupCurrent);
+		CGroupExpression *pgexpr = gp.PgexprFirst();
+		while (nullptr != pgexpr)
+		{
+			COperator::EOperatorId eopid = pgexpr->Pop()->Eopid();
+
+			// Check for CTE-related operators (incompatible with parallel execution)
+			if (COperator::EopLogicalCTEProducer == eopid ||

Review Comment:
   have not found the way.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "zhangwenchao-123 (via GitHub)" <gi...@apache.org>.
zhangwenchao-123 commented on PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#issuecomment-4436699452

   LGTM.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2447303179


##########
src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp:
##########
@@ -2415,15 +2527,34 @@ CTranslatorDXLToPlStmt::TranslateDXLMotion(
 	sendslice->directDispatch.contentIds = NIL;
 	sendslice->directDispatch.haveProcessedAnyCalculations = false;
 
+	// set parallel workers if needed
+	ULONG child_index = motion_dxlop->GetRelationChildIdx();
+	CDXLNode *child_dxlnode = (*motion_dxlnode)[child_index];
+	ULONG child_parallel_workers = ExtractParallelWorkersFromDXL(child_dxlnode);
+	if (child_parallel_workers > 1)
+	{
+		// Determine parallel workers based on enable_parallel and gang type
+		bool supports_parallel = (sendslice->gangType == GANGTYPE_PRIMARY_READER ||
+		                          sendslice->gangType == GANGTYPE_PRIMARY_WRITER);
+
+		if (enable_parallel && supports_parallel)

Review Comment:
   Why we test enable_parallel here. Should we just use supports_parallel to decide whether apply  parallel?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "my-ship-it (via GitHub)" <gi...@apache.org>.
my-ship-it commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2450580541


##########
src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp:
##########
@@ -0,0 +1,265 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CXformGet2ParallelTableScan.cpp
+ *
+ * IDENTIFICATION
+ *	  src/backend/gporca/libgpopt/src/xforms/CXformGet2ParallelTableScan.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "gpopt/xforms/CXformGet2ParallelTableScan.h"
+
+#include "gpos/base.h"
+
+#include "gpopt/base/COptCtxt.h"
+#include "gpopt/hints/CHintUtils.h"
+#include "gpopt/metadata/CTableDescriptor.h"
+#include "gpopt/operators/CExpressionHandle.h"
+#include "gpopt/operators/CLogicalGet.h"
+#include "gpopt/operators/CPhysicalParallelTableScan.h"
+#include "gpopt/optimizer/COptimizerConfig.h"
+#include "naucrates/md/IMDRelation.h"
+#include "gpopt/search/CGroupProxy.h"
+#include "gpopt/search/CMemo.h"
+
+
+// Use gpdbwrappers for parallel checks
+extern int max_parallel_workers_per_gather;
+
+// Forward declarations for gpdbwrappers functions
+namespace gpdb {
+	bool IsParallelModeOK(void);
+}
+
+using namespace gpopt;
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CXformGet2ParallelTableScan::FHasParallelIncompatibleOps
+//
+//	@doc:
+//		Check if memo contains logical operators that are incompatible
+//		with parallel execution (CTE, Dynamic scans, Foreign scans, etc.)
+//
+//---------------------------------------------------------------------------
+BOOL
+CXformGet2ParallelTableScan::FHasParallelIncompatibleOps(CExpressionHandle &exprhdl)
+{
+	CGroupExpression *pgexprHandle = exprhdl.Pgexpr();
+	if (nullptr == pgexprHandle)
+	{
+		return false;
+	}
+
+	CGroup *pgroup = pgexprHandle->Pgroup();
+	if (nullptr == pgroup)
+	{
+		return false;
+	}
+
+	CMemo *pmemo = pgroup->Pmemo();
+	if (nullptr == pmemo)
+	{
+		return false;
+	}
+
+	// Iterate through all groups in memo to check for parallel-incompatible operations
+	const ULONG_PTR ulGroups = pmemo->UlpGroups();
+	for (ULONG_PTR ul = 0; ul < ulGroups; ul++)
+	{
+		CGroup *pgroupCurrent = pmemo->Pgroup(ul);
+		if (nullptr == pgroupCurrent)
+		{
+			continue;
+		}
+
+		// Check all group expressions in this group using CGroupProxy
+		CGroupProxy gp(pgroupCurrent);
+		CGroupExpression *pgexpr = gp.PgexprFirst();
+		while (nullptr != pgexpr)
+		{
+			COperator::EOperatorId eopid = pgexpr->Pop()->Eopid();
+
+			// Check for CTE-related operators (incompatible with parallel execution)
+			if (COperator::EopLogicalCTEProducer == eopid ||

Review Comment:
   Do not consider the case of local parallelizability for now, right? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#issuecomment-3426966577

   
   > Please add more test cases
   see src/test/regress:installcheck-orca-parallel
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org


Re: [PR] orca: implement intra-segment parallel table scan support [cloudberry]

Posted by "yjhjstz (via GitHub)" <gi...@apache.org>.
yjhjstz commented on code in PR #1398:
URL: https://github.com/apache/cloudberry/pull/1398#discussion_r2448611852


##########
src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp:
##########
@@ -2410,6 +2416,120 @@ CCostModelGPDB::CostScan(CMemoryPool *,	 // mp
 }
 
 
+//---------------------------------------------------------------------------
+//	@function:
+//		CCostModelGPDB::CostParallelTableScan
+//
+//	@doc:
+//		Cost of parallel table scan
+//
+//---------------------------------------------------------------------------
+CCost
+CCostModelGPDB::CostParallelTableScan(CMemoryPool *mp,
+									  CExpressionHandle &exprhdl,
+									  const CCostModelGPDB *pcmgpdb,
+									  const SCostingInfo *pci)
+{
+	GPOS_ASSERT(nullptr != pcmgpdb);
+	GPOS_ASSERT(nullptr != pci);
+
+	COperator *pop = exprhdl.Pop();
+	GPOS_ASSERT(COperator::EopPhysicalParallelTableScan == pop->Eopid());
+
+	// Get the parallel table scan operator
+	CPhysicalParallelTableScan *popParallelScan =
+		CPhysicalParallelTableScan::PopConvert(pop);
+	ULONG ulWorkers = popParallelScan->UlParallelWorkers();
+
+	// If only 1 worker, use regular scan cost
+	if (ulWorkers <= 1)
+	{
+		return CostScan(mp, exprhdl, pcmgpdb, pci);
+	}
+
+	// Get base scan parameters
+	const CDouble dInitScan =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpInitScanFactor)
+			->Get();
+	const CDouble dTableWidth =
+		CPhysicalScan::PopConvert(pop)->PstatsBaseTable()->Width();
+	const CDouble dTableScanCostUnit =
+		pcmgpdb->GetCostModelParams()
+			->PcpLookup(CCostModelParamsGPDB::EcpTableScanCostUnit)
+			->Get();
+
+	// Calculate base scan cost
+	CDouble dBaseScanCost = dInitScan + pci->Rows() * dTableWidth * dTableScanCostUnit;
+
+	// Calculate parallel efficiency (decreases with more workers)
+	CDouble dParallelEfficiency = CalculateParallelEfficiency(ulWorkers);
+
+	// Parallel scan cost = base cost / (workers * efficiency)
+	CDouble dParallelScanCost = dBaseScanCost / (ulWorkers * dParallelEfficiency);
+
+	// Add worker startup cost
+	CDouble dWorkerStartupCost = GetWorkerStartupCost(pcmgpdb, ulWorkers);
+
+	// Total cost
+	return CCost(pci->NumRebinds() * (dParallelScanCost + dWorkerStartupCost));
+}
+
+//---------------------------------------------------------------------------
+//	@function:
+//		CCostModelGPDB::CalculateParallelEfficiency
+//
+//	@doc:
+//		Calculate parallel efficiency factor (0-1) based on worker count
+//
+//---------------------------------------------------------------------------
+CDouble
+CCostModelGPDB::CalculateParallelEfficiency(ULONG ulWorkers)
+{
+	if (ulWorkers <= 1)
+	{
+		return 1.0;
+	}
+
+	// Efficiency decreases logarithmically with more workers
+	// Formula: efficiency = 1 / (1 + 0.1 * log2(workers))
+	// This gives: 2 workers = 0.91, 4 workers = 0.83, 8 workers = 0.77

Review Comment:
   A practical approximation to Amdahl's Law
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org