sqlspec 0.72.0__py3-none-any.whl → 0.72.1__py3-none-any.whl

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.
sqlspec/builder/_base.py CHANGED
@@ -1662,30 +1662,38 @@ def _pushdown_predicates_without_simplify(expression: exp.Expr, dialect: Dialect
1662
1662
  where_node = select.args.get("where")
1663
1663
  joins: list[exp.Join] = select.args.get("joins") or []
1664
1664
 
1665
- if any(join.args.get("nearest") for join in joins):
1665
+ if any(
1666
+ join.method in ("ASOF", "POSITIONAL") or join.kind in ("ASOF", "ANY", "PASTE") or join.args.get("nearest")
1667
+ for join in scope.find_all(exp.Join)
1668
+ ):
1666
1669
  continue
1667
1670
 
1668
1671
  if isinstance(where_node, exp.Where) and where_node.this is not None:
1669
1672
  selected_sources: dict[str, tuple[exp.Selectable, exp.Table | Scope]] = dict(scope.selected_sources)
1670
- join_index = {join.alias_or_name: i for i, join in enumerate(joins)}
1673
+ source_positions: dict[str, int] = {}
1671
1674
  last_null_extending = max((i for i, join in enumerate(joins) if join.side in ("RIGHT", "FULL")), default=-1)
1672
1675
  pushdown_allowed = True
1673
1676
  reachable: dict[str, tuple[exp.Selectable, exp.Table | Scope]] = {}
1674
1677
  for name, (node, source) in selected_sources.items():
1675
1678
  parent = node.find_ancestor(exp.Join, exp.From)
1679
+ if parent is None or parent.parent is not select:
1680
+ continue
1676
1681
  if isinstance(parent, exp.Join):
1677
1682
  if isinstance(node, exp.Unnest) and unnest_requires_cross_join:
1678
1683
  pushdown_allowed = False
1679
1684
  break
1680
- position = join_index.get(parent.alias_or_name, -1)
1685
+ position = parent.index
1686
+ if position is None:
1687
+ continue
1681
1688
  else:
1682
1689
  position = -1
1690
+ source_positions[name] = position
1683
1691
  if position >= last_null_extending:
1684
1692
  reachable[name] = (node, source)
1685
1693
 
1686
1694
  if pushdown_allowed:
1687
1695
  preserved_true_ids = {id(node) for node in where_node.this.find_all(exp.Boolean) if node.this is True}
1688
- _pushdown_condition_without_simplify(where_node.this, reachable, scope_ref_count, join_index=join_index)
1696
+ _pushdown_condition_without_simplify(where_node.this, reachable, scope_ref_count, source_positions)
1689
1697
  updated_where = where_node.args.get("this")
1690
1698
  if isinstance(updated_where, exp.Expr):
1691
1699
  pruned_where = _prune_pushed_true(updated_where, preserved_true_ids)
@@ -1717,17 +1725,18 @@ def _pushdown_condition_without_simplify(
1717
1725
  condition: exp.Expr,
1718
1726
  sources: "Mapping[str, tuple[exp.Selectable, exp.Table | Scope]]",
1719
1727
  scope_ref_count: "Mapping[int, int]",
1720
- join_index: "Mapping[str, int] | None" = None,
1728
+ source_positions: "Mapping[str, int] | None" = None,
1721
1729
  ) -> None:
1730
+ """Push down a predicate using the position map accepted by SQLGlot's CNF and DNF rules."""
1722
1731
  unwrapped = condition
1723
1732
  while isinstance(unwrapped, exp.Paren):
1724
1733
  unwrapped = unwrapped.this
1725
1734
  cnf_like = _is_normalized_condition(unwrapped) or not _is_normalized_condition(unwrapped, dnf=True)
1726
1735
  predicates = list(unwrapped.flatten() if isinstance(unwrapped, exp.And if cnf_like else exp.Or) else [unwrapped])
1727
1736
  if cnf_like:
1728
- _pushdown_cnf(predicates, sources, scope_ref_count, join_index=join_index)
1737
+ _pushdown_cnf(predicates, sources, scope_ref_count, source_positions)
1729
1738
  else:
1730
- _pushdown_dnf(predicates, sources, scope_ref_count, join_index=join_index)
1739
+ _pushdown_dnf(predicates, sources, scope_ref_count, source_positions)
1731
1740
 
1732
1741
 
1733
1742
  def _prune_pushed_true(condition: exp.Expr, preserved_true_ids: set[int]) -> exp.Expr | None:
sqlspec/core/hashing.py CHANGED
@@ -281,8 +281,8 @@ def _expression_digest(expr: "exp.Expr") -> str:
281
281
 
282
282
  The walk is iterative, so nesting depth is bounded by memory, not by the interpreter's recursion
283
283
  limit. Every node contributes its type, comments, NULL-ordering markers, and each set argument in
284
- name order; leaf values are length-prefixed so adjacent values cannot run together. A node reached
285
- a second time contributes a reference to its first visit, which also ends cyclic trees.
284
+ name order. Argument counts preserve node boundaries, and leaf lengths delimit adjacent values.
285
+ Repeat visits contribute a reference to the first visit, which also ends cyclic trees.
286
286
  """
287
287
  hasher = hashlib.blake2b(digest_size=16)
288
288
  update = hasher.update
@@ -307,6 +307,7 @@ def _expression_digest(expr: "exp.Expr") -> str:
307
307
  update(b"d")
308
308
  if has_explicit_nulls(node):
309
309
  update(b"x")
310
+ pending = len(stack)
310
311
  comments = node.comments
311
312
  if comments:
312
313
  stack.append(comments)
@@ -318,6 +319,7 @@ def _expression_digest(expr: "exp.Expr") -> str:
318
319
  continue
319
320
  stack.append(value)
320
321
  stack.append(_ArgKey(name))
322
+ update(b"A%d;" % ((len(stack) - pending) // 2))
321
323
  elif type(node) is _ArgKey:
322
324
  update(b"K")
323
325
  update(node.name.encode())
sqlspec/utils/schema.py CHANGED
@@ -79,16 +79,18 @@ def transform_dict_keys(data: dict | list | Any, converter: Callable[[str], str]
79
79
 
80
80
 
81
81
  @overload
82
- def to_schema(data: "list[DataT]", *, schema_type: "type[SchemaT]") -> "list[SchemaT]": ...
82
+ def to_schema(
83
+ data: "list[DataT]", *, schema_type: "type[SchemaT]", normalize_nested: bool = False
84
+ ) -> "list[SchemaT]": ...
83
85
  @overload
84
- def to_schema(data: "list[DataT]", *, schema_type: None = None) -> "list[DataT]": ...
86
+ def to_schema(data: "list[DataT]", *, schema_type: None = None, normalize_nested: bool = False) -> "list[DataT]": ...
85
87
  @overload
86
- def to_schema(data: "DataT", *, schema_type: "type[SchemaT]") -> "SchemaT": ...
88
+ def to_schema(data: "DataT", *, schema_type: "type[SchemaT]", normalize_nested: bool = False) -> "SchemaT": ...
87
89
  @overload
88
- def to_schema(data: "DataT", *, schema_type: None = None) -> "DataT": ...
90
+ def to_schema(data: "DataT", *, schema_type: None = None, normalize_nested: bool = False) -> "DataT": ...
89
91
 
90
92
 
91
- def to_schema(data: Any, *, schema_type: Any = None) -> Any:
93
+ def to_schema(data: Any, *, schema_type: Any = None, normalize_nested: bool = False) -> Any:
92
94
  """Convert data to a specified schema type.
93
95
 
94
96
  Supports transformation to various schema types including:
@@ -101,6 +103,8 @@ def to_schema(data: Any, *, schema_type: Any = None) -> Any:
101
103
  Args:
102
104
  data: Input data to convert (dict, list of dicts, or other)
103
105
  schema_type: Target schema type for conversion. If None, returns data unchanged.
106
+ normalize_nested: Accept Python field names inside nested msgspec Structs in trusted input.
107
+ Defaults to False. Only applies to msgspec Struct targets.
104
108
 
105
109
  Returns:
106
110
  Converted data in the specified schema type, or original data if schema_type is None
@@ -116,6 +120,8 @@ def to_schema(data: Any, *, schema_type: Any = None) -> Any:
116
120
  msg = "`schema_type` should be a valid Dataclass, Pydantic model, Msgspec struct, Attrs class, or TypedDict"
117
121
  raise SQLSpecError(msg)
118
122
 
123
+ if normalize_nested and conv is _convert_msgspec:
124
+ return _convert_msgspec(data, schema_type, normalize_nested=True)
119
125
  return conv(data, schema_type)
120
126
 
121
127
 
@@ -498,15 +504,16 @@ def _get_numpy_recursive_dispatcher() -> "TypeDispatcher[Callable[[Any], Any]]":
498
504
  return _NUMPY_RECURSIVE_DISPATCHER
499
505
 
500
506
 
501
- def _convert_msgspec(data: Any, schema_type: Any) -> Any:
507
+ def _convert_msgspec(data: Any, schema_type: Any, *, normalize_nested: bool = False) -> Any:
502
508
  """Convert data to msgspec Struct."""
509
+ is_batch = _is_sequence_input(data)
503
510
  transformed_data = (
504
- [_normalize_msgspec_input(item, schema_type) for item in data]
505
- if _is_sequence_input(data)
506
- else _normalize_msgspec_input(data, schema_type)
511
+ [_normalize_msgspec_struct(item, schema_type, normalize_nested=normalize_nested) for item in data]
512
+ if is_batch
513
+ else _normalize_msgspec_struct(data, schema_type, normalize_nested=normalize_nested)
507
514
  )
508
515
 
509
- target_type = list[schema_type] if _is_sequence_input(transformed_data) else schema_type
516
+ target_type = list[schema_type] if is_batch else schema_type
510
517
 
511
518
  try:
512
519
  return convert(
@@ -528,7 +535,7 @@ def _normalize_msgspec_input(data: Any, target_type: Any) -> Any:
528
535
  return data
529
536
 
530
537
  if is_msgspec_struct(target_type):
531
- return _normalize_msgspec_struct(data, cast("type", target_type))
538
+ return _normalize_msgspec_struct(data, cast("type", target_type), normalize_nested=True)
532
539
 
533
540
  origin = get_origin(target_type)
534
541
  args = get_args(target_type)
@@ -555,7 +562,7 @@ def _normalize_msgspec_input(data: Any, target_type: Any) -> Any:
555
562
  return data
556
563
 
557
564
 
558
- def _normalize_msgspec_struct(data: Any, schema_type: type) -> Any:
565
+ def _normalize_msgspec_struct(data: Any, schema_type: type, *, normalize_nested: bool) -> Any:
559
566
  if not isinstance(data, Mapping):
560
567
  return data
561
568
 
@@ -568,6 +575,11 @@ def _normalize_msgspec_struct(data: Any, schema_type: type) -> Any:
568
575
  elif name in data:
569
576
  normalized[encode_name] = data[name]
570
577
 
578
+ if normalize_nested:
579
+ for _, encode_name, field_type in fields:
580
+ if encode_name in normalized:
581
+ normalized[encode_name] = _normalize_msgspec_input(normalized[encode_name], field_type)
582
+
571
583
  return normalized
572
584
 
573
585
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: sqlspec
3
- Version: 0.72.0
3
+ Version: 0.72.1
4
4
  Summary: SQL Experiments in Python
5
5
  Project-URL: Discord, https://discord.gg/litestar
6
6
  Project-URL: Documentation, https://sqlspec.dev
@@ -356,7 +356,7 @@ sqlspec/adapters/teradata/events/store.py,sha256=un697vVRCE0IEdaCptLMhSeuk1vg3D5
356
356
  sqlspec/adapters/teradata/litestar/__init__.py,sha256=VjuaGDuyE5gyWAuY7bIOLamiGK7XeUo9Wo5IDzBc2hk,184
357
357
  sqlspec/adapters/teradata/litestar/store.py,sha256=OoCPsfsub48hPmSJrvVN9x6q8X2EplMXUWTG-1AUL7s,10504
358
358
  sqlspec/builder/__init__.py,sha256=zo-S5nL7ZGPPp67jNQDU4dBDAqZUbpBO0ZNtx-Frr68,13195
359
- sqlspec/builder/_base.py,sha256=CTXQVbp8a77deSx7ppAcTacso92IlDAwPCNxRxogCKQ,77259
359
+ sqlspec/builder/_base.py,sha256=aaAmDiVZxoHNAVMZsPALlHm9b5JYNKc10BfcRM39DyI,77641
360
360
  sqlspec/builder/_column.py,sha256=1o0wu3xxq9eNUCQv0tOOFFLZn6ug6qkrw8qU5ybZdbA,19502
361
361
  sqlspec/builder/_ddl.py,sha256=Bo6y8oSJMeQRne9crMa740zmSIJ6IhvFQ6HjzfdZNAQ,183815
362
362
  sqlspec/builder/_delete.py,sha256=6f1jylEdZhDoqcUrjCH7ZKeKZW1pGiHGIUykkETsTKU,2543
@@ -389,7 +389,7 @@ sqlspec/core/config_runtime.py,sha256=HPKxY_FpNmvY-yJGY7WfHThD9ZUqV2p2SpPDFVVGz6
389
389
  sqlspec/core/correlation.py,sha256=eAhHNwDAmBEHRDGR4rIqlvwmna8CKLd1uw2O5PNTm_Q,4861
390
390
  sqlspec/core/explain.py,sha256=V-C2MRW_gPzVIKqL10VESNbnJ3llMSjnigJ7iaVJgII,10350
391
391
  sqlspec/core/filters.py,sha256=Wz1pBI3WUakNbUULIoGhyRZxFiju4AoflAZcLmDcRXM,52396
392
- sqlspec/core/hashing.py,sha256=FmulIbXWwvOx8PxhfgMCfaIPpi--yma7SpnlXGNG1SU,13263
392
+ sqlspec/core/hashing.py,sha256=U-3xPR_4F5fq4B0gFyiarWy21wxUDOxf6JuyQx7gJqE,13352
393
393
  sqlspec/core/metrics.py,sha256=LdWdZDjR4tRmF40yr3iw4hzCnteh8FTvuN4eIkoeIQs,2871
394
394
  sqlspec/core/query_modifiers.py,sha256=LUiv_vdy-MZGrYY_j1r9LnyWohKcPCFi5V44vQKffXE,18783
395
395
  sqlspec/core/routing.py,sha256=iDZHk1qMmhiJU3tqcQ_EY51caIhmDGOlph6ojJudmXw,10288
@@ -848,7 +848,7 @@ sqlspec/utils/logging.py,sha256=ys58-AZjbEF9vOdObOV6asa-xID8DY_P0b9Lb-_QnRo,8581
848
848
  sqlspec/utils/module_loader.py,sha256=mr4-UT-UbxiUXfdEpaV4PwnZZUlRmF31sqhDCrRr9HU,12142
849
849
  sqlspec/utils/portal.py,sha256=RpaPYbvulJHfmfKl-k3uK9n50p78dqheEgxj-WltNfI,14607
850
850
  sqlspec/utils/routing.py,sha256=c5Hnb9d-seDbeobD3Kuxlr6MMpnks7k2NTgtj26N1_c,25869
851
- sqlspec/utils/schema.py,sha256=5BosMatObNYFqptkQBKoXj1G4ok4xIAVfWOvy9l9NCc,33791
851
+ sqlspec/utils/schema.py,sha256=7xK0_ZyOrYWiuJbcNW-jGbIK9VN4lNFdkIW6suCWkBY,34631
852
852
  sqlspec/utils/sync_tools.py,sha256=GfjSx0-amTYdQLCZ9xHunVgfCF6Ncmn7vyunZwcag2M,18658
853
853
  sqlspec/utils/text.py,sha256=J8FEIgALSwUEJNX7R-pqewWSocAJudCzP-GWjFfqlEI,10089
854
854
  sqlspec/utils/type_converters.py,sha256=-cf6-G-P9XO5Cyioclv8nVZX6SfZJKPpUC06xG3Lja8,7149
@@ -863,8 +863,8 @@ sqlspec/utils/tenant/_context.py,sha256=O6lxlr6HSfeeZQP3OTFBum_W1DOG8iq5ChP34XMe
863
863
  sqlspec/utils/tenant/_cursor.py,sha256=kb0wLDytHCmzt90A87UudL2KGxQB0MhDeDqgipFICgI,1545
864
864
  sqlspec/utils/tenant/_mssql.py,sha256=uTnN01Cpr_2m0Pvb8kfZ6wqfDyFXZ4ukiG6FQLTJTcI,7917
865
865
  sqlspec/utils/tenant/_postgres.py,sha256=0gUO1Dl0FaqNyuc2PqNX1sSI3mRKQI6yJhHHQFwrv0M,6796
866
- sqlspec-0.72.0.dist-info/METADATA,sha256=fAGerrD2ctGS8fkYevQBnjP6_cRTGd4Rqa9vImy4ppc,10961
867
- sqlspec-0.72.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
868
- sqlspec-0.72.0.dist-info/entry_points.txt,sha256=hsTkC00AkZrDhV7ZFQ4luVbx9IfSe_FtIRvdySwwe7M,473
869
- sqlspec-0.72.0.dist-info/licenses/LICENSE,sha256=MdujfZ6l5HuLz4mElxlu049itenOR3gnhN1_Nd3nVcM,1078
870
- sqlspec-0.72.0.dist-info/RECORD,,
866
+ sqlspec-0.72.1.dist-info/METADATA,sha256=X97vUetIPk-XFMIdoDNdwV8OdY-8raMDG4z6NTNyF_g,10961
867
+ sqlspec-0.72.1.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
868
+ sqlspec-0.72.1.dist-info/entry_points.txt,sha256=hsTkC00AkZrDhV7ZFQ4luVbx9IfSe_FtIRvdySwwe7M,473
869
+ sqlspec-0.72.1.dist-info/licenses/LICENSE,sha256=MdujfZ6l5HuLz4mElxlu049itenOR3gnhN1_Nd3nVcM,1078
870
+ sqlspec-0.72.1.dist-info/RECORD,,