pgsqlarbiter 0.2.0 → 0.3.0

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.
@@ -3,502 +3,1155 @@
3
3
  require "set"
4
4
 
5
5
  module Pgsqlarbiter
6
- # SQL query analyzer that lexes and walks a SQL string to extract its statement type,
7
- # referenced tables, and function calls. Uses a hand-written lexer and single-pass
8
- # token walker rather than a full parse tree.
6
+ # SQL query analyzer. Lexes the query with {Lexer}, parses it with {Parser}
7
+ # (generated from PostgreSQL's own grammar), and walks the resulting syntax
8
+ # tree to find every table, function and type the query refers to.
9
+ #
10
+ # The walk is fail-closed. Each nonterminal that carries a table, function or
11
+ # type name has exactly one meaning in each production that uses it, and that
12
+ # meaning is spelled out in a handler below. If the walk ever reaches such a
13
+ # name through a production with no handler, it raises instead of guessing.
9
14
  class Analyzer
10
- include TokenType
15
+ STATEMENT_START = Set[:SELECT, :VALUES, :TABLE, :WITH, :WITH_LA, :INSERT, :UPDATE, :DELETE_P, :MERGE, "("].freeze
11
16
 
12
- JOIN_PREFIXES = Set["INNER", "LEFT", "RIGHT", "FULL", "CROSS", "NATURAL"].freeze
13
- FUNCTIONS_WITH_FROM_SYNTAX = Set["EXTRACT", "TRIM", "SUBSTRING"].freeze
17
+ STATEMENT_KINDS = {
18
+ InsertStmt: :insert, UpdateStmt: :update, DeleteStmt: :delete, MergeStmt: :merge
19
+ }.freeze
14
20
 
15
- # Analyze a SQL query string.
21
+ # Nonterminals that are only ever labels: column names, aliases, window
22
+ # names, collations, operator classes, named-argument names. They never
23
+ # name a table, function or type, so the walk does not descend into them.
24
+ LABELS = Set[
25
+ :ColId, :ColLabel, :BareColLabel, :name, :attr_name, :param_name, :any_name,
26
+ # FOR UPDATE OF <names> refers to FROM-clause items that are recorded where they appear.
27
+ :qualified_name_list
28
+ ].freeze
29
+
30
+ # Nonterminals that carry a table, function or type name. The production
31
+ # that contains one must have a handler that says which it is; reaching one
32
+ # any other way is a bug in this class, and is treated as a denial.
33
+ ROLE_BEARING = Set[:qualified_name, :func_name, :type_function_name].freeze
34
+
35
+ # Syntax that parses but that pgsqlarbiter refuses outright.
36
+ UNSUPPORTED = {
37
+ any_operator: "OPERATOR() syntax",
38
+ reloptions: "operator class parameters",
39
+ func_type: "operator class parameters",
40
+ cursor_name: "WHERE CURRENT OF"
41
+ }.freeze
42
+
43
+ NUMERIC_TYPES = {
44
+ INT_P: "integer", INTEGER: "integer", SMALLINT: "smallint", BIGINT: "bigint",
45
+ REAL: "real", FLOAT_P: "float", DOUBLE_P: "double precision",
46
+ DECIMAL_P: "numeric", DEC: "numeric", NUMERIC: "numeric", BOOLEAN_P: "boolean"
47
+ }.freeze
48
+
49
+ # Upper bound on the work spent resolving names, so that a query built to
50
+ # make resolution quadratic is refused instead of tying up the caller.
51
+ WORK_LIMIT = 3_000_000
52
+
53
+ # One query level: a SELECT, or an INSERT, UPDATE, DELETE or MERGE. It
54
+ # holds the things its FROM clause (and target) make visible by name.
55
+ Level = Struct.new(:parent, :entries, :target, :outputs, :index)
56
+
57
+ # Something a qualified name can refer to: a table, subquery, CTE, function
58
+ # or join in FROM, or the target of a data-modifying statement.
16
59
  #
17
- # The analysis proceeds in four phases:
18
- # 1. Reject multiple statements
19
- # 2. Determine the statement type
20
- # 3. Pre-collect CTE names (so they are not treated as table references)
21
- # 4. Walk all tokens to extract table and function references
60
+ # * name: the alias it is known by; nil when the name is not certain, which
61
+ # makes it a candidate for every qualifier; :none when it has no name.
62
+ # * table: the table whose columns it exposes under their own names.
63
+ # * columns: the column names it certainly has. This may be fewer than it
64
+ # really has, never more. nil when nothing is known, :any for a table
65
+ # that is missing from the schema.
66
+ # * source: computes columns once the subquery it comes from has been walked.
67
+ # * members: the tables underneath a join alias.
68
+ # * pseudo: OLD, NEW and EXCLUDED.
69
+ # * hidden: inside a join that has an alias, so not part of "*".
70
+ Entry = Struct.new(:name, :table, :columns, :source, :members, :pseudo, :hidden)
71
+
72
+ Cte = Struct.new(:core, :columns)
73
+
74
+ LevelIndex = Struct.new(:any, :columns, :names, :wildcards)
75
+
76
+ HANDLERS = {
77
+ # statements and CTE scope
78
+ SelectStmt: :visit_children,
79
+ select_no_parens: :visit_statement,
80
+ simple_select: :visit_simple_select,
81
+ InsertStmt: :visit_statement,
82
+ UpdateStmt: :visit_statement,
83
+ DeleteStmt: :visit_statement,
84
+ MergeStmt: :visit_statement,
85
+ into_clause: :visit_into_clause,
86
+ for_locking_clause: :visit_for_locking_clause,
87
+ opt_on_conflict: :visit_on_conflict,
88
+ # relations
89
+ relation_expr: :visit_relation_expr,
90
+ relation_expr_opt_alias: :visit_target_relation,
91
+ insert_target: :visit_target_relation,
92
+ joined_table: :visit_joined_table,
93
+ # columns of the target table
94
+ set_target: :visit_target_column,
95
+ insert_column_item: :visit_target_column,
96
+ index_elem: :visit_index_elem,
97
+ insert_rest: :visit_insert_columns,
98
+ merge_insert: :visit_insert_columns,
99
+ # functions
100
+ func_application: :visit_func_application,
101
+ func_expr_common_subexpr: :visit_keyword_function,
102
+ json_aggregate_func: :visit_keyword_function,
103
+ xmltable: :visit_keyword_function,
104
+ json_table: :visit_keyword_function,
105
+ c_expr: :visit_c_expr,
106
+ tablesample_clause: :visit_tablesample_clause,
107
+ columnref: :visit_columnref,
108
+ table_ref: :visit_table_ref,
109
+ # types
110
+ GenericType: :visit_generic_type,
111
+ Numeric: :visit_numeric_type,
112
+ BitWithLength: :visit_bit_type,
113
+ BitWithoutLength: :visit_bit_type,
114
+ character: :visit_character_type,
115
+ ConstDatetime: :visit_datetime_type,
116
+ ConstInterval: :visit_interval_type,
117
+ JsonType: :visit_json_type,
118
+ AexprConst: :visit_constant
119
+ }.freeze
120
+
121
+ # @param schema [Schema] the tables and columns that exist
122
+ def initialize(schema)
123
+ raise ArgumentError, "schema must be a Pgsqlarbiter::Schema" unless schema.is_a?(Schema)
124
+
125
+ @schema = schema
126
+ end
127
+
128
+ # Analyze a SQL query string.
22
129
  #
23
130
  # @param sql [String] the SQL query to analyze
24
- # @return [Analysis] analysis result with statement_type, tables, and functions
25
- # @raise [ParseError] if the SQL is empty or cannot be parsed
131
+ # @return [Analysis] the statement type(s), tables, columns, functions and types
132
+ # @raise [LexError] if the SQL cannot be tokenized
133
+ # @raise [ParseError] if the SQL is empty or is not valid, supported syntax
134
+ # @raise [UnresolvedNameError] if a column or table alias cannot be resolved
26
135
  # @raise [MultipleStatementsError] if the SQL contains more than one statement
27
- # @raise [DisallowedStatementError] if the statement type is not SELECT, INSERT,
136
+ # @raise [DisallowedStatementError] if the statement is not SELECT, INSERT,
28
137
  # UPDATE, DELETE, MERGE, or VALUES
29
138
  def analyze(sql)
30
- @tokens = Lexer.new.tokenize(sql)
31
- @pos = 0
32
- @paren_depth = 0
139
+ tokens = single_statement(Lexer.new.tokenize(sql))
140
+ tree = Parser.new.parse(tokens)
141
+
33
142
  @tables = Set.new
34
143
  @functions = Set.new
35
- @cte_names = Set.new
36
- @suppress_from_depths = []
37
-
38
- reject_multiple_statements!
39
- stmt_type = determine_statement_type!
40
-
41
- @pos = 0
42
- @paren_depth = 0
43
- pre_collect_cte_names!
144
+ @types = Set.new
145
+ @kinds = Set.new
146
+ @columns = Set.new
147
+ @cte_scopes = []
148
+ @level = nil
149
+ @levels = {}.compare_by_identity
150
+ @outputs = {}.compare_by_identity
151
+ @alias_refs = {}.compare_by_identity
152
+ @refs = []
153
+ @work = WORK_LIMIT
154
+ @final = false
44
155
 
45
- @pos = 0
46
- @paren_depth = 0
47
- walk!
156
+ statement = tree.children[0]
157
+ statement_type = STATEMENT_KINDS[statement.name] || select_kind(statement)
158
+ @kinds << statement_type
159
+ walk(tree)
160
+ @final = true
161
+ @refs.each { |level, names, star, alias_ok| resolve_ref(level, names, star, alias_ok) }
48
162
 
49
163
  Analysis.new(
50
- statement_type: stmt_type,
51
- tables: @tables.to_a.sort,
52
- functions: @functions.to_a.sort
164
+ statement_type: statement_type,
165
+ statement_types: @kinds.to_a.sort.freeze,
166
+ tables: @tables.to_a.sort.freeze,
167
+ columns: @columns.to_a.sort.freeze,
168
+ functions: @functions.to_a.sort.freeze,
169
+ types: @types.to_a.sort.freeze
53
170
  )
54
171
  end
55
172
 
56
173
  private
57
174
 
58
- # --- Token access helpers ---
175
+ # --- Statement boundaries ---
59
176
 
60
- def current
61
- @tokens[@pos]
177
+ # Return the tokens of the one statement in the input. A semicolon can never
178
+ # appear inside a DML statement, so every semicolon separates statements.
179
+ def single_statement(tokens)
180
+ statements = tokens.slice_when { |a, b| a.type == ";" || b.type == ";" }
181
+ .reject { |part| part[0].type == ";" }
182
+ raise ParseError, "empty query" if statements.empty?
183
+ raise MultipleStatementsError, "multiple statements are not allowed" if statements.length > 1
184
+
185
+ statement = statements[0]
186
+ first = statement[0]
187
+ unless STATEMENT_START.include?(first.type)
188
+ if Keywords::TOKENS[first.value] == first.type
189
+ raise DisallowedStatementError, "#{first.value.upcase} statements are not allowed"
190
+ end
191
+ raise DisallowedStatementError, "expected a SQL statement, got #{first.value.inspect}"
192
+ end
193
+ statement
62
194
  end
63
195
 
64
- def peek
65
- @tokens[@pos + 1]
196
+ # :values when the statement is a bare VALUES list, otherwise :select.
197
+ def select_kind(node)
198
+ loop do
199
+ case node.name
200
+ when :SelectStmt, :select_clause then node = node.children[0]
201
+ when :select_with_parens then node = node.children[1]
202
+ when :select_no_parens
203
+ node = node.children.find { |c| c.is_a?(Node) && (c.name == :simple_select || c.name == :select_clause) }
204
+ else
205
+ first = node.children[0]
206
+ return (first.is_a?(Node) && first.name == :values_clause) ? :values : :select
207
+ end
208
+ end
66
209
  end
67
210
 
68
- def advance
69
- @pos += 1
211
+ # --- Tree walk ---
212
+ #
213
+ # Iterative, so that deeply nested or very long queries cannot overflow the
214
+ # Ruby stack. Handlers push the nodes they want visited; the stack is LIFO,
215
+ # so they push in reverse of the order the nodes should be visited in.
216
+
217
+ def walk(root)
218
+ @stack = [root]
219
+ until @stack.empty?
220
+ item = @stack.pop
221
+ if item.is_a?(Node)
222
+ name = item.name
223
+ if (handler = HANDLERS[name])
224
+ __send__(handler, item)
225
+ elsif LABELS.include?(name)
226
+ next
227
+ elsif ROLE_BEARING.include?(name)
228
+ raise ParseError, "unsupported syntax: #{name} in an unrecognized position"
229
+ elsif (what = UNSUPPORTED[name])
230
+ raise ParseError, "#{what} is not supported"
231
+ else
232
+ visit_children(item)
233
+ end
234
+ else
235
+ item.call
236
+ end
237
+ end
238
+ end
239
+
240
+ def visit_children(node)
241
+ node.children.reverse_each { |child| @stack << child if child.is_a?(Node) }
70
242
  end
71
243
 
72
- def at_end?
73
- current.type == EOF
244
+ def visit(children)
245
+ children.reverse_each { |child| @stack << child if child.is_a?(Node) }
74
246
  end
75
247
 
76
- def keyword?(value)
77
- current.type == KEYWORD && current.value == value
248
+ # --- Statements, query levels and CTE scope ---
249
+
250
+ # A statement that may carry a WITH clause. PostgreSQL's scoping rules:
251
+ #
252
+ # * The CTEs are visible in the whole statement the WITH is attached to.
253
+ # * Without RECURSIVE, a CTE sees only the CTEs listed before it. Its own
254
+ # name, inside its own body, means the real table.
255
+ # * With RECURSIVE, every CTE in the list is visible to all of them.
256
+ #
257
+ # The statement is also a query level for name resolution. ORDER BY, LIMIT
258
+ # and the locking clause of a SELECT are written outside the SELECT itself
259
+ # but belong to its level, so the level is made here and found again when
260
+ # the walk reaches the SELECT.
261
+ def visit_statement(node)
262
+ kind = STATEMENT_KINDS[node.name]
263
+ @kinds << kind if kind
264
+
265
+ level =
266
+ if kind
267
+ new_level
268
+ elsif (core = core_of(node, false))
269
+ @levels[core] ||= new_level
270
+ else
271
+ # ORDER BY on a UNION, INTERSECT or EXCEPT can only name result columns.
272
+ new_level(:any)
273
+ end
274
+ previous = nil
275
+ @stack << lambda do
276
+ finish_dml(level, node) if kind
277
+ @level = previous
278
+ end
279
+ kind ? setup_dml(level, node) : mark_output_names(node)
280
+
281
+ body = node.children
282
+ with = body[0]
283
+ with = with.children[0] if with.is_a?(Node) && with.name == :opt_with_clause
284
+ if with.is_a?(Node) && with.name == :with_clause
285
+ scope = {}
286
+ @cte_scopes << scope
287
+ @stack << -> { @cte_scopes.pop }
288
+ visit(body[1..])
289
+
290
+ recursive = with.children.any? { |c| c.is_a?(Token) && c.type == :RECURSIVE }
291
+ ctes = flatten(with.children.last).select { |c| c.is_a?(Node) }
292
+ # name opt_name_list AS opt_materialized '(' PreparableStmt ')' opt_search_clause opt_cycle_clause
293
+ named = ctes.map do |cte|
294
+ declared = cte.children[1].children[1]
295
+ [checked(leaf(cte.children[0])), Cte.new(core_of(cte.children[5], true), declared && list_names(declared))]
296
+ end
297
+ named.each { |name, info| scope[name] = info } if recursive
298
+ ctes.zip(named).reverse_each do |cte, (name, info)|
299
+ @stack << -> { scope[name] = info } unless recursive
300
+ visit(cte.children[1..])
301
+ end
302
+ else
303
+ visit(body)
304
+ end
305
+ @stack << lambda do
306
+ previous = @level
307
+ @level = level
308
+ end
78
309
  end
79
310
 
80
- def keyword_one_of?(*values)
81
- current.type == KEYWORD && values.include?(current.value)
311
+ def cte(name)
312
+ @cte_scopes.reverse_each do |scope|
313
+ info = scope[name]
314
+ return info if info
315
+ end
316
+ nil
82
317
  end
83
318
 
84
- def ident_or_quoted?
85
- current.type == IDENT || current.type == QUOTED_IDENT
319
+ # SELECT ..., VALUES ... and TABLE name are query levels. UNION, INTERSECT
320
+ # and EXCEPT are not: each side is its own level.
321
+ def visit_simple_select(node)
322
+ first = node.children[0]
323
+ return visit_children(node) if first.is_a?(Node) && first.name == :select_clause
324
+
325
+ level = (@levels[node] ||= new_level)
326
+ previous = nil
327
+ @stack << lambda do
328
+ finish_select(level, node)
329
+ @level = previous
330
+ end
331
+ if first.is_a?(Token) && first.type == :TABLE
332
+ add_relation(level, node.children[1], nil, nil)
333
+ elsif first.is_a?(Token)
334
+ group = node.children[6]
335
+ mark_bare_names(group.children[3], :group_by_item) unless group.children.empty?
336
+ end
337
+ visit_children(node)
338
+ @stack << lambda do
339
+ previous = @level
340
+ @level = level
341
+ end
86
342
  end
87
343
 
88
- def can_be_name?
89
- ident_or_quoted? || (current.type == KEYWORD && !clause_keyword?(current.value))
344
+ def new_level(outputs = nil)
345
+ Level.new(@level, [], nil, outputs, nil)
90
346
  end
91
347
 
92
- def identifier_value(token)
93
- case token.type
94
- when IDENT then token.value
95
- when QUOTED_IDENT
96
- if token.value.include?(".")
97
- raise ParseError,
98
- "quoted identifier containing a dot is not supported: \"#{token.value}\""
348
+ def current_level
349
+ @level || raise(ParseError, "unsupported syntax: name outside a statement")
350
+ end
351
+
352
+ # The statement that decides the result columns of a query: the SELECT,
353
+ # VALUES, TABLE, INSERT, UPDATE, DELETE or MERGE left after removing
354
+ # parentheses, WITH, ORDER BY and LIMIT. A set operation takes its columns
355
+ # from its left side; without through_set_operations it has no core.
356
+ def core_of(node, through_set_operations)
357
+ loop do
358
+ case node.name
359
+ when :PreparableStmt, :SelectStmt, :select_clause then node = node.children[0]
360
+ when :select_with_parens then node = node.children[1]
361
+ when :select_no_parens
362
+ node = node.children.find { |c| c.is_a?(Node) && (c.name == :select_clause || c.name == :simple_select) }
363
+ when :simple_select
364
+ first = node.children[0]
365
+ return node unless first.is_a?(Node) && first.name == :select_clause
366
+ return nil unless through_set_operations
367
+
368
+ node = first
369
+ else
370
+ return node
99
371
  end
100
- token.value
101
- when KEYWORD then token.value.downcase
102
- else token.value.to_s
103
372
  end
104
373
  end
105
374
 
106
- # --- Phase 1: Reject multiple statements ---
375
+ # ORDER BY and GROUP BY accept the name of a result column in place of an
376
+ # expression, but only when the whole item is that one name.
377
+ def mark_output_names(select_no_parens)
378
+ select_no_parens.children.each do |child|
379
+ next unless child.is_a?(Node)
107
380
 
108
- def reject_multiple_statements!
109
- @tokens.each_with_index do |token, i|
110
- if token.type == SEMICOLON
111
- rest = @tokens[(i + 1)..]
112
- if rest.any? { |t| t.type != EOF && t.type != SEMICOLON }
113
- raise MultipleStatementsError, "multiple statements are not allowed"
381
+ child = child.children[0] if child.name == :opt_sort_clause
382
+ mark_bare_names(child.children[2], :sortby) if child.is_a?(Node) && child.name == :sort_clause
383
+ end
384
+ end
385
+
386
+ def mark_bare_names(list, item_name)
387
+ flatten(list).each do |item|
388
+ next unless item.is_a?(Node) && item.name == item_name
389
+
390
+ node = item.children[0]
391
+ while node.is_a?(Node)
392
+ kids = node.children
393
+ if node.name == :columnref
394
+ @alias_refs[node] = true if kids.length == 1
395
+ break
396
+ elsif kids.length == 1
397
+ node = kids[0]
398
+ elsif node.name == :c_expr && kids.length == 4 && kids[0].is_a?(Token) && kids[0].type == "(" &&
399
+ kids[3].children.empty?
400
+ node = kids[1]
401
+ else
402
+ break
114
403
  end
115
404
  end
116
405
  end
117
406
  end
118
407
 
119
- # --- Phase 2: Determine statement type ---
408
+ def finish_select(level, node)
409
+ first = node.children[0]
410
+ outputs =
411
+ if first.is_a?(Node)
412
+ row = first
413
+ row = row.children[0] while row.children[0].is_a?(Node)
414
+ count = flatten(row.children[2]).count { |c| c.is_a?(Node) }
415
+ (1..count).map { |n| "column#{n}" }
416
+ elsif first.type == :TABLE
417
+ entry = level.entries[0]
418
+ charge_whole(entry)
419
+ star_columns([entry])
420
+ else
421
+ target = node.children[2]
422
+ target = target.children[0] if target.name == :opt_target_list
423
+ target ? output_names(level, target) : []
424
+ end
425
+ level.outputs = outputs unless level.outputs == :any
426
+ @outputs[node] = outputs
427
+ end
428
+
429
+ # The target of INSERT, UPDATE, DELETE or MERGE is visible by name in the
430
+ # statement, and RETURNING can also reach it as OLD and NEW.
431
+ def setup_dml(level, node)
432
+ target = node.children.find { |c| c.is_a?(Node) && (c.name == :relation_expr_opt_alias || c.name == :insert_target) }
433
+ table = relation_name(target)
434
+ alias_name = target.children.reverse.find { |c| c.is_a?(Node) && c.name == :ColId }
435
+ columns = @schema.all_columns(table) || :any
436
+ entry = Entry.new(alias_name ? leaf(alias_name) : table.split(".").last, table, columns)
437
+ level.entries << entry
438
+ level.target = entry
120
439
 
121
- def determine_statement_type!
122
- raise ParseError, "empty query" if at_end?
123
- raise DisallowedStatementError, "expected a SQL statement, got #{current.value.inspect}" unless current.type == KEYWORD
440
+ returning = node.children.last
441
+ return if returning.children.empty?
124
442
 
125
- case current.value
126
- when "SELECT" then :select
127
- when "INSERT" then :insert
128
- when "UPDATE" then :update
129
- when "DELETE" then :delete
130
- when "MERGE" then :merge
131
- when "VALUES" then :values
132
- when "WITH" then determine_cte_statement_type!
133
- else
134
- raise DisallowedStatementError, "#{current.value} statements are not allowed"
135
- end
443
+ names = %w[old new]
444
+ options = returning.children[1].children[2]
445
+ flatten(options).each { |o| names << leaf(o.children[2]) if o.is_a?(Node) } if options
446
+ names.each { |name| level.entries << Entry.new(name, table, columns, nil, nil, true) }
136
447
  end
137
448
 
138
- def determine_cte_statement_type!
139
- depth = 0
140
- advance # past WITH
141
- advance if keyword?("RECURSIVE")
449
+ def finish_dml(level, node)
450
+ returning = node.children.last
451
+ @outputs[node] = returning.children.empty? ? [] : output_names(level, returning.children[2])
452
+ end
142
453
 
143
- loop do
144
- break if at_end?
145
- case current.type
146
- when LPAREN then depth += 1; advance
147
- when RPAREN then depth -= 1; advance
148
- when KEYWORD
149
- if depth == 0
150
- case current.value
151
- when "SELECT" then return :select
152
- when "INSERT" then return :insert
153
- when "UPDATE" then return :update
154
- when "DELETE" then return :delete
155
- when "MERGE" then return :merge
156
- when "VALUES" then return :values
157
- else advance
158
- end
159
- else
160
- advance
454
+ # The names of the columns a target list produces, in order, with nil for
455
+ # a column whose name is not certain; nil when even the number of columns
456
+ # is unknown; :any when a table missing from the schema is involved.
457
+ def output_names(level, target_list)
458
+ names = []
459
+ any = opaque = false
460
+ flatten(target_list).each do |element|
461
+ next unless element.is_a?(Node)
462
+
463
+ kids = element.children
464
+ if kids.length > 1
465
+ names << leaf(kids.last)
466
+ next
467
+ end
468
+
469
+ if kids[0].is_a?(Token)
470
+ spend(level.entries.length)
471
+ level.entries.each { |entry| charge_whole(entry) unless entry.pseudo }
472
+ columns = star_columns(level.entries)
473
+ else
474
+ figured = figure_name(kids[0])
475
+ unless figured.is_a?(Array)
476
+ names << figured
477
+ next
161
478
  end
479
+ # qualifier.*; charged where the reference itself is resolved.
480
+ spend(level.entries.length)
481
+ matches = level.entries.select { |entry| entry.name == figured[0] || entry.name.nil? }
482
+ columns = (matches.length == 1 && !matches[0].hidden) ? star_columns(matches) : nil
483
+ end
484
+
485
+ if columns == :any
486
+ any = true
487
+ elsif columns
488
+ names.concat(columns)
162
489
  else
163
- advance
490
+ opaque = true
164
491
  end
165
492
  end
166
-
167
- raise ParseError, "could not determine statement type in WITH clause"
493
+ if any
494
+ :any
495
+ else
496
+ opaque ? nil : names
497
+ end
168
498
  end
169
499
 
170
- # --- Phase 3: Pre-collect CTE names (index-based, no @pos modification) ---
500
+ # The columns "*" expands to. Entries inside an aliased join are skipped,
501
+ # because the join's alias stands for them, unless the caller is that join.
502
+ def star_columns(entries, joined = false)
503
+ result = []
504
+ any = opaque = false
505
+ entries.each do |entry|
506
+ next if entry.pseudo || (entry.hidden && !joined)
171
507
 
172
- def pre_collect_cte_names!
173
- collect_cte_names_in_range!(0, @tokens.length)
508
+ columns = entry.table ? (@schema.columns(entry.table) || :any) : columns_of(entry)
509
+ if columns == :any
510
+ any = true
511
+ elsif columns
512
+ spend(columns.size)
513
+ result.concat(columns.to_a)
514
+ else
515
+ opaque = true
516
+ end
517
+ end
518
+ if any
519
+ :any
520
+ else
521
+ opaque ? nil : result
522
+ end
174
523
  end
175
524
 
176
- def collect_cte_names_in_range!(from, to)
177
- i = from
178
- while i < to
179
- if @tokens[i].type == KEYWORD && @tokens[i].value == "WITH"
180
- i += 1
181
- i += 1 if i < to && @tokens[i]&.type == KEYWORD && @tokens[i]&.value == "RECURSIVE"
182
- loop do
183
- break unless i < to && (@tokens[i].type == IDENT || @tokens[i].type == QUOTED_IDENT)
184
- @cte_names << identifier_value(@tokens[i])
185
- i += 1
186
- # Skip optional column list
187
- if i < to && @tokens[i]&.type == LPAREN
188
- depth = 1; i += 1
189
- while depth > 0 && i < to
190
- depth += 1 if @tokens[i].type == LPAREN
191
- depth -= 1 if @tokens[i].type == RPAREN
192
- i += 1
193
- end
194
- end
195
- # Skip AS [NOT] MATERIALIZED
196
- if i < to && @tokens[i]&.type == KEYWORD && @tokens[i]&.value == "AS"
197
- i += 1
198
- if i < to && @tokens[i]&.type == KEYWORD && @tokens[i]&.value == "NOT"
199
- i += 1
200
- i += 1 if i < to && @tokens[i]&.type == KEYWORD && @tokens[i]&.value == "MATERIALIZED"
201
- elsif i < to && @tokens[i]&.type == KEYWORD && @tokens[i]&.value == "MATERIALIZED"
202
- i += 1
203
- end
204
- # Recurse into CTE body for nested WITH clauses, then skip it
205
- if i < to && @tokens[i]&.type == LPAREN
206
- body_start = i + 1
207
- depth = 1; i += 1
208
- while depth > 0 && i < to
209
- depth += 1 if @tokens[i].type == LPAREN
210
- depth -= 1 if @tokens[i].type == RPAREN
211
- i += 1
212
- end
213
- collect_cte_names_in_range!(body_start, i - 1)
214
- end
525
+ # The name PostgreSQL gives an unlabelled result column, when that is
526
+ # certain: the last name of a column reference, or the name of a function.
527
+ # ["q"] for "q.*". nil for anything else, which leaves the column out of
528
+ # what is known; that can deny a valid query but never allows a bad one.
529
+ def figure_name(node)
530
+ loop do
531
+ kids = node.children
532
+ case node.name
533
+ when :a_expr, :b_expr
534
+ if kids.length == 1 || (kids.length == 3 && kids[1].is_a?(Token) && kids[1].type == :TYPECAST)
535
+ node = kids[0]
536
+ else
537
+ return nil
538
+ end
539
+ when :c_expr
540
+ first = kids[0]
541
+ if kids.length == 1 && first.is_a?(Node)
542
+ node = first
543
+ elsif kids.length == 4 && first.is_a?(Token) && first.type == "("
544
+ name = nil
545
+ flatten(kids[3]).each do |element|
546
+ second = element.children[1]
547
+ return nil if second.is_a?(Token) && second.type == "*"
548
+
549
+ name = leaf(second) if second.is_a?(Node) && second.name == :attr_name
215
550
  end
216
- if i < to && @tokens[i]&.type == COMMA
217
- i += 1
551
+ return name if name
552
+
553
+ node = kids[1]
554
+ else
555
+ return nil
556
+ end
557
+ when :columnref
558
+ name = leaf(kids[0])
559
+ return name if kids.length == 1
560
+
561
+ qualifier = name
562
+ subscripted = false
563
+ flatten(kids[1]).each do |element|
564
+ second = element.children[1]
565
+ if second.is_a?(Node) && second.name == :attr_name
566
+ qualifier = name = leaf(second)
567
+ elsif element.children[0].type == "["
568
+ subscripted = true
218
569
  else
219
- break
570
+ return subscripted ? nil : [qualifier]
220
571
  end
221
572
  end
573
+ return name
574
+ when :func_expr
575
+ call = kids[0]
576
+ return nil unless call.name == :func_application
577
+
578
+ return dotted_name(call.children[0]).split(".").last
222
579
  else
223
- i += 1
580
+ return nil
224
581
  end
225
582
  end
226
583
  end
227
584
 
228
- # --- Phase 4: Main walk ---
585
+ # SELECT ... INTO creates a table.
586
+ def visit_into_clause(node)
587
+ raise DisallowedStatementError, "SELECT INTO statements are not allowed" unless node.children.empty?
588
+ end
229
589
 
230
- def walk!
231
- while !at_end?
232
- dispatch_token!
590
+ # FOR UPDATE / FOR SHARE take row locks and need UPDATE privilege.
591
+ def visit_for_locking_clause(node)
592
+ @kinds << :update if node.children[0].is_a?(Node)
593
+ end
594
+
595
+ def visit_on_conflict(node)
596
+ if node.children.any? { |c| c.is_a?(Token) && c.type == :UPDATE }
597
+ @kinds << :update
598
+ level = current_level
599
+ target = level.target
600
+ level.entries << Entry.new("excluded", target.table, target.columns, nil, nil, true) if target
233
601
  end
602
+ visit_children(node)
603
+ end
604
+
605
+ # --- Relations ---
606
+
607
+ # A relation in a FROM clause or after TABLE: a table, unless it is an
608
+ # unqualified name with a CTE of that name in scope.
609
+ def visit_relation_expr(node)
610
+ name = relation_name(node)
611
+ @tables << name unless !name.include?(".") && cte(name)
612
+ end
613
+
614
+ # The target of INSERT, UPDATE, DELETE or MERGE is always a real table;
615
+ # PostgreSQL never resolves it to a CTE.
616
+ def visit_target_relation(node)
617
+ @tables << relation_name(node)
234
618
  end
235
619
 
236
- def dispatch_token!
237
- case current.type
238
- when KEYWORD then handle_keyword!
239
- when IDENT, QUOTED_IDENT then maybe_extract_function_call!
240
- when LPAREN then @paren_depth += 1; advance
241
- when RPAREN then handle_rparen!
242
- else advance
620
+ def relation_name(node)
621
+ node = node.children.find { |c| c.is_a?(Node) } until node.name == :qualified_name
622
+ dotted_name(node)
623
+ end
624
+
625
+ # --- FROM items ---
626
+
627
+ def visit_table_ref(node)
628
+ kids = node.children
629
+ item = kids.find { |c| c.is_a?(Node) }
630
+ level = current_level
631
+ case item.name
632
+ when :relation_expr
633
+ name, list = alias_of(kids[1])
634
+ add_relation(level, item, name, list)
635
+ when :func_table
636
+ add_function(level, item, kids.last)
637
+ when :xmltable, :json_table
638
+ name, list = alias_of(kids.last)
639
+ level.entries << Entry.new(name || item.name.to_s, nil, list || declared_columns(item))
640
+ when :select_with_parens
641
+ name, list = alias_of(kids.last)
642
+ core = core_of(item, true)
643
+ level.entries << Entry.new(name || :none, nil, list, list ? nil : -> { @outputs[core] })
644
+ when :joined_table
645
+ if kids[0].is_a?(Token)
646
+ start = level.entries.length
647
+ name, list = alias_of(kids.last)
648
+ @stack << -> { add_join_alias(level, start, name, list) }
649
+ end
243
650
  end
651
+ visit_children(node)
244
652
  end
245
653
 
246
- def handle_rparen!
247
- if !@suppress_from_depths.empty? && @paren_depth == @suppress_from_depths.last + 1
248
- @suppress_from_depths.pop
654
+ # [alias name, column alias list] of an alias clause; either can be nil.
655
+ def alias_of(node)
656
+ inner = node.children[0]
657
+ node = inner if node.name != :alias_clause && inner.is_a?(Node) && inner.name == :alias_clause
658
+ name = node.children.find { |c| c.is_a?(Node) && c.name == :ColId }
659
+ list = node.children.find { |c| c.is_a?(Node) && (c.name == :name_list || c.name == :TableFuncElementList) }
660
+ [name && leaf(name), list && list_names(list)]
661
+ end
662
+
663
+ def list_names(list)
664
+ flatten(list).filter_map do |item|
665
+ next unless item.is_a?(Node)
666
+
667
+ leaf((item.name == :TableFuncElement) ? item.children[0] : item)
249
668
  end
250
- @paren_depth -= 1
251
- advance
252
669
  end
253
670
 
254
- def suppress_from?
255
- !@suppress_from_depths.empty? && @paren_depth > @suppress_from_depths.last
671
+ # A column alias list renames columns by position. Which columns are left
672
+ # with their own names depends on their order, so only the aliases are
673
+ # taken as known, and a table renamed this way counts as read in full.
674
+ def add_relation(level, relation, alias_name, list)
675
+ table = relation_name(relation)
676
+ if !table.include?(".") && (info = cte(table))
677
+ source = list ? nil : -> { info.columns || @outputs[info.core] }
678
+ level.entries << Entry.new(alias_name || table, nil, list, source)
679
+ elsif list
680
+ @columns << "#{table}.*"
681
+ level.entries << Entry.new(alias_name || table.split(".").last, nil, @schema.table?(table) ? list : :any)
682
+ else
683
+ columns = @schema.all_columns(table) || :any
684
+ level.entries << Entry.new(alias_name || table.split(".").last, table, columns)
685
+ end
256
686
  end
257
687
 
258
- def handle_keyword!
259
- val = current.value
260
- case val
261
- when "FROM"
262
- if suppress_from?
263
- advance
264
- else
265
- advance
266
- extract_from_list!
267
- end
268
- when "JOIN"
269
- advance
270
- extract_single_from_item!
271
- when "INNER"
272
- advance
273
- if keyword?("JOIN")
274
- advance
275
- extract_single_from_item!
276
- end
277
- when "LEFT", "RIGHT", "FULL"
278
- advance
279
- advance if keyword?("OUTER")
280
- if keyword?("JOIN")
281
- advance
282
- extract_single_from_item!
283
- end
284
- when "CROSS"
285
- advance
286
- if keyword?("JOIN")
287
- advance
288
- extract_single_from_item!
688
+ def add_function(level, func_table, alias_clause)
689
+ alias_name, list = alias_of(alias_clause)
690
+ function = nil
691
+ base = nil
692
+ call = func_table.children[0]
693
+ call = call.children[0] if call.is_a?(Node)
694
+ if call.is_a?(Node) && call.name == :func_application
695
+ function = dotted_name(call.children[0])
696
+ # Nothing is assumed about a function the schema does not describe,
697
+ # PostgreSQL's own included.
698
+ case (shape = @schema.function_columns(function))
699
+ when :scalar then base = [alias_name || function.split(".").last]
700
+ when Array then base = shape
289
701
  end
290
- when "NATURAL"
291
- advance
292
- if keyword_one_of?("LEFT", "RIGHT", "FULL", "INNER")
293
- advance
294
- advance if keyword?("OUTER")
295
- end
296
- if keyword?("JOIN")
297
- advance
298
- extract_single_from_item!
702
+ end
703
+ ordinality = !func_table.children.last.children.empty?
704
+ # Like every column list here, this may be fewer names than there really
705
+ # are: WITH ORDINALITY adds a column whatever the function returns.
706
+ columns =
707
+ if list
708
+ (ordinality && base && list.length == base.length) ? list + ["ordinality"] : list
709
+ elsif ordinality
710
+ (base || []) + ["ordinality"]
711
+ else
712
+ base
299
713
  end
300
- when "INTO"
301
- advance
302
- read_table_ref! if can_be_name?
303
- when "UPDATE"
304
- advance
305
- advance if keyword?("ONLY")
306
- read_table_ref! if can_be_name?
307
- when "USING"
308
- advance
309
- # USING (col_list) after JOIN starts with LPAREN — skip
310
- # USING table after DELETE/MERGE — extract
311
- if can_be_name? && current.type != LPAREN
312
- extract_single_from_item!
714
+ # Without an alias a function call is known by its name. Other forms
715
+ # (CURRENT_DATE, COALESCE(...), ROWS FROM (...)) have no certain name.
716
+ level.entries << Entry.new(alias_name || function&.split(".")&.last, nil, columns)
717
+ end
718
+
719
+ # The column names written in XMLTABLE(... COLUMNS ...) or JSON_TABLE(... COLUMNS (...)).
720
+ def declared_columns(node)
721
+ names = []
722
+ pending = [node]
723
+ until pending.empty?
724
+ current = pending.pop
725
+ current.children.each do |child|
726
+ next unless child.is_a?(Node)
727
+
728
+ case child.name
729
+ when :xmltable_column_list, :json_table_column_definition_list
730
+ flatten(child).each { |item| pending << item if item.is_a?(Node) }
731
+ when :ColId
732
+ names << leaf(child) if current.name == :xmltable_column_el || current.name == :json_table_column_definition
733
+ end
313
734
  end
735
+ end
736
+ names
737
+ end
738
+
739
+ # "(a JOIN b ...) AS j": j stands for the joined columns, and a and b can
740
+ # no longer be named from outside the join.
741
+ def add_join_alias(level, start, name, list)
742
+ members = level.entries[start..]
743
+ spend(members.length)
744
+ visible = members.reject(&:hidden)
745
+ tables = members.select(&:table)
746
+ members.each { |member| member.hidden = true }
747
+ entry = Entry.new(name, nil, list, nil, tables)
748
+ if list
749
+ tables.each { |member| charge_whole(member) }
314
750
  else
315
- if FUNCTIONS_WITH_FROM_SYNTAX.include?(val) && peek&.type == LPAREN
316
- @functions << val.downcase
317
- @suppress_from_depths.push(@paren_depth)
318
- advance # past keyword; main loop handles LPAREN
319
- elsif Keywords::FUNCTION_KEYWORDS.include?(val) && peek&.type == LPAREN
320
- @functions << val.downcase
321
- advance
322
- else
323
- advance
751
+ entry.source = -> { star_columns(visible, true) }
752
+ end
753
+ level.entries << entry
754
+ end
755
+
756
+ # NATURAL and USING read columns without naming their tables.
757
+ def visit_joined_table(node)
758
+ kids = node.children
759
+ natural = kids.any? { |c| c.is_a?(Token) && c.type == :NATURAL }
760
+ qual = kids.last
761
+ using = qual.is_a?(Node) && qual.name == :join_qual && qual.children[0].type == :USING
762
+ if natural || using
763
+ level = current_level
764
+ start = level.entries.length
765
+ @stack << lambda do
766
+ tables = level.entries[start..].select(&:table)
767
+ spend(tables.length)
768
+ if natural
769
+ tables.each { |member| charge_whole(member) }
770
+ else
771
+ names = list_names(qual.children[2])
772
+ spend(tables.length * names.length)
773
+ names.each { |column| charge_members(tables, column) }
774
+ alias_node = qual.children[4]
775
+ unless alias_node.children.empty?
776
+ level.entries << Entry.new(leaf(alias_node.children[1]), nil, names, nil, tables)
777
+ end
778
+ end
324
779
  end
325
780
  end
781
+ visit_children(node)
782
+ end
783
+
784
+ # --- Columns of the target table ---
785
+
786
+ # A column assigned by SET or named in an INSERT column list.
787
+ def visit_target_column(node)
788
+ charge_target(leaf(node.children[0]))
789
+ visit(node.children[1..])
790
+ end
791
+
792
+ # ON CONFLICT (column, ...)
793
+ def visit_index_elem(node)
794
+ first = node.children[0]
795
+ charge_target(leaf(first)) if first.is_a?(Node) && first.name == :ColId
796
+ visit_children(node)
797
+ end
798
+
799
+ # INSERT without a column list writes every column.
800
+ def visit_insert_columns(node)
801
+ charge_target("*") unless node.children.any? { |c| c.is_a?(Node) && c.name == :insert_column_list }
802
+ visit_children(node)
803
+ end
804
+
805
+ def charge_target(column)
806
+ target = current_level.target
807
+ raise ParseError, "unsupported syntax: column assignment outside a data-modifying statement" unless target
808
+
809
+ @columns << "#{target.table}.#{column}"
810
+ end
811
+
812
+ # --- Functions ---
813
+
814
+ def visit_func_application(node)
815
+ @functions << dotted_name(node.children[0])
816
+ visit(node.children[1..])
817
+ end
818
+
819
+ # Functions with their own syntax: COALESCE(...), EXTRACT(... FROM ...),
820
+ # CURRENT_USER, JSON_OBJECT(...), XMLTABLE(...), and so on. The function is
821
+ # named after its keyword. CAST and TREAT are type conversions, recorded
822
+ # through their type name.
823
+ def visit_keyword_function(node)
824
+ keyword = node.children[0]
825
+ case keyword.type
826
+ when :CAST, :TREAT then nil
827
+ when :COLLATION then @functions << "pg_collation_for"
828
+ else @functions << keyword.value
829
+ end
830
+ visit_children(node)
326
831
  end
327
832
 
328
- # --- Function call detection ---
833
+ def visit_c_expr(node)
834
+ first = node.children[0]
835
+ @functions << "grouping" if first.is_a?(Token) && first.type == :GROUPING
836
+ node.children.each do |child|
837
+ next unless child.is_a?(Node) && (child.name == :indirection || child.name == :opt_indirection)
329
838
 
330
- def maybe_extract_function_call!
331
- if peek&.type == LPAREN
332
- name = identifier_value(current)
333
- @functions << name
334
- advance
335
- elsif peek&.type == DOT
336
- name1 = identifier_value(current)
337
- advance # past ident
338
- advance # past dot
339
- if (ident_or_quoted? || current.type == KEYWORD) && peek&.type == LPAREN
340
- name2 = identifier_value(current)
341
- @functions << "#{name1}.#{name2}"
342
- advance
839
+ # (expr).name, $1.name, (SELECT ...).name
840
+ flatten(child).each do |element|
841
+ attribute = element.children[1]
842
+ record_attribute_call(leaf(attribute)) if attribute.is_a?(Node) && attribute.name == :attr_name
343
843
  end
344
- else
345
- advance
346
844
  end
845
+ visit_children(node)
347
846
  end
348
847
 
349
- # --- FROM list extraction ---
848
+ # --- Column references and attribute notation ---
849
+ #
850
+ # PostgreSQL treats "x.name" as the function call "name(x)" whenever x has
851
+ # no column called name. That is how computed columns work, but it applies
852
+ # to every function and every x, so it is a second way to write a function
853
+ # call. Every reference is therefore resolved against the schema:
854
+ #
855
+ # * "(expr).name" and anything after a subscript always counts as a
856
+ # possible call of name.
857
+ # * "alias.name" is a column only if name is certainly a column of
858
+ # everything alias could refer to, at this query level or any enclosing
859
+ # one. Otherwise it counts as a call of name on the whole row.
860
+ # * A bare name is never a call, but it must be found: as a column of
861
+ # something in FROM at this level or an enclosing one, or as the name of
862
+ # a FROM item (a whole-row reference). A name that cannot be found is
863
+ # refused, so that a column missing from the schema is never read.
864
+ #
865
+ # Resolution waits until the whole query has been walked, because a name
866
+ # can refer to a FROM item that is written after it.
350
867
 
351
- def extract_from_list!
352
- loop do
353
- break if at_end?
354
- break if should_end_from_list?
868
+ def record_attribute_call(name)
869
+ @functions << checked(name)
870
+ end
355
871
 
356
- extract_from_item!
872
+ def visit_columnref(node)
873
+ names = [leaf(node.children[0])]
874
+ star = subscripted = false
875
+ if (indirection = node.children[1])
876
+ flatten(indirection).each do |element|
877
+ attribute = element.children[1]
878
+ if attribute.is_a?(Node) && attribute.name == :attr_name
879
+ if subscripted
880
+ record_attribute_call(leaf(attribute))
881
+ else
882
+ names << leaf(attribute)
883
+ end
884
+ elsif element.children[0].type == "["
885
+ subscripted = true
886
+ visit_children(element)
887
+ elsif !subscripted
888
+ star = true
889
+ end
890
+ end
891
+ end
892
+ @refs << [current_level, names, star, @alias_refs.key?(node)]
893
+ end
894
+
895
+ def resolve_ref(level, names, star, alias_ok)
896
+ return resolve_bare_name(level, names[0], alias_ok) if names.length == 1 && !star
357
897
 
358
- if current.type == COMMA
359
- advance
898
+ # [catalog.][schema.]qualifier.column, or [catalog.][schema.]qualifier.*
899
+ qualifier = star ? names[-1] : names[-2]
900
+ found = candidates(level, qualifier)
901
+ if found.empty?
902
+ raise UnresolvedNameError, "#{names.join(".").inspect} refers to #{qualifier.inspect}, which is not in FROM"
903
+ end
904
+
905
+ column = names[-1]
906
+ found.each do |entry|
907
+ if star
908
+ charge_whole(entry)
909
+ next
910
+ end
911
+ columns = columns_of(entry)
912
+ next if columns == :any
913
+
914
+ if columns&.include?(column)
915
+ charge(entry, column)
360
916
  else
361
- break
917
+ record_attribute_call(column)
918
+ charge_whole(entry)
362
919
  end
363
920
  end
364
921
  end
365
922
 
366
- def should_end_from_list?
367
- return true if current.type == RPAREN
368
- return true if current.type == SEMICOLON
369
- return true if current.type == EOF
370
- if current.type == KEYWORD
371
- v = current.value
372
- return true if %w[WHERE GROUP HAVING ORDER LIMIT OFFSET FETCH
373
- UNION INTERSECT EXCEPT WINDOW FOR RETURNING
374
- ON SET WHEN].include?(v)
375
- return true if v == "JOIN"
376
- return true if JOIN_PREFIXES.include?(v) && has_join_ahead?
923
+ def resolve_bare_name(level, name, alias_ok)
924
+ outputs = level.outputs
925
+ found = alias_ok && (outputs == :any || (outputs && outputs.include?(name)))
926
+ column_found = false
927
+ rows = nil
928
+ while level
929
+ spend(1)
930
+ index = index_of(level)
931
+ found = true if index.any
932
+ if (tables = index.columns[name])
933
+ found = column_found = true
934
+ tables.each { |table| @columns << "#{table}.#{name}" }
935
+ end
936
+ if (entries = index.names[name])
937
+ (rows ||= []).concat(entries)
938
+ end
939
+ level = level.parent
940
+ end
941
+ # A name that is not a column anywhere is a whole-row reference to the
942
+ # FROM item of that name.
943
+ rows&.each do |entry|
944
+ next if entry.pseudo && column_found
945
+
946
+ found = true
947
+ charge_whole(entry)
377
948
  end
378
- false
949
+ raise UnresolvedNameError, "column #{name.inspect} cannot be resolved" unless found
379
950
  end
380
951
 
381
- def has_join_ahead?
382
- i = @pos + 1
383
- while i < @tokens.length && i <= @pos + 3
384
- return true if @tokens[i].type == KEYWORD && @tokens[i].value == "JOIN"
385
- return false unless @tokens[i].type == KEYWORD && %w[OUTER INNER].include?(@tokens[i].value)
386
- i += 1
952
+ # Everything the qualifier could refer to, at this level and every
953
+ # enclosing one. PostgreSQL uses the nearest; taking all of them means a
954
+ # mistake about which is nearest can only deny a query.
955
+ def candidates(level, qualifier)
956
+ found = []
957
+ while level
958
+ index = index_of(level)
959
+ entries = index.names[qualifier]
960
+ found.concat(entries) if entries
961
+ found.concat(index.wildcards)
962
+ spend(1 + found.length)
963
+ level = level.parent
387
964
  end
388
- false
965
+ found
389
966
  end
390
967
 
391
- def extract_single_from_item!
392
- extract_from_item!
393
- end
968
+ def index_of(level)
969
+ index = level.index
970
+ return index if index
394
971
 
395
- def extract_from_item!
396
- advance if keyword?("LATERAL")
397
- advance if keyword?("ONLY")
972
+ index = level.index = LevelIndex.new(false, {}, {}, [])
973
+ seen = {}
974
+ spend(level.entries.length)
975
+ level.entries.each do |entry|
976
+ name = entry.name
977
+ if name.nil?
978
+ index.wildcards << entry
979
+ elsif name != :none
980
+ (index.names[name] ||= []) << entry
981
+ end
982
+
983
+ columns = columns_of(entry)
984
+ if columns == :any
985
+ index.any = true
986
+ next
987
+ end
988
+ next unless columns
398
989
 
399
- if current.type == LPAREN
400
- # Subquery or parenthesized expression — walk inside for refs
401
- walk_balanced_group!
402
- skip_alias!
403
- return
990
+ table = entry.table
991
+ next if table && seen[table]
992
+
993
+ seen[table] = true if table
994
+ spend(columns.size)
995
+ columns.each do |column|
996
+ tables = (index.columns[column] ||= []) if column
997
+ tables << table if table && column
998
+ end
404
999
  end
1000
+ index
1001
+ end
405
1002
 
406
- return if at_end?
407
- return unless can_be_name?
1003
+ def columns_of(entry)
1004
+ columns = entry.columns
1005
+ return columns if columns || !entry.source
408
1006
 
409
- name = read_qualified_name!
1007
+ columns = entry.source.call
1008
+ entry.columns = columns if @final
1009
+ columns
1010
+ end
410
1011
 
411
- if current.type == LPAREN
412
- # Table-valued function in FROM
413
- @functions << name unless @cte_names.include?(name)
414
- walk_balanced_group! # walk function args for nested refs
415
- skip_alias!
416
- else
417
- @tables << name unless @cte_names.include?(name)
418
- skip_alias!
1012
+ def charge(entry, column)
1013
+ if entry.table
1014
+ @columns << "#{entry.table}.#{column}"
1015
+ elsif entry.members
1016
+ spend(entry.members.length)
1017
+ charge_members(entry.members, column)
1018
+ end
1019
+ end
1020
+
1021
+ def charge_members(tables, column)
1022
+ tables.each { |member| @columns << "#{member.table}.#{column}" if @schema.column?(member.table, column) }
1023
+ end
1024
+
1025
+ def charge_whole(entry)
1026
+ if entry.table
1027
+ @columns << "#{entry.table}.*"
1028
+ elsif entry.members
1029
+ spend(entry.members.length)
1030
+ entry.members.each { |member| @columns << "#{member.table}.*" }
419
1031
  end
420
1032
  end
421
1033
 
422
- # Walk tokens inside balanced parens, extracting refs. Consumes through closing RPAREN.
423
- def walk_balanced_group!
424
- return unless current.type == LPAREN
425
- start_depth = @paren_depth
426
- @paren_depth += 1
427
- advance # past LPAREN
1034
+ def spend(amount)
1035
+ @work -= amount
1036
+ raise ParseError, "query is too complex to analyze" if @work < 0
1037
+ end
1038
+
1039
+ # TABLESAMPLE method(...): the sampling method is a function.
1040
+ def visit_tablesample_clause(node)
1041
+ @functions << dotted_name(node.children[1])
1042
+ visit(node.children[2..])
1043
+ end
428
1044
 
429
- while !at_end? && @paren_depth > start_depth
430
- dispatch_token!
1045
+ # --- Types ---
1046
+ #
1047
+ # Every Typename, SimpleTypename and ConstTypename reduces to one of these.
1048
+ # Array bounds, SETOF and type modifiers are not part of the recorded name.
1049
+
1050
+ def visit_generic_type(node)
1051
+ parts = [leaf(node.children[0])]
1052
+ rest = node.children[1..]
1053
+ if rest[0].is_a?(Node) && rest[0].name == :attrs
1054
+ flatten(rest[0]).each { |c| parts << leaf(c) if c.is_a?(Node) }
431
1055
  end
1056
+ @types << parts.map { |part| checked(part) }.join(".")
1057
+ visit(rest.reject { |c| c.is_a?(Node) && c.name == :attrs })
1058
+ end
1059
+
1060
+ def visit_numeric_type(node)
1061
+ @types << NUMERIC_TYPES.fetch(node.children[0].type)
1062
+ visit_children(node)
1063
+ end
1064
+
1065
+ def visit_bit_type(node)
1066
+ varying = !node.children[1].children.empty?
1067
+ @types << (varying ? "bit varying" : "bit")
1068
+ visit_children(node)
432
1069
  end
433
1070
 
434
- # --- Table reference reading ---
1071
+ def visit_character_type(node)
1072
+ varying = node.children.any? do |c|
1073
+ c.is_a?(Token) ? c.type == :VARCHAR : !c.children.empty?
1074
+ end
1075
+ @types << (varying ? "character varying" : "character")
1076
+ end
435
1077
 
436
- def read_table_ref!
437
- return if at_end?
438
- return unless can_be_name?
1078
+ def visit_datetime_type(node)
1079
+ base = (node.children[0].type == :TIMESTAMP) ? "timestamp" : "time"
1080
+ zone = node.children.last.children[0]
1081
+ @types << ((zone && zone.type == :WITH_LA) ? "#{base} with time zone" : base)
1082
+ end
439
1083
 
440
- name = read_qualified_name!
441
- @tables << name unless @cte_names.include?(name)
442
- skip_alias!
1084
+ def visit_interval_type(_node)
1085
+ @types << "interval"
443
1086
  end
444
1087
 
445
- def read_qualified_name!
446
- part1 = identifier_value(current)
447
- advance
1088
+ def visit_json_type(_node)
1089
+ @types << "json"
1090
+ end
448
1091
 
449
- if current.type == DOT && peek && (peek.type == IDENT || peek.type == QUOTED_IDENT || peek.type == KEYWORD)
450
- advance # past dot
451
- part2 = identifier_value(current)
452
- advance
453
- if current.type == DOT && peek && (peek.type == IDENT || peek.type == QUOTED_IDENT)
454
- advance # past dot
455
- part3 = identifier_value(current)
456
- advance
457
- "#{part1}.#{part2}.#{part3}"
458
- else
459
- "#{part1}.#{part2}"
460
- end
1092
+ # A constant. "name 'literal'" and "name(modifiers) 'literal'" are casts of
1093
+ # the literal to the type called name, even though name is written with the
1094
+ # grammar's func_name.
1095
+ def visit_constant(node)
1096
+ first = node.children[0]
1097
+ if first.is_a?(Node) && first.name == :func_name
1098
+ @types << dotted_name(first)
1099
+ visit(node.children[1..])
461
1100
  else
462
- part1
1101
+ visit_children(node)
463
1102
  end
464
1103
  end
465
1104
 
466
- # --- Alias skipping ---
1105
+ # --- Names ---
467
1106
 
468
- def skip_alias!
469
- return if at_end?
1107
+ # The text of a single-identifier nonterminal (ColId, type_function_name,
1108
+ # ColLabel, name, attr_name, ...).
1109
+ def leaf(node)
1110
+ node = node.children[0] while node.is_a?(Node)
1111
+ node.value
1112
+ end
470
1113
 
471
- if keyword?("AS")
472
- advance
473
- if ident_or_quoted? || (current.type == KEYWORD && !clause_keyword?(current.value))
474
- advance
475
- end
476
- skip_balanced_parens! if current.type == LPAREN
477
- elsif ident_or_quoted?
478
- unless current.type == KEYWORD && clause_keyword?(current.value)
479
- advance
480
- skip_balanced_parens! if current.type == LPAREN
1114
+ # A possibly qualified name: qualified_name, func_name (ColId indirection,
1115
+ # or a single identifier). Subscripts and ".*" are not valid in a name.
1116
+ def dotted_name(node)
1117
+ parts = [leaf(node.children[0])]
1118
+ if (indirection = node.children[1])
1119
+ flatten(indirection).each do |element|
1120
+ attribute = element.children[1]
1121
+ unless element.children.length == 2 && attribute.is_a?(Node) && attribute.name == :attr_name
1122
+ raise ParseError, "unsupported syntax: subscript or * in a name"
1123
+ end
1124
+ parts << leaf(attribute)
481
1125
  end
482
1126
  end
1127
+ parts.map { |part| checked(part) }.join(".")
483
1128
  end
484
1129
 
485
- def clause_keyword?(value)
486
- %w[WHERE GROUP HAVING ORDER LIMIT OFFSET FETCH UNION INTERSECT EXCEPT
487
- JOIN INNER LEFT RIGHT FULL CROSS NATURAL ON USING
488
- WINDOW FOR RETURNING SET WHEN MATCHED FROM INTO].include?(value)
1130
+ # Names are recorded with their parts joined by dots, so a part that itself
1131
+ # contains a dot would be indistinguishable from a qualified name.
1132
+ def checked(part)
1133
+ if part.include?(".")
1134
+ raise ParseError, "quoted identifier containing a dot is not supported: \"#{part}\""
1135
+ end
1136
+ part
489
1137
  end
490
1138
 
491
- def skip_balanced_parens!
492
- return unless current.type == LPAREN
493
- depth = 1
494
- advance
495
- until at_end? || depth == 0
496
- case current.type
497
- when LPAREN then depth += 1
498
- when RPAREN then depth -= 1
1139
+ # The items of a left-recursive list production (x: item | x sep item), in
1140
+ # source order, without recursing.
1141
+ def flatten(node)
1142
+ groups = []
1143
+ loop do
1144
+ children = node.children
1145
+ first = children[0]
1146
+ if first.is_a?(Node) && first.name == node.name
1147
+ groups << children[1..]
1148
+ node = first
1149
+ else
1150
+ groups << children
1151
+ break
499
1152
  end
500
- advance
501
1153
  end
1154
+ groups.reverse.flatten(1)
502
1155
  end
503
1156
  end
504
1157
  end