pgsqlarbiter 0.3.0 → 0.3.1
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.
- checksums.yaml +4 -4
- data/lib/pgsqlarbiter/analyzer.rb +40 -12
- data/lib/pgsqlarbiter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d5178e58b5e02fdb99f9d4a0df9c95b2b8434a6410f33e0a3c05cac5e2b136ba
|
|
4
|
+
data.tar.gz: 5e963c2a73a6e3f4d4e6aa4a97e1d90c87da545a902bfba7223ccf4d37fd18a4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2799c45728604db8a1c2532eedce29d4bcfcf77632ff4315a22ef941c42bc690be06d991e2b0e09087de04e7adc6b2bc6227c3c2fac4cef5c82725cba1eea935
|
|
7
|
+
data.tar.gz: 64d0f4883c2031a8c3345ac0d6106147ece157c1516aa72d67343844192920492629d12ca1320b44a02de3ffddb7f7932e9ce1cf0398b675252972f0e41f105f
|
|
@@ -52,7 +52,11 @@ module Pgsqlarbiter
|
|
|
52
52
|
|
|
53
53
|
# One query level: a SELECT, or an INSERT, UPDATE, DELETE or MERGE. It
|
|
54
54
|
# holds the things its FROM clause (and target) make visible by name.
|
|
55
|
-
|
|
55
|
+
#
|
|
56
|
+
# * from_depth: how many FROM items of this level the walk is inside.
|
|
57
|
+
# * in_parent_from: the level is inside a FROM item of its parent, where
|
|
58
|
+
# PostgreSQL may hide some of the parent's FROM items from it.
|
|
59
|
+
Level = Struct.new(:parent, :entries, :target, :outputs, :index, :from_depth, :in_parent_from)
|
|
56
60
|
|
|
57
61
|
# Something a qualified name can refer to: a table, subquery, CTE, function
|
|
58
62
|
# or join in FROM, or the target of a data-modifying statement.
|
|
@@ -158,7 +162,7 @@ module Pgsqlarbiter
|
|
|
158
162
|
@kinds << statement_type
|
|
159
163
|
walk(tree)
|
|
160
164
|
@final = true
|
|
161
|
-
@refs.each { |
|
|
165
|
+
@refs.each { |ref| resolve_ref(*ref) }
|
|
162
166
|
|
|
163
167
|
Analysis.new(
|
|
164
168
|
statement_type: statement_type,
|
|
@@ -295,10 +299,14 @@ module Pgsqlarbiter
|
|
|
295
299
|
[checked(leaf(cte.children[0])), Cte.new(core_of(cte.children[5], true), declared && list_names(declared))]
|
|
296
300
|
end
|
|
297
301
|
named.each { |name, info| scope[name] = info } if recursive
|
|
302
|
+
# A CTE body cannot see the FROM items of the statement the WITH is
|
|
303
|
+
# attached to, only those of the levels around that statement.
|
|
304
|
+
@stack << -> { @level = level }
|
|
298
305
|
ctes.zip(named).reverse_each do |cte, (name, info)|
|
|
299
306
|
@stack << -> { scope[name] = info } unless recursive
|
|
300
307
|
visit(cte.children[1..])
|
|
301
308
|
end
|
|
309
|
+
@stack << -> { @level = level.parent }
|
|
302
310
|
else
|
|
303
311
|
visit(body)
|
|
304
312
|
end
|
|
@@ -342,7 +350,11 @@ module Pgsqlarbiter
|
|
|
342
350
|
end
|
|
343
351
|
|
|
344
352
|
def new_level(outputs = nil)
|
|
345
|
-
Level.new(@level, [], nil, outputs, nil)
|
|
353
|
+
Level.new(@level, [], nil, outputs, nil, 0, in_from?)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def in_from?
|
|
357
|
+
!!@level && @level.from_depth > 0
|
|
346
358
|
end
|
|
347
359
|
|
|
348
360
|
def current_level
|
|
@@ -628,6 +640,8 @@ module Pgsqlarbiter
|
|
|
628
640
|
kids = node.children
|
|
629
641
|
item = kids.find { |c| c.is_a?(Node) }
|
|
630
642
|
level = current_level
|
|
643
|
+
level.from_depth += 1
|
|
644
|
+
@stack << -> { level.from_depth -= 1 }
|
|
631
645
|
case item.name
|
|
632
646
|
when :relation_expr
|
|
633
647
|
name, list = alias_of(kids[1])
|
|
@@ -855,8 +869,9 @@ module Pgsqlarbiter
|
|
|
855
869
|
# * "(expr).name" and anything after a subscript always counts as a
|
|
856
870
|
# possible call of name.
|
|
857
871
|
# * "alias.name" is a column only if name is certainly a column of
|
|
858
|
-
# everything alias could refer to
|
|
859
|
-
#
|
|
872
|
+
# everything alias could refer to: what candidates finds, from this
|
|
873
|
+
# query level out to the nearest one PostgreSQL would certainly bind it
|
|
874
|
+
# to. Otherwise it counts as a call of name on the whole row.
|
|
860
875
|
# * A bare name is never a call, but it must be found: as a column of
|
|
861
876
|
# something in FROM at this level or an enclosing one, or as the name of
|
|
862
877
|
# a FROM item (a whole-row reference). A name that cannot be found is
|
|
@@ -889,15 +904,18 @@ module Pgsqlarbiter
|
|
|
889
904
|
end
|
|
890
905
|
end
|
|
891
906
|
end
|
|
892
|
-
@refs << [current_level, names, star, @alias_refs.key?(node)]
|
|
907
|
+
@refs << [current_level, names, star, @alias_refs.key?(node), in_from?]
|
|
893
908
|
end
|
|
894
909
|
|
|
895
|
-
def resolve_ref(level, names, star, alias_ok)
|
|
910
|
+
def resolve_ref(level, names, star, alias_ok, in_from)
|
|
896
911
|
return resolve_bare_name(level, names[0], alias_ok) if names.length == 1 && !star
|
|
897
912
|
|
|
898
913
|
# [catalog.][schema.]qualifier.column, or [catalog.][schema.]qualifier.*
|
|
899
914
|
qualifier = star ? names[-1] : names[-2]
|
|
900
|
-
|
|
915
|
+
# A schema-qualified qualifier matches only some of the FROM items of
|
|
916
|
+
# its name, so it is never taken to stop at the nearest.
|
|
917
|
+
nearest = names.length == (star ? 1 : 2)
|
|
918
|
+
found = candidates(level, qualifier, nearest, in_from)
|
|
901
919
|
if found.empty?
|
|
902
920
|
raise UnresolvedNameError, "#{names.join(".").inspect} refers to #{qualifier.inspect}, which is not in FROM"
|
|
903
921
|
end
|
|
@@ -949,10 +967,17 @@ module Pgsqlarbiter
|
|
|
949
967
|
raise UnresolvedNameError, "column #{name.inspect} cannot be resolved" unless found
|
|
950
968
|
end
|
|
951
969
|
|
|
952
|
-
# Everything the qualifier could refer to,
|
|
953
|
-
#
|
|
954
|
-
#
|
|
955
|
-
|
|
970
|
+
# Everything the qualifier could refer to, from this level outward.
|
|
971
|
+
# PostgreSQL binds a qualifier to the nearest level with a FROM item of
|
|
972
|
+
# that name and never looks further out, so the walk stops at a level with
|
|
973
|
+
# an entry that is certainly named qualifier and certainly visible from
|
|
974
|
+
# the reference. Entries of uncertain name at that level or nearer stay
|
|
975
|
+
# candidates. The walk goes on past a level whose FROM items the
|
|
976
|
+
# reference may not see (it is inside that level's FROM clause, where
|
|
977
|
+
# PostgreSQL hides later and non-LATERAL items), and past entries that
|
|
978
|
+
# are hidden by a join alias or are OLD, NEW or EXCLUDED, which are not
|
|
979
|
+
# visible everywhere in their level.
|
|
980
|
+
def candidates(level, qualifier, nearest, in_from)
|
|
956
981
|
found = []
|
|
957
982
|
while level
|
|
958
983
|
index = index_of(level)
|
|
@@ -960,6 +985,9 @@ module Pgsqlarbiter
|
|
|
960
985
|
found.concat(entries) if entries
|
|
961
986
|
found.concat(index.wildcards)
|
|
962
987
|
spend(1 + found.length)
|
|
988
|
+
return found if nearest && !in_from && entries&.any? { |entry| !entry.hidden && !entry.pseudo }
|
|
989
|
+
|
|
990
|
+
in_from = level.in_parent_from
|
|
963
991
|
level = level.parent
|
|
964
992
|
end
|
|
965
993
|
found
|
data/lib/pgsqlarbiter/version.rb
CHANGED