queryparser-python3 0.7.1__py3-none-any.whl → 0.7.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  # All listeners that are with minor modifications shared between PostgreSQL
3
3
  # and MySQL.
4
- from __future__ import (absolute_import, print_function)
4
+ from __future__ import absolute_import, print_function
5
5
 
6
6
  import logging
7
7
  import re
@@ -31,7 +31,7 @@ def parse_alias(alias, quote_char):
31
31
 
32
32
 
33
33
  def process_column_name(column_name_listener, walker, ctx, quote_char):
34
- '''
34
+ """
35
35
  A helper function that strips the quote characters from the column
36
36
  names. The returned list includes:
37
37
 
@@ -44,7 +44,7 @@ def process_column_name(column_name_listener, walker, ctx, quote_char):
44
44
  column_name_listener object
45
45
 
46
46
  :param walker:
47
- antlr walker object
47
+ antlr walker object
48
48
 
49
49
  :param ctx:
50
50
  antlr context to walk through
@@ -52,7 +52,7 @@ def process_column_name(column_name_listener, walker, ctx, quote_char):
52
52
  :param quote_char:
53
53
  which quote character are we expecting?
54
54
 
55
- '''
55
+ """
56
56
  cn = []
57
57
  column_name_listener.column_name = []
58
58
  walker.walk(column_name_listener, ctx)
@@ -81,12 +81,12 @@ def process_column_name(column_name_listener, walker, ctx, quote_char):
81
81
 
82
82
 
83
83
  def get_column_name_listener(base):
84
-
85
84
  class ColumnNameListener(base):
86
85
  """
87
86
  Get all column names.
88
87
 
89
88
  """
89
+
90
90
  def __init__(self):
91
91
  self.column_name = []
92
92
  self.column_as_array = []
@@ -105,12 +105,12 @@ def get_column_name_listener(base):
105
105
 
106
106
 
107
107
  def get_table_name_listener(base, quote_char):
108
-
109
108
  class TableNameListener(base):
110
109
  """
111
110
  Get table names.
112
111
 
113
112
  """
113
+
114
114
  def __init__(self):
115
115
  self.table_names = []
116
116
  self.table_aliases = []
@@ -126,9 +126,7 @@ def get_table_name_listener(base, quote_char):
126
126
 
127
127
 
128
128
  def get_schema_name_listener(base, quote_char):
129
-
130
129
  class SchemaNameListener(base):
131
-
132
130
  def __init__(self, replace_schema_name):
133
131
  self.replace_schema_name = replace_schema_name
134
132
 
@@ -141,9 +139,13 @@ def get_schema_name_listener(base, quote_char):
141
139
  nsn = unicode(nsn, 'utf-8')
142
140
  except NameError:
143
141
  pass
144
- nsn = re.sub(r'(|{})(?!{})[\S]*[^{}](|{})'.format(
145
- quote_char, quote_char, quote_char, quote_char),
146
- r'\1{}\2'.format(nsn), sn)
142
+ nsn = re.sub(
143
+ r'(|{})(?!{})[\S]*[^{}](|{})'.format(
144
+ quote_char, quote_char, quote_char, quote_char
145
+ ),
146
+ r'\1{}\2'.format(nsn),
147
+ sn,
148
+ )
147
149
  ctx.getTokens(ttype)[0].getSymbol().text = nsn
148
150
  except KeyError:
149
151
  pass
@@ -152,42 +154,46 @@ def get_schema_name_listener(base, quote_char):
152
154
 
153
155
 
154
156
  def get_remove_subqueries_listener(base, base_parser):
155
-
156
157
  class RemoveSubqueriesListener(base):
157
158
  """
158
159
  Remove nested select_expressions.
159
160
 
160
161
  """
162
+
161
163
  def __init__(self, depth):
162
164
  self.depth = depth
163
165
 
164
166
  def enterSelect_expression(self, ctx):
165
167
  parent = ctx.parentCtx.parentCtx
166
168
 
167
- if isinstance(parent, base_parser.SubqueryContext) and \
168
- ctx.depth() > self.depth:
169
+ if (
170
+ isinstance(parent, base_parser.SubqueryContext)
171
+ and ctx.depth() > self.depth
172
+ ):
169
173
  # we need to remove all Select_expression instances, not
170
174
  # just the last one so we loop over until we get all of them
171
175
  # out
172
- seinstances = [isinstance(i,
173
- base_parser.Select_expressionContext)
174
- for i in ctx.parentCtx.children]
176
+ seinstances = [
177
+ isinstance(i, base_parser.Select_expressionContext)
178
+ for i in ctx.parentCtx.children
179
+ ]
175
180
  while True in seinstances:
176
181
  ctx.parentCtx.removeLastChild()
177
- seinstances = [isinstance(i,
178
- base_parser.Select_expressionContext)
179
- for i in ctx.parentCtx.children]
182
+ seinstances = [
183
+ isinstance(i, base_parser.Select_expressionContext)
184
+ for i in ctx.parentCtx.children
185
+ ]
180
186
 
181
187
  return RemoveSubqueriesListener
182
188
 
183
189
 
184
190
  def get_query_listener(base, base_parser, quote_char):
185
-
186
191
  class QueryListener(base):
187
192
  """
188
193
  Extract all select_expressions.
189
194
 
190
195
  """
196
+
191
197
  def __init__(self):
192
198
  self.select_expressions = []
193
199
  self.select_list = None
@@ -219,12 +225,12 @@ def get_query_listener(base, base_parser, quote_char):
219
225
 
220
226
 
221
227
  def get_column_keyword_function_listener(base, quote_char):
222
-
223
228
  class ColumnKeywordFunctionListener(base):
224
229
  """
225
230
  Extract columns, keywords and functions.
226
231
 
227
232
  """
233
+
228
234
  def __init__(self):
229
235
  self.tables = []
230
236
  self.columns = []
@@ -232,8 +238,7 @@ def get_column_keyword_function_listener(base, quote_char):
232
238
  self.keywords = []
233
239
  self.functions = []
234
240
  self.column_name_listener = get_column_name_listener(base)()
235
- self.table_name_listener = get_table_name_listener(
236
- base, quote_char)()
241
+ self.table_name_listener = get_table_name_listener(base, quote_char)()
237
242
  self.walker = antlr4.ParseTreeWalker()
238
243
 
239
244
  self.data = []
@@ -247,8 +252,9 @@ def get_column_keyword_function_listener(base, quote_char):
247
252
  return alias
248
253
 
249
254
  def _extract_column(self, ctx, append=True, join_columns=False):
250
- cn = process_column_name(self.column_name_listener, self.walker,
251
- ctx, quote_char)
255
+ cn = process_column_name(
256
+ self.column_name_listener, self.walker, ctx, quote_char
257
+ )
252
258
  alias = self._process_alias(ctx)
253
259
 
254
260
  if len(cn) > 1:
@@ -293,15 +299,20 @@ def get_column_keyword_function_listener(base, quote_char):
293
299
  tn[1] = ts.table_name().getText().replace(quote_char, '')
294
300
  self.tables.append((alias, tn, ctx.depth()))
295
301
 
296
- logging.info((ctx.depth(), ctx.__class__.__name__,
297
- [tn, alias]))
302
+ logging.info((ctx.depth(), ctx.__class__.__name__, [tn, alias]))
298
303
  self.data.append([ctx.depth(), ctx, [tn, alias]])
299
304
 
300
305
  def enterDisplayed_column(self, ctx):
301
- logging.info((ctx.depth(), ctx.__class__.__name__,
302
- self._extract_column(ctx, append=False)[1]))
303
- self.data.append([ctx.depth(), ctx,
304
- self._extract_column(ctx, append=False)[1]])
306
+ logging.info(
307
+ (
308
+ ctx.depth(),
309
+ ctx.__class__.__name__,
310
+ self._extract_column(ctx, append=False)[1],
311
+ )
312
+ )
313
+ self.data.append(
314
+ [ctx.depth(), ctx, self._extract_column(ctx, append=False)[1]]
315
+ )
305
316
  self._extract_column(ctx)
306
317
  if ctx.ASTERISK():
307
318
  self.keywords.append('*')
@@ -312,10 +323,10 @@ def get_column_keyword_function_listener(base, quote_char):
312
323
 
313
324
  def enterSelect_list(self, ctx):
314
325
  if ctx.ASTERISK():
315
- logging.info((ctx.depth(), ctx.__class__.__name__,
316
- [[None, None, '*'], None]))
317
- self.data.append([ctx.depth(), ctx, [[[None, None, '*'],
318
- None]]])
326
+ logging.info(
327
+ (ctx.depth(), ctx.__class__.__name__, [[None, None, '*'], None])
328
+ )
329
+ self.data.append([ctx.depth(), ctx, [[[None, None, '*'], None]]])
319
330
  self.columns.append(('*', None))
320
331
  self.keywords.append('*')
321
332
 
@@ -330,36 +341,60 @@ def get_column_keyword_function_listener(base, quote_char):
330
341
  col = self._extract_column(ctx, append=False)
331
342
  if col[1][0][0][2] not in self.column_aliases:
332
343
  self._extract_column(ctx)
333
- logging.info((ctx.depth(), ctx.__class__.__name__,
334
- self._extract_column(ctx, append=False)[1]))
335
- self.data.append([ctx.depth(), ctx,
336
- self._extract_column(ctx, append=False)[1]])
344
+ logging.info(
345
+ (
346
+ ctx.depth(),
347
+ ctx.__class__.__name__,
348
+ self._extract_column(ctx, append=False)[1],
349
+ )
350
+ )
351
+ self.data.append(
352
+ [ctx.depth(), ctx, self._extract_column(ctx, append=False)[1]]
353
+ )
337
354
 
338
355
  def enterWhere_clause(self, ctx):
339
356
  self.keywords.append('where')
340
357
  self._extract_column(ctx)
341
- logging.info((ctx.depth(), ctx.__class__.__name__,
342
- self._extract_column(ctx, append=False)[1]))
343
- self.data.append([ctx.depth(), ctx,
344
- self._extract_column(ctx, append=False)[1]])
358
+ logging.info(
359
+ (
360
+ ctx.depth(),
361
+ ctx.__class__.__name__,
362
+ self._extract_column(ctx, append=False)[1],
363
+ )
364
+ )
365
+ self.data.append(
366
+ [ctx.depth(), ctx, self._extract_column(ctx, append=False)[1]]
367
+ )
345
368
 
346
369
  def enterHaving_clause(self, ctx):
347
370
  self.keywords.append('having')
348
371
  self._extract_column(ctx)
349
- logging.info((ctx.depth(), ctx.__class__.__name__,
350
- self._extract_column(ctx, append=False)[1]))
351
- self.data.append([ctx.depth(), ctx,
352
- self._extract_column(ctx, append=False)[1]])
372
+ logging.info(
373
+ (
374
+ ctx.depth(),
375
+ ctx.__class__.__name__,
376
+ self._extract_column(ctx, append=False)[1],
377
+ )
378
+ )
379
+ self.data.append(
380
+ [ctx.depth(), ctx, self._extract_column(ctx, append=False)[1]]
381
+ )
353
382
 
354
383
  def enterOrderby_clause(self, ctx):
355
384
  self.keywords.append('order by')
356
385
  col = self._extract_column(ctx, append=False)
357
386
  if col[1][0][0][2] not in self.column_aliases:
358
387
  self._extract_column(ctx)
359
- logging.info((ctx.depth(), ctx.__class__.__name__,
360
- self._extract_column(ctx, append=False)[1]))
361
- self.data.append([ctx.depth(), ctx,
362
- self._extract_column(ctx, append=False)[1]])
388
+ logging.info(
389
+ (
390
+ ctx.depth(),
391
+ ctx.__class__.__name__,
392
+ self._extract_column(ctx, append=False)[1],
393
+ )
394
+ )
395
+ self.data.append(
396
+ [ctx.depth(), ctx, self._extract_column(ctx, append=False)[1]]
397
+ )
363
398
 
364
399
  def enterLimit_clause(self, ctx):
365
400
  self.keywords.append('limit')
@@ -367,10 +402,16 @@ def get_column_keyword_function_listener(base, quote_char):
367
402
  def enterJoin_condition(self, ctx):
368
403
  self.keywords.append('join')
369
404
  self._extract_column(ctx, join_columns=ctx)
370
- logging.info((ctx.depth(), ctx.__class__.__name__,
371
- self._extract_column(ctx, append=False)[1]))
372
- self.data.append([ctx.depth(), ctx,
373
- self._extract_column(ctx, append=False)[1]])
405
+ logging.info(
406
+ (
407
+ ctx.depth(),
408
+ ctx.__class__.__name__,
409
+ self._extract_column(ctx, append=False)[1],
410
+ )
411
+ )
412
+ self.data.append(
413
+ [ctx.depth(), ctx, self._extract_column(ctx, append=False)[1]]
414
+ )
374
415
 
375
416
  def enterSpoint(self, ctx):
376
417
  self.functions.append('spoint')
@@ -437,8 +478,16 @@ class SQLQueryProcessor(object):
437
478
  other types of listeners can be added.
438
479
 
439
480
  """
440
- def __init__(self, base_lexer, base_parser, base_parser_listener,
441
- quote_char, query=None, base_sphere_listener=None):
481
+
482
+ def __init__(
483
+ self,
484
+ base_lexer,
485
+ base_parser,
486
+ base_parser_listener,
487
+ quote_char,
488
+ query=None,
489
+ base_sphere_listener=None,
490
+ ):
442
491
  self.lexer = base_lexer
443
492
  self.parser = base_parser
444
493
  self.parser_listener = base_parser_listener
@@ -495,12 +544,12 @@ class SQLQueryProcessor(object):
495
544
 
496
545
  if isinstance(i[1], self.parser.Select_listContext):
497
546
  if len(i) == 3:
498
- select_list_columns.append([[i[2][0][0] + [i[1]],
499
- i[2][0][1]]])
547
+ select_list_columns.append([[i[2][0][0] + [i[1]], i[2][0][1]]])
500
548
  ctx_stack.append(i)
501
549
 
502
- if isinstance(i[1], self.parser.Where_clauseContext) or\
503
- isinstance(i[1], self.parser.Having_clauseContext):
550
+ if isinstance(i[1], self.parser.Where_clauseContext) or isinstance(
551
+ i[1], self.parser.Having_clauseContext
552
+ ):
504
553
  if len(i[2]) > 1:
505
554
  for j in i[2]:
506
555
  other_columns.append([j])
@@ -514,15 +563,23 @@ class SQLQueryProcessor(object):
514
563
 
515
564
  if i[1].USING_SYM():
516
565
  for ctx in ctx_stack[::-1]:
517
- if not isinstance(ctx[1],
518
- self.parser.Table_atomContext):
566
+ if not isinstance(ctx[1], self.parser.Table_atomContext):
519
567
  break
520
568
  for ju in join_using:
521
569
  if ju[0][1] is None:
522
- other_columns.append([[[ctx[2][0][0],
523
- ctx[2][0][1],
524
- ju[0][2],
525
- ctx[1]], None]])
570
+ other_columns.append(
571
+ [
572
+ [
573
+ [
574
+ ctx[2][0][0],
575
+ ctx[2][0][1],
576
+ ju[0][2],
577
+ ctx[1],
578
+ ],
579
+ None,
580
+ ]
581
+ ]
582
+ )
526
583
  elif i[1].ON():
527
584
  if len(i[2]) > 1:
528
585
  for j in i[2]:
@@ -546,9 +603,16 @@ class SQLQueryProcessor(object):
546
603
  go_columns.append(i[2])
547
604
  ctx_stack.append(i)
548
605
 
549
- return select_list_columns, select_list_tables,\
550
- select_list_table_references, other_columns, go_columns, join,\
551
- join_using, column_aliases
606
+ return (
607
+ select_list_columns,
608
+ select_list_tables,
609
+ select_list_table_references,
610
+ other_columns,
611
+ go_columns,
612
+ join,
613
+ join_using,
614
+ column_aliases,
615
+ )
552
616
 
553
617
  def _get_budget_column(self, c, tab, ref):
554
618
  cname = c[0][2]
@@ -577,10 +641,17 @@ class SQLQueryProcessor(object):
577
641
 
578
642
  return cname, cctx, calias, column_found, t
579
643
 
580
- def _extract_columns(self, columns, select_list_tables, ref_dict, join,
581
- budget, column_aliases, touched_columns=None,
582
- subquery_contents=None):
583
-
644
+ def _extract_columns(
645
+ self,
646
+ columns,
647
+ select_list_tables,
648
+ ref_dict,
649
+ join,
650
+ budget,
651
+ column_aliases,
652
+ touched_columns=None,
653
+ subquery_contents=None,
654
+ ):
584
655
  # Here we store all columns that might have references somewhere
585
656
  # higher up in the tree structure. We'll revisit them later.
586
657
  missing_columns = []
@@ -595,11 +666,11 @@ class SQLQueryProcessor(object):
595
666
  calias = c[1]
596
667
 
597
668
  # if * is selected we don't care too much
598
- if c[0][0] is None and c[0][1] is None and c[0][2] == '*'\
599
- and not join:
669
+ if c[0][0] is None and c[0][1] is None and c[0][2] == '*' and not join:
600
670
  for slt in select_list_tables:
601
- extra_columns.append([[slt[0][0][0], slt[0][0][1], cname,
602
- c[0][3]], calias])
671
+ extra_columns.append(
672
+ [[slt[0][0][0], slt[0][0][1], cname, c[0][3]], calias]
673
+ )
603
674
  remove_column_idxs.append(i)
604
675
  continue
605
676
 
@@ -616,15 +687,17 @@ class SQLQueryProcessor(object):
616
687
 
617
688
  # We have to check if we also have a join on the same level
618
689
  # and we are actually touching a column from the joined table
619
- if join and c[0][2] != '*' and\
620
- (tab[1] != c[0][1] or
621
- (tab[1] is None and c[0][1] is None)):
622
- cname, cctx, calias, column_found, tab =\
623
- self._get_budget_column(c, tab, budget[-1][2])
690
+ if (
691
+ join
692
+ and c[0][2] != '*'
693
+ and (tab[1] != c[0][1] or (tab[1] is None and c[0][1] is None))
694
+ ):
695
+ cname, cctx, calias, column_found, tab = self._get_budget_column(
696
+ c, tab, budget[-1][2]
697
+ )
624
698
  # raise an ambiguous column
625
699
  if column_found and c[0][1] is None:
626
- raise QueryError("Column '%s' is possibly ambiguous."
627
- % c[0][2])
700
+ raise QueryError("Column '%s' is possibly ambiguous." % c[0][2])
628
701
 
629
702
  except IndexError:
630
703
  pass
@@ -636,14 +709,18 @@ class SQLQueryProcessor(object):
636
709
 
637
710
  if isinstance(ref[0], int):
638
711
  # ref is a budget column
639
- cname, cctx, calias, column_found, tab =\
640
- self._get_budget_column(c, tab, ref[2])
712
+ cname, cctx, calias, column_found, tab = self._get_budget_column(
713
+ c, tab, ref[2]
714
+ )
641
715
 
642
716
  ref_cols = [j[0][2] for j in ref[2]]
643
- if not column_found and c[0][1] is not None\
644
- and c[0][1] != tab[0][1] and '*' not in ref_cols:
645
- raise QueryError("Unknown column '%s.%s'." % (c[0][1],
646
- c[0][2]))
717
+ if (
718
+ not column_found
719
+ and c[0][1] is not None
720
+ and c[0][1] != tab[0][1]
721
+ and '*' not in ref_cols
722
+ ):
723
+ raise QueryError("Unknown column '%s.%s'." % (c[0][1], c[0][2]))
647
724
 
648
725
  else:
649
726
  # ref is a table
@@ -662,12 +739,12 @@ class SQLQueryProcessor(object):
662
739
  if subquery_contents is not None:
663
740
  try:
664
741
  contents = subquery_contents[c[0][1]]
665
- cname, cctx, calias, column_found, tab =\
742
+ cname, cctx, calias, column_found, tab = (
666
743
  self._get_budget_column(c, tab, contents)
744
+ )
667
745
 
668
746
  except KeyError:
669
- tabs = [j[0][0][:2] for j in
670
- subquery_contents.values()]
747
+ tabs = [j[0][0][:2] for j in subquery_contents.values()]
671
748
  tabs += [j[0][0] for j in select_list_tables]
672
749
  column_found = False
673
750
  for t in tabs:
@@ -686,18 +763,24 @@ class SQLQueryProcessor(object):
686
763
  continue
687
764
  else:
688
765
  if tab[0][1] == c[0][1]:
689
- columns[i] = [[tab[0][0], tab[0][1],
690
- c[0][2], c[0][3]], c[1]]
766
+ columns[i] = [
767
+ [tab[0][0], tab[0][1], c[0][2], c[0][3]],
768
+ c[1],
769
+ ]
691
770
  else:
692
-
693
771
  missing_columns.append(c)
694
772
  columns[i] = c
695
773
  if touched_columns is not None:
696
774
  touched_columns.append(c)
697
775
  continue
698
776
 
699
- elif c[0][2] is not None and c[0][2] != '*' and c[0][1] is \
700
- None and len(ref_dict.keys()) > 1 and not join:
777
+ elif (
778
+ c[0][2] is not None
779
+ and c[0][2] != '*'
780
+ and c[0][1] is None
781
+ and len(ref_dict.keys()) > 1
782
+ and not join
783
+ ):
701
784
  raise QueryError("Column '%s' is ambiguous." % c[0][2])
702
785
 
703
786
  elif len(budget) and tab[0][0] is None and tab[0][1] is None:
@@ -705,18 +788,21 @@ class SQLQueryProcessor(object):
705
788
  column_found = False
706
789
 
707
790
  if isinstance(ref[0], int):
708
- cname, cctx, calias, column_found, tab =\
709
- self._get_budget_column(c, tab, ref[2])
791
+ cname, cctx, calias, column_found, tab = (
792
+ self._get_budget_column(c, tab, ref[2])
793
+ )
710
794
 
711
795
  # We allow None.None columns because they are produced
712
796
  # by count(*)
713
- if not column_found and c[0][2] is not None\
714
- and c[0][2] not in column_aliases:
797
+ if (
798
+ not column_found
799
+ and c[0][2] is not None
800
+ and c[0][2] not in column_aliases
801
+ ):
715
802
  raise QueryError("Unknown column '%s'." % c[0][2])
716
803
 
717
804
  if touched_columns is not None:
718
- touched_columns.append([[tab[0][0], tab[0][1], cname, cctx],
719
- calias])
805
+ touched_columns.append([[tab[0][0], tab[0][1], cname, cctx], calias])
720
806
  else:
721
807
  columns[i] = [[tab[0][0], tab[0][1], cname, c[0][3]], calias]
722
808
 
@@ -726,7 +812,27 @@ class SQLQueryProcessor(object):
726
812
  columns.extend(extra_columns)
727
813
  return missing_columns
728
814
 
729
- def process_query(self, replace_schema_name=None, indexed_objects=None):
815
+ @staticmethod
816
+ def _match_and_replace_function_name(query, function_name, i):
817
+ """
818
+ This very roughly checks if the function name is present in the query.
819
+ We check for a space, the function name, and an opening parenthesis.
820
+ """
821
+ pattern = r'\s' + re.escape(function_name) + r'\('
822
+ match = re.search(pattern, query)
823
+ if match:
824
+ start, end = match.span()
825
+ # Replace the matched function name with UDF_{i}
826
+ query = query[: start + 1] + f'UDF_{i}' + query[end - 1 :]
827
+
828
+ return match, query
829
+
830
+ def process_query(
831
+ self,
832
+ replace_schema_name=None,
833
+ replace_function_names=None,
834
+ indexed_objects=None,
835
+ ):
730
836
  """
731
837
  Parses and processes the query. After a successful run it fills up
732
838
  columns, keywords, functions and syntax_errors lists.
@@ -737,6 +843,21 @@ class SQLQueryProcessor(object):
737
843
  :param indexed_objects: Deprecated
738
844
 
739
845
  """
846
+ self.replaced_functions = {}
847
+
848
+ if replace_function_names:
849
+ if (n := len(replace_function_names)) > 10:
850
+ raise ValueError(
851
+ f'Too many function names to replace (you passed {n}). Maximum: 10'
852
+ )
853
+ for i, function_name in enumerate(replace_function_names):
854
+ match, query = self._match_and_replace_function_name(
855
+ self.query, function_name, i
856
+ )
857
+ if match:
858
+ self.replaced_functions[i] = function_name
859
+ self.set_query(query)
860
+
740
861
  # Antlr objects
741
862
  inpt = antlr4.InputStream(self.query)
742
863
  lexer = self.lexer(inpt)
@@ -752,12 +873,14 @@ class SQLQueryProcessor(object):
752
873
 
753
874
  if replace_schema_name is not None:
754
875
  schema_name_listener = get_schema_name_listener(
755
- self.parser_listener, self.quote_char)(replace_schema_name)
876
+ self.parser_listener, self.quote_char
877
+ )(replace_schema_name)
756
878
  self.walker.walk(schema_name_listener, tree)
757
879
  self._query = stream.getText()
758
880
 
759
- query_listener = get_query_listener(self.parser_listener,
760
- self.parser, self.quote_char)()
881
+ query_listener = get_query_listener(
882
+ self.parser_listener, self.parser, self.quote_char
883
+ )()
761
884
  subquery_aliases = [None]
762
885
  keywords = []
763
886
  functions = []
@@ -784,10 +907,11 @@ class SQLQueryProcessor(object):
784
907
  # Iterate through subqueries starting with the lowest level
785
908
  for ccc, ctx in enumerate(query_listener.select_expressions[::-1]):
786
909
  remove_subquieries_listener = get_remove_subqueries_listener(
787
- self.parser_listener, self.parser)(ctx.depth())
788
- column_keyword_function_listener = \
789
- get_column_keyword_function_listener(
790
- self.parser_listener, self.quote_char)()
910
+ self.parser_listener, self.parser
911
+ )(ctx.depth())
912
+ column_keyword_function_listener = get_column_keyword_function_listener(
913
+ self.parser_listener, self.quote_char
914
+ )()
791
915
 
792
916
  # Remove nested subqueries from select_expressions
793
917
  self.walker.walk(remove_subquieries_listener, ctx)
@@ -809,10 +933,16 @@ class SQLQueryProcessor(object):
809
933
  # We get the columns from the select list along with all
810
934
  # other touched columns and any possible join conditions
811
935
  column_aliases_from_previous = [i for i in column_aliases]
812
- select_list_columns, select_list_tables,\
813
- select_list_table_references, other_columns, go_columns, join,\
814
- join_using, column_aliases =\
815
- self._extract_instances(column_keyword_function_listener)
936
+ (
937
+ select_list_columns,
938
+ select_list_tables,
939
+ select_list_table_references,
940
+ other_columns,
941
+ go_columns,
942
+ join,
943
+ join_using,
944
+ column_aliases,
945
+ ) = self._extract_instances(column_keyword_function_listener)
816
946
 
817
947
  tables.extend([i[0] for i in select_list_tables])
818
948
 
@@ -837,9 +967,14 @@ class SQLQueryProcessor(object):
837
967
  for table in select_list_tables:
838
968
  ref_dict[table[0][0][1]] = table
839
969
 
840
- mc = self._extract_columns(select_list_columns, select_list_tables,
841
- ref_dict, join, budget,
842
- column_aliases_from_previous)
970
+ mc = self._extract_columns(
971
+ select_list_columns,
972
+ select_list_tables,
973
+ ref_dict,
974
+ join,
975
+ budget,
976
+ column_aliases_from_previous,
977
+ )
843
978
  missing_columns.extend([[i] for i in mc])
844
979
 
845
980
  touched_columns.extend(select_list_columns)
@@ -851,10 +986,15 @@ class SQLQueryProcessor(object):
851
986
  if col[0][0][2] not in aliases:
852
987
  other_columns.append(col)
853
988
 
854
- mc = self._extract_columns(other_columns, select_list_tables,
855
- ref_dict, join, budget,
856
- column_aliases_from_previous,
857
- touched_columns)
989
+ mc = self._extract_columns(
990
+ other_columns,
991
+ select_list_tables,
992
+ ref_dict,
993
+ join,
994
+ budget,
995
+ column_aliases_from_previous,
996
+ touched_columns,
997
+ )
858
998
 
859
999
  missing_columns.extend([[i] for i in mc])
860
1000
 
@@ -863,8 +1003,9 @@ class SQLQueryProcessor(object):
863
1003
  join_columns.append(budget.pop(-1))
864
1004
  if len(join_using) == 1:
865
1005
  for tab in select_list_tables:
866
- touched_columns.append([[tab[0][0][0], tab[0][0][1],
867
- join_using[0][0][2]], None])
1006
+ touched_columns.append(
1007
+ [[tab[0][0][0], tab[0][0][1], join_using[0][0][2]], None]
1008
+ )
868
1009
  bp = []
869
1010
  for b in budget[::-1]:
870
1011
  if b[0] > current_depth:
@@ -876,26 +1017,38 @@ class SQLQueryProcessor(object):
876
1017
  subquery_contents[subquery_alias] = current_columns
877
1018
 
878
1019
  if len(missing_columns):
879
- mc = self._extract_columns(missing_columns, select_list_tables,
880
- ref_dict, join, budget,
881
- column_aliases_from_previous,
882
- touched_columns, subquery_contents)
1020
+ mc = self._extract_columns(
1021
+ missing_columns,
1022
+ select_list_tables,
1023
+ ref_dict,
1024
+ join,
1025
+ budget,
1026
+ column_aliases_from_previous,
1027
+ touched_columns,
1028
+ subquery_contents,
1029
+ )
883
1030
  if len(mc):
884
- unref_cols = "', '".join(['.'.join([j for j in i[0][:3] if j])
885
- for i in mc])
1031
+ unref_cols = "', '".join(
1032
+ ['.'.join([j for j in i[0][:3] if j]) for i in mc]
1033
+ )
886
1034
  raise QueryError("Unreferenced column(s): '%s'." % unref_cols)
887
1035
 
888
1036
  touched_columns = set([tuple(i[0]) for i in touched_columns])
889
1037
 
890
1038
  # extract display_columns
891
1039
  display_columns = []
892
- mc = self._extract_columns([[i] for i in budget[-1][2]],
893
- select_list_tables, ref_dict, join, budget,
894
- column_aliases_from_previous,
895
- display_columns, subquery_contents)
896
-
897
- display_columns = [[i[1] if i[1] else i[0][2], i[0]]
898
- for i in display_columns]
1040
+ mc = self._extract_columns(
1041
+ [[i] for i in budget[-1][2]],
1042
+ select_list_tables,
1043
+ ref_dict,
1044
+ join,
1045
+ budget,
1046
+ column_aliases_from_previous,
1047
+ display_columns,
1048
+ subquery_contents,
1049
+ )
1050
+
1051
+ display_columns = [[i[1] if i[1] else i[0][2], i[0]] for i in display_columns]
899
1052
 
900
1053
  # Let's get rid of all columns that are already covered by
901
1054
  # db.tab.*. Figure out a better way to do it and replace the code
@@ -908,23 +1061,41 @@ class SQLQueryProcessor(object):
908
1061
 
909
1062
  for acol in asterisk_columns:
910
1063
  for col in touched_columns:
911
- if acol[0] == col[0] and acol[1] == col[1] and \
912
- acol[2] != col[2]:
1064
+ if acol[0] == col[0] and acol[1] == col[1] and acol[2] != col[2]:
913
1065
  del_columns.append(col)
914
1066
 
915
1067
  columns = list(set(touched_columns).difference(del_columns))
916
1068
  self.columns = list(set([self._strip_column(i) for i in columns]))
917
1069
  self.keywords = list(set(keywords))
918
1070
  self.functions = list(set(functions))
919
- self.display_columns = [(i[0].lstrip('"').rstrip('"'),
920
- list(self._strip_column(i[1])))
921
- for i in display_columns]
922
-
923
- self.tables = list(set([tuple([i[0][0].lstrip('"').rstrip('"')
924
- if i[0][0] is not None else i[0][0],
925
- i[0][1].lstrip('"').rstrip('"')
926
- if i[0][1] is not None else i[0][1]])
927
- for i in tables]))
1071
+ self.display_columns = [
1072
+ (i[0].lstrip('"').rstrip('"'), list(self._strip_column(i[1])))
1073
+ for i in display_columns
1074
+ ]
1075
+
1076
+ self.tables = list(
1077
+ set(
1078
+ [
1079
+ tuple(
1080
+ [
1081
+ i[0][0].lstrip('"').rstrip('"')
1082
+ if i[0][0] is not None
1083
+ else i[0][0],
1084
+ i[0][1].lstrip('"').rstrip('"')
1085
+ if i[0][1] is not None
1086
+ else i[0][1],
1087
+ ]
1088
+ )
1089
+ for i in tables
1090
+ ]
1091
+ )
1092
+ )
1093
+
1094
+ if len(self.replaced_functions) > 0:
1095
+ for i, function_name in self.replaced_functions.items():
1096
+ self._query = self.query.replace(f'UDF_{i}', function_name)
1097
+ self.functions.remove(f'UDF_{i}')
1098
+ self.functions.append(function_name)
928
1099
 
929
1100
  @property
930
1101
  def query(self):