@@ -240,66 +240,86 @@ protected RelationPlan visitTable(final Table table, final Void context) {
240240 }
241241
242242 final Scope scope = analysis .getScope (table );
243+ final Query namedQuery = analysis .getNamedQuery (table );
243244
244245 // Common Table Expression
245- final Query namedQuery = analysis .getNamedQuery (table );
246246 if (namedQuery != null ) {
247- if (analysis .isExpandableQuery (namedQuery )) {
248- // recursive cte
249- throw new SemanticException ("unexpected recursive cte" );
247+ return processNamedQuery (table , namedQuery , scope );
248+ }
249+
250+ return processPhysicalTable (table , scope );
251+ }
252+
253+ private RelationPlan processNamedQuery (Table table , Query namedQuery , Scope scope ) {
254+ if (analysis .isExpandableQuery (namedQuery )) {
255+ throw new SemanticException ("unexpected recursive cte" );
256+ }
257+
258+ if (namedQuery .isMaterialized () && namedQuery .isDone ()) {
259+ RelationPlan materializedCtePlan = processMaterializedCte (table , scope );
260+ if (materializedCtePlan != null ) {
261+ return materializedCtePlan ;
250262 }
263+ }
251264
252- if (namedQuery .isMaterialized () && namedQuery .isDone ()) {
253- CteDataStore dataStore = queryContext .getCteDataStore (table );
254- if (dataStore != null ) {
255- List <Symbol > cteSymbols =
256- dataStore .getTableSchema ().getColumns ().stream ()
257- .map (column -> symbolAllocator .newSymbol (column .getName (), column .getType ()))
258- .collect (Collectors .toList ());
259-
260- // CTE Scan Node
261- CteScanNode cteScanNode =
262- new CteScanNode (idAllocator .genPlanNodeId (), table .getName (), cteSymbols , dataStore );
263-
264- List <Integer > columnIndex2TsBlockColumnIndexList =
265- dataStore .getColumnIndex2TsBlockColumnIndexList ();
266- if (columnIndex2TsBlockColumnIndexList == null ) {
267- return new RelationPlan (cteScanNode , scope , cteSymbols , outerContext );
268- }
265+ return processRegularCte (table , namedQuery , scope );
266+ }
269267
270- List <Symbol > outputSymbols = new ArrayList <>();
271- Assignments .Builder assignments = Assignments .builder ();
272- for (int index : columnIndex2TsBlockColumnIndexList ) {
273- Symbol columnSymbol = cteSymbols .get (index );
274- outputSymbols .add (columnSymbol );
275- assignments .put (columnSymbol , columnSymbol .toSymbolReference ());
276- }
268+ private RelationPlan processMaterializedCte (Table table , Scope scope ) {
269+ CteDataStore dataStore = queryContext .getCteDataStore (table );
270+ if (dataStore == null ) {
271+ return null ;
272+ }
277273
278- // Project Node
279- ProjectNode projectNode =
280- new ProjectNode (
281- queryContext . getQueryId (). genPlanNodeId (), cteScanNode , assignments . build ());
274+ List < Symbol > cteSymbols =
275+ dataStore . getTableSchema (). getColumns (). stream ()
276+ . map ( column -> symbolAllocator . newSymbol ( column . getName (), column . getType ()))
277+ . collect ( Collectors . toList ());
282278
283- return new RelationPlan (projectNode , scope , outputSymbols , outerContext );
284- }
285- }
279+ CteScanNode cteScanNode =
280+ new CteScanNode (idAllocator .genPlanNodeId (), table .getName (), cteSymbols , dataStore );
286281
287- RelationPlan subPlan = process (namedQuery , null );
288- // Add implicit coercions if view query produces types that don't match the declared output
289- // types of the view (e.g., if the underlying tables referenced by the view changed)
290- List <Type > types =
291- analysis .getOutputDescriptor (table ).getAllFields ().stream ()
292- .map (Field ::getType )
293- .collect (toImmutableList ());
282+ List <Integer > columnIndex2TsBlockColumnIndexList =
283+ dataStore .getColumnIndex2TsBlockColumnIndexList ();
284+ if (columnIndex2TsBlockColumnIndexList == null ) {
285+ return new RelationPlan (cteScanNode , scope , cteSymbols , outerContext );
286+ }
294287
295- NodeAndMappings coerced = coerce (subPlan , types , symbolAllocator , idAllocator );
296- return new RelationPlan (coerced .getNode (), scope , coerced .getFields (), outerContext );
288+ List <Symbol > outputSymbols = new ArrayList <>();
289+ Assignments .Builder assignments = Assignments .builder ();
290+ for (int index : columnIndex2TsBlockColumnIndexList ) {
291+ Symbol columnSymbol = cteSymbols .get (index );
292+ outputSymbols .add (columnSymbol );
293+ assignments .put (columnSymbol , columnSymbol .toSymbolReference ());
297294 }
298295
296+ // Project Node
297+ ProjectNode projectNode =
298+ new ProjectNode (
299+ queryContext .getQueryId ().genPlanNodeId (), cteScanNode , assignments .build ());
300+
301+ return new RelationPlan (projectNode , scope , outputSymbols , outerContext );
302+ }
303+
304+ private RelationPlan processRegularCte (Table table , Query namedQuery , Scope scope ) {
305+ RelationPlan subPlan = process (namedQuery , null );
306+ // Add implicit coercions if view query produces types that don't match the declared output
307+ // types of the view (e.g., if the underlying tables referenced by the view changed)
308+ List <Type > types =
309+ analysis .getOutputDescriptor (table ).getAllFields ().stream ()
310+ .map (Field ::getType )
311+ .collect (toImmutableList ());
312+
313+ NodeAndMappings coerced = coerce (subPlan , types , symbolAllocator , idAllocator );
314+ return new RelationPlan (coerced .getNode (), scope , coerced .getFields (), outerContext );
315+ }
316+
317+ private RelationPlan processPhysicalTable (Table table , Scope scope ) {
299318 final ImmutableList .Builder <Symbol > outputSymbolsBuilder = ImmutableList .builder ();
300319 final ImmutableMap .Builder <Symbol , ColumnSchema > symbolToColumnSchema = ImmutableMap .builder ();
301320 final Collection <Field > fields = scope .getRelationType ().getAllFields ();
302321 final QualifiedName qualifiedName = analysis .getRelationName (table );
322+
303323 if (!qualifiedName .getPrefix ().isPresent ()) {
304324 throw new IllegalStateException ("Table " + table .getName () + " has no prefix!" );
305325 }
@@ -327,7 +347,6 @@ protected RelationPlan visitTable(final Table table, final Void context) {
327347 }
328348
329349 final List <Symbol > outputSymbols = outputSymbolsBuilder .build ();
330-
331350 final Map <Symbol , ColumnSchema > tableColumnSchema = symbolToColumnSchema .build ();
332351 analysis .addTableSchema (qualifiedObjectName , tableColumnSchema );
333352
@@ -359,12 +378,6 @@ protected RelationPlan visitTable(final Table table, final Void context) {
359378 tableColumnSchema ,
360379 tagAndAttributeIndexMap );
361380 return new RelationPlan (tableScanNode , scope , outputSymbols , outerContext );
362-
363- // Collection<Field> fields = analysis.getMaterializedViewStorageTableFields(node);
364- // Query namedQuery = analysis.getNamedQuery(node);
365- // Collection<Field> fields = analysis.getMaterializedViewStorageTableFields(node);
366- // plan = addRowFilters(node, plan);
367- // plan = addColumnMasks(node, plan);
368381 }
369382
370383 @ Override
0 commit comments