dotted-notation 0.55.2__tar.gz → 0.55.4__tar.gz

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 (51) hide show
  1. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/CHANGELOG.md +45 -0
  2. {dotted_notation-0.55.2/dotted_notation.egg-info → dotted_notation-0.55.4}/PKG-INFO +128 -8
  3. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/README.md +127 -7
  4. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/access.py +12 -0
  5. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/api.py +69 -67
  6. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/base.py +35 -10
  7. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/grammar.py +7 -1
  8. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/groups.py +8 -8
  9. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/recursive.py +3 -5
  10. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/translators/core.py +265 -8
  11. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/translators/spark.py +36 -6
  12. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/translators/spark_pyspark.py +11 -3
  13. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/translators/sql.py +33 -5
  14. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/translators/sql_pg.py +15 -3
  15. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/wrappers.py +21 -2
  16. {dotted_notation-0.55.2 → dotted_notation-0.55.4/dotted_notation.egg-info}/PKG-INFO +128 -8
  17. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/pyproject.toml +1 -1
  18. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/LICENSE +0 -0
  19. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/MANIFEST.in +0 -0
  20. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/__init__.py +0 -0
  21. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/__main__.py +0 -0
  22. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/access.pxd +0 -0
  23. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/base.pxd +0 -0
  24. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/cli/__init__.py +0 -0
  25. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/cli/_compat.py +0 -0
  26. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/cli/formats.py +0 -0
  27. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/cli/main.py +0 -0
  28. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/containers.py +0 -0
  29. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/engine.pxd +0 -0
  30. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/engine.py +0 -0
  31. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/filters.pxd +0 -0
  32. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/filters.py +0 -0
  33. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/groups.pxd +0 -0
  34. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/matchers.pxd +0 -0
  35. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/matchers.py +0 -0
  36. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/native.py +0 -0
  37. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/predicates.py +0 -0
  38. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/recursive.pxd +0 -0
  39. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/results.py +0 -0
  40. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/transforms.py +0 -0
  41. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/translators/__init__.py +0 -0
  42. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/utils.py +0 -0
  43. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/utypes.py +0 -0
  44. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted/wrappers.pxd +0 -0
  45. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted_notation.egg-info/SOURCES.txt +0 -0
  46. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted_notation.egg-info/dependency_links.txt +0 -0
  47. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted_notation.egg-info/entry_points.txt +0 -0
  48. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted_notation.egg-info/requires.txt +0 -0
  49. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/dotted_notation.egg-info/top_level.txt +0 -0
  50. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/setup.cfg +0 -0
  51. {dotted_notation-0.55.2 → dotted_notation-0.55.4}/setup.py +0 -0
@@ -3,6 +3,51 @@
3
3
  All notable changes to `dotted` are recorded here. Versions prior to
4
4
  the ones listed are omitted — browse git history for earlier entries.
5
5
 
6
+ ## [0.55.4]
7
+
8
+ ### Changed
9
+ - A `match(..., groups=...)` capture is the path's segment as the path
10
+ writes it, accessor, filters, value guard and transforms included:
11
+ `match('*[*].*', 'lots[*&a=1].d', groups=True)` captures
12
+ `('lots', '[*&a=1]', 'd')`, and `[3]` captures `'[3]'`, not `3`.
13
+ A filtered pattern segment, `*&id=*`, captures once, the whole
14
+ segment, and a negation, `(!a.b)`, captures the segments it consumed
15
+ as one.
16
+ - `translate`: a `$N` that is a whole segment, `.$0` or `[$0]`, becomes
17
+ the captured segment, its accessor included:
18
+ `translate('bye[3]', {'bye[*]': 'gone.$0'})` is `'gone[3]'`.
19
+
20
+ ### Fixed
21
+ - Under partial matching, a pattern with `**` (or a group on either
22
+ side) drops no part of the path: the last capture runs to its end,
23
+ `match('**.b', 'a.b.c', groups=True)` captures `('a', 'b.c')`.
24
+ - A value-guarded path segment, `a=1` or `a.b!=1`, matches an
25
+ unguarded pattern, as a filtered one does.
26
+ - A whole-list slice, `a[]` or `a[:]`, matches a slice filter,
27
+ `a[x=1]`: the filter only narrows the list. A partial slice, `a[1:]`,
28
+ still does not.
29
+ - `a&x=1.b` parses as `(a&x=1, b)`: a dot in a number needs a digit
30
+ or an exponent after it (`1.5`, `1.e5`), so the dot starts the next
31
+ segment instead of ending the float `1.`. `a&x=1.` and `[1.]` no
32
+ longer parse.
33
+
34
+ ## [0.55.3]
35
+
36
+ ### Added
37
+ - Table search in `sqlize` and `sparkize`: `root='table'` reads a
38
+ path's leading keys as a table's name, a row step (`[*]`, `[*&…]`,
39
+ `[n]`, `[a:b]`, a slice filter `[…]`) as WHERE / offset / limit, and
40
+ the rest as a column path under each row.
41
+ `sqlize('books[*&author="kafka"].title', driver=..., root='table')`
42
+ gives `select='title'`, `where='author = $1'`, `r.table='books'`.
43
+ `r.table`, `r.offset` and `r.limit` are plain values on both
44
+ resolvers, outside the Mapping; the caller assembles the query.
45
+ - A path that starts with a row step (`[*&author="kafka"]`, `[0]`)
46
+ searches an unnamed table under either root.
47
+
48
+ ### Fixed
49
+ - A leading index, `[0]`, no longer translates as the column `"0"`.
50
+
6
51
  ## [0.55.2]
7
52
 
8
53
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dotted_notation
3
- Version: 0.55.2
3
+ Version: 0.55.4
4
4
  Summary: Dotted notation for safe nested data traversal with optional chaining, pattern matching, and transforms
5
5
  Author-email: Frey Waid <logophage1@gmail.com>
6
6
  License-Expression: MIT
@@ -619,6 +619,19 @@ With the `groups=True` parameter, you'll see how it was matched:
619
619
  In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
620
620
  matching is enabled by default).
621
621
 
622
+ A capture is the path's segment as the path writes it: its accessor, filters, value
623
+ guard and transforms come with it. A filter, guard or transform on the path only
624
+ narrows a segment, so an undecorated pattern still matches it:
625
+
626
+ >>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
627
+ ('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
628
+ >>> dotted.match('*.*', 'a.b|int', groups=True)
629
+ ('a.b|int', ('a', 'b|int'))
630
+ >>> dotted.match('*', 'a=1', groups=True)
631
+ ('a=1', ('a=1',))
632
+ >>> dotted.match('**.b', 'a.b.c', groups=True)
633
+ ('a.b.c', ('a', 'b.c'))
634
+
622
635
  Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
623
636
  excluding literals:
624
637
 
@@ -680,6 +693,18 @@ only a recursive pattern that subsumes it will match:
680
693
  '*b'
681
694
  >>> dotted.match('*', '**')
682
695
 
696
+ A slice filter reads the list narrowed to the items that pass it, so the
697
+ slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
698
+ depends on the data, so no part of the list covers it, and neither does
699
+ `[*]`, which steps into each item rather than reading the list:
700
+
701
+ >>> dotted.match('a[]', 'a[x=1]')
702
+ 'a[x=1]'
703
+ >>> dotted.match('a[:]', 'a[x=1]', groups=True)
704
+ ('a[x=1]', ('a', '[x=1]'))
705
+ >>> dotted.match('a[1:]', 'a[x=1]')
706
+ >>> dotted.match('a[*]', 'a[x=1]')
707
+
683
708
  <a id="replace"></a>
684
709
  ### Replace
685
710
 
@@ -743,6 +768,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
743
768
  >>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
744
769
  'Y.X'
745
770
 
771
+ A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
772
+ path writes it, its accessor included:
773
+
774
+ >>> translate('bye[3]', {'bye[*]': 'gone.$0'})
775
+ 'gone[3]'
776
+ >>> translate('bye.3', {'bye[*]': 'gone.$0'})
777
+ 'gone.3'
778
+ >>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
779
+ 'gone[*&a=1]'
780
+
746
781
  The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
747
782
  Returns `None` if no pattern matches:
748
783
 
@@ -3541,7 +3576,8 @@ Pick the driver you'll actually hand the SQL to; it determines the
3541
3576
  placeholder syntax and whether explicit SQL casts are emitted.
3542
3577
 
3543
3578
  The first segment of the dotted path is the SQL column; further
3544
- segments are JSON navigation inside it (JSONB). Scalar columns and
3579
+ segments are JSON navigation inside it (JSONB). (`root='table'` makes
3580
+ it a [table search](#sqlize-table-search) instead.) Scalar columns and
3545
3581
  JSONB columns share one surface:
3546
3582
 
3547
3583
  >>> import dotted
@@ -3747,6 +3783,62 @@ Not yet supported:
3747
3783
  - Regex-in-access like `data./prefix_\d+/.field`
3748
3784
  - Pattern refs
3749
3785
 
3786
+ <a id="sqlize-table-search"></a>
3787
+ ### Table search
3788
+
3789
+ By default a path's first segment is a column of the row. With
3790
+ `root='table'` the path searches a table instead: its leading keys name
3791
+ the table, a row step walks its rows, and the rest is a column path
3792
+ under each row, read as above:
3793
+
3794
+ >>> r = dotted.sqlize('books[*&author="kafka"].title', driver='asyncpg', root='table')
3795
+ >>> r.table
3796
+ 'books'
3797
+ >>> r.build('SELECT ' + r.select + f' FROM {r.table} WHERE ' + r.where)
3798
+ ('SELECT title FROM books WHERE author = $1', ['kafka'])
3799
+
3800
+ | row step | gives |
3801
+ |----------|-------|
3802
+ | `[*]` | every row |
3803
+ | `[*&<filters>]` | `where` from the filters; a filter key is a column path (`&data.user.age>30`) |
3804
+ | `[<filters>]` (slice filter) | the same `where` |
3805
+ | `[n]` | `offset=n`, `limit=1` |
3806
+ | `[a:b]`, `[a:]`, `[:b]` | `offset=a`, `limit=b-a` (either may be None) |
3807
+
3808
+ A slice filter and a slice keep the rows one list, as in dotted, so
3809
+ `[*]` comes before a column: filter, then page, then select:
3810
+
3811
+ >>> r = dotted.sqlize('books[author="kafka"][10:20][*].title', driver='asyncpg', root='table')
3812
+ >>> r.offset, r.limit
3813
+ (10, 10)
3814
+
3815
+ With nothing after the row step, `select` is `*`. Everything before
3816
+ it is the table's name, taken whole:
3817
+
3818
+ >>> dotted.sqlize('public.books[0]', driver='asyncpg', root='table')
3819
+ AsyncPGResolver(driver='asyncpg', select='*', table='public.books', offset=0, limit=1)
3820
+
3821
+ A path that starts with a row step searches an unnamed table, so it
3822
+ needs no `root=`; the caller supplies the `FROM`:
3823
+
3824
+ >>> r = dotted.sqlize('[*&author="kafka"]', driver='asyncpg')
3825
+ >>> r.table is None, r.build(r.where)
3826
+ (True, ('author = $1', ['kafka']))
3827
+
3828
+ `r.table`, `r.offset` and `r.limit` are plain values, not fragments,
3829
+ and stay out of the Mapping. The table's name comes back as the path
3830
+ spells it; quoting it is the caller's (`Col(r.table)` validates it).
3831
+
3832
+ Not yet supported (`TranslationError`):
3833
+ - Selecting several columns, `books[*](.title,.author)` or the packed
3834
+ `books[*](.title,.author;)`
3835
+ - A key straight after a slice filter or slice (`books[a=1].title` —
3836
+ dotted reads a key of the list there; use `books[a=1][*].title`)
3837
+ - A row step after `[*]`, `[*&…]` or `[n]` — dotted indexes into each
3838
+ row there
3839
+ - After a slice, anything but `[*]`; slice steps; negative indexes and
3840
+ bounds
3841
+
3750
3842
  ### Composing multiple Resolvers — ParamPool
3751
3843
 
3752
3844
  Each call to `dotted.sqlize()` allocates its own bind-parameter name
@@ -3861,6 +3953,9 @@ registered drivers (built-ins + anything third-party code has added).
3861
3953
  | `.from_` | `SQLFragment` \| `None` | FROM extension (reserved) |
3862
3954
  | `.lateral` | `SQLFragment` \| `None` | `LATERAL jsonb_path_query(...) AS _patN(value)` for pattern paths |
3863
3955
  | `.unbound` | `dict` | Union of fragments' unbound mappings: `{bind_name: original_name}` |
3956
+ | `.table` | `str` \| `None` | Table search: the table's name, as the path spells it |
3957
+ | `.offset` | `int` \| `None` | Table search: rows to skip |
3958
+ | `.limit` | `int` \| `None` | Table search: rows to keep |
3864
3959
 
3865
3960
  The Resolver also implements the `collections.abc.Mapping` protocol
3866
3961
  over its non-None fragment attributes (`select` / `where` / `from_` /
@@ -3874,8 +3969,8 @@ over its non-None fragment attributes (`select` / `where` / `from_` /
3874
3969
  >>> dict(r) # map of present fragments
3875
3970
  {'select': ..., 'where': ..., 'lateral': ...}
3876
3971
 
3877
- `driver` / `paramstyle` / `cast` / `unbound` are reached as plain
3878
- attributes, not via the Mapping.
3972
+ `driver` / `paramstyle` / `cast` / `unbound` / `table` / `offset` /
3973
+ `limit` are reached as plain attributes, not via the Mapping.
3879
3974
 
3880
3975
  A `SQLFragment` carries:
3881
3976
 
@@ -3887,14 +3982,17 @@ A `SQLFragment` carries:
3887
3982
 
3888
3983
  ### API reference
3889
3984
 
3890
- `dotted.sqlize(path, *, driver, bindings=None)`
3985
+ `dotted.sqlize(path, *, driver, bindings=None, root='column')`
3891
3986
 
3892
3987
  - `path` — dotted path string or pre-parsed result.
3893
3988
  - `driver` — **required**. Name of a registered driver
3894
3989
  (`'asyncpg'`, `'psycopg2'`, `'psycopg'`, or anything registered via
3895
3990
  `@register`). An unknown driver raises `TranslationError`.
3896
3991
  - `bindings` — resolve substitutions at sqlize time. Path-position
3897
- substitutions must be resolved here.
3992
+ substitutions must be resolved here, row indexes and slice bounds
3993
+ included.
3994
+ - `root` — `'column'` (default): the first segment is a column.
3995
+ `'table'`: a [table search](#sqlize-table-search).
3898
3996
 
3899
3997
  `r.build(sql, **bindings)`
3900
3998
 
@@ -4001,16 +4099,38 @@ A `Column` can't hold a placeholder, so `r.where` is a
4001
4099
  b = dotted.sparkize('status = "active"')
4002
4100
  df.where(a.build(a.where & b.where, min_age=30))
4003
4101
 
4102
+ <a id="sparkize-table-search"></a>
4103
+ ### Table search
4104
+
4105
+ `root='table'` reads a path as a [table search](#sqlize-table-search), the
4106
+ same as for `sqlize`: the leading keys name a DataFrame or table, and
4107
+ `r.table`, `r.offset` and `r.limit` are plain values the caller chains
4108
+ (skip `.offset` / `.limit` when they are None; `offset` needs Spark
4109
+ 3.4+):
4110
+
4111
+ r = dotted.sparkize('books[author="kafka"][10:20][*].title', root='table')
4112
+ (spark.table(r.table)
4113
+ .where(r.build(r.where))
4114
+ .offset(r.offset).limit(r.limit)
4115
+ .select(r.build(r.select)))
4116
+
4117
+ With nothing after the row step, `select` is every column
4118
+ (`col('*')`). A path that starts with a row step, `[*&author="kafka"]`,
4119
+ needs no name and no `root=`: apply its `where` to any DataFrame.
4120
+
4004
4121
  ### API reference
4005
4122
 
4006
- `dotted.sparkize(path, *, flavor, bindings=None)`
4123
+ `dotted.sparkize(path, *, flavor, bindings=None, root='column')`
4007
4124
 
4008
4125
  - `path` — dotted path string or pre-parsed result.
4009
4126
  - `flavor` — **required**. Name of a registered flavor (`'pyspark'`).
4010
4127
  An unknown flavor raises `TranslationError`.
4011
4128
  `dotted.translators.flavors()` lists them.
4012
4129
  - `bindings` — resolve substitutions at sparkize time. Path-position
4013
- substitutions must be resolved here.
4130
+ substitutions must be resolved here, row indexes and slice bounds
4131
+ included.
4132
+ - `root` — `'column'` (default): the first segment is a column.
4133
+ `'table'`: a [table search](#sparkize-table-search).
4014
4134
 
4015
4135
  `r.build(fragment, **bindings)`
4016
4136
 
@@ -569,6 +569,19 @@ With the `groups=True` parameter, you'll see how it was matched:
569
569
  In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
570
570
  matching is enabled by default).
571
571
 
572
+ A capture is the path's segment as the path writes it: its accessor, filters, value
573
+ guard and transforms come with it. A filter, guard or transform on the path only
574
+ narrows a segment, so an undecorated pattern still matches it:
575
+
576
+ >>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
577
+ ('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
578
+ >>> dotted.match('*.*', 'a.b|int', groups=True)
579
+ ('a.b|int', ('a', 'b|int'))
580
+ >>> dotted.match('*', 'a=1', groups=True)
581
+ ('a=1', ('a=1',))
582
+ >>> dotted.match('**.b', 'a.b.c', groups=True)
583
+ ('a.b.c', ('a', 'b.c'))
584
+
572
585
  Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
573
586
  excluding literals:
574
587
 
@@ -630,6 +643,18 @@ only a recursive pattern that subsumes it will match:
630
643
  '*b'
631
644
  >>> dotted.match('*', '**')
632
645
 
646
+ A slice filter reads the list narrowed to the items that pass it, so the
647
+ slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
648
+ depends on the data, so no part of the list covers it, and neither does
649
+ `[*]`, which steps into each item rather than reading the list:
650
+
651
+ >>> dotted.match('a[]', 'a[x=1]')
652
+ 'a[x=1]'
653
+ >>> dotted.match('a[:]', 'a[x=1]', groups=True)
654
+ ('a[x=1]', ('a', '[x=1]'))
655
+ >>> dotted.match('a[1:]', 'a[x=1]')
656
+ >>> dotted.match('a[*]', 'a[x=1]')
657
+
633
658
  <a id="replace"></a>
634
659
  ### Replace
635
660
 
@@ -693,6 +718,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
693
718
  >>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
694
719
  'Y.X'
695
720
 
721
+ A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
722
+ path writes it, its accessor included:
723
+
724
+ >>> translate('bye[3]', {'bye[*]': 'gone.$0'})
725
+ 'gone[3]'
726
+ >>> translate('bye.3', {'bye[*]': 'gone.$0'})
727
+ 'gone.3'
728
+ >>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
729
+ 'gone[*&a=1]'
730
+
696
731
  The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
697
732
  Returns `None` if no pattern matches:
698
733
 
@@ -3491,7 +3526,8 @@ Pick the driver you'll actually hand the SQL to; it determines the
3491
3526
  placeholder syntax and whether explicit SQL casts are emitted.
3492
3527
 
3493
3528
  The first segment of the dotted path is the SQL column; further
3494
- segments are JSON navigation inside it (JSONB). Scalar columns and
3529
+ segments are JSON navigation inside it (JSONB). (`root='table'` makes
3530
+ it a [table search](#sqlize-table-search) instead.) Scalar columns and
3495
3531
  JSONB columns share one surface:
3496
3532
 
3497
3533
  >>> import dotted
@@ -3697,6 +3733,62 @@ Not yet supported:
3697
3733
  - Regex-in-access like `data./prefix_\d+/.field`
3698
3734
  - Pattern refs
3699
3735
 
3736
+ <a id="sqlize-table-search"></a>
3737
+ ### Table search
3738
+
3739
+ By default a path's first segment is a column of the row. With
3740
+ `root='table'` the path searches a table instead: its leading keys name
3741
+ the table, a row step walks its rows, and the rest is a column path
3742
+ under each row, read as above:
3743
+
3744
+ >>> r = dotted.sqlize('books[*&author="kafka"].title', driver='asyncpg', root='table')
3745
+ >>> r.table
3746
+ 'books'
3747
+ >>> r.build('SELECT ' + r.select + f' FROM {r.table} WHERE ' + r.where)
3748
+ ('SELECT title FROM books WHERE author = $1', ['kafka'])
3749
+
3750
+ | row step | gives |
3751
+ |----------|-------|
3752
+ | `[*]` | every row |
3753
+ | `[*&<filters>]` | `where` from the filters; a filter key is a column path (`&data.user.age>30`) |
3754
+ | `[<filters>]` (slice filter) | the same `where` |
3755
+ | `[n]` | `offset=n`, `limit=1` |
3756
+ | `[a:b]`, `[a:]`, `[:b]` | `offset=a`, `limit=b-a` (either may be None) |
3757
+
3758
+ A slice filter and a slice keep the rows one list, as in dotted, so
3759
+ `[*]` comes before a column: filter, then page, then select:
3760
+
3761
+ >>> r = dotted.sqlize('books[author="kafka"][10:20][*].title', driver='asyncpg', root='table')
3762
+ >>> r.offset, r.limit
3763
+ (10, 10)
3764
+
3765
+ With nothing after the row step, `select` is `*`. Everything before
3766
+ it is the table's name, taken whole:
3767
+
3768
+ >>> dotted.sqlize('public.books[0]', driver='asyncpg', root='table')
3769
+ AsyncPGResolver(driver='asyncpg', select='*', table='public.books', offset=0, limit=1)
3770
+
3771
+ A path that starts with a row step searches an unnamed table, so it
3772
+ needs no `root=`; the caller supplies the `FROM`:
3773
+
3774
+ >>> r = dotted.sqlize('[*&author="kafka"]', driver='asyncpg')
3775
+ >>> r.table is None, r.build(r.where)
3776
+ (True, ('author = $1', ['kafka']))
3777
+
3778
+ `r.table`, `r.offset` and `r.limit` are plain values, not fragments,
3779
+ and stay out of the Mapping. The table's name comes back as the path
3780
+ spells it; quoting it is the caller's (`Col(r.table)` validates it).
3781
+
3782
+ Not yet supported (`TranslationError`):
3783
+ - Selecting several columns, `books[*](.title,.author)` or the packed
3784
+ `books[*](.title,.author;)`
3785
+ - A key straight after a slice filter or slice (`books[a=1].title` —
3786
+ dotted reads a key of the list there; use `books[a=1][*].title`)
3787
+ - A row step after `[*]`, `[*&…]` or `[n]` — dotted indexes into each
3788
+ row there
3789
+ - After a slice, anything but `[*]`; slice steps; negative indexes and
3790
+ bounds
3791
+
3700
3792
  ### Composing multiple Resolvers — ParamPool
3701
3793
 
3702
3794
  Each call to `dotted.sqlize()` allocates its own bind-parameter name
@@ -3811,6 +3903,9 @@ registered drivers (built-ins + anything third-party code has added).
3811
3903
  | `.from_` | `SQLFragment` \| `None` | FROM extension (reserved) |
3812
3904
  | `.lateral` | `SQLFragment` \| `None` | `LATERAL jsonb_path_query(...) AS _patN(value)` for pattern paths |
3813
3905
  | `.unbound` | `dict` | Union of fragments' unbound mappings: `{bind_name: original_name}` |
3906
+ | `.table` | `str` \| `None` | Table search: the table's name, as the path spells it |
3907
+ | `.offset` | `int` \| `None` | Table search: rows to skip |
3908
+ | `.limit` | `int` \| `None` | Table search: rows to keep |
3814
3909
 
3815
3910
  The Resolver also implements the `collections.abc.Mapping` protocol
3816
3911
  over its non-None fragment attributes (`select` / `where` / `from_` /
@@ -3824,8 +3919,8 @@ over its non-None fragment attributes (`select` / `where` / `from_` /
3824
3919
  >>> dict(r) # map of present fragments
3825
3920
  {'select': ..., 'where': ..., 'lateral': ...}
3826
3921
 
3827
- `driver` / `paramstyle` / `cast` / `unbound` are reached as plain
3828
- attributes, not via the Mapping.
3922
+ `driver` / `paramstyle` / `cast` / `unbound` / `table` / `offset` /
3923
+ `limit` are reached as plain attributes, not via the Mapping.
3829
3924
 
3830
3925
  A `SQLFragment` carries:
3831
3926
 
@@ -3837,14 +3932,17 @@ A `SQLFragment` carries:
3837
3932
 
3838
3933
  ### API reference
3839
3934
 
3840
- `dotted.sqlize(path, *, driver, bindings=None)`
3935
+ `dotted.sqlize(path, *, driver, bindings=None, root='column')`
3841
3936
 
3842
3937
  - `path` — dotted path string or pre-parsed result.
3843
3938
  - `driver` — **required**. Name of a registered driver
3844
3939
  (`'asyncpg'`, `'psycopg2'`, `'psycopg'`, or anything registered via
3845
3940
  `@register`). An unknown driver raises `TranslationError`.
3846
3941
  - `bindings` — resolve substitutions at sqlize time. Path-position
3847
- substitutions must be resolved here.
3942
+ substitutions must be resolved here, row indexes and slice bounds
3943
+ included.
3944
+ - `root` — `'column'` (default): the first segment is a column.
3945
+ `'table'`: a [table search](#sqlize-table-search).
3848
3946
 
3849
3947
  `r.build(sql, **bindings)`
3850
3948
 
@@ -3951,16 +4049,38 @@ A `Column` can't hold a placeholder, so `r.where` is a
3951
4049
  b = dotted.sparkize('status = "active"')
3952
4050
  df.where(a.build(a.where & b.where, min_age=30))
3953
4051
 
4052
+ <a id="sparkize-table-search"></a>
4053
+ ### Table search
4054
+
4055
+ `root='table'` reads a path as a [table search](#sqlize-table-search), the
4056
+ same as for `sqlize`: the leading keys name a DataFrame or table, and
4057
+ `r.table`, `r.offset` and `r.limit` are plain values the caller chains
4058
+ (skip `.offset` / `.limit` when they are None; `offset` needs Spark
4059
+ 3.4+):
4060
+
4061
+ r = dotted.sparkize('books[author="kafka"][10:20][*].title', root='table')
4062
+ (spark.table(r.table)
4063
+ .where(r.build(r.where))
4064
+ .offset(r.offset).limit(r.limit)
4065
+ .select(r.build(r.select)))
4066
+
4067
+ With nothing after the row step, `select` is every column
4068
+ (`col('*')`). A path that starts with a row step, `[*&author="kafka"]`,
4069
+ needs no name and no `root=`: apply its `where` to any DataFrame.
4070
+
3954
4071
  ### API reference
3955
4072
 
3956
- `dotted.sparkize(path, *, flavor, bindings=None)`
4073
+ `dotted.sparkize(path, *, flavor, bindings=None, root='column')`
3957
4074
 
3958
4075
  - `path` — dotted path string or pre-parsed result.
3959
4076
  - `flavor` — **required**. Name of a registered flavor (`'pyspark'`).
3960
4077
  An unknown flavor raises `TranslationError`.
3961
4078
  `dotted.translators.flavors()` lists them.
3962
4079
  - `bindings` — resolve substitutions at sparkize time. Path-position
3963
- substitutions must be resolved here.
4080
+ substitutions must be resolved here, row indexes and slice bounds
4081
+ included.
4082
+ - `root` — `'column'` (default): the first segment is a column.
4083
+ `'table'`: a [table search](#sparkize-table-search).
3964
4084
 
3965
4085
  `r.build(fragment, **bindings)`
3966
4086
 
@@ -1103,7 +1103,19 @@ class Slice(SimpleOp):
1103
1103
  def matchable(self, op):
1104
1104
  return isinstance(op, Slice)
1105
1105
 
1106
+ def is_whole(self):
1107
+ """
1108
+ True if this slice reads the whole list: [], [:], [0:], [::1].
1109
+ """
1110
+ start, stop, step = self.args
1111
+ return start in (None, 0) and stop is None and step in (None, 1)
1112
+
1106
1113
  def match(self, op, specials=False):
1114
+ # a slice filter reads the list narrowed to the items that pass
1115
+ # it, which ones depending on the data: only the whole list
1116
+ # covers it
1117
+ if isinstance(op, SliceFilter):
1118
+ return base.MatchResult(slice(None)) if self.is_whole() else None
1107
1119
  if not isinstance(op, Slice):
1108
1120
  return None
1109
1121
  if self.cardinality() < op.cardinality():
@@ -943,77 +943,48 @@ def match(pattern, path, groups=False, partial=True, strict=False):
943
943
  def _match_parsed(pats, path_ops, path, groups, partial):
944
944
  """
945
945
  match() on parsed arguments. `path` is what a successful match returns.
946
- """
947
- # matching is structural: a path op's transforms, a.b|int, play no part
948
- if any(isinstance(op, wrappers.TransformWrap) for op in path_ops):
949
- path_ops = results.Dotted({'ops': [op.inner if isinstance(op, wrappers.TransformWrap) else op for op in path_ops]})
946
+ Each capture is the path segments one pattern segment matched, as the
947
+ path writes them.
948
+ """
949
+ caps = _captures(pats, path_ops, partial, bool(groups))
950
+ if not groups:
951
+ return None if caps is None else path
952
+ if caps is None:
953
+ return (None, ())
950
954
  # groups can be a bool, a GroupMode member, or its string value.
951
- _patterns_only = (groups == GroupMode.patterns
952
- or groups == GroupMode.patterns.value)
955
+ if groups == GroupMode.patterns or groups == GroupMode.patterns.value:
956
+ caps = [c for c in caps if c[1]]
957
+ return (path, tuple(results.assemble(ops) for ops, _ in caps))
953
958
 
954
- def returns(r, matches, is_pat=None):
955
- if not groups:
956
- return r
957
- if _patterns_only and is_pat is not None:
958
- matches = [m for m, ip in zip(matches, is_pat) if ip]
959
- return (r, tuple(matches))
960
959
 
960
+ def _captures(pats, path_ops, partial, collect=True):
961
+ """
962
+ The captures of the path that pats match, as base.match_ops gives
963
+ them: (ops, is_pattern) pairs, each ops the path ops one pattern
964
+ segment matched. None when pats don't match. Without collect, a
965
+ match may give no captures.
966
+ """
961
967
  # Variadic ops (recursive, groups) consume variable-length path
962
968
  # segments — use the recursive matcher. On the path side they make
963
969
  # the path denote multiple expansions, which likewise needs it.
964
970
  if pats.variadic or path_ops.variadic:
965
- result = base.match_ops(list(pats), list(path_ops), partial)
966
- if result is None:
967
- return returns(None, [])
968
- return returns(path, [v for v, _ in result], [p for _, p in result])
969
-
970
- # Original non-recursive match logic
971
- collect = bool(groups)
972
- _matches = []
973
- _is_pat = []
974
- for idx,(pop,kop) in enumerate(zip(pats, path_ops)):
975
- # this means we have more pattern constraints than path segments
976
- if kop is None:
977
- return returns(None, [], [])
978
- if pop is None:
979
- break
980
- m = pop.match(kop, specials=True)
981
- if not m:
982
- return returns(None, [], [])
983
- if not collect:
984
- # captures are only returned with groups
985
- continue
986
- is_pat = pop.is_pattern()
987
- if isinstance(m, (tuple, list)):
988
- for _m in m:
989
- _matches.append(_m.val)
990
- _is_pat.append(is_pat)
991
- else:
992
- _matches.append(m.val)
993
- _is_pat.append(is_pat)
994
-
995
- # we've completed matching but the last item in match groups is treated 'greedily'
996
- assert kop is not None # sanity
997
-
998
- # exact match
999
- if len(pats) == len(path_ops):
1000
- return returns(path, _matches, _is_pat)
1001
-
1002
- # if we're not doing partial matches or we haven't consumed all pats, fail
1003
- if not partial or idx < len(pats) - 1:
1004
- return returns(None, [], [])
1005
-
1006
- # otherwise inexact (partial) match
1007
- if not collect:
1008
- return path
1009
- # assemble remaining segments
1010
- rpath = path_ops.assemble(start=idx)
1011
- if pop is None:
1012
- _matches.append(rpath)
1013
- _is_pat.append(False)
1014
- else:
1015
- _matches[-1] = rpath
1016
- return returns(path, _matches, _is_pat)
971
+ return base.match_ops(list(pats), list(path_ops), partial)
972
+
973
+ # one path segment per pattern segment; a partial match's last
974
+ # capture runs to the end of the path
975
+ if len(pats) > len(path_ops):
976
+ return None
977
+ if len(pats) < len(path_ops) and not partial:
978
+ return None
979
+ caps = []
980
+ for pop, kop in zip(pats, path_ops):
981
+ if not pop.match(kop.match_target(), specials=True):
982
+ return None
983
+ if collect:
984
+ caps.append(((kop,), pop.is_pattern()))
985
+ if collect and len(pats) < len(path_ops):
986
+ caps[-1] = (tuple(path_ops.ops[len(pats) - 1:]), caps[-1][1])
987
+ return caps
1017
988
 
1018
989
 
1019
990
  def match_multi(pattern, iterable, groups=False, partial=True, strict=False):
@@ -1068,17 +1039,48 @@ def translate(path, pattern_map):
1068
1039
  pats = parse(pattern)
1069
1040
  if path_ops is None:
1070
1041
  path_ops = parse(path)
1071
- (r, groups) = _match_parsed(pats, path_ops, path, GroupMode.patterns, False)
1072
- if not r:
1042
+ caps = _captures(pats, path_ops, False)
1043
+ if caps is None:
1073
1044
  continue
1074
1045
  try:
1075
- return replace(template, groups)
1046
+ return _fill(template, [ops for ops, is_pat in caps if is_pat])
1076
1047
  except IndexError:
1077
1048
  continue
1078
1049
 
1079
1050
  return None
1080
1051
 
1081
1052
 
1053
+ def _fill(template, captures):
1054
+ """
1055
+ `template` with each $N filled from captures[N], the path ops of one
1056
+ capture. A $N that is a whole segment, gone.$0 or gone[$0], becomes
1057
+ the captured segment as the path writes it, its accessor included:
1058
+ gone.$0 on a capture of [3] is gone[3]. A $N anywhere else takes the
1059
+ capture's text, as replace() does.
1060
+ """
1061
+ texts = [results.assemble(ops) for ops in captures]
1062
+ ops = []
1063
+ for op in parse(template):
1064
+ n = _segment_subst(op)
1065
+ if n is None:
1066
+ ops.append(op.resolve(texts, False))
1067
+ continue
1068
+ ops.extend(captures[n])
1069
+ return results.assemble(ops)
1070
+
1071
+
1072
+ def _segment_subst(op):
1073
+ """
1074
+ N when template op `op` is a whole $N segment, .$0 or [$0], else None.
1075
+ """
1076
+ if type(op) is not access.Key and type(op) is not access.Slot:
1077
+ return None
1078
+ sub = op.op
1079
+ if type(sub) is not matchers.Subst or sub.transforms or not isinstance(sub.value, int):
1080
+ return None
1081
+ return sub.value
1082
+
1083
+
1082
1084
  def translate_multi(paths, pattern_map):
1083
1085
  """
1084
1086
  Translate multiple paths via pattern_map.