|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.spark; |
| 15 | + |
| 16 | +import com.facebook.presto.Session; |
| 17 | +import com.facebook.presto.cost.StatsAndCosts; |
| 18 | +import com.facebook.presto.metadata.FunctionAndTypeManager; |
| 19 | +import com.facebook.presto.spi.WarningCollector; |
| 20 | +import com.facebook.presto.sql.analyzer.FeaturesConfig; |
| 21 | +import com.facebook.presto.sql.analyzer.FunctionsConfig; |
| 22 | +import com.facebook.presto.sql.planner.Plan; |
| 23 | +import com.facebook.presto.sql.planner.TypeProvider; |
| 24 | +import com.facebook.presto.sql.planner.optimizations.PlanNodeSearcher; |
| 25 | +import com.facebook.presto.sql.planner.plan.ExchangeNode; |
| 26 | +import com.facebook.presto.sql.planner.planPrinter.PlanPrinter; |
| 27 | +import com.facebook.presto.testing.LocalQueryRunner; |
| 28 | +import com.facebook.presto.tpch.TpchConnectorFactory; |
| 29 | +import com.google.common.collect.ImmutableMap; |
| 30 | +import org.testng.annotations.AfterClass; |
| 31 | +import org.testng.annotations.BeforeClass; |
| 32 | +import org.testng.annotations.Test; |
| 33 | + |
| 34 | +import static com.facebook.presto.SystemSessionProperties.PREFER_SORT_MERGE_JOIN; |
| 35 | +import static com.facebook.presto.testing.TestingSession.testSessionBuilder; |
| 36 | +import static com.facebook.presto.tpch.TpchMetadata.TINY_SCHEMA_NAME; |
| 37 | +import static org.testng.Assert.assertTrue; |
| 38 | + |
| 39 | +/** |
| 40 | + * Test to verify that the sort push-down to exchange provider enhancement is working correctly. |
| 41 | + * This test verifies that: |
| 42 | + * 1. SortMergeJoinOptimizer creates ExchangeNode with OrderingScheme |
| 43 | + * 2. The enhanced exchange functionality can be accessed programmatically |
| 44 | + */ |
| 45 | +public class TestPrestoSparkSortMergeJoinQueries |
| 46 | +{ |
| 47 | + private LocalQueryRunner queryRunner; |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public void setUp() |
| 51 | + { |
| 52 | + Session testSession = testSessionBuilder() |
| 53 | + .setCatalog("tpch") |
| 54 | + .setSchema(TINY_SCHEMA_NAME) |
| 55 | + .build(); |
| 56 | + |
| 57 | + queryRunner = new LocalQueryRunner(testSession, new FeaturesConfig(), new FunctionsConfig()); |
| 58 | + queryRunner.createCatalog("tpch", new TpchConnectorFactory(), ImmutableMap.of()); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterClass |
| 62 | + public void tearDown() |
| 63 | + { |
| 64 | + queryRunner.close(); |
| 65 | + queryRunner = null; |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testSortMergeJoinVsHashJoinPlanDifference() |
| 70 | + { |
| 71 | + // Test with sort merge join enabled |
| 72 | + Session sortMergeJoinSession = Session.builder(queryRunner.getDefaultSession()) |
| 73 | + .setSystemProperty(PREFER_SORT_MERGE_JOIN, "true") |
| 74 | + .build(); |
| 75 | + |
| 76 | + // Test with sort merge join disabled (hash join) |
| 77 | + Session hashJoinSession = Session.builder(queryRunner.getDefaultSession()) |
| 78 | + .setSystemProperty(PREFER_SORT_MERGE_JOIN, "false") |
| 79 | + .build(); |
| 80 | + |
| 81 | + String sql = "SELECT o.orderkey, l.partkey " + |
| 82 | + "FROM orders o " + |
| 83 | + "INNER JOIN lineitem l ON o.custkey = l.suppkey"; |
| 84 | + |
| 85 | + Plan sortMergeJoinPlan = queryRunner.inTransaction(sortMergeJoinSession, transactionSession -> { |
| 86 | + return queryRunner.createPlan(transactionSession, sql, WarningCollector.NOOP); |
| 87 | + }); |
| 88 | + Plan hashJoinPlan = queryRunner.inTransaction(hashJoinSession, transactionSession -> { |
| 89 | + return queryRunner.createPlan(transactionSession, sql, WarningCollector.NOOP); |
| 90 | + }); |
| 91 | + |
| 92 | + // Print both plans for comparison |
| 93 | + TypeProvider typeProvider1 = TypeProvider.fromVariables(sortMergeJoinPlan.getRoot().getOutputVariables()); |
| 94 | + TypeProvider typeProvider2 = TypeProvider.fromVariables(hashJoinPlan.getRoot().getOutputVariables()); |
| 95 | + FunctionAndTypeManager functionAndTypeManager = queryRunner.getMetadata().getFunctionAndTypeManager(); |
| 96 | + |
| 97 | + String sortMergePlanText = PlanPrinter.textLogicalPlan( |
| 98 | + sortMergeJoinPlan.getRoot(), |
| 99 | + typeProvider1, |
| 100 | + StatsAndCosts.empty(), |
| 101 | + functionAndTypeManager, |
| 102 | + sortMergeJoinSession, |
| 103 | + 0); |
| 104 | + |
| 105 | + String hashJoinPlanText = PlanPrinter.textLogicalPlan( |
| 106 | + hashJoinPlan.getRoot(), |
| 107 | + typeProvider2, |
| 108 | + StatsAndCosts.empty(), |
| 109 | + functionAndTypeManager, |
| 110 | + hashJoinSession, |
| 111 | + 0); |
| 112 | + |
| 113 | + System.out.println("📋 SORT MERGE JOIN PLAN:"); |
| 114 | + System.out.println("====================================="); |
| 115 | + System.out.println(sortMergePlanText); |
| 116 | + System.out.println("====================================="); |
| 117 | + |
| 118 | + System.out.println("📋 HASH JOIN PLAN:"); |
| 119 | + System.out.println("====================================="); |
| 120 | + System.out.println(hashJoinPlanText); |
| 121 | + System.out.println("====================================="); |
| 122 | + |
| 123 | + // Count exchange nodes with ordering in both plans |
| 124 | + long sortMergeExchangeCount = PlanNodeSearcher.searchFrom(sortMergeJoinPlan.getRoot()) |
| 125 | + .where(ExchangeNode.class::isInstance) |
| 126 | + .findAll() |
| 127 | + .stream() |
| 128 | + .map(ExchangeNode.class::cast) |
| 129 | + .filter(exchange -> exchange.getOrderingScheme().isPresent()) |
| 130 | + .count(); |
| 131 | + |
| 132 | + long hashJoinExchangeCount = PlanNodeSearcher.searchFrom(hashJoinPlan.getRoot()) |
| 133 | + .where(ExchangeNode.class::isInstance) |
| 134 | + .findAll() |
| 135 | + .stream() |
| 136 | + .map(ExchangeNode.class::cast) |
| 137 | + .filter(exchange -> exchange.getOrderingScheme().isPresent()) |
| 138 | + .count(); |
| 139 | + |
| 140 | + System.out.println("🔍 Plan Analysis Results:"); |
| 141 | + System.out.println(" Sort Merge Join Plan - Exchanges with ordering: " + sortMergeExchangeCount); |
| 142 | + System.out.println(" Hash Join Plan - Exchanges with ordering: " + hashJoinExchangeCount); |
| 143 | + |
| 144 | + // Verify that our enhancement is working - sort merge should have more or equal ordered exchanges |
| 145 | + assertTrue(sortMergeExchangeCount >= hashJoinExchangeCount, |
| 146 | + "Sort merge join plan should have same or more exchanges with ordering than hash join plan"); |
| 147 | + } |
| 148 | +} |
0 commit comments