@@ -713,7 +713,7 @@ protected Scope visitRefreshMaterializedView(RefreshMaterializedView refreshMate
713
713
714
714
// analyze the query that creates the data
715
715
Query query = parseView (view .getOriginalSql (), name , refreshMaterializedView );
716
- Scope queryScope = process (query , scope );
716
+ Scope queryScope = analyzeView (query , name , view . getCatalog (), view . getSchema (), view . getRunAsIdentity (), view . getPath (), refreshMaterializedView . getTable (), false );
717
717
718
718
// verify the insert destination columns match the query
719
719
TableHandle targetTableHandle = metadata .getTableHandle (session , targetTable )
@@ -2259,10 +2259,10 @@ protected Scope visitTable(Table table, Optional<Scope> scope)
2259
2259
checkStorageTableNotRedirected (storageTableName );
2260
2260
TableHandle tableHandle = metadata .getTableHandle (session , storageTableName )
2261
2261
.orElseThrow (() -> semanticException (INVALID_VIEW , table , "Storage table '%s' does not exist" , storageTableName ));
2262
- return createScopeForMaterializedView (table , name , scope , materializedViewDefinition , Optional .of (tableHandle ));
2262
+ return createScopeForMaterializedView (table , name , scope , materializedViewDefinition , Optional .of (tableHandle ), true );
2263
2263
}
2264
2264
// This is a stale materialized view and should be expanded like a logical view
2265
- return createScopeForMaterializedView (table , name , scope , materializedViewDefinition , Optional .empty ());
2265
+ return createScopeForMaterializedView (table , name , scope , materializedViewDefinition , Optional .empty (), true );
2266
2266
}
2267
2267
2268
2268
// This could be a reference to a logical view or a table
@@ -2473,7 +2473,7 @@ private Scope createScopeForCommonTableExpression(Table table, Optional<Scope> s
2473
2473
return createAndAssignScope (table , scope , fields );
2474
2474
}
2475
2475
2476
- private Scope createScopeForMaterializedView (Table table , QualifiedObjectName name , Optional <Scope > scope , MaterializedViewDefinition view , Optional <TableHandle > storageTable )
2476
+ private Scope createScopeForMaterializedView (Table table , QualifiedObjectName name , Optional <Scope > scope , MaterializedViewDefinition view , Optional <TableHandle > storageTable , boolean bypassViewAccessControl )
2477
2477
{
2478
2478
return createScopeForView (
2479
2479
table ,
@@ -2486,7 +2486,8 @@ private Scope createScopeForMaterializedView(Table table, QualifiedObjectName na
2486
2486
view .getPath (),
2487
2487
view .getColumns (),
2488
2488
storageTable ,
2489
- true );
2489
+ true ,
2490
+ bypassViewAccessControl );
2490
2491
}
2491
2492
2492
2493
private Scope createScopeForView (Table table , QualifiedObjectName name , Optional <Scope > scope , ViewDefinition view )
@@ -2501,6 +2502,7 @@ private Scope createScopeForView(Table table, QualifiedObjectName name, Optional
2501
2502
view .getPath (),
2502
2503
view .getColumns (),
2503
2504
Optional .empty (),
2505
+ false ,
2504
2506
false );
2505
2507
}
2506
2508
@@ -2515,7 +2517,8 @@ private Scope createScopeForView(
2515
2517
List <CatalogSchemaName > path ,
2516
2518
List <ViewColumn > columns ,
2517
2519
Optional <TableHandle > storageTable ,
2518
- boolean isMaterializedView )
2520
+ boolean isMaterializedView ,
2521
+ boolean bypassViewAccessControl )
2519
2522
{
2520
2523
Statement statement = analysis .getStatement ();
2521
2524
if (statement instanceof CreateView viewStatement ) {
@@ -2541,7 +2544,8 @@ private Scope createScopeForView(
2541
2544
}
2542
2545
2543
2546
analysis .registerTableForView (table , name , isMaterializedView );
2544
- RelationType descriptor = analyzeView (query , name , catalog , schema , owner , path , table );
2547
+ RelationType descriptor = analyzeView (query , name , catalog , schema , owner , path , table , bypassViewAccessControl )
2548
+ .getRelationType ().withAlias (name .objectName (), null );
2545
2549
analysis .unregisterTableForView ();
2546
2550
2547
2551
checkViewStaleness (columns , descriptor .getVisibleFields (), name , table )
@@ -5045,43 +5049,46 @@ private void analyzeAggregations(
5045
5049
}
5046
5050
}
5047
5051
5048
- private RelationType analyzeView (
5052
+ private Scope analyzeView (
5049
5053
Query query ,
5050
5054
QualifiedObjectName name ,
5051
5055
Optional <String > catalog ,
5052
5056
Optional <String > schema ,
5053
5057
Optional <Identity > owner ,
5054
5058
List <CatalogSchemaName > path ,
5055
- Table node )
5059
+ Table node ,
5060
+ boolean bypassAccessControl )
5056
5061
{
5057
5062
try {
5058
5063
// run view as view owner if set; otherwise, run as session user
5059
5064
Identity identity ;
5060
- AccessControl viewAccessControl ;
5065
+ AccessControl viewAccessControl = accessControl ;
5061
5066
if (owner .isPresent ()) {
5062
5067
identity = Identity .from (owner .get ())
5063
5068
.withGroups (groupProvider .getGroups (owner .get ().getUser ()))
5064
5069
.build ();
5065
- if (owner .get ().getUser ().equals (session .getIdentity ().getUser ())) {
5066
- // View owner does not need GRANT OPTION to grant access themselves
5067
- viewAccessControl = accessControl ;
5070
+ if (bypassAccessControl ) {
5071
+ viewAccessControl = new AllowAllAccessControl ();
5068
5072
}
5069
- else {
5073
+ // View owner does not need GRANT OPTION to grant access themselves
5074
+ // All others do need GRANT OPTION
5075
+ else if (!owner .get ().getUser ().equals (session .getIdentity ().getUser ())) {
5070
5076
viewAccessControl = new ViewAccessControl (accessControl );
5071
5077
}
5072
5078
}
5073
5079
else {
5074
5080
identity = session .getIdentity ();
5075
- viewAccessControl = accessControl ;
5081
+ if (bypassAccessControl ) {
5082
+ viewAccessControl = new AllowAllAccessControl ();
5083
+ }
5076
5084
}
5077
5085
5078
5086
Session viewSession = session .createViewSession (catalog , schema , identity , path );
5079
5087
5080
5088
StatementAnalyzer analyzer = statementAnalyzerFactory
5081
5089
.withSpecializedAccessControl (viewAccessControl )
5082
5090
.createStatementAnalyzer (analysis , viewSession , warningCollector , CorrelationSupport .ALLOWED );
5083
- Scope queryScope = analyzer .analyze (query );
5084
- return queryScope .getRelationType ().withAlias (name .objectName (), null );
5091
+ return analyzer .analyze (query );
5085
5092
}
5086
5093
catch (RuntimeException e ) {
5087
5094
throw semanticException (INVALID_VIEW , node , e , "Failed analyzing stored view '%s': %s" , name , e .getMessage ());
0 commit comments