File size: 87,240 Bytes
5b7ed07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

from datetime import datetime, timedelta
from typing import Any
from unittest.mock import MagicMock, patch

import numpy as np
import pandas as pd
import pytest

from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType
from superset.common.chart_data_timing import QueryDataResult, QueryTiming
from superset.common.db_query_status import QueryStatus
from superset.common.query_context_processor import QueryContextProcessor
from superset.exceptions import QueryObjectValidationError
from superset.utils.core import GenericDataType
from superset.utils.date_parser import get_past_or_future


@pytest.fixture
def mock_query_context():
    with patch(
        "superset.common.query_context_processor.QueryContextProcessor"
    ) as mock_query_context_processor:
        yield mock_query_context_processor


def _query_timing() -> QueryTiming:
    return QueryTiming(
        query_planning_ns=0,
        cache_resolution_ns=0,
        data_acquisition_ns=None,
        payload_assembly_ns=0,
        total_ns=0,
    )


@pytest.fixture
def processor(mock_query_context):
    from superset.models.helpers import ExploreMixin

    mock_query_context.datasource.data = MagicMock()
    mock_query_context.datasource.data.get.return_value = {
        "col1": "Column 1",
        "col2": "Column 2",
    }

    # Create a processor instance
    processor = QueryContextProcessor(mock_query_context)

    # Setup datasource methods from ExploreMixin to be real methods
    # by binding them to the mock datasource
    processor._qc_datasource.is_valid_date_range = (
        ExploreMixin.is_valid_date_range.__get__(processor._qc_datasource)
    )
    processor._qc_datasource.is_valid_date = ExploreMixin.is_valid_date.__get__(
        processor._qc_datasource
    )
    processor._qc_datasource.get_offset_custom_or_inherit = (
        ExploreMixin.get_offset_custom_or_inherit.__get__(processor._qc_datasource)
    )
    processor._qc_datasource._get_temporal_column_for_filter = (
        ExploreMixin._get_temporal_column_for_filter.__get__(processor._qc_datasource)
    )
    processor._qc_datasource.join_offset_dfs = ExploreMixin.join_offset_dfs.__get__(
        processor._qc_datasource
    )
    processor._qc_datasource._determine_join_keys = (
        ExploreMixin._determine_join_keys.__get__(processor._qc_datasource)
    )
    processor._qc_datasource._process_date_range_offset = (
        ExploreMixin._process_date_range_offset.__get__(processor._qc_datasource)
    )
    processor._qc_datasource._perform_join = ExploreMixin._perform_join.__get__(
        processor._qc_datasource
    )
    processor._qc_datasource._apply_cleanup_logic = (
        ExploreMixin._apply_cleanup_logic.__get__(processor._qc_datasource)
    )
    processor._qc_datasource.add_offset_join_column = (
        ExploreMixin.add_offset_join_column.__get__(processor._qc_datasource)
    )

    return processor


def test_query_cache_key_binds_annotation_data_to_requesting_user(processor):
    """The cache key for annotated queries must differ per requesting user."""
    query_obj = MagicMock()
    query_obj.annotation_layers = [{"sourceType": "NATIVE", "name": "a", "value": 1}]
    with (
        patch(
            "superset.common.query_context_processor.get_user_id",
            side_effect=[1, 2],
        ),
        patch("superset.common.query_context_processor.security_manager"),
    ):
        processor.query_cache_key(query_obj)
        processor.query_cache_key(query_obj)
    contexts = [
        call.kwargs["annotation_context"] for call in query_obj.cache_key.call_args_list
    ]
    assert contexts[0] != contexts[1]


def test_get_data_table_like(processor, mock_query_context):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON

    result = processor.get_data(df, coltypes)
    expected = [
        {"col1": 1, "col2": "a"},
        {"col1": 2, "col2": "b"},
        {"col1": 3, "col2": "c"},
    ]
    assert result == expected


@patch("superset.common.query_context_processor.csv.df_to_escaped_csv")
def test_get_data_csv(mock_df_to_escaped_csv, processor, mock_query_context):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.CSV

    mock_df_to_escaped_csv.return_value = "col1,col2\n1,a\n2,b\n3,c\n"
    result = processor.get_data(df, coltypes)
    # CSV output is encoded to bytes using the CSV_EXPORT encoding so dashboard
    # chart exports honor the configured encoding (e.g. the utf-8-sig BOM).
    assert result == "col1,col2\n1,a\n2,b\n3,c\n".encode("utf-8-sig")
    mock_df_to_escaped_csv.assert_called_once_with(
        df, index=False, encoding="utf-8-sig"
    )


@patch("superset.common.query_context_processor.excel.df_to_excel")
@patch("superset.common.query_context_processor.excel.apply_column_types")
def test_get_data_xlsx(
    mock_apply_column_types, mock_df_to_excel, processor, mock_query_context
):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.XLSX

    mock_df_to_excel.return_value = b"binary data"
    result = processor.get_data(df, coltypes)
    assert result == b"binary data"
    mock_apply_column_types.assert_called_once_with(df, coltypes)
    mock_df_to_excel.assert_called_once_with(df, index=False)


def test_get_data_json(processor, mock_query_context):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON

    result = processor.get_data(df, coltypes)
    expected = [
        {"col1": 1, "col2": "a"},
        {"col1": 2, "col2": "b"},
        {"col1": 3, "col2": "c"},
    ]
    assert result == expected


def test_get_data_invalid_dataframe(processor, mock_query_context):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON

    with patch.object(df, "to_dict", side_effect=ValueError("Invalid DataFrame")):
        with pytest.raises(ValueError, match="Invalid DataFrame"):
            processor.get_data(df, coltypes)


def test_get_data_non_unique_columns(processor, mock_query_context):
    data = [[1, "a"], [2, "b"], [3, "c"]]
    df = pd.DataFrame(data, columns=["col1", "col1"])
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON

    with pytest.warns(
        UserWarning,
        match="DataFrame columns are not unique, some columns will be omitted",
    ):
        processor.get_data(df, coltypes)


def test_get_data_empty_dataframe_json(processor, mock_query_context):
    df = pd.DataFrame(columns=["col1", "col2"])
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON
    result = processor.get_data(df, coltypes)
    assert result == []


@patch("superset.common.query_context_processor.csv.df_to_escaped_csv")
def test_get_data_empty_dataframe_csv(
    mock_df_to_escaped_csv, processor, mock_query_context
):
    df = pd.DataFrame(columns=["col1", "col2"])
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.CSV
    mock_df_to_escaped_csv.return_value = "col1,col2\n"
    result = processor.get_data(df, coltypes)
    assert result == "col1,col2\n".encode("utf-8-sig")
    mock_df_to_escaped_csv.assert_called_once_with(
        df, index=False, encoding="utf-8-sig"
    )


@patch("superset.common.query_context_processor.excel.df_to_excel")
@patch("superset.common.query_context_processor.excel.apply_column_types")
def test_get_data_empty_dataframe_xlsx(
    mock_apply_column_types, mock_df_to_excel, processor, mock_query_context
):
    df = pd.DataFrame(columns=["col1", "col2"])
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.XLSX
    mock_df_to_excel.return_value = b"binary data empty"
    result = processor.get_data(df, coltypes)
    assert result == b"binary data empty"
    mock_apply_column_types.assert_called_once_with(df, coltypes)
    mock_df_to_excel.assert_called_once_with(df, index=False)


def test_get_data_nan_values_json(processor, mock_query_context):
    df = pd.DataFrame({"col1": [1, np.nan, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON
    result = processor.get_data(df, coltypes)
    assert result[0]["col1"] == 1
    assert pd.isna(result[1]["col1"])
    assert result[2]["col1"] == 3


def test_get_data_invalid_input(processor, mock_query_context):
    df = "not a dataframe"
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON
    with pytest.raises(AttributeError):
        processor.get_data(df, coltypes)


def test_get_data_default_format_when_result_format_is_none(
    processor, mock_query_context
):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = None
    result = processor.get_data(df, coltypes)
    expected = [
        {"col1": 1, "col2": "a"},
        {"col1": 2, "col2": "b"},
        {"col1": 3, "col2": "c"},
    ]
    assert result == expected


def fake_apply_column_types(df, coltypes):
    if len(coltypes) != len(df.columns):
        raise ValueError("Mismatch between column types and dataframe columns")
    return df


@patch("superset.common.query_context_processor.excel.df_to_excel")
@patch(
    "superset.common.query_context_processor.excel.apply_column_types",
    side_effect=fake_apply_column_types,
)
def test_get_data_invalid_coltypes_length_xlsx(
    mock_apply_column_types, mock_df_to_excel, processor, mock_query_context
):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC]  # Mismatched length
    mock_query_context.result_format = ChartDataResultFormat.XLSX
    with pytest.raises(
        ValueError, match="Mismatch between column types and dataframe columns"
    ):
        processor.get_data(df, coltypes)


def test_get_data_does_not_mutate_dataframe(processor, mock_query_context):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    original_df = df.copy(deep=True)
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.JSON
    _ = processor.get_data(df, coltypes)
    pd.testing.assert_frame_equal(df, original_df)


@patch(
    "superset.common.query_context_processor.excel.apply_column_types",
    side_effect=ValueError("Conversion error"),
)
def test_get_data_xlsx_apply_column_types_error(
    mock_apply_column_types, processor, mock_query_context
):
    df = pd.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
    coltypes = [GenericDataType.NUMERIC, GenericDataType.STRING]
    mock_query_context.result_format = ChartDataResultFormat.XLSX
    with pytest.raises(ValueError, match="Conversion error"):
        processor.get_data(df, coltypes)


def test_is_valid_date_range_format(processor):
    """Test that date range format validation works correctly."""
    # Should return True for valid date range format
    assert (
        processor._qc_datasource.is_valid_date_range("2023-01-01 : 2023-01-31") is True
    )
    assert (
        processor._qc_datasource.is_valid_date_range("2020-12-25 : 2020-12-31") is True
    )

    # Should return False for invalid format
    assert processor._qc_datasource.is_valid_date_range("1 day ago") is False
    assert processor._qc_datasource.is_valid_date_range("2023-01-01") is False
    assert processor._qc_datasource.is_valid_date_range("invalid") is False


def test_is_valid_date_range_static_format():
    """Test that static date range format validation works correctly."""
    from superset.models.helpers import ExploreMixin

    # Should return True for valid date range format
    assert ExploreMixin.is_valid_date_range_static("2023-01-01 : 2023-01-31") is True
    assert ExploreMixin.is_valid_date_range_static("2020-12-25 : 2020-12-31") is True

    # Should return False for invalid format
    assert ExploreMixin.is_valid_date_range_static("1 day ago") is False
    assert ExploreMixin.is_valid_date_range_static("2023-01-01") is False
    assert ExploreMixin.is_valid_date_range_static("invalid") is False


def test_processing_time_offsets_date_range_logic(processor):
    """Test that date range timeshift logic works correctly with feature flag checks."""
    from superset.models.helpers import ExploreMixin

    # Test that the date range validation works
    assert (
        processor._qc_datasource.is_valid_date_range("2023-01-01 : 2023-01-31") is True
    )
    assert processor._qc_datasource.is_valid_date_range("1 year ago") is False

    # Test that static method also works
    assert ExploreMixin.is_valid_date_range_static("2023-01-01 : 2023-01-31") is True
    assert ExploreMixin.is_valid_date_range_static("1 year ago") is False


def test_feature_flag_validation_logic():
    """Test that feature flag validation logic works as expected."""
    from superset.extensions import feature_flag_manager

    # This tests the concept - actual feature flag value depends on config
    # The important thing is that the code checks for DATE_RANGE_TIMESHIFTS_ENABLED
    flag_name = "DATE_RANGE_TIMESHIFTS_ENABLED"

    # Test that the feature flag is being checked
    # (This will vary based on actual config but tests the mechanism)
    result = feature_flag_manager.is_feature_enabled(flag_name)
    assert isinstance(result, bool)  # Should return a boolean


def test_join_offset_dfs_date_range_basic(processor):
    """Test basic join logic for date range offsets."""
    # Create simple test data
    main_df = pd.DataFrame({"dim1": ["A", "B", "C"], "metric1": [10, 20, 30]})

    offset_df = pd.DataFrame({"dim1": ["A", "B", "C"], "metric1": [5, 10, 15]})

    # Mock query context
    mock_query = MagicMock()
    mock_query.granularity = "date_col"
    processor._query_context.queries = [mock_query]

    # Test basic join with date range offset
    offset_dfs = {"2023-01-01 : 2023-01-31": offset_df}
    join_keys = ["dim1"]

    with patch("superset.models.helpers.feature_flag_manager") as mock_ff:
        mock_ff.is_feature_enabled.return_value = True
        with patch("superset.common.utils.dataframe_utils.left_join_df") as mock_join:
            mock_join.return_value = pd.DataFrame(
                {
                    "dim1": ["A", "B", "C"],
                    "metric1": [10, 20, 30],
                    "metric1 2023-01-01 : 2023-01-31": [5, 10, 15],
                }
            )

            result_df = processor._qc_datasource.join_offset_dfs(
                main_df, offset_dfs, time_grain=None, join_keys=join_keys
            )

            # Verify join was called
            mock_join.assert_called_once()
            assert len(result_df) == 3


def test_get_offset_custom_or_inherit_with_inherit(processor):
    """Test get_offset_custom_or_inherit with 'inherit' option."""
    from_dttm = pd.Timestamp("2024-01-01")
    to_dttm = pd.Timestamp("2024-01-10")

    result = processor._qc_datasource.get_offset_custom_or_inherit(
        "inherit", from_dttm, to_dttm
    )

    # Should shift back by the length of the range: 9 days, in seconds
    assert result == "777600 seconds ago"


@pytest.mark.parametrize(
    "hours",
    [
        # Sub-day ranges truncated to "0 days ago" and compared the range
        # against itself.
        1,
        12,
        # Ranges that are not a whole number of days truncated downwards and
        # overlapped the range they were compared against.
        36,
        # Whole-day ranges were already correct and must stay so.
        24,
        48,
    ],
)
def test_get_offset_custom_or_inherit_shifts_by_full_range(
    processor, hours: int
) -> None:
    """
    'inherit' compares a range against the period immediately preceding it,
    for any range length. The offset must not be truncated to whole days: it
    has to reproduce the range's exact duration, so that the comparison window
    neither overlaps the range nor leaves a gap before it.
    """
    from_dttm = datetime(2024, 3, 10, 6, 0)
    to_dttm = from_dttm + timedelta(hours=hours)

    offset = processor._qc_datasource.get_offset_custom_or_inherit(
        "inherit", from_dttm, to_dttm
    )

    shifted_from = get_past_or_future(offset, from_dttm)
    shifted_to = get_past_or_future(offset, to_dttm)
    assert shifted_to == from_dttm
    assert shifted_to - shifted_from == to_dttm - from_dttm


def test_get_offset_custom_or_inherit_with_date(processor):
    """Test get_offset_custom_or_inherit with specific date."""
    from_dttm = pd.Timestamp("2024-01-10")
    to_dttm = pd.Timestamp("2024-01-20")

    result = processor._qc_datasource.get_offset_custom_or_inherit(
        "2024-01-05", from_dttm, to_dttm
    )

    # Should return difference between from_dttm and the specified date
    assert result == "5 days ago"


def test_get_offset_custom_or_inherit_with_invalid_date(processor):
    """Test get_offset_custom_or_inherit with invalid date format."""
    from_dttm = pd.Timestamp("2024-01-10")
    to_dttm = pd.Timestamp("2024-01-20")

    result = processor._qc_datasource.get_offset_custom_or_inherit(
        "invalid-date", from_dttm, to_dttm
    )

    # Should return empty string for invalid format
    assert result == ""


def test_get_temporal_column_for_filter_with_granularity(processor):
    """Test _get_temporal_column_for_filter returns granularity when available."""
    query_object = MagicMock()
    query_object.granularity = "date_column"

    result = processor._qc_datasource._get_temporal_column_for_filter(
        query_object, "x_axis_col"
    )

    assert result == "date_column"


def test_get_temporal_column_for_filter_with_x_axis_fallback(processor):
    """Test _get_temporal_column_for_filter falls back to x_axis_label."""
    query_object = MagicMock()
    query_object.granularity = None

    result = processor._qc_datasource._get_temporal_column_for_filter(
        query_object, "x_axis_col"
    )

    assert result == "x_axis_col"


def test_get_temporal_column_for_filter_with_datasource_columns(processor):
    """Test _get_temporal_column_for_filter
    returns None when no clear temporal column."""
    query_object = MagicMock()
    query_object.granularity = None
    query_object.filter = []

    mock_datetime_col = MagicMock()
    mock_datetime_col.is_dttm = True
    mock_datetime_col.column_name = "created_at"

    mock_regular_col = MagicMock()
    mock_regular_col.is_dttm = False
    mock_regular_col.column_name = "name"

    processor._qc_datasource.columns = [mock_regular_col, mock_datetime_col]

    result = processor._qc_datasource._get_temporal_column_for_filter(
        query_object, None
    )

    assert result is None


def test_get_temporal_column_for_filter_prefers_granularity(processor):
    """Test _get_temporal_column_for_filter uses granularity when available."""
    query_object = MagicMock()
    query_object.granularity = "timestamp_col"
    query_object.filter = []

    mock_datetime_col = MagicMock()
    mock_datetime_col.is_dttm = True
    mock_datetime_col.name = "other_col"

    processor._qc_datasource.columns = [mock_datetime_col]

    result = processor._qc_datasource._get_temporal_column_for_filter(
        query_object, None
    )

    assert result == "timestamp_col"


def test_get_temporal_column_for_filter_no_columns_found(processor):
    """Test _get_temporal_column_for_filter
    returns None when no temporal column found."""
    query_object = MagicMock()
    query_object.granularity = None

    # Mock datasource with no datetime columns
    mock_regular_col = MagicMock()
    mock_regular_col.is_dttm = False
    mock_regular_col.column_name = "name"

    processor._qc_datasource.columns = [mock_regular_col]

    result = processor._qc_datasource._get_temporal_column_for_filter(
        query_object, None
    )

    assert result is None


def test_get_temporal_column_for_filter_no_datasource_columns(processor):
    """Test _get_temporal_column_for_filter handles datasource
    without columns attribute."""
    query_object = MagicMock()
    query_object.granularity = None

    # Remove columns attribute from datasource
    if hasattr(processor._qc_datasource, "columns"):
        delattr(processor._qc_datasource, "columns")

    result = processor._qc_datasource._get_temporal_column_for_filter(
        query_object, None
    )

    assert result is None


def test_processing_time_offsets_temporal_column_error(processor):
    """Test processing_time_offsets raises QueryObjectValidationError
    when temporal column can't be determined."""
    from superset.common.query_object import QueryObject
    from superset.exceptions import QueryObjectValidationError

    # Create a dataframe for testing
    df = pd.DataFrame({"dim1": ["A", "B", "C"], "metric1": [10, 20, 30]})

    # Create query object with date range offset and proper time range
    query_object = QueryObject(
        datasource=MagicMock(),
        granularity=None,  # No granularity set
        columns=[],
        is_timeseries=True,
        time_offsets=["2023-01-01 : 2023-01-31"],
        filter=[
            {
                "col": "some_date_col",
                "op": "TEMPORAL_RANGE",
                "val": "2024-01-01 : 2024-01-31",
            }
        ],
    )

    # Mock get_since_until_from_query_object to return valid dates
    with patch(
        "superset.common.utils.time_range_utils.get_since_until_from_query_object"
    ) as mock_dates:
        mock_dates.return_value = (
            pd.Timestamp("2024-01-01"),
            pd.Timestamp("2024-01-31"),
        )

        # Mock feature flag to be enabled
        with patch("superset.models.helpers.feature_flag_manager") as mock_ff:
            mock_ff.is_feature_enabled.return_value = True

            # Mock _get_temporal_column_for_filter to return None
            # (no temporal column found)
            with patch.object(
                processor._qc_datasource,
                "_get_temporal_column_for_filter",
                return_value=None,
            ):
                # Mock the datasource's processing_time_offsets to raise the error
                def raise_error(*args, **kwargs):
                    raise QueryObjectValidationError(
                        "Unable to identify temporal column for date "
                        "range time comparison."
                    )

                with patch.object(
                    processor._qc_datasource,
                    "processing_time_offsets",
                    side_effect=raise_error,
                ):
                    with pytest.raises(
                        QueryObjectValidationError,
                        match="Unable to identify temporal column",
                    ):
                        processor._qc_datasource.processing_time_offsets(
                            df, query_object, None, None, False
                        )


def test_processing_time_offsets_date_range_enabled(processor):
    """Test processing_time_offsets correctly handles
    date range offsets when enabled."""
    from superset.common.query_object import QueryObject

    # Create a dataframe for testing
    df = pd.DataFrame(
        {
            "dim1": ["A", "B", "C"],
            "metric1": [10, 20, 30],
            "__timestamp": pd.date_range("2023-01-01", periods=3, freq="D"),
        }
    )

    # Create a properly mocked datasource
    mock_datasource = MagicMock()
    mock_datasource.id = 123
    mock_datasource.uid = "abc123"
    mock_datasource.cache_timeout = None
    mock_datasource.changed_on = pd.Timestamp("2023-01-01")
    mock_datasource.get_extra_cache_keys.return_value = {}

    # Create query object with date range offset
    query_object = QueryObject(
        datasource=mock_datasource,
        granularity="date_col",
        columns=[],
        is_timeseries=True,
        time_offsets=["2022-01-01 : 2022-01-31"],
        filter=[],
    )

    # Mock the query context and its methods
    processor._query_context.queries = [query_object]

    with patch("superset.models.helpers.feature_flag_manager") as mock_ff:
        mock_ff.is_feature_enabled.return_value = True

        with patch(
            "superset.utils.core.get_base_axis_labels",
            return_value=["__timestamp"],
        ):
            with patch(
                "superset.common.utils.time_range_utils.get_since_until_from_query_object"
            ) as mock_dates:
                mock_dates.return_value = (
                    pd.Timestamp("2023-01-01"),
                    pd.Timestamp("2023-01-03"),
                )

                with patch(
                    "superset.common.utils.time_range_utils.get_since_until_from_time_range"
                ) as mock_time_range:
                    mock_time_range.return_value = (
                        pd.Timestamp("2022-01-01"),
                        pd.Timestamp("2022-01-31"),
                    )

                    with patch.object(
                        processor, "get_query_result"
                    ) as mock_query_result:
                        mock_result = MagicMock()
                        mock_result.df = pd.DataFrame(
                            {
                                "dim1": ["A", "B"],
                                "metric1": [5, 10],
                                "__timestamp": pd.date_range(
                                    "2022-01-01", periods=2, freq="D"
                                ),
                            }
                        )
                        mock_result.query = "SELECT * FROM table"
                        mock_result.cache_key = "offset_cache_key"
                        mock_query_result.return_value = mock_result

                        # Mock the datasource's processing_time_offsets to
                        # return a proper result
                        mock_cached_result = {
                            "df": pd.DataFrame(
                                {
                                    "dim1": ["A", "B", "C"],
                                    "metric1": [10, 20, 30],
                                    "metric1 2022-01-01 : 2022-01-31": [5, 10, 15],
                                    "__timestamp": pd.date_range(
                                        "2023-01-01", periods=3, freq="D"
                                    ),
                                }
                            ),
                            "queries": ["SELECT * FROM table"],
                            "cache_keys": ["mock_cache_key"],
                        }

                        with patch.object(
                            processor._qc_datasource,
                            "processing_time_offsets",
                            return_value=mock_cached_result,
                        ):
                            # Test the method (call datasource method directly)
                            result = processor._qc_datasource.processing_time_offsets(
                                df, query_object, None, None, False
                            )

                            # Verify that the method completes successfully
                            assert "df" in result
                            assert "queries" in result
                            assert "cache_keys" in result

                            # Verify the result has the expected structure
                            assert isinstance(result["df"], pd.DataFrame)
                            assert isinstance(result["queries"], list)
                            assert isinstance(result["cache_keys"], list)


def test_processing_time_offsets_uses_chart_row_limit(processor):
    """Offset subquery inherits the chart's row_limit when one is set."""
    from superset.common.query_object import QueryObject
    from superset.models.helpers import ExploreMixin

    processor._qc_datasource.processing_time_offsets = (
        ExploreMixin.processing_time_offsets.__get__(processor._qc_datasource)
    )

    df = pd.DataFrame({"__timestamp": ["1990-01-01"], "sum__num": [100]})

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity="ds",
        columns=[],
        metrics=["sum__num"],
        is_timeseries=True,
        row_limit=100,
        row_offset=0,
        time_offsets=["1 year ago"],
        filters=[
            {
                "col": "ds",
                "op": "TEMPORAL_RANGE",
                "val": "1990-01-01 : 1991-01-01",
            }
        ],
    )

    captured: list[dict[str, Any]] = []

    def fake_query(dct: dict[str, Any]) -> MagicMock:
        captured.append(dct)
        result = MagicMock()
        result.df = pd.DataFrame()
        result.query = "SELECT 1"
        return result

    processor._qc_datasource.query = fake_query
    processor._qc_datasource.normalize_df = MagicMock(return_value=pd.DataFrame())

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("1990-01-01"), pd.Timestamp("1991-01-01")),
        ),
        patch(
            "superset.common.utils.query_cache_manager.QueryCacheManager"
        ) as mock_cache_manager,
        patch.object(
            processor._qc_datasource,
            "get_time_grain",
            return_value=None,
        ),
        patch.object(
            processor._qc_datasource,
            "join_offset_dfs",
            return_value=df,
        ),
    ):
        mock_cache = MagicMock()
        mock_cache.is_loaded = False
        mock_cache_manager.get.return_value = mock_cache

        processor._qc_datasource.processing_time_offsets(
            df, query_object, None, None, False
        )

    assert len(captured) == 1
    assert captured[0]["row_limit"] == 100
    assert captured[0]["row_offset"] == 0


def test_processing_time_offsets_row_offset_extends_window(processor):
    """Offset subquery limit covers the main query's window (row_limit + row_offset).

    When the chart has pagination (row_offset > 0), fetching only row_limit rows
    in the offset period would likely miss the dimensions present in the main
    query's page, yielding null comparison values. The subquery instead drops
    row_offset and widens row_limit to cover the full window.
    """
    from superset.common.query_object import QueryObject
    from superset.models.helpers import ExploreMixin

    processor._qc_datasource.processing_time_offsets = (
        ExploreMixin.processing_time_offsets.__get__(processor._qc_datasource)
    )

    df = pd.DataFrame({"__timestamp": ["1990-01-01"], "sum__num": [100]})

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity="ds",
        columns=[],
        metrics=["sum__num"],
        is_timeseries=True,
        row_limit=100,
        row_offset=10,
        time_offsets=["1 year ago"],
        filters=[
            {
                "col": "ds",
                "op": "TEMPORAL_RANGE",
                "val": "1990-01-01 : 1991-01-01",
            }
        ],
    )

    captured: list[dict[str, Any]] = []

    def fake_query(dct: dict[str, Any]) -> MagicMock:
        captured.append(dct)
        result = MagicMock()
        result.df = pd.DataFrame()
        result.query = "SELECT 1"
        return result

    processor._qc_datasource.query = fake_query
    processor._qc_datasource.normalize_df = MagicMock(return_value=pd.DataFrame())

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("1990-01-01"), pd.Timestamp("1991-01-01")),
        ),
        patch(
            "superset.common.utils.query_cache_manager.QueryCacheManager"
        ) as mock_cache_manager,
        patch.object(
            processor._qc_datasource,
            "get_time_grain",
            return_value=None,
        ),
        patch.object(
            processor._qc_datasource,
            "join_offset_dfs",
            return_value=df,
        ),
    ):
        mock_cache = MagicMock()
        mock_cache.is_loaded = False
        mock_cache_manager.get.return_value = mock_cache

        processor._qc_datasource.processing_time_offsets(
            df, query_object, None, None, False
        )

    assert len(captured) == 1
    assert captured[0]["row_limit"] == 110
    assert captured[0]["row_offset"] == 0


def test_processing_time_offsets_falls_back_to_config_row_limit(processor):
    """Offset subquery uses app config ROW_LIMIT when chart has offset but no limit."""
    from superset.common.query_object import QueryObject
    from superset.models.helpers import ExploreMixin

    processor._qc_datasource.processing_time_offsets = (
        ExploreMixin.processing_time_offsets.__get__(processor._qc_datasource)
    )

    df = pd.DataFrame({"__timestamp": ["1990-01-01"], "sum__num": [100]})

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity="ds",
        columns=[],
        metrics=["sum__num"],
        is_timeseries=True,
        row_limit=None,
        row_offset=10,
        time_offsets=["1 year ago"],
        filters=[
            {
                "col": "ds",
                "op": "TEMPORAL_RANGE",
                "val": "1990-01-01 : 1991-01-01",
            }
        ],
    )

    captured: list[dict[str, Any]] = []

    def fake_query(dct: dict[str, Any]) -> MagicMock:
        captured.append(dct)
        result = MagicMock()
        result.df = pd.DataFrame()
        result.query = "SELECT 1"
        return result

    processor._qc_datasource.query = fake_query
    processor._qc_datasource.normalize_df = MagicMock(return_value=pd.DataFrame())

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("1990-01-01"), pd.Timestamp("1991-01-01")),
        ),
        patch(
            "superset.common.utils.query_cache_manager.QueryCacheManager"
        ) as mock_cache_manager,
        patch.object(
            processor._qc_datasource,
            "get_time_grain",
            return_value=None,
        ),
        patch.object(
            processor._qc_datasource,
            "join_offset_dfs",
            return_value=df,
        ),
        patch("superset.models.helpers.app") as mock_app,
    ):
        mock_app.config = {"ROW_LIMIT": 4242}
        mock_cache = MagicMock()
        mock_cache.is_loaded = False
        mock_cache_manager.get.return_value = mock_cache

        processor._qc_datasource.processing_time_offsets(
            df, query_object, None, None, False
        )

    assert len(captured) == 1
    assert captured[0]["row_limit"] == 4242
    assert captured[0]["row_offset"] == 0


def test_processing_time_offsets_updates_temporal_filter_with_adhoc_x_axis(processor):
    """Offset query's TEMPORAL_RANGE filter must be shifted when the X-axis is
    an adhoc Custom SQL column whose label differs from the underlying time
    column. Previously the filter was matched against the X-axis label, which
    never equals the dataset column, so the filter stayed at the original
    range and the offset query AND'd both ranges together (empty intersection).
    """
    from superset.common.query_object import QueryObject
    from superset.models.helpers import ExploreMixin

    processor._qc_datasource.processing_time_offsets = (
        ExploreMixin.processing_time_offsets.__get__(processor._qc_datasource)
    )

    df = pd.DataFrame(
        {
            "wedding_date_cast": pd.to_datetime(["2025-01-01", "2025-02-01"]),
            "SUM(revenue)": [110, 120],
        }
    )

    adhoc_x_axis = {
        "label": "wedding_date_cast",
        "sqlExpression": "CAST(wedding_date AS TIMESTAMP)",
        "expressionType": "SQL",
        "columnType": "BASE_AXIS",
        "timeGrain": "P1M",
    }

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity=None,
        columns=[adhoc_x_axis],
        metrics=["SUM(revenue)"],
        is_timeseries=True,
        row_limit=10000,
        time_offsets=["1 year ago"],
        filters=[
            {
                "col": "wedding_date",
                "op": "TEMPORAL_RANGE",
                "val": "2025-01-01 : 2026-06-01",
            }
        ],
    )

    captured: list[dict[str, Any]] = []

    def fake_query(dct: dict[str, Any]) -> MagicMock:
        captured.append(dct)
        result = MagicMock()
        result.df = pd.DataFrame()
        result.query = "SELECT 1"
        return result

    processor._qc_datasource.query = fake_query
    processor._qc_datasource.normalize_df = MagicMock(return_value=pd.DataFrame())

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("2025-01-01"), pd.Timestamp("2026-06-01")),
        ),
        patch(
            "superset.common.utils.query_cache_manager.QueryCacheManager"
        ) as mock_cache_manager,
        patch.object(
            processor._qc_datasource,
            "get_time_grain",
            return_value="P1M",
        ),
        patch.object(
            processor._qc_datasource,
            "join_offset_dfs",
            return_value=df,
        ),
    ):
        mock_cache = MagicMock()
        mock_cache.is_loaded = False
        mock_cache_manager.get.return_value = mock_cache

        processor._qc_datasource.processing_time_offsets(
            df, query_object, None, None, False
        )

    assert len(captured) == 1
    temporal_filters = [
        flt for flt in captured[0]["filter"] if flt.get("op") == "TEMPORAL_RANGE"
    ]
    assert len(temporal_filters) == 1
    val = temporal_filters[0]["val"]
    assert "2024-01-01" in val, f"Expected shifted-from-dttm in val, got: {val!r}"
    assert "2025-06-01" in val, f"Expected shifted-to-dttm in val, got: {val!r}"


def test_processing_time_offsets_quarter_offset_shifts_query_window(
    processor: QueryContextProcessor,
) -> None:
    """A quarter offset must shift the offset query's window, not just the
    join keys. parsedatetime does not understand "1 quarter ago", so without
    rewriting quarters to months the offset subquery silently runs against
    the current period and the comparison series joins to nulls. The fake
    query below derives its rows from the requested window, so the join only
    yields the expected values when the window was actually shifted.
    """
    from superset.common.query_object import QueryObject
    from superset.models.helpers import ExploreMixin

    # The fixture's datasource is a MagicMock, not a real Explorable
    datasource: Any = processor._qc_datasource

    for method in (
        "processing_time_offsets",
        "_align_offset_without_time_grain",
        "_coalesce_offset_index",
    ):
        setattr(
            datasource,
            method,
            getattr(ExploreMixin, method).__get__(datasource),
        )

    df = pd.DataFrame(
        {
            "__timestamp": pd.to_datetime(["2024-04-01", "2024-05-01", "2024-06-01"]),
            "sum__num": [100, 200, 300],
        }
    )

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity="ds",
        columns=[],
        metrics=["sum__num"],
        is_timeseries=True,
        time_offsets=["1 quarter ago"],
        filters=[
            {
                "col": "ds",
                "op": "TEMPORAL_RANGE",
                "val": "2024-04-01 : 2024-07-01",
            }
        ],
    )

    captured: list[dict[str, Any]] = []

    def fake_query(dct: dict[str, Any]) -> MagicMock:
        captured.append(dct)
        result = MagicMock()
        result.df = pd.DataFrame(
            {
                "__timestamp": pd.date_range(
                    start=dct["from_dttm"], periods=3, freq="MS"
                ),
                "sum__num": [1.0, 2.0, 3.0],
            }
        )
        result.query = "SELECT 1"
        return result

    datasource.query = fake_query
    datasource.normalize_df = MagicMock(
        side_effect=lambda offset_df, _query_object: offset_df
    )

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("2024-04-01"), pd.Timestamp("2024-07-01")),
        ),
        patch(
            "superset.common.utils.query_cache_manager.QueryCacheManager"
        ) as mock_cache_manager,
        patch.object(
            datasource,
            "get_time_grain",
            return_value=None,
        ),
    ):
        mock_cache = MagicMock()
        mock_cache.is_loaded = False
        mock_cache_manager.get.return_value = mock_cache

        result = datasource.processing_time_offsets(df, query_object, None, None, False)

    assert len(captured) == 1
    assert captured[0]["from_dttm"] == pd.Timestamp("2024-01-01")
    assert captured[0]["to_dttm"] == pd.Timestamp("2024-04-01")
    assert result["df"]["sum__num__1 quarter ago"].tolist() == [1.0, 2.0, 3.0]


def test_processing_time_offsets_accepts_zero_shift_offset(
    processor: QueryContextProcessor,
) -> None:
    """An offset that legitimately parses to no shift (e.g. "0 days ago")
    must render a self-comparison instead of being rejected as
    uninterpretable: a zero delta is not a parse failure.
    """
    from superset.common.query_object import QueryObject
    from superset.models.helpers import ExploreMixin

    # The fixture's datasource is a MagicMock, not a real Explorable
    datasource: Any = processor._qc_datasource

    for method in (
        "processing_time_offsets",
        "_align_offset_without_time_grain",
        "_coalesce_offset_index",
    ):
        setattr(
            datasource,
            method,
            getattr(ExploreMixin, method).__get__(datasource),
        )

    df = pd.DataFrame(
        {
            "__timestamp": pd.to_datetime(["2024-04-01", "2024-05-01", "2024-06-01"]),
            "sum__num": [100, 200, 300],
        }
    )

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity="ds",
        columns=[],
        metrics=["sum__num"],
        is_timeseries=True,
        time_offsets=["0 days ago"],
        filters=[
            {
                "col": "ds",
                "op": "TEMPORAL_RANGE",
                "val": "2024-04-01 : 2024-07-01",
            }
        ],
    )

    captured: list[dict[str, Any]] = []

    def fake_query(dct: dict[str, Any]) -> MagicMock:
        captured.append(dct)
        result = MagicMock()
        result.df = pd.DataFrame(
            {
                "__timestamp": pd.date_range(
                    start=dct["from_dttm"], periods=3, freq="MS"
                ),
                "sum__num": [1.0, 2.0, 3.0],
            }
        )
        result.query = "SELECT 1"
        return result

    datasource.query = fake_query
    datasource.normalize_df = MagicMock(
        side_effect=lambda offset_df, _query_object: offset_df
    )

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("2024-04-01"), pd.Timestamp("2024-07-01")),
        ),
        patch(
            "superset.common.utils.query_cache_manager.QueryCacheManager"
        ) as mock_cache_manager,
        patch.object(
            datasource,
            "get_time_grain",
            return_value=None,
        ),
    ):
        mock_cache = MagicMock()
        mock_cache.is_loaded = False
        mock_cache_manager.get.return_value = mock_cache

        result = datasource.processing_time_offsets(df, query_object, None, None, False)

    # The offset query runs against the unshifted window and its rows join
    # back onto the main series one-to-one
    assert len(captured) == 1
    assert captured[0]["from_dttm"] == pd.Timestamp("2024-04-01")
    assert captured[0]["to_dttm"] == pd.Timestamp("2024-07-01")
    assert result["df"]["sum__num__0 days ago"].tolist() == [1.0, 2.0, 3.0]


def test_processing_time_offsets_rejects_unparseable_offset(
    processor: QueryContextProcessor,
) -> None:
    """An offset no parser understands must fail with a validation error
    instead of querying an unshifted window and presenting the current
    period's rows as the comparison series.
    """
    from superset.common.query_object import QueryObject
    from superset.exceptions import QueryObjectValidationError
    from superset.models.helpers import ExploreMixin

    # The fixture's datasource is a MagicMock, not a real Explorable
    datasource: Any = processor._qc_datasource

    datasource.processing_time_offsets = ExploreMixin.processing_time_offsets.__get__(
        datasource
    )

    df = pd.DataFrame(
        {
            "__timestamp": pd.to_datetime(["2024-04-01"]),
            "sum__num": [100],
        }
    )

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity="ds",
        columns=[],
        metrics=["sum__num"],
        is_timeseries=True,
        time_offsets=["not a real offset"],
        filters=[
            {
                "col": "ds",
                "op": "TEMPORAL_RANGE",
                "val": "2024-04-01 : 2024-07-01",
            }
        ],
    )

    datasource.query = MagicMock()

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("2024-04-01"), pd.Timestamp("2024-07-01")),
        ),
        patch.object(
            datasource,
            "get_time_grain",
            return_value=None,
        ),
    ):
        with pytest.raises(
            QueryObjectValidationError, match="Unable to interpret the time offset"
        ):
            datasource.processing_time_offsets(df, query_object, None, None, False)

    datasource.query.assert_not_called()


def test_ensure_totals_available_updates_cache_values():
    """
    Test that ensure_totals_available() updates the query objects AND
    cache_values to keep them in sync.

    The issue was that ensure_totals_available() modified QueryObject instances
    (e.g., setting row_limit=None on totals queries and adding contribution_totals
    to post_processing), but cache_values still contained the original queries.
    This caused cache key mismatches between worker execution and cache fetch.
    """
    import pandas as pd

    from superset.common.query_object import QueryObject

    # Create a mock datasource
    mock_datasource = MagicMock()
    mock_datasource.uid = "test_datasource"
    mock_datasource.database.db_engine_spec.engine = "postgresql"
    mock_datasource.cache_timeout = None
    mock_datasource.changed_on = None

    # Create QueryObjects that would trigger ensure_totals_available logic
    # Query 1: Main query with contribution post-processing (needs totals)
    main_query = QueryObject(
        datasource=mock_datasource,
        columns=["brokerage"],
        metrics=["Net Amount In", "Amount Out", "Amount In"],
        row_limit=50000,
        orderby=[["Net Amount In", False]],
        post_processing=[
            {
                "operation": "contribution",
                "options": {
                    "columns": ["Amount In", "Amount Out"],
                    "rename_columns": ["%Amount In", "%Amount Out"],
                },
            }
        ],
    )

    # Query 2: Totals query (no columns, has metrics, no post-processing)
    totals_query = QueryObject(
        datasource=mock_datasource,
        columns=[],  # No columns = totals query
        metrics=["Net Amount In", "Amount Out", "Amount In"],
        row_limit=50000,
        post_processing=[],  # No post-processing
    )

    # Create mock query context
    mock_query_context = MagicMock()
    mock_query_context.force = False
    mock_query_context.datasource = mock_datasource
    mock_query_context.queries = [main_query, totals_query]
    mock_query_context.result_type = "full"
    mock_query_context.cache_values = {
        "datasource": {"type": "table", "id": 1},
        "queries": [
            # These are the original queries as they would be stored in cache_values
            {
                "columns": ["brokerage"],
                "metrics": ["Net Amount In", "Amount Out", "Amount In"],
                "row_limit": 50000,
                "orderby": [("Net Amount In", False)],
                "post_processing": [
                    {
                        "operation": "contribution",
                        "options": {
                            "columns": ["Amount In", "Amount Out"],
                            "rename_columns": ["%Amount In", "%Amount Out"],
                        },
                    }
                ],
            },
            {
                "columns": [],
                "metrics": ["Net Amount In", "Amount Out", "Amount In"],
                "row_limit": 50000,
                "post_processing": [],
            },
        ],
        "result_type": "full",
        "result_format": "json",
    }

    # Create processor
    processor = QueryContextProcessor(mock_query_context)
    processor._qc_datasource = mock_datasource

    # Mock the query execution result for totals query
    mock_query_result = MagicMock()
    mock_df = pd.DataFrame(
        {
            "Net Amount In": [20228060486.838825],
            "Amount Out": [-20543489614.980007],
            "Amount In": [40771550101.81883],
        }
    )
    mock_query_result.df = mock_df

    with patch.object(
        mock_query_context, "get_query_result", return_value=mock_query_result
    ):
        # Call ensure_totals_available
        processor.ensure_totals_available()

        # Now call get_payload which should update cache_values
        with patch(
            "superset.common.query_context_processor.get_query_results_with_timing"
        ) as mock_get_query_results_with_timing:
            # Mock the query results
            mock_query_results_response = [
                {
                    "data": [{"brokerage": "Test", "Net Amount In": 100}],
                    "query": "SELECT ...",
                }
            ]
            mock_get_query_results_with_timing.return_value = QueryDataResult(
                payload=mock_query_results_response[0],
                timing=_query_timing(),
            )

            # Mock cache manager to avoid actual caching
            with patch(
                "superset.common.query_context_processor.QueryCacheManager"
            ) as mock_cache_manager:
                mock_cache = MagicMock()
                mock_cache.is_loaded = True
                mock_cache.df = pd.DataFrame(
                    {"brokerage": ["Test"], "Net Amount In": [100]}
                )
                mock_cache.query = "SELECT ..."
                mock_cache.error_message = None
                mock_cache.status = "success"
                mock_cache_manager.get.return_value = mock_cache

                # This should update cache_values to match the modified queries
                processor.get_payload(cache_query_context=False)

    # Verify that cache_values has been updated to reflect the modifications
    updated_cache_queries = mock_query_context.cache_values["queries"]

    # Check that totals query has row_limit=None (modified by ensure_totals_available)
    assert updated_cache_queries[1]["row_limit"] is None, (
        "Expected totals query to have row_limit=None after ensure_totals_available, "
        f"but got: {updated_cache_queries[1]['row_limit']}"
    )

    # Check that the main query has contribution_totals in post_processing
    assert (
        "contribution_totals"
        in updated_cache_queries[0]["post_processing"][0]["options"]
    ), "Expected main query post_processing to have contribution_totals added"

    # Verify the contribution_totals match what we mocked
    expected_totals = {
        "Net Amount In": 20228060486.838825,
        "Amount Out": -20543489614.980007,
        "Amount In": 40771550101.81883,
    }
    assert (
        updated_cache_queries[0]["post_processing"][0]["options"]["contribution_totals"]
        == expected_totals
    )


def test_get_df_payload_validates_before_cache_key_generation():
    """
    Test that get_df_payload calls validate() before generating cache key.
    """
    from superset.common.query_object import QueryObject

    # Create a mock query context
    mock_query_context = MagicMock()
    mock_query_context.force = False
    mock_query_context.result_type = "full"

    # Create a mock datasource
    mock_datasource = MagicMock()
    mock_datasource.id = 123
    mock_datasource.uid = "test_datasource"
    mock_datasource.cache_timeout = None
    mock_datasource.database.db_engine_spec.engine = "postgresql"
    mock_datasource.database.extra = "{}"
    mock_datasource.get_extra_cache_keys.return_value = []
    mock_datasource.changed_on = None

    # Create processor
    processor = QueryContextProcessor(mock_query_context)
    processor._qc_datasource = mock_datasource

    # Create a query object with unsanitized where clause
    query_obj = QueryObject(
        datasource=mock_datasource,
        columns=["col1"],
        metrics=[],
        extras={"where": "(\n  col1 > 0\n)"},  # Unsanitized with newlines
    )

    # Track the order of calls
    call_order = []

    original_validate = query_obj.validate

    def mock_validate(*args, **kwargs):
        call_order.append("validate")
        # Update extras to simulate sanitization
        query_obj.extras["where"] = "(col1 > 0)"  # Sanitized, compact format
        return original_validate(*args, **kwargs)

    original_cache_key = query_obj.cache_key

    def mock_cache_key(*args, **kwargs):
        call_order.append("cache_key")
        # Verify that extras have been sanitized at this point
        assert query_obj.extras["where"] == "(col1 > 0)", (
            f"Expected sanitized clause in cache_key, got: {query_obj.extras['where']}"
        )
        return original_cache_key(*args, **kwargs)

    with patch.object(query_obj, "validate", side_effect=mock_validate):
        with patch.object(query_obj, "cache_key", side_effect=mock_cache_key):
            with patch(
                "superset.common.query_context_processor.QueryCacheManager"
            ) as mock_cache_manager:
                mock_cache = MagicMock()
                mock_cache.is_loaded = True
                mock_cache.df = pd.DataFrame({"col1": [1, 2, 3]})
                mock_cache.query = "SELECT * FROM table"
                mock_cache.error_message = None
                mock_cache.status = "success"
                mock_cache.bq_memory_limited = False
                mock_cache_manager.get.return_value = mock_cache

                # Call get_df_payload
                processor.get_df_payload(query_obj, force_cached=False)

    # Verify validate was called before cache_key
    assert call_order == ["validate", "cache_key"], (
        f"Expected validate to be called before cache_key, "
        f"but got call order: {call_order}"
    )


def test_cache_values_sync_after_ensure_totals_available():
    """
    Test that cache_values is synchronized with QueryObject modifications
    after ensure_totals_available() runs.

    This is a focused regression test for the cache key mismatch issue.
    It verifies that when ensure_totals_available() modifies QueryObject
    instances, those changes are reflected in cache_values before the
    QueryContext cache key is generated.
    """
    import pandas as pd

    from superset.common.query_object import QueryObject

    # Create a mock datasource
    mock_datasource = MagicMock()
    mock_datasource.uid = "test_datasource_456"
    mock_datasource.database.db_engine_spec.engine = "pinot"
    mock_datasource.cache_timeout = None
    mock_datasource.changed_on = None

    # Create two queries: one totals query and one main query with contribution
    totals_query = QueryObject(
        datasource=mock_datasource,
        columns=[],
        metrics=["sales"],
        row_limit=1000,
        post_processing=[],
    )

    main_query = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales"],
        row_limit=1000,
        post_processing=[{"operation": "contribution", "options": {}}],
    )

    # Create mock query context with initial cache_values
    mock_query_context = MagicMock()
    mock_query_context.force = False
    mock_query_context.datasource = mock_datasource
    mock_query_context.queries = [main_query, totals_query]
    mock_query_context.result_type = "full"
    mock_query_context.cache_values = {
        "datasource": {"type": "table", "id": 20},
        "queries": [
            {
                "columns": ["region"],
                "metrics": ["sales"],
                "row_limit": 1000,
                "post_processing": [{"operation": "contribution", "options": {}}],
            },
            {
                "columns": [],
                "metrics": ["sales"],
                "row_limit": 1000,
                "post_processing": [],
            },
        ],
        "result_type": "full",
        "result_format": "json",
    }

    # Create processor
    processor = QueryContextProcessor(mock_query_context)
    processor._qc_datasource = mock_datasource

    # Mock query execution result (totals query execution)
    mock_query_result = MagicMock()
    mock_df = pd.DataFrame({"sales": [1000.0]})
    mock_query_result.df = mock_df

    # Patch methods to isolate the test
    with patch.object(
        mock_query_context, "get_query_result", return_value=mock_query_result
    ):
        # Mock cache management to prevent actual caching
        with patch(
            "superset.common.query_context_processor.QueryCacheManager"
        ) as mock_cache_manager:
            mock_cache = MagicMock()
            mock_cache.is_loaded = True
            mock_cache.df = pd.DataFrame({"region": ["North"], "sales": [100]})
            mock_cache.query = "SELECT region, SUM(sales) FROM table GROUP BY region"
            mock_cache.error_message = None
            mock_cache.status = "success"
            mock_cache_manager.get.return_value = mock_cache

            # Mock the query results
            with patch(
                "superset.common.query_context_processor.get_query_results_with_timing"
            ) as mock_get_query_results_with_timing:
                mock_query_results_response = [
                    {
                        "data": [{"region": "North", "sales": 100}],
                        "query": "SELECT region, SUM(sales) FROM table GROUP BY region",
                    }
                ]
                mock_get_query_results_with_timing.return_value = QueryDataResult(
                    payload=mock_query_results_response[0],
                    timing=_query_timing(),
                )

                # Call get_payload - this internally calls ensure_totals_available()
                # and then should update cache_values
                processor.get_payload(cache_query_context=False)

    # Verify the fix: cache_values should now reflect the modifications
    updated_cache_queries = mock_query_context.cache_values["queries"]
    updated_totals_row_limit = updated_cache_queries[1]["row_limit"]

    # Before the fix: row_limit would remain 1000 in cache_values
    # After the fix: row_limit should be None (modified by
    # ensure_totals_available)
    assert updated_totals_row_limit is None, (
        "Expected row_limit to be None after ensure_totals_available, "
        f"but got: {updated_totals_row_limit}"
    )

    # Verify that contribution_totals was added to the main query
    assert (
        "contribution_totals"
        in updated_cache_queries[0]["post_processing"][0]["options"]
    )

    # Verify that the main query row_limit is still 1000 (only totals query
    # should be modified)
    assert updated_cache_queries[0]["row_limit"] == 1000


def test_cache_key_excludes_contribution_totals():
    """
    Test that cache_key() excludes contribution_totals from post_processing.

    contribution_totals is computed at runtime by ensure_totals_available() and
    varies per request. Including it in the cache key would cause mismatches
    between workers that compute different totals for the same query.
    """
    from superset.common.query_object import QueryObject

    mock_datasource = MagicMock()
    mock_datasource.uid = "test_datasource"
    mock_datasource.database.extra = "{}"
    mock_datasource.get_extra_cache_keys.return_value = []

    # Create query with contribution post-processing that includes contribution_totals
    query_with_totals = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales", "profit"],
        post_processing=[
            {
                "operation": "contribution",
                "options": {
                    "columns": ["sales", "profit"],
                    "rename_columns": ["%sales", "%profit"],
                    "contribution_totals": {"sales": 1000.0, "profit": 200.0},
                },
            }
        ],
    )

    # Create identical query without contribution_totals
    query_without_totals = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales", "profit"],
        post_processing=[
            {
                "operation": "contribution",
                "options": {
                    "columns": ["sales", "profit"],
                    "rename_columns": ["%sales", "%profit"],
                },
            }
        ],
    )

    # Cache keys should be identical since contribution_totals is excluded
    cache_key_with = query_with_totals.cache_key()
    cache_key_without = query_without_totals.cache_key()

    assert cache_key_with == cache_key_without, (
        "Cache keys should match regardless of contribution_totals. "
        f"With totals: {cache_key_with}, Without totals: {cache_key_without}"
    )


def test_cache_key_preserves_other_post_processing_options():
    """
    Test that cache_key() only excludes contribution_totals, not other options.
    """
    from superset.common.query_object import QueryObject

    mock_datasource = MagicMock()
    mock_datasource.uid = "test_datasource"
    mock_datasource.database.extra = "{}"
    mock_datasource.get_extra_cache_keys.return_value = []

    # Create query with contribution post-processing
    query1 = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales"],
        post_processing=[
            {
                "operation": "contribution",
                "options": {
                    "columns": ["sales"],
                    "rename_columns": ["%sales"],
                    "contribution_totals": {"sales": 1000.0},
                },
            }
        ],
    )

    # Create query with different rename_columns
    query2 = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales"],
        post_processing=[
            {
                "operation": "contribution",
                "options": {
                    "columns": ["sales"],
                    "rename_columns": ["%sales_pct"],  # Different!
                    "contribution_totals": {"sales": 1000.0},
                },
            }
        ],
    )

    # Cache keys should differ because rename_columns is different
    assert query1.cache_key() != query2.cache_key(), (
        "Cache keys should differ when other post_processing options differ"
    )


def test_cache_key_non_contribution_post_processing_unchanged():
    """
    Test that non-contribution post_processing operations are unchanged in cache key.
    """
    from superset.common.query_object import QueryObject

    mock_datasource = MagicMock()
    mock_datasource.uid = "test_datasource"
    mock_datasource.database.extra = "{}"
    mock_datasource.get_extra_cache_keys.return_value = []

    # Create query with non-contribution post-processing
    query1 = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales"],
        post_processing=[
            {
                "operation": "pivot",
                "options": {"columns": ["region"], "aggregates": {"sales": "sum"}},
            }
        ],
    )

    query2 = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales"],
        post_processing=[
            {
                "operation": "pivot",
                "options": {"columns": ["region"], "aggregates": {"sales": "mean"}},
            }
        ],
    )

    # Cache keys should differ because aggregates option is different
    assert query1.cache_key() != query2.cache_key(), (
        "Cache keys should differ for different non-contribution post_processing"
    )


def test_force_cached_normalizes_totals_query_row_limit():
    """
    When fetching from cache (force_cached=True), the totals query should still be
    normalized so its cache key matches the cached entry, but the totals query should
    not be executed.
    """
    from superset.common.query_object import QueryObject

    mock_datasource = MagicMock()
    mock_datasource.uid = "test_datasource"
    mock_datasource.column_names = ["region", "sales"]
    mock_datasource.cache_timeout = None
    mock_datasource.changed_on = None
    mock_datasource.get_extra_cache_keys.return_value = []
    mock_datasource.database.extra = "{}"
    mock_datasource.database.impersonate_user = False
    mock_datasource.database.db_engine_spec.get_impersonation_key.return_value = None

    totals_query = QueryObject(
        datasource=mock_datasource,
        columns=[],
        metrics=["sales"],
        row_limit=1000,
    )
    main_query = QueryObject(
        datasource=mock_datasource,
        columns=["region"],
        metrics=["sales"],
        row_limit=1000,
        post_processing=[{"operation": "contribution", "options": {}}],
    )

    totals_query.validate = MagicMock()
    main_query.validate = MagicMock()

    captured_limits: list[int | None] = []

    def totals_cache_key(**kwargs: Any) -> str:
        captured_limits.append(totals_query.row_limit)
        return "totals-cache-key"

    totals_query.cache_key = totals_cache_key
    main_query.cache_key = lambda **kwargs: "main-cache-key"

    mock_query_context = MagicMock()
    mock_query_context.force = False
    mock_query_context.datasource = mock_datasource
    mock_query_context.queries = [main_query, totals_query]
    mock_query_context.result_type = ChartDataResultType.FULL
    mock_query_context.result_format = ChartDataResultFormat.JSON
    mock_query_context.cache_values = {
        "queries": [main_query.to_dict(), totals_query.to_dict()]
    }
    mock_query_context.get_query_result = MagicMock()

    processor = QueryContextProcessor(mock_query_context)
    processor._qc_datasource = mock_datasource
    mock_query_context.get_df_payload = processor.get_df_payload
    mock_query_context.get_df_payload_result = processor.get_df_payload_result
    mock_query_context.get_data = processor.get_data

    with patch(
        "superset.common.query_context_processor.security_manager"
    ) as mock_security_manager:
        mock_security_manager.get_rls_cache_key.return_value = None

        with patch(
            "superset.common.query_context_processor.QueryCacheManager"
        ) as mock_cache_manager:

            def cache_get(*args: Any, **kwargs: Any) -> Any:
                df = pd.DataFrame({"region": ["North"], "sales": [100]})
                cache = MagicMock()
                cache.is_loaded = True
                cache.df = df
                cache.query = "SELECT 1"
                cache.error_message = None
                cache.status = QueryStatus.SUCCESS
                cache.applied_template_filters = []
                cache.applied_filter_columns = []
                cache.rejected_filter_columns = []
                cache.annotation_data = {}
                cache.is_cached = True
                cache.sql_rowcount = len(df)
                cache.cache_dttm = "2024-01-01T00:00:00"
                cache.bq_memory_limited = False
                return cache

            mock_cache_manager.get.side_effect = cache_get

            processor.get_payload(cache_query_context=False, force_cached=True)

    assert captured_limits == [None], "Totals query should be normalized before caching"
    mock_query_context.get_query_result.assert_not_called()


def test_get_df_payload_invalidates_cache_missing_applied_filter_columns():
    """
    Test that get_df_payload invalidates cache when cache is loaded but missing
    applied_filter_columns and query has filters.

    This ensures that old cache entries without applied_filter_columns are
    invalidated and fresh queries are executed to populate the field correctly.
    """
    from superset.common.query_object import QueryObject

    # Minimal setup
    mock_query_context = MagicMock()
    mock_query_context.force = False
    mock_datasource = MagicMock()
    mock_datasource.column_names = ["col1"]

    processor = QueryContextProcessor(mock_query_context)
    processor._qc_datasource = mock_datasource

    # Create query object with filters (note: `filters` kwarg, not `filter`)
    query_obj = QueryObject(
        datasource=mock_datasource,
        columns=["col1"],
        filters=[{"col": "col1", "op": "IN", "val": ["value1"]}],
    )

    # Simple cache class that tracks is_loaded changes
    class MockCache:
        def __init__(self):
            self.is_loaded = True
            self.applied_filter_columns = []  # Empty = missing
            self.df = pd.DataFrame()
            self.query = ""
            self.status = "success"
            self.cache_dttm = "2024-01-01T00:00:00"
            self.queried_dttm = "2024-01-01T00:00:00"
            self.stacktrace = None
            self.error_message = None
            self.is_cached = True
            self.sql_rowcount = 0
            self.cache_value = None
            self.cache_timeout = 3600
            self.datasource_uid = "test_datasource"
            self.applied_template_filters = []
            self.rejected_filter_columns = []
            self.annotation_data = {}
            self.bq_memory_limited = False
            self.bq_memory_limited_row_count = 0
            self.set_query_result = MagicMock()

    mock_cache = MockCache()

    with patch(
        "superset.common.query_context_processor.QueryCacheManager"
    ) as mock_cache_manager:
        mock_cache_manager.get.return_value = mock_cache

        # Prevent validate from doing any heavy work; it shouldn't modify filters
        with patch.object(query_obj, "validate", return_value=None):
            with patch.object(processor, "query_cache_key", return_value="key"):
                with patch.object(processor, "get_cache_timeout", return_value=3600):
                    # Call get_df_payload - should invalidate cache
                    processor.get_df_payload(query_obj, force_cached=False)

    # Verify cache was invalidated
    assert mock_cache.is_loaded is False, (
        "Cache should be inv when no applied_filter_columns and query has filters"
    )


def test_get_df_payload_bq_memory_limited_warning() -> None:
    """
    Test that get_df_payload includes a warning when BigQuery results are
    truncated due to the memory limit (g.bq_memory_limited is set).
    """
    from superset.common.query_object import QueryObject

    mock_query_context = MagicMock()
    mock_query_context.force = False
    mock_query_context.form_data = {"slice_id": 42}

    mock_datasource = MagicMock()
    mock_datasource.column_names = ["col1"]
    mock_datasource.uid = "test_ds"
    mock_datasource.cache_timeout = None
    mock_datasource.changed_on = None
    mock_datasource.get_extra_cache_keys.return_value = []
    mock_datasource.data = MagicMock()
    mock_datasource.data.get.return_value = {}

    processor = QueryContextProcessor(mock_query_context)
    processor._qc_datasource = mock_datasource

    query_obj = QueryObject(
        datasource=mock_datasource,
        columns=["col1"],
    )

    with patch(
        "superset.common.query_context_processor.QueryCacheManager"
    ) as mock_cache_manager:
        mock_cache = MagicMock()
        mock_cache.is_loaded = True
        mock_cache.df = pd.DataFrame({"col1": [1, 2, 3]})
        mock_cache.query = "SELECT col1 FROM table"
        mock_cache.error_message = None
        mock_cache.status = "success"
        mock_cache.applied_filter_columns = ["col1"]
        mock_cache.applied_template_filters = []
        mock_cache.rejected_filter_columns = []
        mock_cache.annotation_data = {}
        mock_cache.is_cached = True
        mock_cache.sql_rowcount = 3
        mock_cache.cache_dttm = "2024-01-01T00:00:00"
        mock_cache.queried_dttm = "2024-01-01T00:00:00"
        mock_cache.bq_memory_limited = True
        mock_cache.bq_memory_limited_row_count = 5000
        mock_cache_manager.get.return_value = mock_cache

        with patch.object(query_obj, "validate", return_value=None):
            with patch.object(processor, "query_cache_key", return_value="key"):
                with patch.object(processor, "get_cache_timeout", return_value=3600):
                    result = processor.get_df_payload(query_obj, force_cached=False)

    assert result["warning"] is not None
    assert "Chart 42" in result["warning"]
    assert "5,000 rows" in result["warning"]
    assert "memory constraints" in result["warning"]


def test_get_df_payload_no_warning_when_not_memory_limited() -> None:
    """
    Test that get_df_payload does not include a warning when BigQuery
    results were not truncated.
    """
    from superset.common.query_object import QueryObject

    mock_query_context = MagicMock()
    mock_query_context.force = False
    mock_query_context.form_data = {}

    mock_datasource = MagicMock()
    mock_datasource.column_names = ["col1"]
    mock_datasource.uid = "test_ds"
    mock_datasource.cache_timeout = None
    mock_datasource.changed_on = None
    mock_datasource.get_extra_cache_keys.return_value = []
    mock_datasource.data = MagicMock()
    mock_datasource.data.get.return_value = {}

    processor = QueryContextProcessor(mock_query_context)
    processor._qc_datasource = mock_datasource

    query_obj = QueryObject(
        datasource=mock_datasource,
        columns=["col1"],
    )

    with patch(
        "superset.common.query_context_processor.QueryCacheManager"
    ) as mock_cache_manager:
        mock_cache = MagicMock()
        mock_cache.is_loaded = True
        mock_cache.df = pd.DataFrame({"col1": [1, 2]})
        mock_cache.query = "SELECT col1 FROM table"
        mock_cache.error_message = None
        mock_cache.status = "success"
        mock_cache.applied_filter_columns = ["col1"]
        mock_cache.applied_template_filters = []
        mock_cache.rejected_filter_columns = []
        mock_cache.annotation_data = {}
        mock_cache.is_cached = True
        mock_cache.sql_rowcount = 2
        mock_cache.cache_dttm = "2024-01-01T00:00:00"
        mock_cache.queried_dttm = "2024-01-01T00:00:00"
        mock_cache.bq_memory_limited = False
        mock_cache_manager.get.return_value = mock_cache

        with patch.object(query_obj, "validate", return_value=None):
            with patch.object(processor, "query_cache_key", return_value="key"):
                with patch.object(processor, "get_cache_timeout", return_value=3600):
                    result = processor.get_df_payload(query_obj, force_cached=False)

    assert result["warning"] is None


def test_raise_for_access_evaluates_access_before_validate():
    """
    Access must be evaluated before the queries are validated, because query
    validation renders the request's filter expressions. When access is denied,
    no query is validated (so caller-supplied input is never rendered).
    """
    from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
    from superset.exceptions import SupersetSecurityException
    from superset.utils.core import DatasourceType

    query = MagicMock()
    query_context = MagicMock()
    query_context.queries = [query]
    query_context.datasource.type = DatasourceType.TABLE

    processor = QueryContextProcessor(query_context)

    denied = SupersetSecurityException(
        SupersetError(
            message="denied",
            error_type=SupersetErrorType.DATASOURCE_SECURITY_ACCESS_ERROR,
            level=ErrorLevel.ERROR,
        )
    )
    with patch(
        "superset.common.query_context_processor.security_manager.raise_for_access",
        side_effect=denied,
    ):
        with pytest.raises(SupersetSecurityException):
            processor.raise_for_access()

    query.validate.assert_not_called()


def test_grouping_sets_fallback_handles_adhoc_and_physical_columns() -> None:
    """
    The fallback used on engines without native GROUPING SETS support must
    build ``label_to_column`` from the same labels used to run each level's
    subquery, for both physical (string) and adhoc (dict) columns, and in the
    original column order. Otherwise an adhoc column ahead of a physical one
    in ``query_object.columns`` misaligns the mapping, and adhoc groupby
    columns referenced in ``grouping_sets`` are silently dropped.
    """
    import copy
    from datetime import timedelta

    from superset.common.query_object import QueryObject
    from superset.models.helpers import QueryResult
    from superset.superset_typing import AdhocColumn

    adhoc_col: AdhocColumn = {
        "sqlExpression": "DATE_TRUNC('month', dttm)",
        "label": "month",
    }
    mock_datasource = MagicMock()

    query_obj = QueryObject(
        datasource=mock_datasource,
        columns=[adhoc_col, "state"],
        grouping_sets=[["month", "state"], ["month"], []],
    )

    processor = QueryContextProcessor(MagicMock())
    processor._qc_datasource = mock_datasource

    captured_columns: list[list[Any]] = []

    def fake_get_query_result(sub_query: QueryObject) -> QueryResult:
        captured_columns.append(copy.copy(sub_query.columns))
        return QueryResult(
            df=pd.DataFrame({"metric": [1]}),
            query="SELECT 1",
            duration=timedelta(seconds=0),
        )

    mock_datasource.get_query_result.side_effect = fake_get_query_result

    processor._grouping_sets_fallback(query_obj)

    # Level ["month", "state"] must include both the adhoc and physical column,
    # each mapped to its own definition (not swapped).
    assert captured_columns[0] == [adhoc_col, "state"]
    # Level ["month"] must still include the adhoc column, not drop it.
    assert captured_columns[1] == [adhoc_col]
    # Grand total level has no groupby columns.
    assert captured_columns[2] == []


def test_grouping_sets_fallback_applies_row_offset_once_globally() -> None:
    """
    The native GROUPING SETS path applies `row_offset` exactly once, to the
    combined multi-level result (see the unconditional `qry.offset()` call in
    `models/helpers.py`). The fallback must match that: it must not apply the
    same offset independently to every per-level subquery, since that would
    apply it once per level (and can drop an entire low-row-count level, e.g.
    a single grand-total row, outright).
    """
    from datetime import timedelta

    from superset.common.query_object import QueryObject
    from superset.models.helpers import QueryResult

    mock_datasource = MagicMock()

    query_obj = QueryObject(
        datasource=mock_datasource,
        columns=["state"],
        grouping_sets=[["state"], []],
        row_offset=1,
    )

    processor = QueryContextProcessor(MagicMock())
    processor._qc_datasource = mock_datasource

    captured_offsets: list[int] = []

    # Each level returns 2 rows regardless of the (should-be-ignored) offset,
    # emulating a real datasource that would otherwise apply row_offset itself.
    def fake_get_query_result(sub_query: QueryObject) -> QueryResult:
        captured_offsets.append(sub_query.row_offset)
        return QueryResult(
            df=pd.DataFrame({"state": ["CA", "NY"]})
            if sub_query.columns
            else pd.DataFrame({"state": ["total"]}),
            query="SELECT 1",
            duration=timedelta(seconds=0),
        )

    mock_datasource.get_query_result.side_effect = fake_get_query_result

    result = processor._grouping_sets_fallback(query_obj)

    # Each per-level subquery must run unoffset...
    assert captured_offsets == [0, 0]
    # ...and the requested offset is applied exactly once, to the combined
    # result: 2 + 1 = 3 total rows in, minus an offset of 1 = 2 rows out.
    assert len(result.df) == 2


def test_relative_offset_preserves_inner_bounds(
    processor: QueryContextProcessor,
) -> None:
    """
    Regression test for #40501: Relative time comparison offset should
    preserve inner bounds as the original (unshifted) period, not the shifted one.

    When comparing 2026-05-01 : 2026-05-28 with offset "365 days ago":
    - inner_from_dttm should be 2026-05-01 (original), NOT 2025-05-01 (shifted)
    - inner_to_dttm should be 2026-05-28 (original), NOT 2025-05-28 (shifted)
    """
    from superset.common.query_object import QueryObject
    from superset.models.helpers import ExploreMixin

    datasource: Any = processor._qc_datasource

    for method in (
        "processing_time_offsets",
        "_align_offset_without_time_grain",
        "_coalesce_offset_index",
    ):
        setattr(
            datasource,
            method,
            getattr(ExploreMixin, method).__get__(datasource),
        )

    df = pd.DataFrame(
        {
            "__timestamp": pd.to_datetime(["2026-05-01", "2026-05-15", "2026-05-28"]),
            "sum__num": [100, 200, 300],
        }
    )

    query_object = QueryObject(
        datasource=MagicMock(),
        granularity="ds",
        columns=[],
        metrics=["sum__num"],
        is_timeseries=True,
        time_offsets=["365 days ago"],
        filters=[
            {
                "col": "ds",
                "op": "TEMPORAL_RANGE",
                "val": "2026-05-01 : 2026-05-28",
            }
        ],
    )

    captured: list[dict[str, Any]] = []

    def fake_query(dct: dict[str, Any]) -> MagicMock:
        captured.append(dct)
        result = MagicMock()
        result.df = pd.DataFrame(
            {
                "__timestamp": pd.date_range(
                    start=dct["from_dttm"], periods=3, freq="14D"
                ),
                "sum__num": [1.0, 2.0, 3.0],
            }
        )
        result.query = "SELECT 1"
        return result

    datasource.query = fake_query
    datasource.normalize_df = MagicMock(
        side_effect=lambda offset_df, _query_object: offset_df
    )

    with (
        patch(
            "superset.models.helpers.get_since_until_from_query_object",
            return_value=(pd.Timestamp("2026-05-01"), pd.Timestamp("2026-05-28")),
        ),
        patch(
            "superset.common.utils.query_cache_manager.QueryCacheManager"
        ) as mock_cache_manager,
        patch.object(
            datasource,
            "get_time_grain",
            return_value=None,
        ),
    ):
        mock_cache = MagicMock()
        mock_cache.is_loaded = False
        mock_cache_manager.get.return_value = mock_cache

        datasource.processing_time_offsets(df, query_object, None, None, False)

    # The offset query should use shifted dates for the main window
    assert len(captured) == 1
    assert captured[0]["from_dttm"] == pd.Timestamp("2025-05-01")
    assert captured[0]["to_dttm"] == pd.Timestamp("2025-05-28")

    # The inner bounds (used for series-limit subquery) should be the
    # ORIGINAL unshifted dates, not the shifted ones — this is the fix
    # for #40501. Without the fix, inner_from/to_dttm == shifted dates.
    assert captured[0]["inner_from_dttm"] == pd.Timestamp("2026-05-01")
    assert captured[0]["inner_to_dttm"] == pd.Timestamp("2026-05-28")


def test_get_native_annotation_data_requires_annotation_read_access():
    """Native annotation layers are only served to users who can read them."""
    query_obj = MagicMock()
    query_obj.annotation_layers = [{"sourceType": "NATIVE", "name": "a", "value": 1}]
    with (
        patch(
            "superset.common.query_context_processor.security_manager"
        ) as security_manager_mock,
        patch(
            "superset.common.query_context_processor.AnnotationLayerDAO.find_by_ids",
            return_value=[],
        ) as find_by_ids_mock,
    ):
        # ``can_access`` is synchronous; force a plain Mock so the patched
        # manager doesn't hand back a truthy coroutine that slips past the
        # ``not can_access(...)`` guard.
        security_manager_mock.can_access = MagicMock(return_value=False)
        with pytest.raises(QueryObjectValidationError):
            QueryContextProcessor.get_native_annotation_data(query_obj)
    security_manager_mock.can_access.assert_called_once_with("can_read", "Annotation")
    find_by_ids_mock.assert_not_called()