lattice-grid 0.1.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.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +179 -0
  4. data/lib/lattice/grid/aggregate_sql.rb +98 -0
  5. data/lib/lattice/grid/cells.rb +85 -0
  6. data/lib/lattice/grid/collab/axis.rb +40 -0
  7. data/lib/lattice/grid/collab/channel.rb +31 -0
  8. data/lib/lattice/grid/collab/channel_handler.rb +84 -0
  9. data/lib/lattice/grid/collab/connection.rb +72 -0
  10. data/lib/lattice/grid/collab/doc_model.rb +99 -0
  11. data/lib/lattice/grid/collab/doc_model_apply.rb +105 -0
  12. data/lib/lattice/grid/collab/doc_model_doc.rb +95 -0
  13. data/lib/lattice/grid/collab/doc_model_normalize.rb +120 -0
  14. data/lib/lattice/grid/collab/doc_server.rb +84 -0
  15. data/lib/lattice/grid/collab/doc_server_messages.rb +129 -0
  16. data/lib/lattice/grid/collab/iron_calc.rb +84 -0
  17. data/lib/lattice/grid/collab/iron_calc_ops.rb +118 -0
  18. data/lib/lattice/grid/collab/iron_calc_sheets.rb +69 -0
  19. data/lib/lattice/grid/collab/memory_store.rb +63 -0
  20. data/lib/lattice/grid/collab/refs.rb +131 -0
  21. data/lib/lattice/grid/collab/refs_shrink.rb +56 -0
  22. data/lib/lattice/grid/collab/wasm_book.rb +97 -0
  23. data/lib/lattice/grid/collab/wire.rb +135 -0
  24. data/lib/lattice/grid/collab/wire_model_ops.rb +33 -0
  25. data/lib/lattice/grid/collab/wire_ops.rb +120 -0
  26. data/lib/lattice/grid/collab/wire_style.rb +60 -0
  27. data/lib/lattice/grid/collab.rb +44 -0
  28. data/lib/lattice/grid/context.rb +82 -0
  29. data/lib/lattice/grid/dates.rb +76 -0
  30. data/lib/lattice/grid/filter_sql.rb +87 -0
  31. data/lib/lattice/grid/json.rb +60 -0
  32. data/lib/lattice/grid/keyset.rb +49 -0
  33. data/lib/lattice/grid/kind.rb +58 -0
  34. data/lib/lattice/grid/levels.rb +121 -0
  35. data/lib/lattice/grid/limits.rb +51 -0
  36. data/lib/lattice/grid/pager.rb +118 -0
  37. data/lib/lattice/grid/parser/filter_rules.rb +119 -0
  38. data/lib/lattice/grid/parser/group_rules.rb +105 -0
  39. data/lib/lattice/grid/parser/request_rules.rb +131 -0
  40. data/lib/lattice/grid/parser/tree_rules.rb +68 -0
  41. data/lib/lattice/grid/parser.rb +126 -0
  42. data/lib/lattice/grid/pivot_sql.rb +78 -0
  43. data/lib/lattice/grid/query.rb +76 -0
  44. data/lib/lattice/grid/request.rb +19 -0
  45. data/lib/lattice/grid/respond.rb +60 -0
  46. data/lib/lattice/grid/sql.rb +103 -0
  47. data/lib/lattice/grid/tree_sql.rb +59 -0
  48. data/lib/lattice/grid/values.rb +124 -0
  49. data/lib/lattice/grid/version.rb +7 -0
  50. data/lib/lattice/grid.rb +29 -0
  51. data/wasm/ironcalc_wasi.wasm +0 -0
  52. metadata +110 -0
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lattice
4
+ module Grid
5
+ # Tree data as Arel nodes: the scope of one level and the child count of each row. The semantics are
6
+ # in `treeLevel` in packages/protocol/src/evaluate.ts. `Column.list` permits only one parent or path column.
7
+ class TreeSql
8
+ CHILD = "lattice_child"
9
+
10
+ def initialize(context)
11
+ @context = context
12
+ @sql = context.sql
13
+ @keys = context.request.tree.keys
14
+ @key = context.key
15
+ @parent = context.columns.find(&:parent)&.name
16
+ @path = context.columns.find(&:path)&.name unless @parent
17
+ end
18
+
19
+ # The rows of the level.
20
+ def level
21
+ return @sql.equal(@path, @keys.join(TREE_PATH_SEPARATOR)) if @path
22
+
23
+ Arel::Nodes::And.new([@sql.equal(@parent, @keys.last), *chain])
24
+ end
25
+
26
+ # The number of direct children of the outer row that match the filter and the quick search, in
27
+ # the caller scope. The subquery reads the table as CHILD. The `IN` list reads the table by its
28
+ # own name, so the filter SQL binds to that inner table and the correlation binds to the outer row.
29
+ def count
30
+ child = @sql.table.alias(CHILD)
31
+ query = Arel::SelectManager.new(child).project(Arel.star.count).where(correlation(child))
32
+ Arel::Nodes::Grouping.new(query.where(child[@key].in(matched)).ast)
33
+ end
34
+
35
+ private
36
+
37
+ def matched
38
+ @context.matched.select(@sql.table[@key]).arel
39
+ end
40
+
41
+ # Each key is a row of the caller scope whose parent is the key before it. The filter does not
42
+ # apply to the ancestors. The subqueries have no correlation, so the table name binds to them.
43
+ def chain
44
+ @keys.each_with_index.map do |k, i|
45
+ parent = i.zero? ? nil : @keys[i - 1]
46
+ @context.caller_scope.where(@sql.equal(@key, k)).where(@sql.equal(@parent, parent)).arel.exists
47
+ end
48
+ end
49
+
50
+ # Arel writes Concat as `||`, and as CONCAT() on MySQL.
51
+ def correlation(child)
52
+ return child[@parent].eq(@sql.table[@key]) if @parent
53
+
54
+ prefix = @keys.map { |k| "#{k}#{TREE_PATH_SEPARATOR}" }.join
55
+ child[@path].eq(Arel::Nodes::Concat.new(@sql.bind(prefix, Sql::STRING), @sql.text(@sql.table[@key])))
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lattice
4
+ module Grid
5
+ # One error in a request. `code` is a protocol IssueCode.
6
+ Issue = Data.define(:path, :code, :message)
7
+
8
+ # A request that the query rejects after the parser: a bad cursor, or a pivot that is too wide.
9
+ class Failure < StandardError
10
+ attr_reader :issue
11
+
12
+ def initialize(issue)
13
+ @issue = issue
14
+ super(issue.message)
15
+ end
16
+ end
17
+
18
+ # The output of Request.parse: a value, or a list of errors.
19
+ Result = Data.define(:value, :errors) do
20
+ def ok?
21
+ errors.empty?
22
+ end
23
+ end
24
+
25
+ # Gives a protocol value back as plain JSON data. It leaves out absent optional keys.
26
+ module Wire
27
+ def to_h
28
+ self.class.members.each_with_object({}) do |member, out|
29
+ value = public_send(member)
30
+ out[member.to_s] = Wire.plain(value) unless value.nil? && member != :filter
31
+ end
32
+ end
33
+
34
+ def self.plain(value)
35
+ case value
36
+ when Wire then value.to_h
37
+ when Array then value.map { |v| plain(v) }
38
+ else value
39
+ end
40
+ end
41
+ end
42
+
43
+ Sort = Data.define(:column, :dir, :nulls) { include Wire }
44
+ OffsetRange = Data.define(:start, :end) { include Wire }
45
+ CursorRange = Data.define(:after, :before, :limit) { include Wire }
46
+ Group = Data.define(:by, :keys) { include Wire }
47
+ Pivot = Data.define(:by, :values) { include Wire }
48
+ Tree = Data.define(:keys) { include Wire }
49
+ # `and` and `or` nodes.
50
+ Logic = Data.define(:op, :nodes) { include Wire }
51
+ Not = Data.define(:op, :node) { include Wire }
52
+ Leaf = Data.define(:column, :op, :value) { include Wire }
53
+
54
+ Aggregate = Data.define(:column, :fn, :as) do
55
+ include Wire
56
+
57
+ # The output key: `as`, else `fn(column)`.
58
+ def key
59
+ as || "#{fn}(#{column})"
60
+ end
61
+ end
62
+
63
+ # One entry of the server allow-list. The allow-list is the trust boundary.
64
+ Column = Data.define(:name, :type, :key, :filter, :sort, :group, :aggregate, :search, :parent, :path) do
65
+ # Makes the allow-list from hashes such as `{ name: "id", type: :number, key: true, sort: true }`.
66
+ # `key:` names the key column. Bad configuration raises ArgumentError.
67
+ def self.list(columns, key: nil)
68
+ list = columns.map { |c| from(c.to_h.transform_keys(&:to_s), key) }
69
+ keys = list.count(&:key)
70
+ raise ArgumentError, "at most one key column" if keys > 1
71
+ raise ArgumentError, "unknown key column #{key}" if key && keys.zero?
72
+
73
+ check_tree(list)
74
+ list.freeze
75
+ end
76
+
77
+ # The same rule as `createRowsHandler` in Node: one parent column of the key type, or one string
78
+ # path column.
79
+ def self.check_tree(list)
80
+ tree = list.select(&:tree?)
81
+ key_type = list.find(&:key)&.type
82
+ return if tree.size <= 1 && tree.all? { |c| c.tree_type?(key_type) }
83
+
84
+ raise ArgumentError, "tree data needs one parent column of the key type, or one string path column"
85
+ end
86
+
87
+ def tree?
88
+ parent || path
89
+ end
90
+
91
+ # True when a parent column has the key type, and a path column is a string.
92
+ def tree_type?(key_type)
93
+ return type == key_type if parent
94
+
95
+ !path || type == "string"
96
+ end
97
+
98
+ def self.from(hash, key)
99
+ name = name(hash)
100
+ new(name: name, type: pick(hash.fetch("type"), COLUMN_TYPES, "type"),
101
+ key: hash["key"] == true || name == key.to_s,
102
+ filter: picks(hash["filter"], FILTER_OPS, "filter op"),
103
+ aggregate: picks(hash["aggregate"], AGGREGATE_FNS, "aggregate fn"),
104
+ **%w[sort group search parent path].to_h { |flag| [flag.to_sym, hash[flag] == true] })
105
+ end
106
+
107
+ def self.name(hash)
108
+ name = hash.fetch("name").to_s
109
+ raise ArgumentError, "bad column name #{name.inspect}" unless IDENTIFIER.match?(name)
110
+ raise ArgumentError, "reserved column name #{name.inspect}" if RESERVED_NAMES.include?(name)
111
+
112
+ name.freeze
113
+ end
114
+
115
+ def self.pick(value, allowed, what)
116
+ allowed.find { |a| a == value.to_s } || raise(ArgumentError, "unknown #{what} #{value.inspect}")
117
+ end
118
+
119
+ def self.picks(values, allowed, what)
120
+ Array(values).map { |v| pick(v, allowed, what) }.freeze
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lattice
4
+ module Grid
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require_relative "grid/version"
5
+ require_relative "grid/limits"
6
+ require_relative "grid/kind"
7
+ require_relative "grid/dates"
8
+ require_relative "grid/json"
9
+ require_relative "grid/values"
10
+ require_relative "grid/parser"
11
+ require_relative "grid/request"
12
+ require_relative "grid/sql"
13
+ require_relative "grid/filter_sql"
14
+ require_relative "grid/keyset"
15
+ require_relative "grid/cells"
16
+ require_relative "grid/context"
17
+ require_relative "grid/tree_sql"
18
+ require_relative "grid/pivot_sql"
19
+ require_relative "grid/aggregate_sql"
20
+ require_relative "grid/pager"
21
+ require_relative "grid/levels"
22
+ require_relative "grid/query"
23
+ require_relative "grid/respond"
24
+
25
+ module Lattice
26
+ # The Lattice grid wire protocol, version 1, for ActiveRecord.
27
+ module Grid
28
+ end
29
+ end
Binary file
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lattice-grid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Gaudet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.1'
27
+ description:
28
+ email:
29
+ - joe@joegaudet.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/lattice/grid.rb
37
+ - lib/lattice/grid/aggregate_sql.rb
38
+ - lib/lattice/grid/cells.rb
39
+ - lib/lattice/grid/collab.rb
40
+ - lib/lattice/grid/collab/axis.rb
41
+ - lib/lattice/grid/collab/channel.rb
42
+ - lib/lattice/grid/collab/channel_handler.rb
43
+ - lib/lattice/grid/collab/connection.rb
44
+ - lib/lattice/grid/collab/doc_model.rb
45
+ - lib/lattice/grid/collab/doc_model_apply.rb
46
+ - lib/lattice/grid/collab/doc_model_doc.rb
47
+ - lib/lattice/grid/collab/doc_model_normalize.rb
48
+ - lib/lattice/grid/collab/doc_server.rb
49
+ - lib/lattice/grid/collab/doc_server_messages.rb
50
+ - lib/lattice/grid/collab/iron_calc.rb
51
+ - lib/lattice/grid/collab/iron_calc_ops.rb
52
+ - lib/lattice/grid/collab/iron_calc_sheets.rb
53
+ - lib/lattice/grid/collab/memory_store.rb
54
+ - lib/lattice/grid/collab/refs.rb
55
+ - lib/lattice/grid/collab/refs_shrink.rb
56
+ - lib/lattice/grid/collab/wasm_book.rb
57
+ - lib/lattice/grid/collab/wire.rb
58
+ - lib/lattice/grid/collab/wire_model_ops.rb
59
+ - lib/lattice/grid/collab/wire_ops.rb
60
+ - lib/lattice/grid/collab/wire_style.rb
61
+ - lib/lattice/grid/context.rb
62
+ - lib/lattice/grid/dates.rb
63
+ - lib/lattice/grid/filter_sql.rb
64
+ - lib/lattice/grid/json.rb
65
+ - lib/lattice/grid/keyset.rb
66
+ - lib/lattice/grid/kind.rb
67
+ - lib/lattice/grid/levels.rb
68
+ - lib/lattice/grid/limits.rb
69
+ - lib/lattice/grid/pager.rb
70
+ - lib/lattice/grid/parser.rb
71
+ - lib/lattice/grid/parser/filter_rules.rb
72
+ - lib/lattice/grid/parser/group_rules.rb
73
+ - lib/lattice/grid/parser/request_rules.rb
74
+ - lib/lattice/grid/parser/tree_rules.rb
75
+ - lib/lattice/grid/pivot_sql.rb
76
+ - lib/lattice/grid/query.rb
77
+ - lib/lattice/grid/request.rb
78
+ - lib/lattice/grid/respond.rb
79
+ - lib/lattice/grid/sql.rb
80
+ - lib/lattice/grid/tree_sql.rb
81
+ - lib/lattice/grid/values.rb
82
+ - lib/lattice/grid/version.rb
83
+ - wasm/ironcalc_wasi.wasm
84
+ homepage: https://joegaudet.com/ember-lattice/
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ source_code_uri: https://github.com/joegaudet/ember-lattice/tree/main/ruby/lattice-grid
89
+ bug_tracker_uri: https://github.com/joegaudet/ember-lattice/issues
90
+ rubygems_mfa_required: 'true'
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '3.2'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.5.11
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: The Lattice grid wire protocol for Rails and ActiveRecord.
110
+ test_files: []