omnata-plugin-runtime 0.16.0a413__tar.gz → 0.16.1a415__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 (16) hide show
  1. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/PKG-INFO +1 -1
  2. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/pyproject.toml +1 -1
  3. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/json_schema.py +107 -17
  4. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/plugin_entrypoints.py +7 -0
  5. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/LICENSE +0 -0
  6. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/README.md +0 -0
  7. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/__init__.py +0 -0
  8. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/api.py +0 -0
  9. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/configuration.py +0 -0
  10. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/decorators.py +0 -0
  11. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/forms.py +0 -0
  12. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/logging.py +0 -0
  13. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/omnata_plugin.py +0 -0
  14. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/rate_limiting.py +0 -0
  15. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/record_filter.py +0 -0
  16. {omnata_plugin_runtime-0.16.0a413 → omnata_plugin_runtime-0.16.1a415}/src/omnata_plugin_runtime/threading_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: omnata-plugin-runtime
3
- Version: 0.16.0a413
3
+ Version: 0.16.1a415
4
4
  Summary: Classes and common runtime components for building and running Omnata Plugins
5
5
  License-File: LICENSE
6
6
  Author: James Weakley
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "omnata-plugin-runtime"
3
- version = "0.16.0a413"
3
+ version = "0.16.1a415"
4
4
  description = "Classes and common runtime components for building and running Omnata Plugins"
5
5
  authors = ["James Weakley <james.weakley@omnata.com>"]
6
6
  readme = "README.md"
@@ -33,6 +33,9 @@ class JsonSchemaProperty(BaseModel):
33
33
  properties: Optional[Dict[str, Self]] = Field(
34
34
  None, description="The sub-properties of the property, if the property is an object type"
35
35
  )
36
+ propertyOrder: Optional[List[str]] = Field(
37
+ None, description="The original order of the keys in 'properties'. JSON object key order is not preserved once a schema is stored in Snowflake (OBJECT keys are sorted), so this array carries it. Keys missing from this list are placed after the listed ones, in their existing order."
38
+ )
36
39
  snowflakeTimestampType: Optional[Literal['TIMESTAMP_TZ','TIMESTAMP_NTZ','TIMESTAMP_LTZ']] = Field(
37
40
  None, description="The Snowflake timestamp type to use when interpreting a date-time string."
38
41
  )
@@ -282,13 +285,17 @@ class SnowflakeViewColumn(BaseModel):
282
285
  )
283
286
 
284
287
  @classmethod
285
- def order_by_reference(cls, current_stream_name: str, columns: List[Self]) -> List[Self]:
288
+ def order_by_reference(cls, current_stream_name: str, columns: List[Self], preserve_order: bool = False) -> List[Self]:
286
289
  """
287
290
  Uses topological sorting to order columns so that if a column references another column,
288
291
  the referenced column appears first in the list. This is required by Snowflake when
289
292
  column expressions reference the alias of another column.
290
293
 
291
294
  OMNATA_ system columns are always placed at the front of the result.
295
+
296
+ If preserve_order is True (the schema carried a propertyOrder), a stable sort is used so
297
+ columns keep their incoming order and only move as far as their references require.
298
+ Otherwise graphlib's TopologicalSorter is used, as before, so existing views are unchanged.
292
299
  """
293
300
  logger.debug(
294
301
  f"Ordering columns by reference for stream: {current_stream_name} ({len(columns)} columns)"
@@ -323,17 +330,42 @@ class SnowflakeViewColumn(BaseModel):
323
330
  f"Column {column.original_name} depends on {ref_col_name}"
324
331
  )
325
332
 
326
- # Use TopologicalSorter to sort the columns
327
- try:
328
- ts = TopologicalSorter(graph)
329
- sorted_column_names = list(ts.static_order())
330
- except ValueError as e:
331
- # This would indicate a circular dependency
332
- raise ValueError(f"Circular dependency detected in column references for stream {current_stream_name}: {e}")
333
-
333
+ if not preserve_order:
334
+ try:
335
+ ts = TopologicalSorter(graph)
336
+ sorted_column_names = list(ts.static_order())
337
+ except ValueError as e:
338
+ # This would indicate a circular dependency
339
+ raise ValueError(f"Circular dependency detected in column references for stream {current_stream_name}: {e}")
340
+ else:
341
+ # Stable topological sort: columns keep their incoming order (which reflects the
342
+ # schema's propertyOrder when present), and are only moved as far as needed so
343
+ # that every referenced column is defined before the column that references it.
344
+ sorted_column_names: List[str] = []
345
+ visited: Set[str] = set()
346
+ in_progress: Set[str] = set()
347
+
348
+ def visit(name: str, path: List[str]):
349
+ if name in visited:
350
+ return
351
+ if name in in_progress:
352
+ raise ValueError(
353
+ f"Circular dependency detected in column references for stream {current_stream_name}: {' -> '.join(path + [name])}"
354
+ )
355
+ in_progress.add(name)
356
+ for dependency in graph.get(name, []):
357
+ if dependency in column_by_name:
358
+ visit(dependency, path + [name])
359
+ in_progress.discard(name)
360
+ visited.add(name)
361
+ sorted_column_names.append(name)
362
+
363
+ for column in regular_columns:
364
+ visit(column.original_name, [])
365
+
334
366
  # Reconstruct the column list in topological order
335
367
  sorted_columns = [column_by_name[name] for name in sorted_column_names if name in column_by_name]
336
-
368
+
337
369
  # Return OMNATA system columns first, followed by sorted regular columns
338
370
  return omnata_system_columns + sorted_columns
339
371
 
@@ -564,7 +596,17 @@ class SnowflakeViewPart(BaseModel):
564
596
  from_clause = raw_table_fqn
565
597
 
566
598
  if external_join_aliases and self.joins:
567
- needed_joins = [j for j in self.joins if j.join_stream_alias in external_join_aliases]
599
+ # A join can start from another join's alias rather than this stream (a lookup two hops away),
600
+ # in which case the join it starts from is needed too
601
+ join_by_alias = {j.join_stream_alias: j for j in self.joins}
602
+ needed_aliases = set(external_join_aliases)
603
+ pending = list(needed_aliases)
604
+ while pending:
605
+ join = join_by_alias.get(pending.pop())
606
+ if join is not None and join.left_alias in join_by_alias and join.left_alias not in needed_aliases:
607
+ needed_aliases.add(join.left_alias)
608
+ pending.append(join.left_alias)
609
+ needed_joins = [j for j in self.joins if j.join_stream_alias in needed_aliases]
568
610
  if needed_joins:
569
611
  # Wrap the raw table in a subquery that exposes all direct columns by name.
570
612
  # This lets ON-clause references like "Account"."ParentId" resolve correctly,
@@ -586,9 +628,12 @@ class SnowflakeViewPart(BaseModel):
586
628
  elif joined_parts_by_name and j.join_stream_name in joined_parts_by_name:
587
629
  referenced_via_alias = {ref_col for c in selected_columns
588
630
  for ref_col in (c.referenced_columns or {}).get(j.join_stream_alias, [])}
631
+ # join keys read from this alias by joins chained off it
632
+ keys_via_alias = {other.left_column for other in needed_joins
633
+ if other.left_alias == j.join_stream_alias}
589
634
  target_cte_columns = (preceding_cte_columns or {}).get(j.join_stream_name)
590
635
  if target_cte_columns is not None and \
591
- (referenced_via_alias | {j.join_stream_column}) <= target_cte_columns:
636
+ (referenced_via_alias | keys_via_alias | {j.join_stream_column}) <= target_cte_columns:
592
637
  # The target's CTE is already defined and computes everything we need,
593
638
  # including formulas that depend on its own joins
594
639
  join_sqls.append(j.definition())
@@ -775,11 +820,15 @@ class SnowflakeViewParts(BaseModel):
775
820
  break
776
821
  if not referenced_by_other_columns:
777
822
  columns_only_referencing_own_stream.setdefault(part.stream_name, []).append(column.original_name)
778
- # if this part has joins to other streams, we need to include the join columns
823
+ # if this part has joins to other streams, we need to include the join columns.
824
+ # The left side of a join can be another join's alias (a lookup two hops away), in which case
825
+ # the left column belongs to that alias's stream rather than this part's
779
826
  for join in part.joins:
780
827
  all_referenced_columns.setdefault(join.join_stream_name, []).append(join.join_stream_column)
781
828
  all_referenced_columns.setdefault(join.join_stream_alias, []).append(join.join_stream_column)
782
- all_referenced_columns.setdefault(part.stream_name, []).append(join.left_column)
829
+ left_stream = next((j.join_stream_name for j in part.joins if j.join_stream_alias == join.left_alias),
830
+ part.stream_name)
831
+ all_referenced_columns.setdefault(left_stream, []).append(join.left_column)
783
832
  joined_parts_by_name = {part.stream_name: part for part in joined_parts_deduped}
784
833
  ctes = [
785
834
  self.main_part.cte_text(original_name=True,include_extra_columns=columns_only_referencing_own_stream.get(self.main_part.stream_name),joined_parts_by_name=joined_parts_by_name)
@@ -1262,6 +1311,39 @@ def prune(view_part: SnowflakeViewPart, joined_parts: List[SnowflakeViewPart]) -
1262
1311
 
1263
1312
  return columns_removed
1264
1313
 
1314
+ def ordered_properties(
1315
+ properties: Dict[str, Any], property_order: Optional[List[str]]
1316
+ ) -> List[Tuple[str, Any]]:
1317
+ """
1318
+ Returns the (name, value) pairs of a json-schema "properties" dict, ordered by the
1319
+ propertyOrder extension if provided. Names in propertyOrder that are not present are
1320
+ ignored; properties not named in propertyOrder follow, in their existing dict order.
1321
+ """
1322
+ if not property_order:
1323
+ return list(properties.items())
1324
+ ordered = [(name, properties[name]) for name in dict.fromkeys(property_order) if name in properties]
1325
+ listed = {name for name, _ in ordered}
1326
+ return ordered + [(name, value) for name, value in properties.items() if name not in listed]
1327
+
1328
+
1329
+ def with_property_order(schema: Optional[Dict[str, Any]], _depth: int = 0) -> Optional[Dict[str, Any]]:
1330
+ """
1331
+ Stamps a propertyOrder array onto a json-schema dict (and its nested object properties),
1332
+ capturing the current key order of each "properties" dict. Existing propertyOrder values are
1333
+ left untouched. Must be called while the schema is still a Python dict, before it is
1334
+ serialised into Snowflake, because Snowflake OBJECTs do not preserve key order.
1335
+ """
1336
+ if not isinstance(schema, dict) or _depth > 10:
1337
+ return schema
1338
+ properties = schema.get("properties")
1339
+ if isinstance(properties, dict):
1340
+ if not schema.get("propertyOrder"):
1341
+ schema["propertyOrder"] = list(properties.keys())
1342
+ for value in properties.values():
1343
+ with_property_order(value, _depth + 1)
1344
+ return schema
1345
+
1346
+
1265
1347
  class JsonSchemaTopLevel(BaseModel):
1266
1348
  """
1267
1349
  This model is used as a starting point for parsing a JSON schema.
@@ -1277,6 +1359,9 @@ class JsonSchemaTopLevel(BaseModel):
1277
1359
  properties: Optional[Dict[str, Any]] = Field(
1278
1360
  None, description="The properties of the schema. This is left as a dictionary, and parsed on demand."
1279
1361
  )
1362
+ propertyOrder: Optional[List[str]] = Field(
1363
+ None, description="The original order of the keys in 'properties'. See JsonSchemaProperty.propertyOrder."
1364
+ )
1280
1365
 
1281
1366
  def build_view_columns(self,
1282
1367
  column_name_environment: Environment,
@@ -1296,7 +1381,7 @@ class JsonSchemaTopLevel(BaseModel):
1296
1381
  column_name_expression=column_name_expression,
1297
1382
  plugin_app_database=plugin_app_database,
1298
1383
  )
1299
- for property_name, property_value in self.properties.items()
1384
+ for property_name, property_value in ordered_properties(self.properties, self.propertyOrder)
1300
1385
  ]
1301
1386
  return [item for sublist in columns for item in sublist]
1302
1387
 
@@ -1334,7 +1419,9 @@ class JsonSchemaTopLevel(BaseModel):
1334
1419
  current_comment_path=current_comment_path + [json_property.description or ""],
1335
1420
  plugin_app_database=plugin_app_database,
1336
1421
  )
1337
- for child_property_name, child_property_value in json_property.properties.items()
1422
+ for child_property_name, child_property_value in ordered_properties(
1423
+ json_property.properties, json_property.propertyOrder
1424
+ )
1338
1425
  ]
1339
1426
  children = [item for sublist in children for item in sublist]
1340
1427
  current_field_name_path = current_field_name_path + [property_name]
@@ -1420,6 +1507,7 @@ def normalized_view_part(
1420
1507
  view_columns = snowflake_columns
1421
1508
  joins = []
1422
1509
  comment = None
1510
+ preserve_order = False
1423
1511
  if stream_schema is not None:
1424
1512
  logger.debug(
1425
1513
  f"Building view columns for stream: {stream_name}"
@@ -1433,6 +1521,7 @@ def normalized_view_part(
1433
1521
  if json_schema.joins:
1434
1522
  joins = json_schema.joins
1435
1523
  comment = json_schema.description
1524
+ preserve_order = bool(json_schema.propertyOrder)
1436
1525
 
1437
1526
  direct_view_columns = [c for c in view_columns if c.is_join_column]
1438
1527
  join_view_columns = [c for c in view_columns if not c.is_join_column]
@@ -1441,7 +1530,8 @@ def normalized_view_part(
1441
1530
  #- Direct and joined columns, ordered so that columns that reference other columns are defined after the columns they reference
1442
1531
  #- OMNATA_RETRIEVE_DATE, OMNATA_RAW_RECORD, OMNATA_IS_DELETED, OMNATA_RUN_ID
1443
1532
  view_columns = SnowflakeViewColumn.order_by_reference(stream_name,direct_view_columns +
1444
- join_view_columns)
1533
+ join_view_columns,
1534
+ preserve_order=preserve_order)
1445
1535
  return SnowflakeViewPart(
1446
1536
  stream_name=stream_name,
1447
1537
  raw_table_location=raw_table_location,
@@ -35,6 +35,7 @@ from .omnata_plugin import (
35
35
  )
36
36
  from pydantic import TypeAdapter
37
37
  from .rate_limiting import ApiLimits, RateLimitState
38
+ from .json_schema import with_property_order
38
39
  from opentelemetry import trace
39
40
 
40
41
  IMPORT_DIRECTORY_NAME = "snowflake_import_directory"
@@ -454,6 +455,12 @@ class PluginEntrypoint:
454
455
  elif isinstance(script_result, List):
455
456
  if len(script_result) > 0 and isinstance(script_result[0], BaseModel):
456
457
  script_result = [r.model_dump() for r in script_result]
458
+ # Capture the plugin's property ordering before the schema is stored in Snowflake,
459
+ # where OBJECT key order is lost (CS-850)
460
+ if isinstance(script_result, List):
461
+ for stream in script_result:
462
+ if isinstance(stream, dict) and isinstance(stream.get("json_schema"), dict):
463
+ with_property_order(stream["json_schema"])
457
464
  return script_result
458
465
 
459
466