|
37 | 37 | import com.facebook.presto.sql.tree.Lateral;
|
38 | 38 | import com.facebook.presto.sql.tree.Node;
|
39 | 39 | import com.facebook.presto.sql.tree.NodeRef;
|
| 40 | +import com.facebook.presto.sql.tree.QualifiedName; |
40 | 41 | import com.facebook.presto.sql.tree.Query;
|
41 | 42 | import com.facebook.presto.sql.tree.QuerySpecification;
|
42 | 43 | import com.facebook.presto.sql.tree.Relation;
|
|
48 | 49 | import com.facebook.presto.sql.tree.Union;
|
49 | 50 | import com.facebook.presto.sql.tree.Unnest;
|
50 | 51 | import com.facebook.presto.sql.tree.Values;
|
| 52 | +import com.facebook.presto.sql.tree.WithQuery; |
51 | 53 | import com.google.common.collect.HashMultimap;
|
52 | 54 | import com.google.common.collect.ImmutableSet;
|
53 | 55 |
|
|
57 | 59 | import java.util.Map.Entry;
|
58 | 60 | import java.util.Set;
|
59 | 61 |
|
| 62 | +import static com.google.common.base.Preconditions.checkState; |
| 63 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
60 | 64 | import static com.google.common.collect.Sets.intersection;
|
61 | 65 | import static java.lang.String.format;
|
62 | 66 |
|
@@ -271,6 +275,15 @@ protected Void visitQuerySpecification(QuerySpecification querySpec, Context con
|
271 | 275 | return null;
|
272 | 276 | }
|
273 | 277 |
|
| 278 | + @Override |
| 279 | + protected Void visitWithQuery(WithQuery withQuery, Context context) |
| 280 | + { |
| 281 | + context.copyFieldIdsToExploreForWithQuery(withQuery); |
| 282 | + process(withQuery.getQuery(), context); |
| 283 | + |
| 284 | + return null; |
| 285 | + } |
| 286 | + |
274 | 287 | @Override
|
275 | 288 | protected Void visitSampledRelation(SampledRelation sampledRelation, Context context)
|
276 | 289 | {
|
@@ -493,5 +506,19 @@ private void addFieldIdToExplore(FieldId fieldId)
|
493 | 506 | {
|
494 | 507 | fieldsToExplore.put(fieldId.getRelationId(), fieldId);
|
495 | 508 | }
|
| 509 | + |
| 510 | + // Associate the relation from the with clause with the fieldIdsToExplore that we collected for it |
| 511 | + // when processing the main part of the query |
| 512 | + public void copyFieldIdsToExploreForWithQuery(WithQuery withQuery) |
| 513 | + { |
| 514 | + QualifiedName name = QualifiedName.of(withQuery.getName().getValue()); |
| 515 | + List<RelationId> relationIds = fieldsToExplore.keySet().stream() |
| 516 | + .filter(key -> key.getSourceNode() instanceof Table && ((Table) key.getSourceNode()).getName().equals(name)) |
| 517 | + .collect(toImmutableList()); |
| 518 | + checkState(relationIds.size() == 1, "expected only one matching relationId"); |
| 519 | + fieldsToExplore.putAll( |
| 520 | + RelationId.of(withQuery.getQuery().getQueryBody()), |
| 521 | + fieldsToExplore.get(relationIds.get(0))); |
| 522 | + } |
496 | 523 | }
|
497 | 524 | }
|
0 commit comments