-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathexample_rewrite_engine.py
More file actions
553 lines (446 loc) · 16.1 KB
/
Copy pathexample_rewrite_engine.py
File metadata and controls
553 lines (446 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
"""Examples demonstrating the RewriteEngine for py3plex Graph Programs.
This script shows:
1. Basic rewrite application
2. Individual rule demonstrations
3. Provenance tracking
4. Context-aware optimization
5. Custom rule sets
6. Integration with GraphProgram.optimize()
"""
from py3plex.dsl.ast import (
ComputeItem,
Comparison,
ConditionAtom,
ConditionExpr,
OrderItem,
Query,
SelectStmt,
Target,
UQConfig,
AutoCommunityConfig,
ExportSpec,
)
from py3plex.dsl.program import (
GraphProgram,
RewriteEngine,
RewriteContext,
apply_rewrites,
get_standard_rules,
get_conservative_rules,
get_aggressive_rules,
)
def example_1_basic_rewrites():
"""Example 1: Basic rewrite application."""
print("=" * 60)
print("Example 1: Basic Rewrite Application")
print("=" * 60)
# Create a query: COMPUTE(degree, betweenness) WHERE(layer="social")
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[
ComputeItem(name="degree"),
ComputeItem(name="betweenness_centrality"),
],
where=ConditionExpr(
atoms=[ConditionAtom(comparison=Comparison(left="layer", op="=", right="social"))]
),
)
)
# Create program
program = GraphProgram.from_ast(query)
print(f"\nOriginal program hash: {program.hash()[:16]}...")
print(f"Provenance: {program.metadata.provenance_chain}")
# Apply rewrites
optimized = apply_rewrites(program)
print(f"\nOptimized program hash: {optimized.hash()[:16]}...")
print(f"Provenance: {optimized.metadata.provenance_chain}")
# Check if rewrites were applied
if program.hash() != optimized.hash():
print("\nOK Program was optimized!")
else:
print("\nOK No optimizations needed (already optimal)")
print()
def example_2_pushdown_rules():
"""Example 2: Pushdown optimization rules."""
print("=" * 60)
print("Example 2: Pushdown Rules (WHERE before COMPUTE)")
print("=" * 60)
# Create query where WHERE can be pushed past COMPUTE
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[
ComputeItem(name="degree"),
ComputeItem(name="clustering"),
ComputeItem(name="betweenness_centrality"),
],
where=ConditionExpr(
atoms=[
ConditionAtom(comparison=Comparison(left="layer", op="=", right="social")),
ConditionAtom(comparison=Comparison(left="type", op="=", right="person")),
],
ops=["AND"]
),
)
)
program = GraphProgram.from_ast(query)
# Apply rewrites
optimized = apply_rewrites(program)
print("\nOriginal query:")
print(" SELECT nodes COMPUTE(degree, clustering, betweenness)")
print(" WHERE layer='social' AND type='person'")
print("\nOptimized query (conceptually):")
print(" SELECT nodes WHERE layer='social' AND type='person'")
print(" COMPUTE(degree, clustering, betweenness)")
print(" -> Fewer nodes to compute metrics for!")
print(f"\nProvenance chain shows applied rules:")
for step in optimized.metadata.provenance_chain:
print(f" - {step}")
print()
def example_3_projection_pushdown():
"""Example 3: Projection pushdown to eliminate unused metrics."""
print("=" * 60)
print("Example 3: Projection Pushdown")
print("=" * 60)
# Create query that computes more than it needs
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[
ComputeItem(name="degree"),
ComputeItem(name="betweenness_centrality"),
ComputeItem(name="closeness_centrality"),
ComputeItem(name="clustering"),
],
select_cols=["node", "degree", "betweenness_centrality"],
)
)
program = GraphProgram.from_ast(query)
print("\nOriginal query computes 4 metrics:")
print(" COMPUTE(degree, betweenness, closeness, clustering)")
print(" SELECT_COLS(node, degree, betweenness)")
# Apply rewrites
optimized = apply_rewrites(program)
print("\nOptimized query only computes what's needed:")
print(" COMPUTE(degree, betweenness)")
print(" SELECT_COLS(node, degree, betweenness)")
print(" -> Closeness and clustering eliminated!")
# Check compute items in optimized
compute_names = [c.name for c in optimized.canonical_ast.select.compute]
print(f"\nCompute items after optimization: {compute_names}")
print()
def example_4_layer_distributivity():
"""Example 4: Layer distributivity for parallel processing."""
print("=" * 60)
print("Example 4: Layer Distributivity")
print("=" * 60)
# Create query with PER_LAYER and layer-local metrics
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[
ComputeItem(name="degree"),
ComputeItem(name="clustering"),
],
group_by=['layer'],
)
)
program = GraphProgram.from_ast(query)
print("\nQuery with layer-local metrics:")
print(" SELECT nodes")
print(" COMPUTE(degree, clustering)")
print(" PER_LAYER()")
# Apply rewrites
optimized = apply_rewrites(program)
print("\nOptimization recognizes layer-local metrics:")
print(" PER_LAYER() can be moved early for parallelization")
print(" -> Each layer processed independently!")
print()
def example_5_uq_aware_rewrites():
"""Example 5: UQ-aware optimizations."""
print("=" * 60)
print("Example 5: UQ-Aware Rewrites")
print("=" * 60)
# Create query with UQ and deterministic filters
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[ComputeItem(name="degree")],
uq_config=UQConfig(method="bootstrap", n_samples=100),
where=ConditionExpr(
atoms=[ConditionAtom(comparison=Comparison(left="layer", op="=", right="social"))]
),
)
)
program = GraphProgram.from_ast(query)
print("\nOriginal query with UQ:")
print(" UQ(COMPUTE(degree)) WHERE layer='social'")
print(" -> Filter applied after 100 bootstrap samples")
# Apply rewrites
optimized = apply_rewrites(program)
print("\nOptimized query moves filter inside UQ:")
print(" UQ(WHERE layer='social' COMPUTE(degree))")
print(" -> Filter applied before sampling, reducing cost!")
print()
def example_6_community_optimization():
"""Example 6: Community-specific optimizations."""
print("=" * 60)
print("Example 6: Community Optimizations")
print("=" * 60)
# Create query for communities with single ID filter
query = Query(
explain=False,
select=SelectStmt(
target=Target.COMMUNITIES,
where=ConditionExpr(
atoms=[ConditionAtom(comparison=Comparison(left="community_id", op="=", right=5))]
),
)
)
program = GraphProgram.from_ast(query)
print("\nOriginal query:")
print(" SELECT communities WHERE community_id = 5")
print(" -> Full community detection + filtering")
# Apply rewrites
optimized = apply_rewrites(program)
print("\nOptimized query uses partition slice:")
print(" PARTITION_SLICE(community_id=5)")
print(" -> Direct access to single community!")
print()
def example_7_cse_caching():
"""Example 7: Common subexpression elimination and caching."""
print("=" * 60)
print("Example 7: CSE and Caching")
print("=" * 60)
# Create query that uses 'degree' multiple times
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[ComputeItem(name="degree")],
where=ConditionExpr(
atoms=[ConditionAtom(comparison=Comparison(left="degree", op=">", right=5))]
),
order_by=[OrderItem(key="degree", desc=True)],
)
)
program = GraphProgram.from_ast(query)
print("\nOriginal query uses 'degree' 3 times:")
print(" COMPUTE(degree)")
print(" WHERE degree > 5")
print(" ORDER_BY degree DESC")
# Apply rewrites
optimized = apply_rewrites(program)
print("\nOptimized query marks degree for caching:")
print(" COMPUTE(degree) [cache=True]")
print(" WHERE degree > 5")
print(" ORDER_BY degree DESC")
print(" -> Degree computed once and reused!")
print()
def example_8_top_k_optimization():
"""Example 8: TOP-K optimization."""
print("=" * 60)
print("Example 8: TOP-K Optimization")
print("=" * 60)
# Create query: ORDER_BY + LIMIT (classic TOP-K pattern)
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[ComputeItem(name="betweenness_centrality")],
order_by=[OrderItem(key="betweenness_centrality", desc=True)],
limit=10,
)
)
program = GraphProgram.from_ast(query)
print("\nOriginal query:")
print(" COMPUTE(betweenness)")
print(" ORDER_BY betweenness DESC")
print(" LIMIT 10")
print(" -> Full sort O(n log n)")
# Apply rewrites with context
context = RewriteContext(network_stats={'node_count': 10000})
optimized = apply_rewrites(program, context=context)
print("\nOptimized query uses heap-based TOP-K:")
print(" COMPUTE(betweenness)")
print(" TOP_K(betweenness, 10)")
print(" -> Heap-based selection O(n log k)")
print(" -> Much faster for k << n!")
print()
def example_9_custom_context():
"""Example 9: Context-aware optimization."""
print("=" * 60)
print("Example 9: Context-Aware Optimization")
print("=" * 60)
# Create query with expensive metric
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[ComputeItem(name="betweenness_centrality")],
)
)
program = GraphProgram.from_ast(query)
print("\nQuery with expensive metric:")
print(" COMPUTE(betweenness_centrality)")
# Create context with network statistics
context = RewriteContext(
network_stats={
'node_count': 10000,
'edge_count': 50000,
'layer_count': 3,
},
safety_mode=False,
)
print("\nContext provided:")
print(f" Nodes: {context.network_stats['node_count']}")
print(f" Edges: {context.network_stats['edge_count']}")
print(f" Layers: {context.network_stats['layer_count']}")
# Apply rewrites with context
optimized = apply_rewrites(program, context=context)
print("\nOptimized with context:")
print(" Betweenness marked for aggressive caching")
print(" -> Expensive metrics get special treatment!")
print()
def example_10_rule_sets():
"""Example 10: Different rule sets."""
print("=" * 60)
print("Example 10: Conservative vs. Aggressive Rules")
print("=" * 60)
# Create complex query
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[
ComputeItem(name="degree"),
ComputeItem(name="betweenness_centrality"),
ComputeItem(name="clustering"),
],
where=ConditionExpr(
atoms=[ConditionAtom(comparison=Comparison(left="layer", op="=", right="social"))]
),
group_by=['layer'],
)
)
program = GraphProgram.from_ast(query)
# Conservative optimization
print("\nConservative optimization:")
conservative_rules = get_conservative_rules()
print(f" Using {len(conservative_rules)} safe rules")
optimized_conservative = apply_rewrites(program, rules=conservative_rules)
# Aggressive optimization
print("\nAggressive optimization:")
aggressive_rules = get_aggressive_rules()
print(f" Using {len(aggressive_rules)} total rules")
optimized_aggressive = apply_rewrites(program, rules=aggressive_rules)
print("\nDifference in applied rewrites:")
conservative_chain = optimized_conservative.metadata.provenance_chain
aggressive_chain = optimized_aggressive.metadata.provenance_chain
print(f" Conservative: {len(conservative_chain)} steps")
print(f" Aggressive: {len(aggressive_chain)} steps")
print()
def example_11_explain_rewrites():
"""Example 11: Explaining which rewrites would apply."""
print("=" * 60)
print("Example 11: Explaining Applicable Rewrites")
print("=" * 60)
# Create query
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[
ComputeItem(name="degree"),
ComputeItem(name="clustering"),
],
where=ConditionExpr(
atoms=[ConditionAtom(comparison=Comparison(left="layer", op="=", right="social"))]
),
)
)
program = GraphProgram.from_ast(query)
print("\nQuery:")
print(" SELECT nodes")
print(" COMPUTE(degree, clustering)")
print(" WHERE layer='social'")
# Create engine and explain
engine = RewriteEngine(rules=get_standard_rules())
applicable = engine.explain_rewrites(program)
print(f"\nApplicable rewrites ({len(applicable)}):")
for rule_name in applicable:
print(f" - {rule_name}")
print()
def example_12_provenance_tracking():
"""Example 12: Detailed provenance tracking."""
print("=" * 60)
print("Example 12: Provenance Tracking")
print("=" * 60)
# Create query
query = Query(
explain=False,
select=SelectStmt(
target=Target.NODES,
compute=[
ComputeItem(name="degree"),
ComputeItem(name="betweenness_centrality"),
ComputeItem(name="clustering"),
],
where=ConditionExpr(
atoms=[ConditionAtom(comparison=Comparison(left="layer", op="=", right="social"))]
),
select_cols=["node", "degree"],
)
)
# Create program and track transformations
print("\nStep 1: Create original program")
program = GraphProgram.from_ast(query)
print(f" Hash: {program.hash()[:16]}...")
print(f" Provenance: {program.metadata.provenance_chain}")
# Apply rewrites
print("\nStep 2: Apply rewrites")
optimized = apply_rewrites(program)
print(f" Hash: {optimized.hash()[:16]}...")
print(f" Provenance: {optimized.metadata.provenance_chain}")
# Show full provenance chain
print("\nFull provenance chain:")
for i, step in enumerate(optimized.metadata.provenance_chain, 1):
print(f" {i}. {step}")
print()
def main():
"""Run all examples."""
print("\n" + "=" * 60)
print("RewriteEngine Examples for py3plex Graph Programs")
print("=" * 60 + "\n")
examples = [
example_1_basic_rewrites,
example_2_pushdown_rules,
example_3_projection_pushdown,
example_4_layer_distributivity,
example_5_uq_aware_rewrites,
example_6_community_optimization,
example_7_cse_caching,
example_8_top_k_optimization,
example_9_custom_context,
example_10_rule_sets,
example_11_explain_rewrites,
example_12_provenance_tracking,
]
for example in examples:
try:
example()
except Exception as e:
print(f"Error in {example.__name__}: {e}")
import traceback
traceback.print_exc()
print("\n" + "=" * 60)
print("All examples completed!")
print("=" * 60 + "\n")
if __name__ == '__main__':
main()