@@ -160,7 +160,8 @@ protected Scope visitTable(Table node, Optional<Scope> scope)
160
160
if (withQuery .isPresent ()) {
161
161
// currently we only care about the table that is actually a model instead of a alias table that use cte table
162
162
// return empty scope here.
163
- Scope outputScope = createScopeForCommonTableExpression (node , withQuery .get (), scope );
163
+ Optional <Scope > withScope = analysis .tryGetScope (withQuery .get ().getQuery ());
164
+ Scope outputScope = createScopeForCommonTableExpression (node , withQuery .get (), withScope );
164
165
analysis .setScope (node , outputScope );
165
166
return outputScope ;
166
167
}
@@ -202,8 +203,7 @@ protected Scope visitTable(Table node, Optional<Scope> scope)
202
203
private Scope createScopeForCommonTableExpression (Table table , WithQuery withQuery , Optional <Scope > scope )
203
204
{
204
205
Query query = withQuery .getQuery ();
205
- Analysis analyzed = new Analysis (query );
206
- Optional <Scope > queryScope = Optional .ofNullable (analyze (analyzed , query , sessionContext , wrenMDL ));
206
+ Optional <Scope > queryScope = analysis .tryGetScope (query );
207
207
List <Field > fields ;
208
208
Optional <List <Identifier >> columnNames = withQuery .getColumnNames ();
209
209
if (columnNames .isPresent ()) {
@@ -243,7 +243,7 @@ private List<Field> createScopeForQuery(Query query, QualifiedName scopeName, Op
243
243
else {
244
244
SingleColumn singleColumn = (SingleColumn ) selectItem ;
245
245
String name = singleColumn .getAlias ().map (Identifier ::getValue )
246
- .or (() -> Optional .ofNullable (QueryUtil .getQualifiedName (singleColumn .getExpression ())). map ( QualifiedName :: toString ))
246
+ .or (() -> Optional .ofNullable (QueryUtil .getQualifiedName (singleColumn .getExpression ()). getSuffix () ))
247
247
.orElse (singleColumn .getExpression ().toString ());
248
248
if (scope .isPresent ()) {
249
249
Optional <Field > fieldOptional = scope .get ().getRelationType ().resolveAnyField (QueryUtil .getQualifiedName (singleColumn .getExpression ()));
@@ -402,7 +402,7 @@ private Scope analyzeFrom(QuerySpecification node, Optional<Scope> scope)
402
402
403
403
private void analyzeWhere (Expression node , Scope scope )
404
404
{
405
- ExpressionAnalysis expressionAnalysis = analyzeExpression (node , scope );
405
+ analyzeExpression (node , scope );
406
406
}
407
407
408
408
private void analyzeWindowSpecification (WindowSpecification windowSpecification , Scope scope )
@@ -561,7 +561,7 @@ protected Scope visitTableSubquery(TableSubquery node, Optional<Scope> scope)
561
561
private Optional <Scope > analyzeWith (Query node , Optional <Scope > scope )
562
562
{
563
563
if (node .getWith ().isEmpty ()) {
564
- return Optional .empty ( );
564
+ return scope . map ( s -> Scope . builder (). parent ( Optional .of ( s )). build () );
565
565
}
566
566
567
567
With with = node .getWith ().get ();
@@ -572,7 +572,8 @@ private Optional<Scope> analyzeWith(Query node, Optional<Scope> scope)
572
572
if (withScopeBuilder .containsNamedQuery (name )) {
573
573
throw new IllegalArgumentException (format ("WITH query name '%s' specified more than once" , name ));
574
574
}
575
- process (withQuery .getQuery (), withScopeBuilder .build ());
575
+ Scope withQueryScope = process (withQuery .getQuery (), withScopeBuilder .build ());
576
+ analysis .setScope (withQuery .getQuery (), withQueryScope );
576
577
withScopeBuilder .namedQuery (name , withQuery );
577
578
}
578
579
0 commit comments