简体   繁体   English

如何修改 RelNode 树?

[英]How to modify a RelNode tree?

I am using Apache Calcite to validate and rewrite SQL based on policies that put certain restrictions on these SQL queries.我正在使用 Apache Calcite 根据对这些 SQL 查询施加某些限制的策略来验证和重写 SQL。 I am trying to modify a RelNode tree in order to rewrite the query to enforce these restrictions.我正在尝试修改RelNode树以重写查询以强制执行这些限制。 I want to be able to remove certain parts from a query (after it has been validated).我希望能够从查询中删除某些部分(在验证之后)。 For example, I want to be able to remove projection fields (which I managed to do using RelBuilder.projectExcept ) and to remove a table scan and its corresponding column references from a query.例如,我希望能够删除投影字段(我设法使用RelBuilder.projectExcept )并从查询中删除表扫描及其对应的列引用。

Simple example:简单的例子:

SELECT a.foo, b.bar, c.baz
FROM a, b, c
WHERE a.index = b.index AND b.index = c.index

Let's say we want to remove table c from the query, to get to the following:假设我们要从查询中删除表c ,以得到以下结果:

SELECT a.foo, b.bar
FROM a, b
WHERE a.index = b.index

I have tried using RelBuilder but this does not support removing nodes from the tree.我试过使用RelBuilder但这不支持从树中删除节点。 I have also thought about an approach using RelVisitor but this seems quite complicated for this purpose.我也考虑过使用RelVisitor的方法,但这似乎非常复杂。 I think it would essentially require building a new RelNode tree.我认为它本质上需要构建一个新的RelNode树。 Lastly, implementing rules using RelRule seems like it would be a suitable option, but I cannot figure out from the Calcite documentation how to remove a particular RelNode and how to parameterize this (eg conditionally apply the rule if the table name is c ).最后,使用RelRule实施规则似乎是一个合适的选择,但我无法从 Calcite 文档中弄清楚如何删除特定的RelNode以及如何对其进行参数化(例如,如果表名是c ,则有条件地应用规则)。

Can anyone point me to a good approach?谁能指出我的好方法? Alternatively, would it be easier to just modify the SqlNode parse tree?或者,只修改SqlNode解析树会更容易吗?

A rule transforms a RelNode to an equivalent RelNode ie both should have the same row.规则将RelNode转换为等效RelNode ,即两者应具有相同的行。 Assumming you want to use HepPlanner with your custom rule registered, it will check eventually whether the original rel and the transformed rel have the same row using RelOptUtil#verifyTypeEquivalence .假设您想在注册了自定义规则的情况下使用HepPlanner ,它将最终使用RelOptUtil#verifyTypeEquivalence检查原始 rel 和转换后的 rel 是否具有相同的行。 I think mutating the relNode via RelVisitor or mutating the sqlNode via SqlVisitor is your best bet.我认为通过RelVisitor relNode通过SqlVisitor sqlNode你最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM