Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6061,7 +6061,7 @@ public void testRefreshMaterializedView()
assertQueryFails("REFRESH MATERIALIZED VIEW test_customer_view_5 WHERE nationname = 'UNITED STATES'", ".*Refresh materialized view by column nationname is not supported.*");
assertQueryFails("REFRESH MATERIALIZED VIEW test_customer_view_5 WHERE regionkey = 1 OR nationkey = 24", ".*Only logical AND is supported in WHERE clause.*");
assertQueryFails("REFRESH MATERIALIZED VIEW test_customer_view_5 WHERE regionkey + nationkey = 25", ".*Only columns specified on literals are supported in WHERE clause.*");
assertQueryFails("REFRESH MATERIALIZED VIEW test_customer_view_5", ".*mismatched input '<EOF>'\\. Expecting: '\\.', 'WHERE'.*");
assertQueryFails("REFRESH MATERIALIZED VIEW test_customer_view_5", ".*Refresh Materialized View without predicates is not supported.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,10 @@ protected Scope visitRefreshMaterializedView(RefreshMaterializedView node, Optio
// Use AllowAllAccessControl; otherwise Analyzer will check SELECT permission on the materialized view, which is not necessary.
StatementAnalyzer viewAnalyzer = new StatementAnalyzer(analysis, metadata, sqlParser, new AllowAllAccessControl(), session, warningCollector);
Scope viewScope = viewAnalyzer.analyze(node.getTarget(), scope);
Map<SchemaTableName, Expression> tablePredicates = extractTablePredicates(viewName, node.getWhere(), viewScope, metadata, session);
if (!node.getWhere().isPresent()) {
throw new SemanticException(NOT_SUPPORTED, node, "Refresh Materialized View without predicates is not supported.");
}
Map<SchemaTableName, Expression> tablePredicates = extractTablePredicates(viewName, node.getWhere().get(), viewScope, metadata, session);

Query viewQuery = parseView(view.getOriginalSql(), viewName, node);
Query refreshQuery = tablePredicates.containsKey(toSchemaTableName(viewName)) ?
Expand Down Expand Up @@ -907,7 +910,10 @@ private Optional<RelationType> analyzeBaseTableForRefreshMaterializedView(Table
// Use AllowAllAccessControl; otherwise Analyzer will check SELECT permission on the materialized view, which is not necessary.
StatementAnalyzer viewAnalyzer = new StatementAnalyzer(analysis, metadata, sqlParser, new AllowAllAccessControl(), session, warningCollector);
Scope viewScope = viewAnalyzer.analyze(refreshMaterializedView.getTarget(), scope);
Map<SchemaTableName, Expression> tablePredicates = extractTablePredicates(viewName, refreshMaterializedView.getWhere(), viewScope, metadata, session);
if (!refreshMaterializedView.getWhere().isPresent()) {
throw new SemanticException(NOT_SUPPORTED, "Refresh Materialized View without predicates is not supported.");
}
Map<SchemaTableName, Expression> tablePredicates = extractTablePredicates(viewName, refreshMaterializedView.getWhere().get(), viewScope, metadata, session);

SchemaTableName baseTableName = toSchemaTableName(createQualifiedObjectName(session, baseTable, baseTable.getName(), metadata));
if (tablePredicates.containsKey(baseTableName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,12 @@ select && sameElement(node.getFrom(), from) && sameElement(node.getWhere(), wher
protected Node visitRefreshMaterializedView(RefreshMaterializedView node, C context)
{
Node table = process(node.getTarget(), context);
Node where = process(node.getWhere(), context);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
if (node.getTarget() == table && node.getWhere() == where) {
Optional<Expression> where = process(node.getWhere(), context);
if (node.getTarget() == table && sameElement(node.getWhere(), where)) {
return node;
}

return new RefreshMaterializedView((Table) table, (Expression) where);
return new RefreshMaterializedView((Table) table, where);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ statement
(COMMENT string)?
(WITH properties)? AS (query | '('query')') #createMaterializedView
| DROP MATERIALIZED VIEW (IF EXISTS)? qualifiedName #dropMaterializedView
| REFRESH MATERIALIZED VIEW qualifiedName WHERE booleanExpression #refreshMaterializedView
| REFRESH MATERIALIZED VIEW qualifiedName
(WHERE where=booleanExpression)? #refreshMaterializedView
| CREATE (OR REPLACE)? TEMPORARY? FUNCTION functionName=qualifiedName
'(' (sqlParameterDeclaration (',' sqlParameterDeclaration)*)? ')'
RETURNS returnType=type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,12 @@ protected Void visitDropMaterializedView(DropMaterializedView node, Integer cont
protected Void visitRefreshMaterializedView(RefreshMaterializedView node, Integer context)
{
builder.append("REFRESH MATERIALIZED VIEW ")
.append(formatName(node.getTarget().getName()))
.append(" WHERE ")
.append(formatExpression(node.getWhere(), parameters));
.append(formatName(node.getTarget().getName()));

if (node.getWhere().isPresent()) {
builder.append(" WHERE ")
.append(formatExpression(node.getWhere().get(), parameters));
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public Node visitRefreshMaterializedView(SqlBaseParser.RefreshMaterializedViewCo
return new RefreshMaterializedView(
getLocation(context),
new Table(getLocation(context), getQualifiedName(context.qualifiedName())),
(Expression) visit(context.booleanExpression()));
visitIfPresent(context.booleanExpression(), Expression.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ protected R visitCreateMaterializedView(CreateMaterializedView node, C context)
protected R visitRefreshMaterializedView(RefreshMaterializedView node, C context)
{
process(node.getTarget(), context);
process(node.getWhere(), context);
node.getWhere().ifPresent(where -> process(where, context));

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public class RefreshMaterializedView
extends Statement
{
private final Table target;
private final Expression where;
private final Optional<Expression> where;

public RefreshMaterializedView(Table target, Expression where)
public RefreshMaterializedView(Table target, Optional<Expression> where)
{
this(Optional.empty(), target, where);
}

public RefreshMaterializedView(NodeLocation location, Table target, Expression where)
public RefreshMaterializedView(NodeLocation location, Table target, Optional<Expression> where)
{
this(Optional.of(location), target, where);
}

private RefreshMaterializedView(Optional<NodeLocation> location, Table target, Expression where)
private RefreshMaterializedView(Optional<NodeLocation> location, Table target, Optional<Expression> where)
{
super(location);
this.target = requireNonNull(target, "target is null");
Expand All @@ -50,7 +50,7 @@ public Table getTarget()
return target;
}

public Expression getWhere()
public Optional<Expression> getWhere()
{
return where;
}
Expand All @@ -64,7 +64,10 @@ public <R, C> R accept(AstVisitor<R, C> visitor, C context)
@Override
public List<Node> getChildren()
{
return ImmutableList.of(where);
ImmutableList.Builder<Node> nodes = ImmutableList.builder();
nodes.add(target);
where.ifPresent(nodes::add);
return nodes.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1569,13 +1569,21 @@ public void testRefreshMaterializedView()
assertStatement(
"REFRESH MATERIALIZED VIEW a WHERE p = 'x'",
new RefreshMaterializedView(
table(QualifiedName.of("a")),
new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new Identifier("p"), new StringLiteral("x"))));
table(QualifiedName.of("a")), Optional.of(
new ComparisonExpression(ComparisonExpression.Operator.EQUAL,
new Identifier("p"),
new StringLiteral("x")))));
assertStatement(
"REFRESH MATERIALIZED VIEW a.b WHERE p = 'x'",
new RefreshMaterializedView(
Comment thread
zation99 marked this conversation as resolved.
table(QualifiedName.of("a", "b")),
new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new Identifier("p"), new StringLiteral("x"))));
table(QualifiedName.of("a", "b")), Optional.of(
new ComparisonExpression(ComparisonExpression.Operator.EQUAL,
new Identifier("p"),
new StringLiteral("x")))));

assertStatement(
"REFRESH MATERIALIZED VIEW mv",
new RefreshMaterializedView(table(QualifiedName.of("mv")), Optional.empty()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,12 @@ select && sameElement(node.getFrom(), from) && sameElement(node.getWhere(), wher
protected Node visitRefreshMaterializedView(RefreshMaterializedView node, C context)
{
Node table = process(node.getTarget(), context);
Node where = process(node.getWhere(), context);
if (node.getTarget() == table && node.getWhere() == where) {
Optional<Expression> where = process(node.getWhere(), context);
if (node.getTarget() == table && sameElement(node.getWhere(), where)) {
return node;
}

return new RefreshMaterializedView((Table) table, (Expression) where);
return new RefreshMaterializedView((Table) table, where);
}

@Override
Expand Down
Loading