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