OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlIdentifierCapturer.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 package org.apache.calcite.prepare;
17 
18 import com.google.common.collect.ImmutableList;
19 
20 import org.apache.calcite.sql.SqlBasicCall;
21 import org.apache.calcite.sql.SqlDataTypeSpec;
22 import org.apache.calcite.sql.SqlDelete;
23 import org.apache.calcite.sql.SqlIdentifier;
24 import org.apache.calcite.sql.SqlInsert;
25 import org.apache.calcite.sql.SqlJoin;
26 import org.apache.calcite.sql.SqlKind;
27 import org.apache.calcite.sql.SqlLiteral;
28 import org.apache.calcite.sql.SqlNode;
29 import org.apache.calcite.sql.SqlNodeList;
30 import org.apache.calcite.sql.SqlOrderBy;
31 import org.apache.calcite.sql.SqlSelect;
32 import org.apache.calcite.sql.SqlUpdate;
33 import org.apache.calcite.sql.SqlWith;
34 import org.apache.calcite.sql.SqlWithItem;
35 import org.apache.calcite.sql.parser.SqlParser;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 
39 import java.lang.reflect.Method;
40 import java.lang.reflect.Modifier;
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.IdentityHashMap;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.Stack;
47 import java.util.concurrent.ConcurrentHashMap;
48 
53 public class SqlIdentifierCapturer {
54  final static Logger MAPDLOGGER = LoggerFactory.getLogger(SqlIdentifierCapturer.class);
55 
56  private static final Map<Class<?>, Set<Method>> GETTERS_CACHE =
57  new ConcurrentHashMap<>();
58 
59  private IdentityHashMap<SqlNode, SqlNode> visitedNodes = new IdentityHashMap<>();
60 
61  private Stack<Set<ImmutableList<String>>> currentList = new Stack<>();
62 
63  public final Set<ImmutableList<String>> selects = new HashSet<>();
64  public final Set<ImmutableList<String>> inserts = new HashSet<>();
65  public final Set<ImmutableList<String>> updates = new HashSet<>();
66  public final Set<ImmutableList<String>> deletes = new HashSet<>();
67 
68  private final Set<ImmutableList<String>> ignore = new HashSet<>();
69 
70  { currentList.push(ignore); }
71 
72  public void scan(SqlNode root) {
73  if (null == root) {
74  return;
75  }
76 
77  if (root instanceof SqlLiteral || root instanceof SqlDataTypeSpec) {
78  return;
79  }
80 
81  if (null != visitedNodes.put(root, root)) {
82  return;
83  }
84 
85  if (root instanceof SqlNodeList) {
86  SqlNodeList snl = (SqlNodeList) root;
87  for (SqlNode node : snl) {
88  scan(node);
89  }
90  return;
91  }
92 
93  if (root instanceof SqlIdentifier) {
94  // we need all the hierachy now to deal with multischema
95  currentList.peek().add(((SqlIdentifier) root).names.reverse());
96  return;
97  }
98 
99  if (root instanceof SqlBasicCall) {
100  SqlBasicCall call = (SqlBasicCall) root;
101  if (call.getOperator().getKind() == SqlKind.AS) {
102  // only really interested in the first operand
103  scan(call.getOperands()[0]);
104  return;
105  }
106  }
107 
108  if (root instanceof SqlOrderBy) {
109  scan(((SqlOrderBy) root).fetch);
110  scan(((SqlOrderBy) root).offset);
111  scan(((SqlOrderBy) root).query);
112  return;
113  }
114 
115  boolean needsPop = false;
116  if (root instanceof SqlSelect) {
117  currentList.push(selects);
118  scan(((SqlSelect) root).getFrom());
119  currentList.pop();
120  currentList.push(ignore);
121  needsPop = true;
122  } else if (root instanceof SqlInsert) {
123  currentList.push(inserts);
124  scan(((SqlInsert) root).getTargetTable());
125  currentList.pop();
126  currentList.push(ignore);
127  needsPop = true;
128  } else if (root instanceof SqlUpdate) {
129  currentList.push(updates);
130  scan(((SqlUpdate) root).getTargetTable());
131  currentList.pop();
132  currentList.push(ignore);
133  needsPop = true;
134  } else if (root instanceof SqlDelete) {
135  currentList.push(deletes);
136  scan(((SqlDelete) root).getTargetTable());
137  currentList.pop();
138  currentList.push(ignore);
139  needsPop = true;
140  } else if (root instanceof SqlJoin) {
141  currentList.push(ignore);
142  scan(((SqlJoin) root).getCondition());
143  currentList.pop();
144  }
145 
146  Set<Method> methods = getRelevantGetters(root);
147  for (Method m : methods) {
148  Object value = null;
149  try {
150  value = m.invoke(root);
151  } catch (Exception e) {
152  }
153 
154  if (value instanceof SqlNode[]) {
155  SqlNode[] nodes = (SqlNode[]) value;
156  for (SqlNode node : nodes) {
157  scan(node);
158  }
159  } else if (value instanceof SqlNode) {
160  scan((SqlNode) value);
161  } else if (value instanceof Collection) {
162  for (Object vobj : ((Collection<?>) value)) {
163  if (vobj instanceof SqlNode) {
164  scan((SqlNode) vobj);
165  }
166  }
167  }
168  }
169 
170  if (root instanceof SqlWith) {
171  SqlWith with = (SqlWith) root;
172 
173  for (SqlNode node : with.withList) {
174  SqlWithItem item = (SqlWithItem) node;
175  selects.remove(((SqlIdentifier) item.name).names.reverse());
176  }
177  }
178 
179  if (needsPop) {
180  currentList.pop();
181  }
182  }
183 
184  Set<Method> getRelevantGetters(Object obj) {
185  Class<?> root = obj.getClass();
186 
187  Set<Method> methods = GETTERS_CACHE.get(root);
188  if (null != methods) {
189  return methods;
190  } else {
191  methods = new HashSet<>();
192  }
193 
194  while (root != null) {
195  if (root == SqlNode.class) break;
196 
197  for (Method m : root.getDeclaredMethods()) {
198  if (m.getParameterTypes().length > 0) continue;
199 
200  if (!Modifier.isPublic(m.getModifiers())) continue;
201 
202  Class<?> returnType = m.getReturnType();
203  if (!SqlNode.class.isAssignableFrom(returnType) && SqlNode[].class != returnType
204  && !Collection.class.isAssignableFrom(returnType)) {
205  continue;
206  }
207 
208  methods.add(m);
209  }
210 
211  root = root.getSuperclass();
212  }
213 
214  GETTERS_CACHE.put(obj.getClass(), methods);
215 
216  return methods;
217  }
218 
219  public String toString() {
220  String out = "";
221  out += " Selects: " + selects + "\n";
222  out += " Inserts: " + inserts + "\n";
223  out += " Updates: " + updates + "\n";
224  out += " Deletes: " + deletes + "\n";
225  out += " Ignore : " + ignore + "\n";
226 
227  return out;
228  }
229 
230  public static void main(String[] args) throws Exception {
231  String sql = "UPDATE sales set f=(SELECT max(r.num) from report as r)";
232  sql = "INSER INTO sales (a, b, c ) VALUES(10, (SELECT max(foo) from bob), 0)";
233  sql = "SELECT * FROM sales a left outer join (select (select max(id) from rupert) from report2) r on a.id=(select max(r.di) from test)";
234 
235  SqlParser parser = SqlParser.create(sql);
236 
238  capturer.scan(parser.parseQuery());
239 
240  System.out.println(capturer.selects);
241  System.out.println(capturer.inserts);
242  System.out.println(capturer.updates);
243  System.out.println(capturer.deletes);
244  System.out.println(capturer.ignore);
245  }
246 }
tuple root
Definition: setup.in.py:14
static final Map< Class<?>, Set< Method > > GETTERS_CACHE
IdentityHashMap< SqlNode, SqlNode > visitedNodes
Stack< Set< ImmutableList< String > > > currentList