exonware-xwnode 0.0.1.12__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.
Files changed (132) hide show
  1. exonware/__init__.py +14 -0
  2. exonware/xwnode/__init__.py +127 -0
  3. exonware/xwnode/base.py +676 -0
  4. exonware/xwnode/config.py +178 -0
  5. exonware/xwnode/contracts.py +730 -0
  6. exonware/xwnode/errors.py +503 -0
  7. exonware/xwnode/facade.py +460 -0
  8. exonware/xwnode/strategies/__init__.py +158 -0
  9. exonware/xwnode/strategies/advisor.py +463 -0
  10. exonware/xwnode/strategies/edges/__init__.py +32 -0
  11. exonware/xwnode/strategies/edges/adj_list.py +227 -0
  12. exonware/xwnode/strategies/edges/adj_matrix.py +391 -0
  13. exonware/xwnode/strategies/edges/base.py +169 -0
  14. exonware/xwnode/strategies/flyweight.py +328 -0
  15. exonware/xwnode/strategies/impls/__init__.py +13 -0
  16. exonware/xwnode/strategies/impls/_base_edge.py +403 -0
  17. exonware/xwnode/strategies/impls/_base_node.py +307 -0
  18. exonware/xwnode/strategies/impls/edge_adj_list.py +353 -0
  19. exonware/xwnode/strategies/impls/edge_adj_matrix.py +445 -0
  20. exonware/xwnode/strategies/impls/edge_bidir_wrapper.py +455 -0
  21. exonware/xwnode/strategies/impls/edge_block_adj_matrix.py +539 -0
  22. exonware/xwnode/strategies/impls/edge_coo.py +533 -0
  23. exonware/xwnode/strategies/impls/edge_csc.py +447 -0
  24. exonware/xwnode/strategies/impls/edge_csr.py +492 -0
  25. exonware/xwnode/strategies/impls/edge_dynamic_adj_list.py +503 -0
  26. exonware/xwnode/strategies/impls/edge_flow_network.py +555 -0
  27. exonware/xwnode/strategies/impls/edge_hyperedge_set.py +516 -0
  28. exonware/xwnode/strategies/impls/edge_neural_graph.py +650 -0
  29. exonware/xwnode/strategies/impls/edge_octree.py +574 -0
  30. exonware/xwnode/strategies/impls/edge_property_store.py +655 -0
  31. exonware/xwnode/strategies/impls/edge_quadtree.py +519 -0
  32. exonware/xwnode/strategies/impls/edge_rtree.py +820 -0
  33. exonware/xwnode/strategies/impls/edge_temporal_edgeset.py +558 -0
  34. exonware/xwnode/strategies/impls/edge_tree_graph_basic.py +271 -0
  35. exonware/xwnode/strategies/impls/edge_weighted_graph.py +411 -0
  36. exonware/xwnode/strategies/manager.py +775 -0
  37. exonware/xwnode/strategies/metrics.py +538 -0
  38. exonware/xwnode/strategies/migration.py +432 -0
  39. exonware/xwnode/strategies/nodes/__init__.py +50 -0
  40. exonware/xwnode/strategies/nodes/_base_node.py +307 -0
  41. exonware/xwnode/strategies/nodes/adjacency_list.py +267 -0
  42. exonware/xwnode/strategies/nodes/aho_corasick.py +345 -0
  43. exonware/xwnode/strategies/nodes/array_list.py +209 -0
  44. exonware/xwnode/strategies/nodes/base.py +247 -0
  45. exonware/xwnode/strategies/nodes/deque.py +200 -0
  46. exonware/xwnode/strategies/nodes/hash_map.py +135 -0
  47. exonware/xwnode/strategies/nodes/heap.py +307 -0
  48. exonware/xwnode/strategies/nodes/linked_list.py +232 -0
  49. exonware/xwnode/strategies/nodes/node_aho_corasick.py +520 -0
  50. exonware/xwnode/strategies/nodes/node_array_list.py +175 -0
  51. exonware/xwnode/strategies/nodes/node_avl_tree.py +371 -0
  52. exonware/xwnode/strategies/nodes/node_b_plus_tree.py +542 -0
  53. exonware/xwnode/strategies/nodes/node_bitmap.py +420 -0
  54. exonware/xwnode/strategies/nodes/node_bitset_dynamic.py +513 -0
  55. exonware/xwnode/strategies/nodes/node_bloom_filter.py +347 -0
  56. exonware/xwnode/strategies/nodes/node_btree.py +357 -0
  57. exonware/xwnode/strategies/nodes/node_count_min_sketch.py +470 -0
  58. exonware/xwnode/strategies/nodes/node_cow_tree.py +473 -0
  59. exonware/xwnode/strategies/nodes/node_cuckoo_hash.py +392 -0
  60. exonware/xwnode/strategies/nodes/node_fenwick_tree.py +301 -0
  61. exonware/xwnode/strategies/nodes/node_hash_map.py +269 -0
  62. exonware/xwnode/strategies/nodes/node_heap.py +191 -0
  63. exonware/xwnode/strategies/nodes/node_hyperloglog.py +407 -0
  64. exonware/xwnode/strategies/nodes/node_linked_list.py +409 -0
  65. exonware/xwnode/strategies/nodes/node_lsm_tree.py +400 -0
  66. exonware/xwnode/strategies/nodes/node_ordered_map.py +390 -0
  67. exonware/xwnode/strategies/nodes/node_ordered_map_balanced.py +565 -0
  68. exonware/xwnode/strategies/nodes/node_patricia.py +512 -0
  69. exonware/xwnode/strategies/nodes/node_persistent_tree.py +378 -0
  70. exonware/xwnode/strategies/nodes/node_radix_trie.py +452 -0
  71. exonware/xwnode/strategies/nodes/node_red_black_tree.py +497 -0
  72. exonware/xwnode/strategies/nodes/node_roaring_bitmap.py +570 -0
  73. exonware/xwnode/strategies/nodes/node_segment_tree.py +289 -0
  74. exonware/xwnode/strategies/nodes/node_set_hash.py +354 -0
  75. exonware/xwnode/strategies/nodes/node_set_tree.py +480 -0
  76. exonware/xwnode/strategies/nodes/node_skip_list.py +316 -0
  77. exonware/xwnode/strategies/nodes/node_splay_tree.py +393 -0
  78. exonware/xwnode/strategies/nodes/node_suffix_array.py +487 -0
  79. exonware/xwnode/strategies/nodes/node_treap.py +387 -0
  80. exonware/xwnode/strategies/nodes/node_tree_graph_hybrid.py +1434 -0
  81. exonware/xwnode/strategies/nodes/node_trie.py +252 -0
  82. exonware/xwnode/strategies/nodes/node_union_find.py +187 -0
  83. exonware/xwnode/strategies/nodes/node_xdata_optimized.py +369 -0
  84. exonware/xwnode/strategies/nodes/priority_queue.py +209 -0
  85. exonware/xwnode/strategies/nodes/queue.py +161 -0
  86. exonware/xwnode/strategies/nodes/sparse_matrix.py +206 -0
  87. exonware/xwnode/strategies/nodes/stack.py +152 -0
  88. exonware/xwnode/strategies/nodes/trie.py +274 -0
  89. exonware/xwnode/strategies/nodes/union_find.py +283 -0
  90. exonware/xwnode/strategies/pattern_detector.py +603 -0
  91. exonware/xwnode/strategies/performance_monitor.py +487 -0
  92. exonware/xwnode/strategies/queries/__init__.py +24 -0
  93. exonware/xwnode/strategies/queries/base.py +236 -0
  94. exonware/xwnode/strategies/queries/cql.py +201 -0
  95. exonware/xwnode/strategies/queries/cypher.py +181 -0
  96. exonware/xwnode/strategies/queries/datalog.py +70 -0
  97. exonware/xwnode/strategies/queries/elastic_dsl.py +70 -0
  98. exonware/xwnode/strategies/queries/eql.py +70 -0
  99. exonware/xwnode/strategies/queries/flux.py +70 -0
  100. exonware/xwnode/strategies/queries/gql.py +70 -0
  101. exonware/xwnode/strategies/queries/graphql.py +240 -0
  102. exonware/xwnode/strategies/queries/gremlin.py +181 -0
  103. exonware/xwnode/strategies/queries/hiveql.py +214 -0
  104. exonware/xwnode/strategies/queries/hql.py +70 -0
  105. exonware/xwnode/strategies/queries/jmespath.py +219 -0
  106. exonware/xwnode/strategies/queries/jq.py +66 -0
  107. exonware/xwnode/strategies/queries/json_query.py +66 -0
  108. exonware/xwnode/strategies/queries/jsoniq.py +248 -0
  109. exonware/xwnode/strategies/queries/kql.py +70 -0
  110. exonware/xwnode/strategies/queries/linq.py +238 -0
  111. exonware/xwnode/strategies/queries/logql.py +70 -0
  112. exonware/xwnode/strategies/queries/mql.py +68 -0
  113. exonware/xwnode/strategies/queries/n1ql.py +210 -0
  114. exonware/xwnode/strategies/queries/partiql.py +70 -0
  115. exonware/xwnode/strategies/queries/pig.py +215 -0
  116. exonware/xwnode/strategies/queries/promql.py +70 -0
  117. exonware/xwnode/strategies/queries/sparql.py +220 -0
  118. exonware/xwnode/strategies/queries/sql.py +275 -0
  119. exonware/xwnode/strategies/queries/xml_query.py +66 -0
  120. exonware/xwnode/strategies/queries/xpath.py +223 -0
  121. exonware/xwnode/strategies/queries/xquery.py +258 -0
  122. exonware/xwnode/strategies/queries/xwnode_executor.py +332 -0
  123. exonware/xwnode/strategies/queries/xwquery_strategy.py +424 -0
  124. exonware/xwnode/strategies/registry.py +604 -0
  125. exonware/xwnode/strategies/simple.py +273 -0
  126. exonware/xwnode/strategies/utils.py +532 -0
  127. exonware/xwnode/types.py +912 -0
  128. exonware/xwnode/version.py +78 -0
  129. exonware_xwnode-0.0.1.12.dist-info/METADATA +169 -0
  130. exonware_xwnode-0.0.1.12.dist-info/RECORD +132 -0
  131. exonware_xwnode-0.0.1.12.dist-info/WHEEL +4 -0
  132. exonware_xwnode-0.0.1.12.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ LogQL Query Strategy
4
+
5
+ This module implements the LogQL query strategy for Grafana Loki Log Query Language operations.
6
+
7
+ Company: eXonware.com
8
+ Author: Eng. Muhammad AlShehri
9
+ Email: connect@exonware.com
10
+ Version: 0.0.1.12
11
+ Generation Date: January 2, 2025
12
+ """
13
+
14
+ from typing import Any, Dict, List, Optional
15
+ from .base import AStructuredQueryStrategy
16
+ from ...errors import XWNodeValueError
17
+ from ...contracts import QueryMode, QueryTrait
18
+
19
+
20
+ class LogQLStrategy(AStructuredQueryStrategy):
21
+ """LogQL query strategy for Grafana Loki Log Query Language operations."""
22
+
23
+ def __init__(self, **options):
24
+ super().__init__(**options)
25
+ self._mode = QueryMode.LOGQL
26
+ self._traits = QueryTrait.STRUCTURED | QueryTrait.ANALYTICAL | QueryTrait.TEMPORAL | QueryTrait.STREAMING
27
+
28
+ def execute(self, query: str, **kwargs) -> Any:
29
+ """Execute LogQL query."""
30
+ if not self.validate_query(query):
31
+ raise XWNodeValueError(f"Invalid LogQL query: {query}")
32
+ return {"result": "LogQL query executed", "query": query}
33
+
34
+ def validate_query(self, query: str) -> bool:
35
+ """Validate LogQL query syntax."""
36
+ if not query or not isinstance(query, str):
37
+ return False
38
+ return any(op in query for op in ["{", "}", "|", "rate", "sum", "count", "avg", "max", "min"])
39
+
40
+ def get_query_plan(self, query: str) -> Dict[str, Any]:
41
+ """Get LogQL query execution plan."""
42
+ return {
43
+ "query_type": "LogQL",
44
+ "complexity": "MEDIUM",
45
+ "estimated_cost": 90
46
+ }
47
+
48
+ def select_query(self, table: str, columns: List[str], where_clause: str = None) -> Any:
49
+ """Execute SELECT query."""
50
+ return self.execute(f"{{job=\"{table}\"}}")
51
+
52
+ def insert_query(self, table: str, data: Dict[str, Any]) -> Any:
53
+ """Execute INSERT query."""
54
+ return self.execute(f"INSERT INTO {table} VALUES {data}")
55
+
56
+ def update_query(self, table: str, data: Dict[str, Any], where_clause: str = None) -> Any:
57
+ """Execute UPDATE query."""
58
+ return self.execute(f"UPDATE {table} SET {data}")
59
+
60
+ def delete_query(self, table: str, where_clause: str = None) -> Any:
61
+ """Execute DELETE query."""
62
+ return self.execute(f"DELETE FROM {table}")
63
+
64
+ def join_query(self, tables: List[str], join_conditions: List[str]) -> Any:
65
+ """Execute JOIN query."""
66
+ return self.execute(f"{{job=\"{tables[0]}\"}} | join {{job=\"{tables[1]}\"}}")
67
+
68
+ def aggregate_query(self, table: str, functions: List[str], group_by: List[str] = None) -> Any:
69
+ """Execute aggregate query."""
70
+ return self.execute(f"{{job=\"{table}\"}} | {functions[0]}()")
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ MQL Query Strategy
4
+
5
+ This module implements the MQL query strategy for MongoDB Query Language operations.
6
+
7
+ Company: eXonware.com
8
+ Author: Eng. Muhammad AlShehri
9
+ Email: connect@exonware.com
10
+ Version: 0.0.1.12
11
+ Generation Date: January 2, 2025
12
+ """
13
+
14
+ from typing import Any, Dict, List, Optional
15
+ from .base import ADocumentQueryStrategy
16
+ from ...errors import XWNodeValueError
17
+ from ...contracts import QueryMode, QueryTrait
18
+
19
+
20
+ class MQLStrategy(ADocumentQueryStrategy):
21
+ """MQL query strategy for MongoDB Query Language operations."""
22
+
23
+ def __init__(self, **options):
24
+ super().__init__(**options)
25
+ self._mode = QueryMode.MQL
26
+ self._traits = QueryTrait.DOCUMENT | QueryTrait.STRUCTURED | QueryTrait.ANALYTICAL
27
+
28
+ def execute(self, query: str, **kwargs) -> Any:
29
+ """Execute MQL query."""
30
+ if not self.validate_query(query):
31
+ raise XWNodeValueError(f"Invalid MQL query: {query}")
32
+ return {"result": "MQL query executed", "query": query}
33
+
34
+ def validate_query(self, query: str) -> bool:
35
+ """Validate MQL query syntax."""
36
+ if not query or not isinstance(query, str):
37
+ return False
38
+ return any(op in query for op in ["find", "aggregate", "insert", "update", "delete", "createIndex"])
39
+
40
+ def get_query_plan(self, query: str) -> Dict[str, Any]:
41
+ """Get MQL query execution plan."""
42
+ return {
43
+ "query_type": "MQL",
44
+ "complexity": "MEDIUM",
45
+ "estimated_cost": 80
46
+ }
47
+
48
+ def path_query(self, path: str) -> Any:
49
+ """Execute path-based query."""
50
+ return self.execute(f"db.collection.find({{{path}: {{$exists: true}}}})")
51
+
52
+ def filter_query(self, filter_expression: str) -> Any:
53
+ """Execute filter query."""
54
+ return self.execute(f"db.collection.find({filter_expression})")
55
+
56
+ def projection_query(self, fields: List[str]) -> Any:
57
+ """Execute projection query."""
58
+ projection = {field: 1 for field in fields}
59
+ return self.execute(f"db.collection.find({{}}, {projection})")
60
+
61
+ def sort_query(self, sort_fields: List[str], order: str = "asc") -> Any:
62
+ """Execute sort query."""
63
+ sort_order = 1 if order == "asc" else -1
64
+ return self.execute(f"db.collection.find().sort({{{sort_fields[0]}: {sort_order}}})")
65
+
66
+ def limit_query(self, limit: int, offset: int = 0) -> Any:
67
+ """Execute limit query."""
68
+ return self.execute(f"db.collection.find().skip({offset}).limit({limit})")
@@ -0,0 +1,210 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ N1QL Query Strategy
4
+
5
+ This module implements the N1QL query strategy for Couchbase Query Language operations.
6
+
7
+ Company: eXonware.com
8
+ Author: Eng. Muhammad AlShehri
9
+ Email: connect@exonware.com
10
+ Version: 0.0.1.12
11
+ Generation Date: January 2, 2025
12
+ """
13
+
14
+ import re
15
+ from typing import Any, Dict, List, Optional, Union
16
+ from .base import AStructuredQueryStrategy
17
+ from ...errors import XWNodeTypeError, XWNodeValueError
18
+ from ...contracts import QueryMode, QueryTrait
19
+
20
+
21
+ class N1QLStrategy(AStructuredQueryStrategy):
22
+ """
23
+ N1QL query strategy for Couchbase Query Language operations.
24
+
25
+ Supports:
26
+ - N1QL 1.0+ features
27
+ - SELECT, INSERT, UPDATE, DELETE operations
28
+ - CREATE, DROP, ALTER operations
29
+ - JSON document operations
30
+ - Array and object operations
31
+ """
32
+
33
+ def __init__(self, **options):
34
+ super().__init__(**options)
35
+ self._mode = QueryMode.N1QL
36
+ self._traits = QueryTrait.STRUCTURED | QueryTrait.ANALYTICAL | QueryTrait.BATCH
37
+
38
+ def execute(self, query: str, **kwargs) -> Any:
39
+ """Execute N1QL query."""
40
+ if not self.validate_query(query):
41
+ raise XWNodeValueError(f"Invalid N1QL query: {query}")
42
+
43
+ query_type = self._get_query_type(query)
44
+
45
+ if query_type == "SELECT":
46
+ return self._execute_select(query, **kwargs)
47
+ elif query_type == "INSERT":
48
+ return self._execute_insert(query, **kwargs)
49
+ elif query_type == "UPDATE":
50
+ return self._execute_update(query, **kwargs)
51
+ elif query_type == "DELETE":
52
+ return self._execute_delete(query, **kwargs)
53
+ else:
54
+ raise XWNodeValueError(f"Unsupported query type: {query_type}")
55
+
56
+ def validate_query(self, query: str) -> bool:
57
+ """Validate N1QL query syntax."""
58
+ if not query or not isinstance(query, str):
59
+ return False
60
+
61
+ # Basic N1QL validation
62
+ query = query.strip().upper()
63
+ valid_operations = ["SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "ALTER", "USE", "EXPLAIN", "PREPARE", "EXECUTE", "INFER", "BUILD", "REBUILD", "ANALYZE", "UPDATE STATISTICS"]
64
+
65
+ for operation in valid_operations:
66
+ if query.startswith(operation):
67
+ return True
68
+
69
+ return False
70
+
71
+ def get_query_plan(self, query: str) -> Dict[str, Any]:
72
+ """Get N1QL query execution plan."""
73
+ query_type = self._get_query_type(query)
74
+
75
+ return {
76
+ "query_type": query_type,
77
+ "operation": query_type,
78
+ "complexity": self._estimate_complexity(query),
79
+ "estimated_cost": self._estimate_cost(query),
80
+ "operations": self._extract_operations(query),
81
+ "optimization_hints": self._get_optimization_hints(query)
82
+ }
83
+
84
+ def select_query(self, table: str, columns: List[str], where_clause: str = None) -> Any:
85
+ """Execute SELECT query."""
86
+ query = f"SELECT {', '.join(columns)} FROM {table}"
87
+ if where_clause:
88
+ query += f" WHERE {where_clause}"
89
+
90
+ return self.execute(query)
91
+
92
+ def insert_query(self, table: str, data: Dict[str, Any]) -> Any:
93
+ """Execute INSERT query."""
94
+ columns = list(data.keys())
95
+ values = list(data.values())
96
+
97
+ query = f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({', '.join(['?' for _ in values])})"
98
+ return self.execute(query, values=values)
99
+
100
+ def update_query(self, table: str, data: Dict[str, Any], where_clause: str = None) -> Any:
101
+ """Execute UPDATE query."""
102
+ set_clause = ', '.join([f"{k} = ?" for k in data.keys()])
103
+ query = f"UPDATE {table} SET {set_clause}"
104
+
105
+ if where_clause:
106
+ query += f" WHERE {where_clause}"
107
+
108
+ return self.execute(query, values=list(data.values()))
109
+
110
+ def delete_query(self, table: str, where_clause: str = None) -> Any:
111
+ """Execute DELETE query."""
112
+ query = f"DELETE FROM {table}"
113
+ if where_clause:
114
+ query += f" WHERE {where_clause}"
115
+
116
+ return self.execute(query)
117
+
118
+ def join_query(self, tables: List[str], join_conditions: List[str]) -> Any:
119
+ """Execute JOIN query."""
120
+ if len(tables) < 2:
121
+ raise XWNodeValueError("JOIN requires at least 2 tables")
122
+
123
+ query = f"SELECT * FROM {tables[0]}"
124
+ for i, table in enumerate(tables[1:], 1):
125
+ if i <= len(join_conditions):
126
+ query += f" JOIN {table} ON {join_conditions[i-1]}"
127
+ else:
128
+ query += f" CROSS JOIN {table}"
129
+
130
+ return self.execute(query)
131
+
132
+ def aggregate_query(self, table: str, functions: List[str], group_by: List[str] = None) -> Any:
133
+ """Execute aggregate query."""
134
+ query = f"SELECT {', '.join(functions)} FROM {table}"
135
+ if group_by:
136
+ query += f" GROUP BY {', '.join(group_by)}"
137
+
138
+ return self.execute(query)
139
+
140
+ def _get_query_type(self, query: str) -> str:
141
+ """Extract query type from N1QL query."""
142
+ query = query.strip().upper()
143
+ for operation in ["SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "ALTER", "USE", "EXPLAIN", "PREPARE", "EXECUTE", "INFER", "BUILD", "REBUILD", "ANALYZE", "UPDATE STATISTICS"]:
144
+ if query.startswith(operation):
145
+ return operation
146
+ return "UNKNOWN"
147
+
148
+ def _execute_select(self, query: str, **kwargs) -> Any:
149
+ """Execute SELECT query."""
150
+ return {"result": "N1QL SELECT executed", "query": query}
151
+
152
+ def _execute_insert(self, query: str, **kwargs) -> Any:
153
+ """Execute INSERT query."""
154
+ return {"result": "N1QL INSERT executed", "query": query}
155
+
156
+ def _execute_update(self, query: str, **kwargs) -> Any:
157
+ """Execute UPDATE query."""
158
+ return {"result": "N1QL UPDATE executed", "query": query}
159
+
160
+ def _execute_delete(self, query: str, **kwargs) -> Any:
161
+ """Execute DELETE query."""
162
+ return {"result": "N1QL DELETE executed", "query": query}
163
+
164
+ def _estimate_complexity(self, query: str) -> str:
165
+ """Estimate query complexity."""
166
+ operations = self._extract_operations(query)
167
+
168
+ if len(operations) > 5:
169
+ return "HIGH"
170
+ elif len(operations) > 2:
171
+ return "MEDIUM"
172
+ else:
173
+ return "LOW"
174
+
175
+ def _estimate_cost(self, query: str) -> int:
176
+ """Estimate query cost."""
177
+ complexity = self._estimate_complexity(query)
178
+ if complexity == "HIGH":
179
+ return 140
180
+ elif complexity == "MEDIUM":
181
+ return 70
182
+ else:
183
+ return 35
184
+
185
+ def _extract_operations(self, query: str) -> List[str]:
186
+ """Extract N1QL operations from query."""
187
+ operations = []
188
+
189
+ n1ql_operations = ["SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "ALTER", "USE", "EXPLAIN", "PREPARE", "EXECUTE", "INFER", "BUILD", "REBUILD", "ANALYZE", "UPDATE STATISTICS"]
190
+
191
+ for operation in n1ql_operations:
192
+ if operation in query.upper():
193
+ operations.append(operation)
194
+
195
+ return operations
196
+
197
+ def _get_optimization_hints(self, query: str) -> List[str]:
198
+ """Get query optimization hints."""
199
+ hints = []
200
+
201
+ if "SELECT *" in query.upper():
202
+ hints.append("Consider specifying columns instead of using *")
203
+
204
+ if "WHERE" not in query.upper() and "SELECT" in query.upper():
205
+ hints.append("Consider adding WHERE clause to limit results")
206
+
207
+ if "JOIN" in query.upper():
208
+ hints.append("Consider using indexes for JOIN operations")
209
+
210
+ return hints
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ PartiQL Query Strategy
4
+
5
+ This module implements the PartiQL query strategy for AWS PartiQL operations.
6
+
7
+ Company: eXonware.com
8
+ Author: Eng. Muhammad AlShehri
9
+ Email: connect@exonware.com
10
+ Version: 0.0.1.12
11
+ Generation Date: January 2, 2025
12
+ """
13
+
14
+ from typing import Any, Dict, List, Optional
15
+ from .base import AStructuredQueryStrategy
16
+ from ...errors import XWNodeValueError
17
+ from ...contracts import QueryMode, QueryTrait
18
+
19
+
20
+ class PartiQLStrategy(AStructuredQueryStrategy):
21
+ """PartiQL query strategy for AWS PartiQL operations."""
22
+
23
+ def __init__(self, **options):
24
+ super().__init__(**options)
25
+ self._mode = QueryMode.PARTIQL
26
+ self._traits = QueryTrait.STRUCTURED | QueryTrait.ANALYTICAL | QueryTrait.BATCH
27
+
28
+ def execute(self, query: str, **kwargs) -> Any:
29
+ """Execute PartiQL query."""
30
+ if not self.validate_query(query):
31
+ raise XWNodeValueError(f"Invalid PartiQL query: {query}")
32
+ return {"result": "PartiQL query executed", "query": query}
33
+
34
+ def validate_query(self, query: str) -> bool:
35
+ """Validate PartiQL query syntax."""
36
+ if not query or not isinstance(query, str):
37
+ return False
38
+ return any(op in query.upper() for op in ["SELECT", "FROM", "WHERE", "INSERT", "UPDATE", "DELETE"])
39
+
40
+ def get_query_plan(self, query: str) -> Dict[str, Any]:
41
+ """Get PartiQL query execution plan."""
42
+ return {
43
+ "query_type": "PartiQL",
44
+ "complexity": "MEDIUM",
45
+ "estimated_cost": 85
46
+ }
47
+
48
+ def select_query(self, table: str, columns: List[str], where_clause: str = None) -> Any:
49
+ """Execute SELECT query."""
50
+ return self.execute(f"SELECT {', '.join(columns)} FROM {table}")
51
+
52
+ def insert_query(self, table: str, data: Dict[str, Any]) -> Any:
53
+ """Execute INSERT query."""
54
+ return self.execute(f"INSERT INTO {table} VALUE {data}")
55
+
56
+ def update_query(self, table: str, data: Dict[str, Any], where_clause: str = None) -> Any:
57
+ """Execute UPDATE query."""
58
+ return self.execute(f"UPDATE {table} SET {data}")
59
+
60
+ def delete_query(self, table: str, where_clause: str = None) -> Any:
61
+ """Execute DELETE query."""
62
+ return self.execute(f"DELETE FROM {table}")
63
+
64
+ def join_query(self, tables: List[str], join_conditions: List[str]) -> Any:
65
+ """Execute JOIN query."""
66
+ return self.execute(f"SELECT * FROM {tables[0]} JOIN {tables[1]}")
67
+
68
+ def aggregate_query(self, table: str, functions: List[str], group_by: List[str] = None) -> Any:
69
+ """Execute aggregate query."""
70
+ return self.execute(f"SELECT {', '.join(functions)} FROM {table}")
@@ -0,0 +1,215 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Pig Query Strategy
4
+
5
+ This module implements the Pig query strategy for Apache Pig Latin operations.
6
+
7
+ Company: eXonware.com
8
+ Author: Eng. Muhammad AlShehri
9
+ Email: connect@exonware.com
10
+ Version: 0.0.1.12
11
+ Generation Date: January 2, 2025
12
+ """
13
+
14
+ import re
15
+ from typing import Any, Dict, List, Optional, Union
16
+ from .base import AStructuredQueryStrategy
17
+ from ...errors import XWNodeTypeError, XWNodeValueError
18
+ from ...contracts import QueryMode, QueryTrait
19
+
20
+
21
+ class PigStrategy(AStructuredQueryStrategy):
22
+ """
23
+ Pig query strategy for Apache Pig Latin operations.
24
+
25
+ Supports:
26
+ - Pig Latin language
27
+ - LOAD, STORE, FILTER, FOREACH operations
28
+ - GROUP BY and COGROUP operations
29
+ - JOIN and UNION operations
30
+ - Built-in functions and UDFs
31
+ """
32
+
33
+ def __init__(self, **options):
34
+ super().__init__(**options)
35
+ self._mode = QueryMode.PIG
36
+ self._traits = QueryTrait.STRUCTURED | QueryTrait.ANALYTICAL | QueryTrait.BATCH
37
+
38
+ def execute(self, query: str, **kwargs) -> Any:
39
+ """Execute Pig query."""
40
+ if not self.validate_query(query):
41
+ raise XWNodeValueError(f"Invalid Pig query: {query}")
42
+
43
+ query_type = self._get_query_type(query)
44
+
45
+ if query_type == "LOAD":
46
+ return self._execute_load(query, **kwargs)
47
+ elif query_type == "STORE":
48
+ return self._execute_store(query, **kwargs)
49
+ elif query_type == "FILTER":
50
+ return self._execute_filter(query, **kwargs)
51
+ elif query_type == "FOREACH":
52
+ return self._execute_foreach(query, **kwargs)
53
+ else:
54
+ raise XWNodeValueError(f"Unsupported query type: {query_type}")
55
+
56
+ def validate_query(self, query: str) -> bool:
57
+ """Validate Pig query syntax."""
58
+ if not query or not isinstance(query, str):
59
+ return False
60
+
61
+ # Basic Pig validation
62
+ query = query.strip().upper()
63
+ valid_operations = ["LOAD", "STORE", "FILTER", "FOREACH", "GROUP", "COGROUP", "JOIN", "UNION", "SPLIT", "CROSS", "DISTINCT", "ORDER", "LIMIT", "SAMPLE", "PARALLEL", "REGISTER", "DEFINE", "IMPORT"]
64
+
65
+ for operation in valid_operations:
66
+ if query.startswith(operation):
67
+ return True
68
+
69
+ return False
70
+
71
+ def get_query_plan(self, query: str) -> Dict[str, Any]:
72
+ """Get Pig query execution plan."""
73
+ query_type = self._get_query_type(query)
74
+
75
+ return {
76
+ "query_type": query_type,
77
+ "operation": query_type,
78
+ "complexity": self._estimate_complexity(query),
79
+ "estimated_cost": self._estimate_cost(query),
80
+ "operations": self._extract_operations(query),
81
+ "optimization_hints": self._get_optimization_hints(query)
82
+ }
83
+
84
+ def select_query(self, table: str, columns: List[str], where_clause: str = None) -> Any:
85
+ """Execute SELECT query."""
86
+ query = f"data = LOAD '{table}' AS ({', '.join(columns)});"
87
+ if where_clause:
88
+ query += f" filtered = FILTER data BY {where_clause};"
89
+ query += f" result = FOREACH filtered GENERATE {', '.join(columns)};"
90
+ else:
91
+ query += f" result = FOREACH data GENERATE {', '.join(columns)};"
92
+
93
+ return self.execute(query)
94
+
95
+ def insert_query(self, table: str, data: Dict[str, Any]) -> Any:
96
+ """Execute INSERT query."""
97
+ # Pig doesn't support INSERT, use LOAD and STORE
98
+ query = f"data = LOAD '{table}'; new_data = LOAD 'new_data' AS ({', '.join(data.keys())}); combined = UNION data, new_data; STORE combined INTO '{table}';"
99
+ return self.execute(query)
100
+
101
+ def update_query(self, table: str, data: Dict[str, Any], where_clause: str = None) -> Any:
102
+ """Execute UPDATE query."""
103
+ # Pig doesn't support UPDATE, use FILTER and FOREACH
104
+ query = f"data = LOAD '{table}';"
105
+ if where_clause:
106
+ query += f" filtered = FILTER data BY {where_clause};"
107
+ query += f" updated = FOREACH filtered GENERATE {', '.join([f'{k} AS {k}' for k in data.keys()])};"
108
+ else:
109
+ query += f" updated = FOREACH data GENERATE {', '.join([f'{k} AS {k}' for k in data.keys()])};"
110
+
111
+ return self.execute(query)
112
+
113
+ def delete_query(self, table: str, where_clause: str = None) -> Any:
114
+ """Execute DELETE query."""
115
+ # Pig doesn't support DELETE, use FILTER
116
+ query = f"data = LOAD '{table}';"
117
+ if where_clause:
118
+ query += f" filtered = FILTER data BY NOT ({where_clause});"
119
+ else:
120
+ query += f" filtered = FILTER data BY false;"
121
+
122
+ return self.execute(query)
123
+
124
+ def join_query(self, tables: List[str], join_conditions: List[str]) -> Any:
125
+ """Execute JOIN query."""
126
+ if len(tables) < 2:
127
+ raise XWNodeValueError("JOIN requires at least 2 tables")
128
+
129
+ query = f"table1 = LOAD '{tables[0]}'; table2 = LOAD '{tables[1]}';"
130
+ query += f" joined = JOIN table1 BY {join_conditions[0]}, table2 BY {join_conditions[0]};"
131
+
132
+ return self.execute(query)
133
+
134
+ def aggregate_query(self, table: str, functions: List[str], group_by: List[str] = None) -> Any:
135
+ """Execute aggregate query."""
136
+ query = f"data = LOAD '{table}';"
137
+ if group_by:
138
+ query += f" grouped = GROUP data BY ({', '.join(group_by)});"
139
+ query += f" aggregated = FOREACH grouped GENERATE group, {', '.join(functions)};"
140
+ else:
141
+ query += f" aggregated = FOREACH data GENERATE {', '.join(functions)};"
142
+
143
+ return self.execute(query)
144
+
145
+ def _get_query_type(self, query: str) -> str:
146
+ """Extract query type from Pig query."""
147
+ query = query.strip().upper()
148
+ for operation in ["LOAD", "STORE", "FILTER", "FOREACH", "GROUP", "COGROUP", "JOIN", "UNION", "SPLIT", "CROSS", "DISTINCT", "ORDER", "LIMIT", "SAMPLE", "PARALLEL", "REGISTER", "DEFINE", "IMPORT"]:
149
+ if query.startswith(operation):
150
+ return operation
151
+ return "UNKNOWN"
152
+
153
+ def _execute_load(self, query: str, **kwargs) -> Any:
154
+ """Execute LOAD query."""
155
+ return {"result": "Pig LOAD executed", "query": query}
156
+
157
+ def _execute_store(self, query: str, **kwargs) -> Any:
158
+ """Execute STORE query."""
159
+ return {"result": "Pig STORE executed", "query": query}
160
+
161
+ def _execute_filter(self, query: str, **kwargs) -> Any:
162
+ """Execute FILTER query."""
163
+ return {"result": "Pig FILTER executed", "query": query}
164
+
165
+ def _execute_foreach(self, query: str, **kwargs) -> Any:
166
+ """Execute FOREACH query."""
167
+ return {"result": "Pig FOREACH executed", "query": query}
168
+
169
+ def _estimate_complexity(self, query: str) -> str:
170
+ """Estimate query complexity."""
171
+ operations = self._extract_operations(query)
172
+
173
+ if len(operations) > 8:
174
+ return "HIGH"
175
+ elif len(operations) > 4:
176
+ return "MEDIUM"
177
+ else:
178
+ return "LOW"
179
+
180
+ def _estimate_cost(self, query: str) -> int:
181
+ """Estimate query cost."""
182
+ complexity = self._estimate_complexity(query)
183
+ if complexity == "HIGH":
184
+ return 250 # Higher cost due to MapReduce
185
+ elif complexity == "MEDIUM":
186
+ return 125
187
+ else:
188
+ return 60
189
+
190
+ def _extract_operations(self, query: str) -> List[str]:
191
+ """Extract Pig operations from query."""
192
+ operations = []
193
+
194
+ pig_operations = ["LOAD", "STORE", "FILTER", "FOREACH", "GROUP", "COGROUP", "JOIN", "UNION", "SPLIT", "CROSS", "DISTINCT", "ORDER", "LIMIT", "SAMPLE", "PARALLEL", "REGISTER", "DEFINE", "IMPORT"]
195
+
196
+ for operation in pig_operations:
197
+ if operation in query.upper():
198
+ operations.append(operation)
199
+
200
+ return operations
201
+
202
+ def _get_optimization_hints(self, query: str) -> List[str]:
203
+ """Get query optimization hints."""
204
+ hints = []
205
+
206
+ if "GROUP" in query.upper():
207
+ hints.append("Consider using COGROUP for multiple group operations")
208
+
209
+ if "JOIN" in query.upper():
210
+ hints.append("Consider using replicated joins for small datasets")
211
+
212
+ if "FOREACH" in query.upper():
213
+ hints.append("Consider using nested FOREACH for complex transformations")
214
+
215
+ return hints