-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metric view to DAG and move metric rollup to subquery (#381)
* Add metric view to DAG * Move metric rollup to subquery instead of CTE
- Loading branch information
Showing
13 changed files
with
443 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
accio-sqlrewrite/src/main/java/io/accio/sqlrewrite/DateSpineInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.accio.sqlrewrite; | ||
|
||
import io.accio.base.dto.DateSpine; | ||
import io.trino.sql.tree.Query; | ||
|
||
import java.util.Set; | ||
|
||
import static io.accio.sqlrewrite.Utils.createDateSpineQuery; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class DateSpineInfo | ||
implements QueryDescriptor | ||
{ | ||
public static final String NAME = "date_spine"; | ||
|
||
private final Query query; | ||
|
||
public static DateSpineInfo get(DateSpine dateSpine) | ||
{ | ||
return new DateSpineInfo(dateSpine); | ||
} | ||
|
||
private DateSpineInfo(DateSpine dateSpine) | ||
{ | ||
this.query = createDateSpineQuery(requireNonNull(dateSpine)); | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Set<String> getRequiredObjects() | ||
{ | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public Query getQuery() | ||
{ | ||
return query; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
accio-sqlrewrite/src/main/java/io/accio/sqlrewrite/MetricInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.accio.sqlrewrite; | ||
|
||
import io.accio.base.AccioMDL; | ||
import io.accio.base.dto.CumulativeMetric; | ||
import io.accio.base.dto.Metric; | ||
import io.trino.sql.tree.Query; | ||
|
||
import java.util.Set; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class MetricInfo | ||
implements QueryDescriptor | ||
{ | ||
private final String name; | ||
private final Set<String> requiredObjects; | ||
private final Query query; | ||
|
||
public static MetricInfo get(Metric metric) | ||
{ | ||
return new MetricInfo( | ||
metric.getName(), | ||
Set.of(metric.getBaseModel()), | ||
Utils.parseMetricSql(metric)); | ||
} | ||
|
||
public static MetricInfo get(CumulativeMetric metric, AccioMDL mdl) | ||
{ | ||
return new MetricInfo( | ||
metric.getName(), | ||
Set.of(metric.getBaseModel()), | ||
Utils.parseCumulativeMetricSql(metric, mdl)); | ||
} | ||
|
||
private MetricInfo(String name, Set<String> requiredObjects, Query query) | ||
{ | ||
this.name = requireNonNull(name); | ||
this.requiredObjects = requireNonNull(requiredObjects); | ||
this.query = requireNonNull(query); | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return name; | ||
} | ||
|
||
@Override | ||
public Set<String> getRequiredObjects() | ||
{ | ||
return requiredObjects; | ||
} | ||
|
||
@Override | ||
public Query getQuery() | ||
{ | ||
return query; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
accio-sqlrewrite/src/main/java/io/accio/sqlrewrite/MetricRollupRewrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.accio.sqlrewrite; | ||
|
||
import io.accio.base.AccioMDL; | ||
import io.accio.base.SessionContext; | ||
import io.accio.sqlrewrite.analyzer.Analysis; | ||
import io.accio.sqlrewrite.analyzer.MetricRollupInfo; | ||
import io.accio.sqlrewrite.analyzer.StatementAnalyzer; | ||
import io.trino.sql.tree.AliasedRelation; | ||
import io.trino.sql.tree.FunctionRelation; | ||
import io.trino.sql.tree.Identifier; | ||
import io.trino.sql.tree.Node; | ||
import io.trino.sql.tree.NodeRef; | ||
import io.trino.sql.tree.Query; | ||
import io.trino.sql.tree.Statement; | ||
import io.trino.sql.tree.TableSubquery; | ||
|
||
import java.util.List; | ||
|
||
import static io.accio.sqlrewrite.Utils.parseMetricRollupSql; | ||
|
||
public class MetricRollupRewrite | ||
implements AccioRule | ||
{ | ||
public static final MetricRollupRewrite METRIC_ROLLUP_REWRITE = new MetricRollupRewrite(); | ||
|
||
@Override | ||
public Statement apply(Statement root, SessionContext sessionContext, AccioMDL accioMDL) | ||
{ | ||
return apply(root, sessionContext, StatementAnalyzer.analyze(root, sessionContext, accioMDL), accioMDL); | ||
} | ||
|
||
@Override | ||
public Statement apply(Statement root, SessionContext sessionContext, Analysis analysis, AccioMDL accioMDL) | ||
{ | ||
return (Statement) new Rewriter(analysis).process(root); | ||
} | ||
|
||
private static class Rewriter | ||
extends BaseRewriter<Void> | ||
{ | ||
private final Analysis analysis; | ||
|
||
Rewriter(Analysis analysis) | ||
{ | ||
this.analysis = analysis; | ||
} | ||
|
||
@Override | ||
protected Node visitFunctionRelation(FunctionRelation node, Void context) | ||
{ | ||
if (analysis.getMetricRollups().containsKey(NodeRef.of(node))) { | ||
MetricRollupInfo info = analysis.getMetricRollups().get(NodeRef.of(node)); | ||
Query query = parseMetricRollupSql(info); | ||
return new AliasedRelation(new TableSubquery(query), new Identifier(info.getMetric().getName()), List.of()); | ||
} | ||
// this should not happen, every MetricRollup node should be captured and syntax checked in StatementAnalyzer | ||
throw new IllegalArgumentException("MetricRollup node is not replaced"); | ||
} | ||
} | ||
} |
Oops, something went wrong.