pgsqlarbiter 0.3.1 → 0.3.2
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 +79 -24
- 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: 4067c884e419ee948793b1299f54a66d425774cec2c978b476a3c33e454c71bb
|
|
4
|
+
data.tar.gz: eb68412e2fdbd807b5cb97189b77554648c07409c47702f60b2d8fd8a00afc57
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7d278dbaf36bf2269029db5073cb454daa6c9f7482f291d1f5f6c737cb9efa13d61e6f9aa3aaa8ce2e6ce22a5410e538ae7c131d3a5e94bd93d9a21f7ab86fcb
|
|
7
|
+
data.tar.gz: 2e73fef88dcc0a09e22faddcf0bb2e82aa5a4b915dfb28720fdeb36d4cbc7f93f31e87b890e9622829eee8a56acefe8122902135d9460301cc99f14312c296a5
|
|
@@ -54,9 +54,12 @@ module Pgsqlarbiter
|
|
|
54
54
|
# holds the things its FROM clause (and target) make visible by name.
|
|
55
55
|
#
|
|
56
56
|
# * from_depth: how many FROM items of this level the walk is inside.
|
|
57
|
-
# *
|
|
58
|
-
#
|
|
59
|
-
|
|
57
|
+
# * on_clause: while the walk is in a join's ON condition, the range of
|
|
58
|
+
# positions in entries that holds that join's members.
|
|
59
|
+
# * parent_scope: what from_scope was in the parent where this level
|
|
60
|
+
# starts: nil, or the range of the parent's FROM items that may be
|
|
61
|
+
# visible from inside this level (see visible_in?).
|
|
62
|
+
Level = Struct.new(:parent, :entries, :target, :outputs, :index, :from_depth, :on_clause, :parent_scope)
|
|
60
63
|
|
|
61
64
|
# Something a qualified name can refer to: a table, subquery, CTE, function
|
|
62
65
|
# or join in FROM, or the target of a data-modifying statement.
|
|
@@ -70,8 +73,11 @@ module Pgsqlarbiter
|
|
|
70
73
|
# * source: computes columns once the subquery it comes from has been walked.
|
|
71
74
|
# * members: the tables underneath a join alias.
|
|
72
75
|
# * pseudo: OLD, NEW and EXCLUDED.
|
|
73
|
-
# * hidden: inside a join that has an alias, so not part of "*"
|
|
74
|
-
|
|
76
|
+
# * hidden: inside a join that has an alias, so not part of "*": the
|
|
77
|
+
# position in its level's entries of the innermost such alias.
|
|
78
|
+
# * position: its place in its level's entries, known once the level is
|
|
79
|
+
# indexed.
|
|
80
|
+
Entry = Struct.new(:name, :table, :columns, :source, :members, :pseudo, :hidden, :position)
|
|
75
81
|
|
|
76
82
|
Cte = Struct.new(:core, :columns)
|
|
77
83
|
|
|
@@ -350,11 +356,30 @@ module Pgsqlarbiter
|
|
|
350
356
|
end
|
|
351
357
|
|
|
352
358
|
def new_level(outputs = nil)
|
|
353
|
-
Level.new(@level, [], nil, outputs, nil, 0,
|
|
359
|
+
Level.new(@level, [], nil, outputs, nil, 0, nil, from_scope)
|
|
354
360
|
end
|
|
355
361
|
|
|
356
|
-
|
|
357
|
-
|
|
362
|
+
NOTHING_VISIBLE = (0...0).freeze
|
|
363
|
+
|
|
364
|
+
# Which FROM items of the current level are certainly visible at this point
|
|
365
|
+
# of the walk, as a range of positions in the level's entries. nil outside
|
|
366
|
+
# the level's FROM clause, where all of them are. Inside it PostgreSQL
|
|
367
|
+
# hides items written later and, outside LATERAL, items written earlier,
|
|
368
|
+
# so none are, except in a join's ON condition, which always sees both
|
|
369
|
+
# sides of its join: there, the members of the join.
|
|
370
|
+
def from_scope
|
|
371
|
+
level = @level
|
|
372
|
+
return nil unless level && level.from_depth > 0
|
|
373
|
+
|
|
374
|
+
level.on_clause || NOTHING_VISIBLE
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# Whether an entry is a member of the join whose ON condition scope stands
|
|
378
|
+
# for, and no join alias inside that join hides it. An alias written
|
|
379
|
+
# around the join comes after its members, and hides them only outside it.
|
|
380
|
+
def visible_in?(scope, entry)
|
|
381
|
+
hidden = entry.hidden
|
|
382
|
+
scope.cover?(entry.position) && !(hidden && hidden < scope.end) && !entry.pseudo
|
|
358
383
|
end
|
|
359
384
|
|
|
360
385
|
def current_level
|
|
@@ -757,7 +782,7 @@ module Pgsqlarbiter
|
|
|
757
782
|
spend(members.length)
|
|
758
783
|
visible = members.reject(&:hidden)
|
|
759
784
|
tables = members.select(&:table)
|
|
760
|
-
members.each { |member| member.hidden
|
|
785
|
+
members.each { |member| member.hidden ||= level.entries.length }
|
|
761
786
|
entry = Entry.new(name, nil, list, nil, tables)
|
|
762
787
|
if list
|
|
763
788
|
tables.each { |member| charge_whole(member) }
|
|
@@ -767,12 +792,14 @@ module Pgsqlarbiter
|
|
|
767
792
|
level.entries << entry
|
|
768
793
|
end
|
|
769
794
|
|
|
770
|
-
# NATURAL and USING read columns without naming their tables.
|
|
795
|
+
# NATURAL and USING read columns without naming their tables. ON sees the
|
|
796
|
+
# members of its join.
|
|
771
797
|
def visit_joined_table(node)
|
|
772
798
|
kids = node.children
|
|
773
799
|
natural = kids.any? { |c| c.is_a?(Token) && c.type == :NATURAL }
|
|
774
800
|
qual = kids.last
|
|
775
801
|
using = qual.is_a?(Node) && qual.name == :join_qual && qual.children[0].type == :USING
|
|
802
|
+
on = qual.is_a?(Node) && qual.name == :join_qual && qual.children[0].type == :ON
|
|
776
803
|
if natural || using
|
|
777
804
|
level = current_level
|
|
778
805
|
start = level.entries.length
|
|
@@ -792,7 +819,20 @@ module Pgsqlarbiter
|
|
|
792
819
|
end
|
|
793
820
|
end
|
|
794
821
|
end
|
|
795
|
-
|
|
822
|
+
if on
|
|
823
|
+
level = current_level
|
|
824
|
+
start = level.entries.length
|
|
825
|
+
outer = nil
|
|
826
|
+
@stack << -> { level.on_clause = outer }
|
|
827
|
+
@stack << qual
|
|
828
|
+
@stack << lambda do
|
|
829
|
+
outer = level.on_clause
|
|
830
|
+
level.on_clause = (start...level.entries.length)
|
|
831
|
+
end
|
|
832
|
+
visit(kids[0...-1])
|
|
833
|
+
else
|
|
834
|
+
visit_children(node)
|
|
835
|
+
end
|
|
796
836
|
end
|
|
797
837
|
|
|
798
838
|
# --- Columns of the target table ---
|
|
@@ -904,10 +944,10 @@ module Pgsqlarbiter
|
|
|
904
944
|
end
|
|
905
945
|
end
|
|
906
946
|
end
|
|
907
|
-
@refs << [current_level, names, star, @alias_refs.key?(node),
|
|
947
|
+
@refs << [current_level, names, star, @alias_refs.key?(node), from_scope]
|
|
908
948
|
end
|
|
909
949
|
|
|
910
|
-
def resolve_ref(level, names, star, alias_ok,
|
|
950
|
+
def resolve_ref(level, names, star, alias_ok, scope)
|
|
911
951
|
return resolve_bare_name(level, names[0], alias_ok) if names.length == 1 && !star
|
|
912
952
|
|
|
913
953
|
# [catalog.][schema.]qualifier.column, or [catalog.][schema.]qualifier.*
|
|
@@ -915,7 +955,7 @@ module Pgsqlarbiter
|
|
|
915
955
|
# A schema-qualified qualifier matches only some of the FROM items of
|
|
916
956
|
# its name, so it is never taken to stop at the nearest.
|
|
917
957
|
nearest = names.length == (star ? 1 : 2)
|
|
918
|
-
found = candidates(level, qualifier, nearest,
|
|
958
|
+
found = candidates(level, qualifier, nearest, scope)
|
|
919
959
|
if found.empty?
|
|
920
960
|
raise UnresolvedNameError, "#{names.join(".").inspect} refers to #{qualifier.inspect}, which is not in FROM"
|
|
921
961
|
end
|
|
@@ -972,22 +1012,36 @@ module Pgsqlarbiter
|
|
|
972
1012
|
# that name and never looks further out, so the walk stops at a level with
|
|
973
1013
|
# an entry that is certainly named qualifier and certainly visible from
|
|
974
1014
|
# the reference. Entries of uncertain name at that level or nearer stay
|
|
975
|
-
# candidates.
|
|
976
|
-
#
|
|
977
|
-
#
|
|
978
|
-
#
|
|
979
|
-
#
|
|
980
|
-
|
|
1015
|
+
# candidates. Inside a level's FROM clause, where PostgreSQL hides later
|
|
1016
|
+
# and non-LATERAL items, scope is the range of items the reference
|
|
1017
|
+
# certainly sees (the members of the join whose ON condition it is in).
|
|
1018
|
+
# If qualifier names one of them the walk stops there, and nothing else of
|
|
1019
|
+
# that name at the level is a candidate; otherwise it goes on past the
|
|
1020
|
+
# level. It also goes on past entries that are hidden by a join alias or
|
|
1021
|
+
# are OLD, NEW or EXCLUDED, which are not visible everywhere in their
|
|
1022
|
+
# level.
|
|
1023
|
+
def candidates(level, qualifier, nearest, scope)
|
|
981
1024
|
found = []
|
|
982
1025
|
while level
|
|
983
1026
|
index = index_of(level)
|
|
984
1027
|
entries = index.names[qualifier]
|
|
1028
|
+
spend(1 + (entries ? entries.length : 0))
|
|
1029
|
+
stop = false
|
|
1030
|
+
if nearest && entries
|
|
1031
|
+
if scope
|
|
1032
|
+
seen = entries.select { |entry| visible_in?(scope, entry) }
|
|
1033
|
+
entries = seen unless seen.empty?
|
|
1034
|
+
stop = !seen.empty?
|
|
1035
|
+
else
|
|
1036
|
+
stop = entries.any? { |entry| !entry.hidden && !entry.pseudo }
|
|
1037
|
+
end
|
|
1038
|
+
end
|
|
985
1039
|
found.concat(entries) if entries
|
|
986
1040
|
found.concat(index.wildcards)
|
|
987
|
-
spend(
|
|
988
|
-
return found if
|
|
1041
|
+
spend(found.length)
|
|
1042
|
+
return found if stop
|
|
989
1043
|
|
|
990
|
-
|
|
1044
|
+
scope = level.parent_scope
|
|
991
1045
|
level = level.parent
|
|
992
1046
|
end
|
|
993
1047
|
found
|
|
@@ -1000,7 +1054,8 @@ module Pgsqlarbiter
|
|
|
1000
1054
|
index = level.index = LevelIndex.new(false, {}, {}, [])
|
|
1001
1055
|
seen = {}
|
|
1002
1056
|
spend(level.entries.length)
|
|
1003
|
-
level.entries.
|
|
1057
|
+
level.entries.each_with_index do |entry, position|
|
|
1058
|
+
entry.position = position
|
|
1004
1059
|
name = entry.name
|
|
1005
1060
|
if name.nil?
|
|
1006
1061
|
index.wildcards << entry
|
data/lib/pgsqlarbiter/version.rb
CHANGED