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