39
39
import java .nio .file .Path ;
40
40
import java .nio .file .Paths ;
41
41
import java .util .Map ;
42
+ import java .util .Map .Entry ;
42
43
import java .util .stream .Stream ;
43
44
44
45
import static com .facebook .presto .SystemSessionProperties .OPTIMIZER_USE_HISTOGRAMS ;
45
46
import static com .facebook .presto .SystemSessionProperties .SCALAR_FUNCTION_STATS_PROPAGATION_ENABLED ;
46
47
import static com .facebook .presto .spi .plan .JoinDistributionType .REPLICATED ;
47
48
import static com .facebook .presto .spi .plan .JoinType .INNER ;
48
49
import static com .facebook .presto .sql .Optimizer .PlanStage .OPTIMIZED_AND_VALIDATED ;
49
- import static com .facebook .presto .testing .TestngUtils .toDataProvider ;
50
+ import static com .facebook .presto .testing .TestngUtils .toDataProviderFromArray ;
50
51
import static com .google .common .base .Preconditions .checkState ;
51
52
import static com .google .common .base .Verify .verify ;
52
53
import static com .google .common .io .Files .createParentDirs ;
62
63
public abstract class AbstractCostBasedPlanTest
63
64
extends BasePlanTest
64
65
{
65
- private final Map <String , String > featureToOutputDir =
66
- ImmutableMap .of (OPTIMIZER_USE_HISTOGRAMS , "histogram" ,
67
- SCALAR_FUNCTION_STATS_PROPAGATION_ENABLED , "scalar_function_stats_propagation" );
66
+ private static final String NO_FEATURE_ENABLED = "no_feature_enabled" ;
67
+
68
+ private final Map <String , String > featuresMap =
69
+ ImmutableMap .of (NO_FEATURE_ENABLED , "" , "histogram" , OPTIMIZER_USE_HISTOGRAMS ,
70
+ "scalar_function_stats_propagation" , SCALAR_FUNCTION_STATS_PROPAGATION_ENABLED );
68
71
69
72
public AbstractCostBasedPlanTest (LocalQueryRunnerSupplier supplier )
70
73
{
@@ -76,33 +79,29 @@ public AbstractCostBasedPlanTest(LocalQueryRunnerSupplier supplier)
76
79
@ DataProvider
77
80
public Object [][] getQueriesDataProvider ()
78
81
{
79
- return getQueryResourcePaths ()
80
- .collect (toDataProvider ());
82
+ return featuresMap .keySet ().stream ().flatMap (feature -> getQueryResourcePaths ().map (path -> new String [] {feature , path })).collect (toDataProviderFromArray ());
81
83
}
82
84
83
85
@ Test (dataProvider = "getQueriesDataProvider" )
84
- public void test (String queryResourcePath )
85
- {
86
- assertEquals (generateQueryPlan (read (queryResourcePath )), read (getQueryPlanResourcePath (queryResourcePath )));
87
- }
88
-
89
- @ Test (dataProvider = "getQueriesDataProvider" )
90
- public void featureSpecificPlansMatch (String queryResourcePath )
86
+ public void test (String feature , String queryResourcePath )
91
87
{
92
88
String sql = read (queryResourcePath );
93
- for ( Map . Entry < String , String > featureEntry : featureToOutputDir . entrySet ( )) {
89
+ if (! feature . equals ( NO_FEATURE_ENABLED )) {
94
90
Session featureEnabledSession = Session .builder (getQueryRunner ().getDefaultSession ())
95
- .setSystemProperty (featureEntry . getKey ( ), "true" )
91
+ .setSystemProperty (featuresMap . get ( feature ), "true" )
96
92
.build ();
97
93
Session featureDisabledSession = Session .builder (getQueryRunner ().getDefaultSession ())
98
- .setSystemProperty (featureEntry . getKey ( ), "false" )
94
+ .setSystemProperty (featuresMap . get ( feature ), "false" )
99
95
.build ();
100
96
String regularPlan = generateQueryPlan (sql , featureDisabledSession );
101
97
String featureEnabledPlan = generateQueryPlan (sql , featureEnabledSession );
102
98
if (!regularPlan .equals (featureEnabledPlan )) {
103
- assertEquals (featureEnabledPlan , read (getSpecificPlanResourcePath (featureEntry . getValue () , getQueryPlanResourcePath (queryResourcePath ))));
99
+ assertEquals (featureEnabledPlan , read (getSpecificPlanResourcePath (feature , getQueryPlanResourcePath (queryResourcePath ))));
104
100
}
105
101
}
102
+ else {
103
+ assertEquals (generateQueryPlan (sql ), read (getQueryPlanResourcePath (queryResourcePath )));
104
+ }
106
105
}
107
106
108
107
private String getQueryPlanResourcePath (String queryResourcePath )
@@ -113,7 +112,7 @@ private String getQueryPlanResourcePath(String queryResourcePath)
113
112
private String getSpecificPlanResourcePath (String outDirPath , String regularPlanResourcePath )
114
113
{
115
114
Path root = Paths .get (regularPlanResourcePath );
116
- return root .getParent ().resolve (String . format ("%s/%s" , outDirPath , root .getFileName ())).toString ();
115
+ return root .getParent ().resolve (format ("%s/%s" , outDirPath , root .getFileName ())).toString ();
117
116
}
118
117
119
118
private Path getResourceWritePath (String queryResourcePath )
@@ -133,25 +132,28 @@ public void generate()
133
132
.parallel ()
134
133
.forEach (queryResourcePath -> {
135
134
try {
136
- for (Map .Entry <String , String > featureEntry : featureToOutputDir .entrySet ()) {
137
- Path queryPlanWritePath = getResourceWritePath (queryResourcePath );
138
- createParentDirs (queryPlanWritePath .toFile ());
135
+ for (Entry <String , String > featureEntry : featuresMap .entrySet ()) {
139
136
String sql = read (queryResourcePath );
140
- Session featuredisabledSession = Session .builder (getQueryRunner ().getDefaultSession ())
141
- .setSystemProperty (featureEntry .getKey (), "false" )
142
- .build ();
143
- String regularPlan = generateQueryPlan (sql , featuredisabledSession );
144
- Session featureEnabledSession = Session .builder (getQueryRunner ().getDefaultSession ())
145
- .setSystemProperty (featureEntry .getKey (), "true" )
146
- .build ();
147
-
148
- String featureEnabledPlan = generateQueryPlan (sql , featureEnabledSession );
149
- write (regularPlan .getBytes (UTF_8 ), queryPlanWritePath .toFile ());
150
- // write out the feature enabled plan if it differs
151
- if (!regularPlan .equals (featureEnabledPlan )) {
152
- Path featureEnabledPlanWritePath = getResourceWritePath (getSpecificPlanResourcePath (featureEntry .getValue (), queryResourcePath ));
153
- createParentDirs (featureEnabledPlanWritePath .toFile ());
154
- write (featureEnabledPlan .getBytes (UTF_8 ), featureEnabledPlanWritePath .toFile ());
137
+ if (!featureEntry .getKey ().equals (NO_FEATURE_ENABLED )) {
138
+ Session featureDisabledSession = Session .builder (getQueryRunner ().getDefaultSession ())
139
+ .setSystemProperty (featureEntry .getValue (), "false" )
140
+ .build ();
141
+ String regularPlan = generateQueryPlan (sql , featureDisabledSession );
142
+ Session featureEnabledSession = Session .builder (getQueryRunner ().getDefaultSession ())
143
+ .setSystemProperty (featureEntry .getValue (), "true" )
144
+ .build ();
145
+ String featureEnabledPlan = generateQueryPlan (sql , featureEnabledSession );
146
+ // write out the feature enabled plan if it differs
147
+ if (!regularPlan .equals (featureEnabledPlan )) {
148
+ Path featureEnabledPlanWritePath = getResourceWritePath (getSpecificPlanResourcePath (featureEntry .getKey (), queryResourcePath ));
149
+ createParentDirs (featureEnabledPlanWritePath .toFile ());
150
+ write (featureEnabledPlan .getBytes (UTF_8 ), featureEnabledPlanWritePath .toFile ());
151
+ }
152
+ }
153
+ else {
154
+ Path queryPlanWritePath = getResourceWritePath (queryResourcePath );
155
+ createParentDirs (queryPlanWritePath .toFile ());
156
+ write (generateQueryPlan (sql ).getBytes (UTF_8 ), queryPlanWritePath .toFile ());
155
157
}
156
158
System .out .println ("Generated expected plan for query: " + queryResourcePath );
157
159
}
0 commit comments