OmniSciDB  94e8789169
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InjectFilterRule.java
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.apache.calcite.rel.rules;
18 
19 import org.apache.calcite.plan.RelOptRuleCall;
20 import org.apache.calcite.plan.RelOptTable;
21 import org.apache.calcite.plan.RelRule;
22 import org.apache.calcite.rel.RelNode;
23 import org.apache.calcite.rel.logical.LogicalTableScan;
24 import org.apache.calcite.rel.type.RelDataTypeFamily;
25 import org.apache.calcite.rel.type.RelDataTypeField;
26 import org.apache.calcite.rex.RexBuilder;
27 import org.apache.calcite.rex.RexNode;
28 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
29 import org.apache.calcite.sql.type.SqlTypeName;
30 import org.apache.calcite.tools.RelBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 
34 import java.util.ArrayList;
35 import java.util.HashSet;
36 import java.util.Set;
37 
38 public class InjectFilterRule extends RelRule<InjectFilterRule.Config> {
39  // goal: customer entitlements first swipe
40 
41  public static Set<String> visitedMemo = new HashSet<>();
42  final static Logger MAPDLOGGER = LoggerFactory.getLogger(InjectFilterRule.class);
44 
46  super(config);
47  this.restriction = restriction;
48  clearMemo();
49  }
50 
51  void clearMemo() {
52  visitedMemo.clear();
53  }
54 
55  @Override
56  public void onMatch(RelOptRuleCall call) {
57  LogicalTableScan parentNode = call.rel(0);
58  if (visitedMemo.contains(parentNode.toString())) {
59  return;
60  } else {
61  visitedMemo.add(parentNode.toString());
62  }
63  RelOptTable table = parentNode.getTable();
64 
65  RelDataTypeField field =
66  table.getRowType().getField(restriction.getRestrictionColumn(), false, false);
67  if (field != null) {
68  MAPDLOGGER.debug(
69  " Scan is " + parentNode.toString() + " TABLE is " + table.toString());
70  MAPDLOGGER.debug("Column " + restriction.getRestrictionColumn()
71  + " exists in table " + table.getQualifiedName());
72  RelBuilder builder = call.builder();
73  RexBuilder rBuilder = builder.getRexBuilder();
74  builder = builder.push(parentNode);
75 
76  ArrayList<RexNode> orList = new ArrayList<RexNode>();
77  for (String val : restriction.getRestrictionValues()) {
78  MAPDLOGGER.debug(" Column is " + restriction.getRestrictionColumn()
79  + " literal is '" + val + "'");
80  RexNode lit;
81  if (SqlTypeName.NUMERIC_TYPES.indexOf(field.getType().getSqlTypeName()) == -1) {
82  lit = rBuilder.makeLiteral(val, field.getType(), false);
83  } else {
84  lit = rBuilder.makeLiteral(Integer.parseInt(val), field.getType(), false);
85  }
86  RexNode rn = builder.call(SqlStdOperatorTable.EQUALS,
87  builder.field(restriction.getRestrictionColumn()),
88  lit);
89  orList.add(rn);
90  }
91 
92  RexNode relOr = builder.call(SqlStdOperatorTable.OR, orList);
93 
94  final RelNode newNode = builder.filter(relOr).build();
95  call.transformTo(newNode);
96  }
97  };
98 
100  public interface Config extends RelRule.Config {
102  EMPTY.withOperandSupplier(b0 -> b0.operand(LogicalTableScan.class).noInputs())
103  .as(Config.class);
104 
105  @Override
107  return new InjectFilterRule(this, null);
108  }
109 
111  return new InjectFilterRule(this, rest);
112  }
113  }
114 }
default InjectFilterRule toRule(Restriction rest)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
InjectFilterRule(Config config, Restriction restriction)