OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
org.apache.calcite.prepare.SqlIdentifierCapturer Class Reference
+ Collaboration diagram for org.apache.calcite.prepare.SqlIdentifierCapturer:

Public Member Functions

void scan (SqlNode root)
 
String toString ()
 

Static Public Member Functions

static void main (String[] args) throws Exception
 

Public Attributes

Set< ImmutableList< String > > selects = new HashSet<>()
 
Set< ImmutableList< String > > inserts = new HashSet<>()
 
Set< ImmutableList< String > > updates = new HashSet<>()
 
Set< ImmutableList< String > > deletes = new HashSet<>()
 

Package Functions

 [instance initializer]
 
Set< Method > getRelevantGetters (Object obj)
 

Static Package Attributes

static final Logger HEAVYDBLOGGER
 

Private Attributes

IdentityHashMap< SqlNode, SqlNode > visitedNodes = new IdentityHashMap<>()
 
Stack< Set< ImmutableList
< String > > > 
currentList = new Stack<>()
 
final Set< ImmutableList
< String > > 
ignore = new HashSet<>()
 

Static Private Attributes

static final Map< Class
<?>, Set< Method > > 
GETTERS_CACHE
 

Detailed Description

scans from a root SqlNode and retrieves all SqlIdentifiers used in a query.

Definition at line 54 of file SqlIdentifierCapturer.java.

Member Function Documentation

org.apache.calcite.prepare.SqlIdentifierCapturer.[instance initializer] ( )
inlinepackage
Set<Method> org.apache.calcite.prepare.SqlIdentifierCapturer.getRelevantGetters ( Object  obj)
inlinepackage

Definition at line 205 of file SqlIdentifierCapturer.java.

References setup.root.

Referenced by org.apache.calcite.prepare.SqlIdentifierCapturer.scan().

205  {
206  Class<?> root = obj.getClass();
207 
208  Set<Method> methods = GETTERS_CACHE.get(root);
209  if (null != methods) {
210  return methods;
211  } else {
212  methods = new HashSet<>();
213  }
214 
215  while (root != null) {
216  if (root == SqlNode.class) break;
217 
218  for (Method m : root.getDeclaredMethods()) {
219  if (m.getParameterTypes().length > 0) continue;
220 
221  if (!Modifier.isPublic(m.getModifiers())) continue;
222 
223  Class<?> returnType = m.getReturnType();
224  if (!SqlNode.class.isAssignableFrom(returnType) && SqlNode[].class != returnType
225  && !Collection.class.isAssignableFrom(returnType)) {
226  continue;
227  }
228 
229  methods.add(m);
230  }
231 
232  root = root.getSuperclass();
233  }
234 
235  GETTERS_CACHE.put(obj.getClass(), methods);
236 
237  return methods;
238  }
tuple root
Definition: setup.in.py:14

+ Here is the caller graph for this function:

static void org.apache.calcite.prepare.SqlIdentifierCapturer.main ( String[]  args) throws Exception
inlinestatic

Definition at line 251 of file SqlIdentifierCapturer.java.

References run_benchmark_import.parser.

251  {
252  String sql = "UPDATE sales set f=(SELECT max(r.num) from report as r)";
253  sql = "INSER INTO sales (a, b, c ) VALUES(10, (SELECT max(foo) from bob), 0)";
254  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)";
255 
256  SqlParser parser = SqlParser.create(sql);
257 
258  SqlIdentifierCapturer capturer = new SqlIdentifierCapturer();
259  capturer.scan(parser.parseQuery());
260 
261  System.out.println(capturer.selects);
262  System.out.println(capturer.inserts);
263  System.out.println(capturer.updates);
264  System.out.println(capturer.deletes);
265  System.out.println(capturer.ignore);
266  }
void org.apache.calcite.prepare.SqlIdentifierCapturer.scan ( SqlNode  root)
inline

Definition at line 75 of file SqlIdentifierCapturer.java.

References org.apache.calcite.prepare.SqlIdentifierCapturer.deletes, org.apache.calcite.prepare.SqlIdentifierCapturer.getRelevantGetters(), org.apache.calcite.prepare.SqlIdentifierCapturer.ignore, org.apache.calcite.prepare.SqlIdentifierCapturer.inserts, setup.root, org.apache.calcite.prepare.SqlIdentifierCapturer.selects, org.apache.calcite.prepare.SqlIdentifierCapturer.updates, and org.apache.calcite.prepare.SqlIdentifierCapturer.visitedNodes.

75  {
76  if (null == root) {
77  return;
78  }
79 
80  if (root instanceof SqlLiteral || root instanceof SqlDataTypeSpec) {
81  return;
82  }
83 
84  if (null != visitedNodes.put(root, root)) {
85  return;
86  }
87 
88  if (root instanceof SqlNodeList) {
89  SqlNodeList snl = (SqlNodeList) root;
90  for (SqlNode node : snl) {
91  scan(node);
92  }
93  return;
94  }
95 
96  if (root instanceof SqlIdentifier) {
97  // we need all the hierachy now to deal with multischema
98  currentList.peek().add(((SqlIdentifier) root).names.reverse());
99  return;
100  }
101 
102  if (root instanceof SqlBasicCall) {
103  SqlBasicCall call = (SqlBasicCall) root;
104  if (call.getOperator().getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
105  // We have a => named parameter operator
106  // We need to ignore it as otherwise we will pick up literal args
107  // as tables, EXCEPT if it points to a CURSOR operator, as there
108  // will be at least one table inside of the CURSOR.
109  if (call.operandCount() == 0) {
110  return;
111  }
112  if (call.getOperands()[0].getKind() == SqlKind.CURSOR) {
113  SqlBasicCall cursor_call = (SqlBasicCall) call.getOperands()[0];
114  if (cursor_call.operandCount() == 0) {
115  return;
116  }
117  scan(cursor_call.getOperands()[0]);
118  return;
119  } else {
120  return;
121  }
122  } else if (call.getOperator().getKind() == SqlKind.AS) {
123  // only really interested in the first operand
124  scan(call.getOperands()[0]);
125  return;
126  }
127  }
128 
129  if (root instanceof SqlOrderBy) {
130  scan(((SqlOrderBy) root).fetch);
131  scan(((SqlOrderBy) root).offset);
132  scan(((SqlOrderBy) root).query);
133  return;
134  }
135 
136  boolean needsPop = false;
137  if (root instanceof SqlSelect) {
138  currentList.push(selects);
139  scan(((SqlSelect) root).getFrom());
140  currentList.pop();
141  currentList.push(ignore);
142  needsPop = true;
143  } else if (root instanceof SqlInsert) {
144  currentList.push(inserts);
145  scan(((SqlInsert) root).getTargetTable());
146  currentList.pop();
147  currentList.push(ignore);
148  needsPop = true;
149  } else if (root instanceof SqlUpdate) {
150  currentList.push(updates);
151  scan(((SqlUpdate) root).getTargetTable());
152  currentList.pop();
153  currentList.push(ignore);
154  needsPop = true;
155  } else if (root instanceof SqlDelete) {
156  currentList.push(deletes);
157  scan(((SqlDelete) root).getTargetTable());
158  currentList.pop();
159  currentList.push(ignore);
160  needsPop = true;
161  } else if (root instanceof SqlJoin) {
162  currentList.push(ignore);
163  scan(((SqlJoin) root).getCondition());
164  currentList.pop();
165  }
166 
167  Set<Method> methods = getRelevantGetters(root);
168  for (Method m : methods) {
169  Object value = null;
170  try {
171  value = m.invoke(root);
172  } catch (Exception e) {
173  }
174 
175  if (value instanceof SqlNode[]) {
176  SqlNode[] nodes = (SqlNode[]) value;
177  for (SqlNode node : nodes) {
178  scan(node);
179  }
180  } else if (value instanceof SqlNode) {
181  scan((SqlNode) value);
182  } else if (value instanceof Collection) {
183  for (Object vobj : ((Collection<?>) value)) {
184  if (vobj instanceof SqlNode) {
185  scan((SqlNode) vobj);
186  }
187  }
188  }
189  }
190 
191  if (root instanceof SqlWith) {
192  SqlWith with = (SqlWith) root;
193 
194  for (SqlNode node : with.withList) {
195  SqlWithItem item = (SqlWithItem) node;
196  selects.remove(((SqlIdentifier) item.name).names.reverse());
197  }
198  }
199 
200  if (needsPop) {
201  currentList.pop();
202  }
203  }
tuple root
Definition: setup.in.py:14
IdentityHashMap< SqlNode, SqlNode > visitedNodes

+ Here is the call graph for this function:

String org.apache.calcite.prepare.SqlIdentifierCapturer.toString ( )
inline

Definition at line 240 of file SqlIdentifierCapturer.java.

References org.apache.calcite.prepare.SqlIdentifierCapturer.deletes, org.apache.calcite.prepare.SqlIdentifierCapturer.ignore, org.apache.calcite.prepare.SqlIdentifierCapturer.inserts, org.apache.calcite.prepare.SqlIdentifierCapturer.selects, and org.apache.calcite.prepare.SqlIdentifierCapturer.updates.

240  {
241  String out = "";
242  out += " Selects: " + selects + "\n";
243  out += " Inserts: " + inserts + "\n";
244  out += " Updates: " + updates + "\n";
245  out += " Deletes: " + deletes + "\n";
246  out += " Ignore : " + ignore + "\n";
247 
248  return out;
249  }

Member Data Documentation

Stack<Set<ImmutableList<String> > > org.apache.calcite.prepare.SqlIdentifierCapturer.currentList = new Stack<>()
private

Definition at line 63 of file SqlIdentifierCapturer.java.

final Map<Class<?>, Set<Method> > org.apache.calcite.prepare.SqlIdentifierCapturer.GETTERS_CACHE
staticprivate
Initial value:
=
new ConcurrentHashMap<>()

Definition at line 58 of file SqlIdentifierCapturer.java.

final Logger org.apache.calcite.prepare.SqlIdentifierCapturer.HEAVYDBLOGGER
staticpackage
Initial value:
=
LoggerFactory.getLogger(SqlIdentifierCapturer.class)

Definition at line 55 of file SqlIdentifierCapturer.java.

final Set<ImmutableList<String> > org.apache.calcite.prepare.SqlIdentifierCapturer.ignore = new HashSet<>()
private
IdentityHashMap<SqlNode, SqlNode> org.apache.calcite.prepare.SqlIdentifierCapturer.visitedNodes = new IdentityHashMap<>()
private

The documentation for this class was generated from the following file: