-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfql_utilities.py
More file actions
533 lines (411 loc) Β· 18.1 KB
/
fql_utilities.py
File metadata and controls
533 lines (411 loc) Β· 18.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
"""Example: Working with FQL (Fiddler Query Language) using fql utilities.
This script demonstrates how to use fiddler_utils.fql module to:
* Extract column references from FQL expressions
* Validate FQL syntax (quote matching, parentheses, etc.)
* Replace column names (useful for cross-model asset migration)
* Normalize expressions for comparison
* Detect FQL functions used
* Identify simple filters vs aggregations
* Split complex expressions
* Validate column references against model schemas
FQL is used in segments, custom metrics, and other Fiddler assets.
FQL Syntax Rules:
- Column names: Always in double quotes (e.g., "column_name")
- String values: Always in single quotes (e.g., 'value')
- Numeric values: No quotes (e.g., 42, 3.14)
- Boolean: true, false (lowercase, no quotes)
"""
import fiddler as fdl
from fiddler_utils import fql, get_or_init, SchemaValidator
# ============================================================================
# Configuration
# ============================================================================
# Fiddler instance (needed for schema validation examples)
FIDDLER_URL = 'https://your-instance.fiddler.ai'
FIDDLER_TOKEN = 'your_api_token'
PROJECT_NAME = 'my_project'
MODEL_NAME = 'my_model'
# ============================================================================
# Example 1: Extract Column References
# ============================================================================
def extract_columns_example():
"""Extract column names referenced in FQL expressions."""
print('=' * 70)
print('EXAMPLE 1: Extract Column References')
print('=' * 70)
# Simple filter expression
expr1 = '"age" > 30 and "geography" == \'California\''
columns1 = fql.extract_columns(expr1)
print(f'\nExpression: {expr1}')
print(f'Columns: {columns1}')
# Output: {'age', 'geography'}
# Custom metric with aggregation
expr2 = 'sum(if(fp(), 1, 0) * "transaction_value")'
columns2 = fql.extract_columns(expr2)
print(f'\nExpression: {expr2}')
print(f'Columns: {columns2}')
# Output: {'transaction_value'}
# Complex segment definition
expr3 = '("age" > 50 or "income" > 100000) and "status" == \'active\' and "region" in [\'US\', \'CA\']'
columns3 = fql.extract_columns(expr3)
print(f'\nExpression: {expr3}')
print(f'Columns: {columns3}')
# Output: {'age', 'income', 'status', 'region'}
# LLM enrichment reference
expr4 = '"pii_detection.has_pii" == true and "sentiment" < 0.3'
columns4 = fql.extract_columns(expr4)
print(f'\nExpression: {expr4}')
print(f'Columns: {columns4}')
# Output: {'pii_detection.has_pii', 'sentiment'}
# ============================================================================
# Example 2: Validate FQL Syntax
# ============================================================================
def validate_syntax_example():
"""Validate FQL syntax for common errors."""
print('\n' + '=' * 70)
print('EXAMPLE 2: Validate FQL Syntax')
print('=' * 70)
# Valid expression
valid_expr = '"age" > 30 and "status" == \'active\''
is_valid, error = fql.validate_fql_syntax(valid_expr)
print(f'\nExpression: {valid_expr}')
print(f'Valid: {is_valid}')
print(f'Error: {error}')
# Invalid: Unbalanced quotes
invalid_expr1 = '"age > 30' # Missing closing quote
is_valid, error = fql.validate_fql_syntax(invalid_expr1)
print(f'\nExpression: {invalid_expr1}')
print(f'Valid: {is_valid}')
print(f'Error: {error}')
# Invalid: Unbalanced parentheses
invalid_expr2 = 'sum(if(fp(), 1, 0)' # Missing closing paren
is_valid, error = fql.validate_fql_syntax(invalid_expr2)
print(f'\nExpression: {invalid_expr2}')
print(f'Valid: {is_valid}')
print(f'Error: {error}')
# Invalid: Empty column reference
invalid_expr3 = '"" > 30' # Empty column name
is_valid, error = fql.validate_fql_syntax(invalid_expr3)
print(f'\nExpression: {invalid_expr3}')
print(f'Valid: {is_valid}')
print(f'Error: {error}')
# Check multiple expressions
test_expressions = [
'"age" > 30'
'"status" == \'active\''
'sum("revenue")'
'("a" > 1', # Invalid
'"name == \'test\'', # Invalid
]
print(f'\n\nBatch validation:')
for expr in test_expressions:
is_valid, error = fql.validate_fql_syntax(expr)
status = 'β' if is_valid else 'β'
print(f'{status} {expr:40s} - {error or "OK"}')
# ============================================================================
# Example 3: Replace Column Names
# ============================================================================
def replace_column_names_example():
"""Replace column names in FQL expressions (for cross-model migration)."""
print('\n' + '=' * 70)
print('EXAMPLE 3: Replace Column Names')
print('=' * 70)
# Original expression
original = '"customer_age" > 30 and "customer_region" == \'US\''
print(f'\nOriginal: {original}')
# Define mapping (old name -> new name)
mapping = {
'customer_age': 'age'
'customer_region': 'geography'
}
# Apply mapping
replaced = fql.replace_column_names(original, mapping)
print(f'Mapping: {mapping}')
print(f'Result: {replaced}')
# Output: '"age" > 30 and "geography" == \'US\''
# Complex example with multiple occurrences
complex_expr = '"old_col" > 10 and ("old_col" < 50 or "status" == \'active\')'
print(f'\nOriginal: {complex_expr}')
complex_mapping = {'old_col': 'new_column'}
replaced_complex = fql.replace_column_names(complex_expr, complex_mapping)
print(f'Mapping: {complex_mapping}')
print(f'Result: {replaced_complex}')
# Output: '"new_column" > 10 and ("new_column" < 50 or "status" == \'active\')'
# Use case: Migrating segment from one model to another
print(f'\nπ‘ Use Case: Migrating assets between models with different column names')
segment_def = '"user_age" > 25 and "user_country" == \'USA\''
model_mapping = {
'user_age': 'customer_age'
'user_country': 'country_code'
}
migrated_def = fql.replace_column_names(segment_def, model_mapping)
print(f'Source model segment: {segment_def}')
print(f'Target model segment: {migrated_def}')
# ============================================================================
# Example 4: Normalize Expressions
# ============================================================================
def normalize_expression_example():
"""Normalize FQL expressions for comparison."""
print('\n' + '=' * 70)
print('EXAMPLE 4: Normalize Expressions')
print('=' * 70)
# Same expression with different whitespace
expr1 = '"age" > 30 and "status"== \'active\''
expr2 = '"age" > 30 and "status" == \'active\''
norm1 = fql.normalize_expression(expr1)
norm2 = fql.normalize_expression(expr2)
print(f'\nExpression 1: {expr1}')
print(f'Normalized: {norm1}')
print(f'\nExpression 2: {expr2}')
print(f'Normalized: {norm2}')
print(f'\nAre they equivalent? {norm1 == norm2}')
# Useful for comparing segments/metrics from different sources
segment_a = 'sum( if( fp( ) , 1 , 0 ) )'
segment_b = 'sum(if(fp(), 1, 0))'
norm_a = fql.normalize_expression(segment_a)
norm_b = fql.normalize_expression(segment_b)
print(f'\nπ‘ Use Case: Comparing definitions')
print(f'Segment A: {segment_a}')
print(f'Segment B: {segment_b}')
print(f'Normalized match: {norm_a == norm_b}')
# ============================================================================
# Example 5: Detect FQL Functions
# ============================================================================
def detect_functions_example():
"""Extract function names used in FQL expressions."""
print('\n' + '=' * 70)
print('EXAMPLE 5: Detect FQL Functions')
print('=' * 70)
# Simple aggregation
expr1 = 'sum("revenue")'
funcs1 = fql.get_fql_functions(expr1)
print(f'\nExpression: {expr1}')
print(f'Functions: {funcs1}')
# Nested functions
expr2 = 'sum(if(fp(), 1, 0) * "transaction_value")'
funcs2 = fql.get_fql_functions(expr2)
print(f'\nExpression: {expr2}')
print(f'Functions: {funcs2}')
# Output: {'sum', 'if', 'fp'}
# Complex custom metric
expr3 = 'avg(if(tn(), "processing_time", 0)) / max("processing_time")'
funcs3 = fql.get_fql_functions(expr3)
print(f'\nExpression: {expr3}')
print(f'Functions: {funcs3}')
# Output: {'avg', 'if', 'tn', 'max'}
# Check if specific functions are used
print(f'\nπ‘ Check for specific function usage:')
print(f'Uses fp()? {\'fp\' in funcs2}')
print(f'Uses aggregation? {bool({\'sum\', \'avg\', \'count\', \'max\', \'min\'} & funcs3)}')
# ============================================================================
# Example 6: Identify Simple Filters vs Aggregations
# ============================================================================
def identify_filter_type_example():
"""Distinguish between simple filters (segments) and aggregations (metrics)."""
print('\n' + '=' * 70)
print('EXAMPLE 6: Identify Filter Type')
print('=' * 70)
# Simple filter (can be used in segments)
filter1 = '"age" > 30 and "status" == \'active\''
is_simple1 = fql.is_simple_filter(filter1)
print(f'\nExpression: {filter1}')
print(f'Is simple filter: {is_simple1}')
print(f'Can be used in: {"Segment" if is_simple1 else "Custom Metric"}')
# Aggregation (must be custom metric)
filter2 = 'sum(if(fp(), 1, 0))'
is_simple2 = fql.is_simple_filter(filter2)
print(f'\nExpression: {filter2}')
print(f'Is simple filter: {is_simple2}')
print(f'Can be used in: {"Segment" if is_simple2 else "Custom Metric"}')
# Complex filter with logical operators (still simple)
filter3 = '("age" > 50 or "income" > 100000) and "region" == \'US\''
is_simple3 = fql.is_simple_filter(filter3)
print(f'\nExpression: {filter3}')
print(f'Is simple filter: {is_simple3}')
print(f'Can be used in: {"Segment" if is_simple3 else "Custom Metric"}')
# Batch check
expressions = [
('"age" > 30', 'Simple age filter')
('sum("revenue")', 'Revenue sum')
('"status" == \'active\'', 'Status filter')
('avg(if(fp(), 1, 0))', 'False positive rate')
('"price" > 100 and "quantity" < 5', 'Combined filter')
]
print(f'\n\nBatch classification:')
for expr, description in expressions:
is_simple = fql.is_simple_filter(expr)
asset_type = 'Segment' if is_simple else 'Custom Metric'
print(f'{asset_type:15s} - {description:30s} | {expr}')
# ============================================================================
# Example 7: Split Complex Expressions
# ============================================================================
def split_expression_example():
"""Split complex AND-conditions into parts."""
print('\n' + '=' * 70)
print('EXAMPLE 7: Split Complex Expressions')
print('=' * 70)
# Complex segment with multiple conditions
complex_segment = '"age" > 30 and "status" == \'active\' and "region" == \'US\''
parts = fql.split_fql_and_condition(complex_segment)
print(f'\nOriginal: {complex_segment}')
print(f'Split into {len(parts)} parts:')
for i, part in enumerate(parts, 1):
print(f' {i}. {part}')
# Very complex expression
very_complex = '("age" > 50 or "income" > 100000) and "status" == \'active\' and "region" in [\'US\', \'CA\'] and "verified" == true'
parts2 = fql.split_fql_and_condition(very_complex)
print(f'\nOriginal: {very_complex}')
print(f'Split into {len(parts2)} parts:')
for i, part in enumerate(parts2, 1):
print(f' {i}. {part}')
# Use case: Break down complex segment for documentation
print(f'\nπ‘ Use Case: Document complex segment logic')
for i, part in enumerate(parts, 1):
columns = fql.extract_columns(part)
print(f'Condition {i}: {part}')
print(f' Columns: {columns}')
# ============================================================================
# Example 8: Validate Column References Against Model Schema
# ============================================================================
def validate_against_schema_example():
"""Validate that FQL expressions reference valid model columns."""
print('\n' + '=' * 70)
print('EXAMPLE 8: Validate Against Model Schema')
print('=' * 70)
# Suppress verbose logs
# Connect to Fiddler
get_or_init(url=FIDDLER_URL, token=FIDDLER_TOKEN, log_level='ERROR')
# Get model
project = fdl.Project.from_name(PROJECT_NAME)
model = fdl.Model.from_name(MODEL_NAME, project_id=project.id)
# Get model columns for reference
model_columns = SchemaValidator.get_column_names(model)
print(f'\nModel: {model.name}')
print(f'Available columns: {len(model_columns)}')
print(f'Sample columns: {list(model_columns)[:5]}')
# Test expressions
expr1 = '"age" > 30 and "status" == \'active\'' # Adjust column names as needed
expr2 = '"age" > 30 and "nonexistent_column" == 1' # Has invalid column
# Validate first expression
print(f'\n--- Expression 1 ---')
print(f'Expression: {expr1}')
is_valid1, missing1 = fql.validate_column_references(expr1, model_columns)
if is_valid1:
print('β All columns are valid')
else:
print(f'β Missing columns: {missing1}')
# Validate second expression
print(f'\n--- Expression 2 ---')
print(f'Expression: {expr2}')
is_valid2, missing2 = fql.validate_column_references(expr2, model_columns)
if is_valid2:
print('β All columns are valid')
else:
print(f'β Missing columns: {missing2}')
# Alternative: Use SchemaValidator directly
print(f'\n--- Using SchemaValidator ---')
try:
is_valid3, missing3 = SchemaValidator.validate_fql_expression(
expr2, model, strict=False
)
print(f'Valid: {is_valid3}')
print(f'Missing: {missing3}')
except Exception as e:
print(f'Validation error: {e}')
# ============================================================================
# Example 9: Complete Workflow - Migrate Segment
# ============================================================================
def complete_workflow_example():
"""Complete workflow: Extract, validate, transform, and re-validate FQL."""
print('\n' + '=' * 70)
print('EXAMPLE 9: Complete Workflow - Migrate Segment')
print('=' * 70)
# Source segment definition
source_segment = '"customer_age" > 30 and "customer_region" == \'California\''
print(f'Source segment definition:')
print(f' {source_segment}')
# Step 1: Validate syntax
print(f'\n[Step 1] Validate syntax...')
is_valid, error = fql.validate_fql_syntax(source_segment)
if not is_valid:
print(f' β Invalid syntax: {error}')
return
print(f' β Syntax valid')
# Step 2: Extract columns
print(f'\n[Step 2] Extract columns...')
columns = fql.extract_columns(source_segment)
print(f' Columns: {columns}')
# Step 3: Check if it's a simple filter
print(f'\n[Step 3] Check filter type...')
is_simple = fql.is_simple_filter(source_segment)
if not is_simple:
print(f' β οΈ This is an aggregation - must be a custom metric')
else:
print(f' β Simple filter - can be used as segment')
# Step 4: Map columns to target model
print(f'\n[Step 4] Map columns to target model...')
column_mapping = {
'customer_age': 'age'
'customer_region': 'geography'
}
print(f' Mapping: {column_mapping}')
target_segment = fql.replace_column_names(source_segment, column_mapping)
print(f' Target segment: {target_segment}')
# Step 5: Validate against target model columns (simulated)
print(f'\n[Step 5] Validate against target model...')
target_columns = {'age', 'geography', 'income', 'status'} # Simulated
target_segment_columns = fql.extract_columns(target_segment)
is_valid_target, missing = fql.validate_column_references(
target_segment, target_columns
)
if is_valid_target:
print(f' β All columns exist in target model')
print(f' β
Ready to import!')
else:
print(f' β Missing columns in target: {missing}')
print(f' β Cannot import - fix schema first')
# Summary
print(f'\n--- Migration Summary ---')
print(f'Source: {source_segment}')
print(f'Target: {target_segment}')
print(f'Status: {"β
Ready" if is_valid_target else "β Blocked"}')
# ============================================================================
# Main
# ============================================================================
def main():
"""Run all FQL utility examples."""
print('\n')
print('β' + '=' * 68 + 'β')
print('β' + ' ' * 24 + 'FQL UTILITIES EXAMPLES' + ' ' * 22 + 'β')
print('β' + '=' * 68 + 'β')
# Example 1: Extract columns
extract_columns_example()
# Example 2: Validate syntax
validate_syntax_example()
# Example 3: Replace column names
replace_column_names_example()
# Example 4: Normalize expressions
normalize_expression_example()
# Example 5: Detect functions
detect_functions_example()
# Example 6: Identify filter type
identify_filter_type_example()
# Example 7: Split expressions
split_expression_example()
# Example 8: Validate against schema (requires connection)
# validate_against_schema_example()
# Example 9: Complete workflow
# complete_workflow_example()
print('\n' + '=' * 70)
print('IMPORTANT NOTES:')
print('=' * 70)
print('* FQL column names must be in double quotes: "column_name"')
print('* FQL string values must be in single quotes: \'value\'')
print('* Use extract_columns() to find all column references')
print('* Use validate_fql_syntax() to catch basic syntax errors')
print('* Use replace_column_names() for cross-model asset migration')
print('* Use is_simple_filter() to determine if expression is a segment or metric')
print('* Always validate against model schema before importing assets')
print('=' * 70)
if __name__ == '__main__':
main()