algeon-datafusion 0.1.2

DataFusion physical-plan adapter for native GPU execution.
Documentation
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
//! Named SQL argument contracts for the cuGraph function family.
//!
//! The table in this module is the adapter-owned name and shape contract. It
//! deliberately keeps native parameter defaults behind typed resolvers so the
//! metadata and binder surfaces do not copy numbers from the native defaults.

use std::fmt;

use serde_json::{Number, Value, json};

use super::{
    CUGRAPH_BETWEENNESS_CENTRALITY_FUNCTION, CUGRAPH_BFS_FUNCTION, CUGRAPH_CORE_NUMBER_FUNCTION,
    CUGRAPH_COSINE_FUNCTION, CUGRAPH_DEGREES_ALL_FUNCTION, CUGRAPH_ECG_FUNCTION,
    CUGRAPH_EDGE_BETWEENNESS_CENTRALITY_FUNCTION, CUGRAPH_EIGENVECTOR_CENTRALITY_FUNCTION,
    CUGRAPH_FORCE_ATLAS2_FUNCTION, CUGRAPH_HITS_FUNCTION, CUGRAPH_IN_DEGREES_ALL_FUNCTION, CUGRAPH_JACCARD_FUNCTION,
    CUGRAPH_K_CORE_FUNCTION, CUGRAPH_KATZ_CENTRALITY_FUNCTION, CUGRAPH_LEIDEN_FUNCTION, CUGRAPH_LOUVAIN_FUNCTION,
    CUGRAPH_MINIMUM_SPANNING_TREE_FUNCTION, CUGRAPH_OUT_DEGREES_ALL_FUNCTION, CUGRAPH_OVERLAP_FUNCTION,
    CUGRAPH_PAGERANK_FUNCTION, CUGRAPH_PERSONALIZED_PAGERANK_FUNCTION, CUGRAPH_SORENSEN_FUNCTION,
    CUGRAPH_SPECTRAL_MODULARITY_MAXIMIZATION_FUNCTION, CUGRAPH_SSSP_FUNCTION,
    CUGRAPH_STRONGLY_CONNECTED_COMPONENTS_FUNCTION, CUGRAPH_TRIANGLE_COUNT_ALL_FUNCTION,
    CUGRAPH_WEAKLY_CONNECTED_COMPONENTS_FUNCTION, CugraphAlgorithm,
    types::{CugraphAlgorithmParams, CugraphGraphOptions},
};
use crate::error::Result;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CugraphArgumentType {
    Boolean,
    Int64,
    UInt32,
    UInt64,
    Float64,
    Utf8,
    VertexId,
    VertexIdArray,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CugraphValueRange {
    MinInclusiveF64(f64),
    MinExclusiveF64(f64),
    ClosedF64 { min: f64, max: f64 },
    OpenClosedF64 { min: f64, max: f64 },
    ClosedOpenF64 { min: f64, max: f64 },
    MinInclusiveI64(i64),
    MinInclusiveU64(u64),
    ClosedU64 { min: u64, max: u64 },
}

impl CugraphValueRange {
    /// Whether `value` satisfies the declared bounds, compared in the kind the
    /// range bounds: `f64` for float ranges, exact integers for integer ranges.
    /// `None` when `value` is not of that kind (a fractional number for an
    /// integer range), which the typed parser rejects with its own error.
    pub(super) fn contains(self, value: &Number) -> Option<bool> {
        let integer = || value.as_i64().map(i128::from).or_else(|| value.as_u64().map(i128::from));
        Some(match self {
            Self::MinInclusiveF64(min) => value.as_f64()? >= min,
            Self::MinExclusiveF64(min) => value.as_f64()? > min,
            Self::ClosedF64 { min, max } => (min..=max).contains(&value.as_f64()?),
            Self::OpenClosedF64 { min, max } => {
                let value = value.as_f64()?;
                value > min && value <= max
            },
            Self::ClosedOpenF64 { min, max } => (min..max).contains(&value.as_f64()?),
            Self::MinInclusiveI64(min) => integer()? >= i128::from(min),
            Self::MinInclusiveU64(min) => integer()? >= i128::from(min),
            Self::ClosedU64 { min, max } => (i128::from(min)..=i128::from(max)).contains(&integer()?),
        })
    }
}

impl fmt::Display for CugraphValueRange {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MinInclusiveF64(min) => write!(formatter, ">= {min}"),
            Self::MinExclusiveF64(min) => write!(formatter, "> {min}"),
            Self::ClosedF64 { min, max } => write!(formatter, "in [{min}, {max}]"),
            Self::OpenClosedF64 { min, max } => write!(formatter, "in ({min}, {max}]"),
            Self::ClosedOpenF64 { min, max } => write!(formatter, "in [{min}, {max})"),
            Self::MinInclusiveI64(min) => write!(formatter, ">= {min}"),
            Self::MinInclusiveU64(min) => write!(formatter, ">= {min}"),
            Self::ClosedU64 { min, max } => write!(formatter, "in [{min}, {max}]"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CugraphAllowedValue {
    Boolean(bool),
    Text(&'static str),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CugraphRelationColumnType {
    VertexId,
    EdgeId,
    Numeric,
    Float32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CugraphRelationColumnDefinition {
    pub name: &'static str,
    pub column_type: CugraphRelationColumnType,
    pub required: bool,
    pub description: &'static str,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CugraphArgumentGroup {
    pub name: &'static str,
    pub min_count: u8,
    pub max_count: u8,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CugraphArgumentApplicability {
    Always,
    WhenIn { argument: &'static str, values: &'static [&'static str] },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CugraphNativeDefault {
    Alpha,
    Epsilon,
    MaxIterations,
    MaxLevel,
    DepthLimit,
    OutputMode,
    Threshold,
    Resolution,
    Beta,
    Normalize,
    Normalized,
    Seeds,
    K,
    IncludeEndpoints,
    ExactVertexThreshold,
    Cutoff,
    DegreeType,
    Seed,
    MinWeight,
    EnsembleSize,
    NClusters,
    NEigenvectors,
    EvsTolerance,
    MaxIter,
    OutboundAttractionDistribution,
    LinLogMode,
    PreventOverlapping,
    OverlapScalingRatio,
    EdgeWeightInfluence,
    JitterTolerance,
    BarnesHutOptimize,
    BarnesHutTheta,
    ScalingRatio,
    StrongGravityMode,
    Gravity,
    Verbose,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CugraphDefaultValue {
    None,
    GraphDirected,
    GraphRenumber,
    GraphConstructionPolicy,
    Native(CugraphNativeDefault),
}

impl CugraphDefaultValue {
    pub fn resolve(self, algorithm: CugraphAlgorithm) -> Option<Value> {
        self.resolve_with_graph_options(algorithm, &CugraphGraphOptions::default())
    }

    pub fn resolve_with_graph_options(
        self,
        algorithm: CugraphAlgorithm,
        defaults: &CugraphGraphOptions,
    ) -> Option<Value> {
        match self {
            Self::None => None,
            Self::GraphDirected => Some(json!(algorithm.effective_default_graph_options(defaults).directed)),
            Self::GraphRenumber => Some(json!(defaults.renumber)),
            Self::GraphConstructionPolicy => Some(json!(defaults.construction_policy.as_str())),
            Self::Native(field) => native_default(field, algorithm).ok(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CugraphValueDefinition {
    pub value_type: CugraphArgumentType,
    pub nullable: bool,
    pub default: CugraphDefaultValue,
    pub range: Option<CugraphValueRange>,
    pub allowed_values: &'static [CugraphAllowedValue],
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CugraphArgumentKind {
    Relation { columns: &'static [CugraphRelationColumnDefinition] },
    Value(CugraphValueDefinition),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CugraphArgumentDefinition {
    pub name: &'static str,
    pub kind: CugraphArgumentKind,
    pub required: bool,
    pub group: Option<CugraphArgumentGroup>,
    pub applies_when: CugraphArgumentApplicability,
    pub description: &'static str,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CugraphFunctionDefinition {
    pub algorithm: CugraphAlgorithm,
    pub sql_function_name: &'static str,
    pub arguments: &'static [CugraphArgumentDefinition],
}

const EMPTY_ALLOWED: &[CugraphAllowedValue] = &[];
const ALLOWED_FALSE: &[CugraphAllowedValue] = &[CugraphAllowedValue::Boolean(false)];
const ALLOWED_TRUE: &[CugraphAllowedValue] = &[CugraphAllowedValue::Boolean(true)];
const CONSTRUCTION_POLICIES: &[CugraphAllowedValue] =
    &[CugraphAllowedValue::Text("python_cugraph"), CugraphAllowedValue::Text("raw_libcugraph")];
const OUTPUT_MODES: &[CugraphAllowedValue] =
    &[CugraphAllowedValue::Text("vertices"), CugraphAllowedValue::Text("path")];
const DEGREE_TYPES: &[CugraphAllowedValue] =
    &[CugraphAllowedValue::Text("in"), CugraphAllowedValue::Text("out"), CugraphAllowedValue::Text("in_out")];
const WHEN_PATH: &[&str] = &["path"];

const EDGE_COLUMNS: &[CugraphRelationColumnDefinition] = &[
    CugraphRelationColumnDefinition {
        name: "src",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "source vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "dst",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "destination vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "weight",
        column_type: CugraphRelationColumnType::Numeric,
        required: false,
        description: "optional edge weight",
    },
    CugraphRelationColumnDefinition {
        name: "edge_id",
        column_type: CugraphRelationColumnType::EdgeId,
        required: false,
        description: "optional edge identifier",
    },
];
const MST_EDGE_COLUMNS: &[CugraphRelationColumnDefinition] = &[
    CugraphRelationColumnDefinition {
        name: "src",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "source vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "dst",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "destination vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "weight",
        column_type: CugraphRelationColumnType::Numeric,
        required: true,
        description: "edge weight used by the spanning tree",
    },
    CugraphRelationColumnDefinition {
        name: "edge_id",
        column_type: CugraphRelationColumnType::EdgeId,
        required: false,
        description: "optional edge identifier",
    },
];
const PERSONALIZATION_COLUMNS: &[CugraphRelationColumnDefinition] = &[
    CugraphRelationColumnDefinition {
        name: "vertex",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "personalization vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "value",
        column_type: CugraphRelationColumnType::Numeric,
        required: true,
        description: "personalization value",
    },
];
const VERTEX_COLUMNS: &[CugraphRelationColumnDefinition] = &[CugraphRelationColumnDefinition {
    name: "vertex",
    column_type: CugraphRelationColumnType::VertexId,
    required: true,
    description: "vertex identifier",
}];
const EDGE_ID_COLUMNS: &[CugraphRelationColumnDefinition] = &[CugraphRelationColumnDefinition {
    name: "edge_id",
    column_type: CugraphRelationColumnType::EdgeId,
    required: true,
    description: "edge identifier",
}];
const INCLUDE_EDGES_COLUMNS: &[CugraphRelationColumnDefinition] = &[
    CugraphRelationColumnDefinition {
        name: "src",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "source vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "dst",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "destination vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "edge_id",
        column_type: CugraphRelationColumnType::EdgeId,
        required: false,
        description: "optional edge identifier",
    },
];
const VERTEX_PAIRS_COLUMNS: &[CugraphRelationColumnDefinition] = &[
    CugraphRelationColumnDefinition {
        name: "first",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "first vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "second",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "second vertex identifier",
    },
];
const VERTEX_ATTRIBUTES_COLUMNS: &[CugraphRelationColumnDefinition] = &[
    CugraphRelationColumnDefinition {
        name: "vertex",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "radius",
        column_type: CugraphRelationColumnType::Float32,
        required: false,
        description: "optional node radius",
    },
    CugraphRelationColumnDefinition {
        name: "mobility",
        column_type: CugraphRelationColumnType::Float32,
        required: false,
        description: "optional node mobility",
    },
    CugraphRelationColumnDefinition {
        name: "mass",
        column_type: CugraphRelationColumnType::Float32,
        required: false,
        description: "optional node mass",
    },
];
const INITIAL_POSITIONS_COLUMNS: &[CugraphRelationColumnDefinition] = &[
    CugraphRelationColumnDefinition {
        name: "vertex",
        column_type: CugraphRelationColumnType::VertexId,
        required: true,
        description: "vertex identifier",
    },
    CugraphRelationColumnDefinition {
        name: "x",
        column_type: CugraphRelationColumnType::Float32,
        required: true,
        description: "initial x coordinate",
    },
    CugraphRelationColumnDefinition {
        name: "y",
        column_type: CugraphRelationColumnType::Float32,
        required: true,
        description: "initial y coordinate",
    },
];

const SOURCE_GROUP: CugraphArgumentGroup =
    CugraphArgumentGroup { name: "source_vertex_selector", min_count: 1, max_count: 1 };
const SSSP_SOURCE_GROUP: CugraphArgumentGroup =
    CugraphArgumentGroup { name: "sssp_source_vertex", min_count: 1, max_count: 1 };
const VERTEX_FILTER_GROUP: CugraphArgumentGroup =
    CugraphArgumentGroup { name: "bfs_vertex_filter", min_count: 0, max_count: 1 };
const BETWEENNESS_SELECTOR_GROUP: CugraphArgumentGroup =
    CugraphArgumentGroup { name: "betweenness_selector", min_count: 0, max_count: 1 };

const ARG_EDGES: CugraphArgumentDefinition = CugraphArgumentDefinition {
    name: "edges",
    kind: CugraphArgumentKind::Relation { columns: EDGE_COLUMNS },
    required: true,
    group: None,
    applies_when: CugraphArgumentApplicability::Always,
    description: "edge relation with canonical src and dst columns, plus optional weight and edge_id columns",
};
const ARG_EDGES_WEIGHTED: CugraphArgumentDefinition = CugraphArgumentDefinition {
    kind: CugraphArgumentKind::Relation { columns: MST_EDGE_COLUMNS },
    description: "weighted edge relation with canonical src, dst, and required weight columns",
    ..ARG_EDGES
};
const ARG_DIRECTED: CugraphArgumentDefinition = CugraphArgumentDefinition {
    name: "directed",
    kind: CugraphArgumentKind::Value(CugraphValueDefinition {
        value_type: CugraphArgumentType::Boolean,
        nullable: false,
        default: CugraphDefaultValue::GraphDirected,
        range: None,
        allowed_values: EMPTY_ALLOWED,
    }),
    required: false,
    group: None,
    applies_when: CugraphArgumentApplicability::Always,
    description: "whether the edge relation is directed; some algorithms require false or true",
};
const ARG_DIRECTED_FALSE: CugraphArgumentDefinition = CugraphArgumentDefinition {
    kind: CugraphArgumentKind::Value(CugraphValueDefinition {
        allowed_values: ALLOWED_FALSE,
        ..match_value(ARG_DIRECTED)
    }),
    ..ARG_DIRECTED
};
const ARG_DIRECTED_TRUE: CugraphArgumentDefinition = CugraphArgumentDefinition {
    kind: CugraphArgumentKind::Value(CugraphValueDefinition {
        allowed_values: ALLOWED_TRUE,
        ..match_value(ARG_DIRECTED)
    }),
    ..ARG_DIRECTED
};

const ARG_RENUMBER: CugraphArgumentDefinition = value_arg(
    "renumber",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::GraphRenumber,
    None,
    EMPTY_ALLOWED,
    "whether to renumber vertex identifiers during graph construction",
);
const ARG_CONSTRUCTION_POLICY: CugraphArgumentDefinition = value_arg(
    "construction_policy",
    CugraphArgumentType::Utf8,
    false,
    CugraphDefaultValue::GraphConstructionPolicy,
    None,
    CONSTRUCTION_POLICIES,
    "graph construction policy",
);

const ARG_PERSONALIZATION: CugraphArgumentDefinition = relation_arg(
    "personalization",
    PERSONALIZATION_COLUMNS,
    true,
    None,
    CugraphArgumentApplicability::Always,
    "personalization relation with vertex and numeric value columns",
);
const ARG_SOURCES: CugraphArgumentDefinition = relation_arg(
    "sources",
    VERTEX_COLUMNS,
    false,
    Some(SOURCE_GROUP),
    CugraphArgumentApplicability::Always,
    "optional BFS source relation; exactly one of sources, source_vertex, and source_vertices is required",
);
const ARG_INCLUDE_VERTICES: CugraphArgumentDefinition = CugraphArgumentDefinition {
    group: Some(VERTEX_FILTER_GROUP),
    ..relation_arg(
        "include_vertices",
        VERTEX_COLUMNS,
        false,
        None,
        CugraphArgumentApplicability::Always,
        "vertices retained by BFS predicate filtering",
    )
};
const ARG_EXCLUDE_VERTICES: CugraphArgumentDefinition = CugraphArgumentDefinition {
    group: Some(VERTEX_FILTER_GROUP),
    ..relation_arg(
        "exclude_vertices",
        VERTEX_COLUMNS,
        false,
        None,
        CugraphArgumentApplicability::Always,
        "vertices removed by BFS predicate filtering",
    )
};
const ARG_TARGET_VERTICES: CugraphArgumentDefinition = relation_arg(
    "target_vertices",
    VERTEX_COLUMNS,
    false,
    None,
    CugraphArgumentApplicability::WhenIn { argument: "output_mode", values: WHEN_PATH },
    "BFS target relation required only for path output; it must contain exactly one row at execution",
);
const ARG_INCLUDE_EDGE_IDS: CugraphArgumentDefinition = relation_arg(
    "include_edge_ids",
    EDGE_ID_COLUMNS,
    false,
    None,
    CugraphArgumentApplicability::Always,
    "edge identifiers retained by BFS predicate filtering",
);
const ARG_INCLUDE_EDGES: CugraphArgumentDefinition = relation_arg(
    "include_edges",
    INCLUDE_EDGES_COLUMNS,
    false,
    None,
    CugraphArgumentApplicability::Always,
    "edge predicate relation with src, dst, and optional edge_id columns",
);
const ARG_VERTEX_PAIRS: CugraphArgumentDefinition = relation_arg(
    "vertex_pairs",
    VERTEX_PAIRS_COLUMNS,
    true,
    None,
    CugraphArgumentApplicability::Always,
    "explicit vertex pairs used by similarity scoring",
);
const ARG_VERTEX_ATTRIBUTES: CugraphArgumentDefinition = relation_arg(
    "vertex_attributes",
    VERTEX_ATTRIBUTES_COLUMNS,
    false,
    None,
    CugraphArgumentApplicability::Always,
    "optional per-vertex ForceAtlas2 attributes; at least one attribute column is required",
);
const ARG_INITIAL_POSITIONS: CugraphArgumentDefinition = relation_arg(
    "initial_positions",
    INITIAL_POSITIONS_COLUMNS,
    false,
    None,
    CugraphArgumentApplicability::Always,
    "optional complete ForceAtlas2 initial positions",
);

const ARG_ALPHA: CugraphArgumentDefinition = value_arg(
    "alpha",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Alpha),
    Some(CugraphValueRange::ClosedF64 { min: 0.0, max: 1.0 }),
    EMPTY_ALLOWED,
    "PageRank damping factor in [0, 1]",
);
// libcugraph accepts alpha in [0, 1]. Zero is excluded because it degenerates
// Katz centrality to the constant `beta` vector.
const ARG_KATZ_ALPHA: CugraphArgumentDefinition = value_arg(
    "alpha",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Alpha),
    Some(CugraphValueRange::OpenClosedF64 { min: 0.0, max: 1.0 }),
    EMPTY_ALLOWED,
    "Katz alpha in (0, 1]",
);
// libcugraph accepts epsilon = 0, but eigenvector, Katz and HITS test a sum of
// absolute differences against a bound proportional to epsilon. At zero that
// test never passes, so the call burns `max_iterations` of GPU work and then
// fails. PageRank shares the descriptor; at zero it runs every iteration
// without converging.
const ARG_EPSILON: CugraphArgumentDefinition = value_arg(
    "epsilon",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Epsilon),
    Some(CugraphValueRange::MinExclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "positive convergence tolerance",
);
const ARG_MAX_ITERATIONS: CugraphArgumentDefinition = value_arg(
    "max_iterations",
    CugraphArgumentType::UInt32,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::MaxIterations),
    Some(CugraphValueRange::MinInclusiveU64(1)),
    EMPTY_ALLOWED,
    "maximum iteration count, at least 1",
);
const ARG_DEPTH_LIMIT: CugraphArgumentDefinition = value_arg(
    "depth_limit",
    CugraphArgumentType::Int64,
    true,
    CugraphDefaultValue::Native(CugraphNativeDefault::DepthLimit),
    Some(CugraphValueRange::MinInclusiveI64(0)),
    EMPTY_ALLOWED,
    "number of hops to expand; 0 reaches only the sources, and null or absent is unlimited; limits above the vertex type's maximum are unlimited",
);
const ARG_OUTPUT_MODE: CugraphArgumentDefinition = value_arg(
    "output_mode",
    CugraphArgumentType::Utf8,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::OutputMode),
    None,
    OUTPUT_MODES,
    "BFS output shape",
);
const ARG_MAX_LEVEL: CugraphArgumentDefinition = value_arg(
    "max_level",
    CugraphArgumentType::UInt32,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::MaxLevel),
    Some(CugraphValueRange::MinInclusiveU64(1)),
    EMPTY_ALLOWED,
    "maximum hierarchy level, at least 1",
);
const ARG_THRESHOLD: CugraphArgumentDefinition = value_arg(
    "threshold",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Threshold),
    Some(CugraphValueRange::MinInclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "non-negative convergence threshold",
);
const ARG_RESOLUTION: CugraphArgumentDefinition = value_arg(
    "resolution",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Resolution),
    Some(CugraphValueRange::MinExclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "positive community resolution",
);
// libcugraph's ECG requires threshold and resolution in (0, 1]
// (`ecg_impl.cuh`), unlike Louvain and Leiden, so it cannot share
// `ARG_THRESHOLD` or `ARG_RESOLUTION`.
const ARG_ECG_THRESHOLD: CugraphArgumentDefinition = value_arg(
    "threshold",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Threshold),
    Some(CugraphValueRange::OpenClosedF64 { min: 0.0, max: 1.0 }),
    EMPTY_ALLOWED,
    "convergence threshold in (0, 1]",
);
const ARG_ECG_RESOLUTION: CugraphArgumentDefinition = value_arg(
    "resolution",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Resolution),
    Some(CugraphValueRange::OpenClosedF64 { min: 0.0, max: 1.0 }),
    EMPTY_ALLOWED,
    "community resolution in (0, 1]",
);
// With beta = 0 and no initial guess every Katz score stays 0, and libcugraph
// then fails its positive L2 norm check.
const ARG_BETA: CugraphArgumentDefinition = value_arg(
    "beta",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Beta),
    Some(CugraphValueRange::MinExclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "positive Katz beta",
);
const ARG_NORMALIZE: CugraphArgumentDefinition = value_arg(
    "normalize",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Normalize),
    None,
    EMPTY_ALLOWED,
    "whether HITS scores are normalized",
);
const ARG_SEEDS: CugraphArgumentDefinition = CugraphArgumentDefinition {
    group: Some(BETWEENNESS_SELECTOR_GROUP),
    ..value_arg(
        "seeds",
        CugraphArgumentType::VertexIdArray,
        true,
        CugraphDefaultValue::Native(CugraphNativeDefault::Seeds),
        None,
        EMPTY_ALLOWED,
        "optional non-empty homogeneous seed vertex array",
    )
};
const ARG_K: CugraphArgumentDefinition = CugraphArgumentDefinition {
    group: Some(BETWEENNESS_SELECTOR_GROUP),
    ..value_arg(
        "k",
        CugraphArgumentType::UInt64,
        true,
        CugraphDefaultValue::Native(CugraphNativeDefault::K),
        Some(CugraphValueRange::MinInclusiveU64(1)),
        EMPTY_ALLOWED,
        "optional sample count, at least 1",
    )
};
const ARG_K_CORE: CugraphArgumentDefinition = value_arg(
    "k",
    CugraphArgumentType::UInt32,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::K),
    Some(CugraphValueRange::MinInclusiveU64(1)),
    EMPTY_ALLOWED,
    "k-core degree threshold, at least 1",
);
const ARG_NORMALIZED: CugraphArgumentDefinition = value_arg(
    "normalized",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Normalized),
    None,
    EMPTY_ALLOWED,
    "whether centrality scores are normalized",
);
const ARG_INCLUDE_ENDPOINTS: CugraphArgumentDefinition = value_arg(
    "include_endpoints",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::IncludeEndpoints),
    None,
    EMPTY_ALLOWED,
    "whether betweenness includes endpoints",
);
const ARG_EXACT_VERTEX_THRESHOLD: CugraphArgumentDefinition = value_arg(
    "exact_vertex_threshold",
    CugraphArgumentType::UInt64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::ExactVertexThreshold),
    None,
    EMPTY_ALLOWED,
    "vertex count threshold for exact betweenness",
);
const ARG_SOURCE_VERTEX: CugraphArgumentDefinition = CugraphArgumentDefinition {
    name: "source_vertex",
    kind: CugraphArgumentKind::Value(CugraphValueDefinition {
        value_type: CugraphArgumentType::VertexId,
        nullable: false,
        default: CugraphDefaultValue::None,
        range: None,
        allowed_values: EMPTY_ALLOWED,
    }),
    required: false,
    group: Some(SOURCE_GROUP),
    applies_when: CugraphArgumentApplicability::Always,
    description: "scalar source vertex; BFS source selectors are mutually exclusive",
};
const ARG_SOURCE_VERTICES: CugraphArgumentDefinition = CugraphArgumentDefinition {
    name: "source_vertices",
    kind: CugraphArgumentKind::Value(CugraphValueDefinition {
        value_type: CugraphArgumentType::VertexIdArray,
        nullable: false,
        default: CugraphDefaultValue::None,
        range: None,
        allowed_values: EMPTY_ALLOWED,
    }),
    required: false,
    group: Some(SOURCE_GROUP),
    applies_when: CugraphArgumentApplicability::Always,
    description: "non-empty homogeneous BFS source vertex array; mutually exclusive with source_vertex and sources",
};
const ARG_SSSP_SOURCE_VERTEX: CugraphArgumentDefinition = CugraphArgumentDefinition {
    required: true,
    group: Some(SSSP_SOURCE_GROUP),
    description: "required scalar SSSP source vertex",
    ..ARG_SOURCE_VERTEX
};
const ARG_CUTOFF: CugraphArgumentDefinition = value_arg(
    "cutoff",
    CugraphArgumentType::Float64,
    true,
    CugraphDefaultValue::Native(CugraphNativeDefault::Cutoff),
    Some(CugraphValueRange::MinInclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "optional non-negative SSSP distance cutoff",
);
const ARG_DEGREE_TYPE: CugraphArgumentDefinition = value_arg(
    "degree_type",
    CugraphArgumentType::Utf8,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::DegreeType),
    None,
    DEGREE_TYPES,
    "degree direction",
);
const ARG_SEED: CugraphArgumentDefinition = value_arg(
    "seed",
    CugraphArgumentType::UInt64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Seed),
    None,
    EMPTY_ALLOWED,
    "random initialization seed",
);
const ARG_MIN_WEIGHT: CugraphArgumentDefinition = value_arg(
    "min_weight",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::MinWeight),
    Some(CugraphValueRange::MinInclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "minimum ECG edge weight",
);
const ARG_ENSEMBLE_SIZE: CugraphArgumentDefinition = value_arg(
    "ensemble_size",
    CugraphArgumentType::UInt32,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::EnsembleSize),
    Some(CugraphValueRange::MinInclusiveU64(1)),
    EMPTY_ALLOWED,
    "number of ECG ensemble iterations",
);
const ARG_N_CLUSTERS: CugraphArgumentDefinition = value_arg(
    "n_clusters",
    CugraphArgumentType::UInt32,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::NClusters),
    Some(CugraphValueRange::ClosedU64 { min: 2, max: i32::MAX as u64 }),
    EMPTY_ALLOWED,
    "number of spectral clusters, at least 2",
);
// cuVS spectral clustering keeps the trivial eigenvector (`drop_first = false`), so
// a single eigenvector gives k-means a constant embedding and one community.
const ARG_N_EIGENVECTORS: CugraphArgumentDefinition = value_arg(
    "n_eigenvectors",
    CugraphArgumentType::UInt32,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::NEigenvectors),
    Some(CugraphValueRange::ClosedU64 { min: 2, max: i32::MAX as u64 }),
    EMPTY_ALLOWED,
    "number of smallest-eigenvalue Laplacian eigenvectors in the embedding, from 2 to n_clusters; the first is constant on a connected graph",
);
const ARG_EVS_TOLERANCE: CugraphArgumentDefinition = value_arg(
    "evs_tolerance",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::EvsTolerance),
    Some(CugraphValueRange::ClosedOpenF64 { min: 0.0, max: 1.0 }),
    EMPTY_ALLOWED,
    "eigenvalue solver tolerance in [0, 1)",
);
const ARG_MAX_ITER: CugraphArgumentDefinition = value_arg(
    "max_iter",
    CugraphArgumentType::UInt32,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::MaxIter),
    Some(CugraphValueRange::ClosedU64 { min: 1, max: i32::MAX as u64 }),
    EMPTY_ALLOWED,
    "ForceAtlas2 iteration limit",
);
const ARG_OUTBOUND_ATTRACTION_DISTRIBUTION: CugraphArgumentDefinition = value_arg(
    "outbound_attraction_distribution",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::OutboundAttractionDistribution),
    None,
    EMPTY_ALLOWED,
    "distribute ForceAtlas2 attraction along outbound edges",
);
const ARG_LIN_LOG_MODE: CugraphArgumentDefinition = value_arg(
    "lin_log_mode",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::LinLogMode),
    None,
    EMPTY_ALLOWED,
    "use the ForceAtlas2 lin-log attraction model",
);
const ARG_PREVENT_OVERLAPPING: CugraphArgumentDefinition = value_arg(
    "prevent_overlapping",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::PreventOverlapping),
    None,
    EMPTY_ALLOWED,
    "prevent node overlap; requires a radius attribute",
);
const ARG_OVERLAP_SCALING_RATIO: CugraphArgumentDefinition = value_arg(
    "overlap_scaling_ratio",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::OverlapScalingRatio),
    Some(CugraphValueRange::MinExclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "positive overlap repulsion scaling ratio",
);
const ARG_EDGE_WEIGHT_INFLUENCE: CugraphArgumentDefinition = value_arg(
    "edge_weight_influence",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::EdgeWeightInfluence),
    Some(CugraphValueRange::MinInclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "non-negative edge weight influence",
);
const ARG_JITTER_TOLERANCE: CugraphArgumentDefinition = value_arg(
    "jitter_tolerance",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::JitterTolerance),
    Some(CugraphValueRange::MinInclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "non-negative ForceAtlas2 jitter tolerance",
);
const ARG_BARNES_HUT_OPTIMIZE: CugraphArgumentDefinition = value_arg(
    "barnes_hut_optimize",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::BarnesHutOptimize),
    None,
    EMPTY_ALLOWED,
    "use the Barnes-Hut ForceAtlas2 approximation",
);
const ARG_BARNES_HUT_THETA: CugraphArgumentDefinition = value_arg(
    "barnes_hut_theta",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::BarnesHutTheta),
    Some(CugraphValueRange::ClosedF64 { min: 0.0, max: 1.0 }),
    EMPTY_ALLOWED,
    "Barnes-Hut speed and accuracy trade-off",
);
const ARG_SCALING_RATIO: CugraphArgumentDefinition = value_arg(
    "scaling_ratio",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::ScalingRatio),
    Some(CugraphValueRange::MinExclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "positive ForceAtlas2 repulsion scaling ratio",
);
const ARG_STRONG_GRAVITY_MODE: CugraphArgumentDefinition = value_arg(
    "strong_gravity_mode",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::StrongGravityMode),
    None,
    EMPTY_ALLOWED,
    "use stronger gravity for distant nodes",
);
const ARG_GRAVITY: CugraphArgumentDefinition = value_arg(
    "gravity",
    CugraphArgumentType::Float64,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Gravity),
    Some(CugraphValueRange::MinInclusiveF64(0.0)),
    EMPTY_ALLOWED,
    "non-negative ForceAtlas2 gravity strength",
);
const ARG_VERBOSE: CugraphArgumentDefinition = value_arg(
    "verbose",
    CugraphArgumentType::Boolean,
    false,
    CugraphDefaultValue::Native(CugraphNativeDefault::Verbose),
    None,
    EMPTY_ALLOWED,
    "whether ForceAtlas2 emits convergence information",
);

const fn value_arg(
    name: &'static str,
    value_type: CugraphArgumentType,
    nullable: bool,
    default: CugraphDefaultValue,
    range: Option<CugraphValueRange>,
    allowed_values: &'static [CugraphAllowedValue],
    description: &'static str,
) -> CugraphArgumentDefinition {
    CugraphArgumentDefinition {
        name,
        kind: CugraphArgumentKind::Value(CugraphValueDefinition {
            value_type,
            nullable,
            default,
            range,
            allowed_values,
        }),
        required: false,
        group: None,
        applies_when: CugraphArgumentApplicability::Always,
        description,
    }
}

const fn relation_arg(
    name: &'static str,
    columns: &'static [CugraphRelationColumnDefinition],
    required: bool,
    group: Option<CugraphArgumentGroup>,
    applies_when: CugraphArgumentApplicability,
    description: &'static str,
) -> CugraphArgumentDefinition {
    CugraphArgumentDefinition {
        name,
        kind: CugraphArgumentKind::Relation { columns },
        required,
        group,
        applies_when,
        description,
    }
}

const fn match_value(definition: CugraphArgumentDefinition) -> CugraphValueDefinition {
    match definition.kind {
        CugraphArgumentKind::Value(value) => value,
        CugraphArgumentKind::Relation { .. } => panic!("expected a value argument"),
    }
}

const PAGERANK_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_ALPHA, ARG_EPSILON, ARG_MAX_ITERATIONS];
const PERSONALIZED_PAGERANK_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_PERSONALIZATION,
    ARG_ALPHA,
    ARG_EPSILON,
    ARG_MAX_ITERATIONS,
];
const BFS_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_SOURCES,
    ARG_INCLUDE_VERTICES,
    ARG_EXCLUDE_VERTICES,
    ARG_TARGET_VERTICES,
    ARG_INCLUDE_EDGE_IDS,
    ARG_INCLUDE_EDGES,
    ARG_SOURCE_VERTEX,
    ARG_SOURCE_VERTICES,
    ARG_DEPTH_LIMIT,
    ARG_OUTPUT_MODE,
];
const LOUVAIN_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED_FALSE,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_MAX_LEVEL,
    ARG_THRESHOLD,
    ARG_RESOLUTION,
];
const EIGENVECTOR_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_EPSILON, ARG_MAX_ITERATIONS];
const KATZ_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_KATZ_ALPHA,
    ARG_BETA,
    ARG_EPSILON,
    ARG_MAX_ITERATIONS,
];
const HITS_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_EPSILON, ARG_MAX_ITERATIONS, ARG_NORMALIZE];
const BETWEENNESS_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_SEEDS,
    ARG_K,
    ARG_NORMALIZED,
    ARG_INCLUDE_ENDPOINTS,
    ARG_EXACT_VERTEX_THRESHOLD,
];
const EDGE_BETWEENNESS_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_SEEDS,
    ARG_K,
    ARG_NORMALIZED,
    ARG_EXACT_VERTEX_THRESHOLD,
];
const SSSP_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_SSSP_SOURCE_VERTEX, ARG_CUTOFF];
const DEGREES_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY];
const IN_DEGREES_ARGUMENTS: &[CugraphArgumentDefinition] = DEGREES_ARGUMENTS;
const OUT_DEGREES_ARGUMENTS: &[CugraphArgumentDefinition] = DEGREES_ARGUMENTS;
const WCC_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED_FALSE, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY];
const SCC_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED_TRUE, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY];
const TRIANGLE_ARGUMENTS: &[CugraphArgumentDefinition] = WCC_ARGUMENTS;
const CORE_NUMBER_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED_FALSE, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_DEGREE_TYPE];
const K_CORE_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED_FALSE, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_K_CORE, ARG_DEGREE_TYPE];
const MST_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES_WEIGHTED, ARG_DIRECTED_FALSE, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY];
const LEIDEN_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED_FALSE, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_MAX_LEVEL, ARG_RESOLUTION, ARG_SEED];
const ECG_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED_FALSE,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_MIN_WEIGHT,
    ARG_ENSEMBLE_SIZE,
    ARG_MAX_LEVEL,
    ARG_ECG_THRESHOLD,
    ARG_ECG_RESOLUTION,
    ARG_SEED,
];
const SPECTRAL_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED_FALSE,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_N_CLUSTERS,
    ARG_N_EIGENVECTORS,
    ARG_EVS_TOLERANCE,
    ARG_SEED,
];
const FORCE_ATLAS2_ARGUMENTS: &[CugraphArgumentDefinition] = &[
    ARG_EDGES,
    ARG_DIRECTED_FALSE,
    ARG_RENUMBER,
    ARG_CONSTRUCTION_POLICY,
    ARG_VERTEX_ATTRIBUTES,
    ARG_INITIAL_POSITIONS,
    ARG_MAX_ITER,
    ARG_OUTBOUND_ATTRACTION_DISTRIBUTION,
    ARG_LIN_LOG_MODE,
    ARG_PREVENT_OVERLAPPING,
    ARG_OVERLAP_SCALING_RATIO,
    ARG_EDGE_WEIGHT_INFLUENCE,
    ARG_JITTER_TOLERANCE,
    ARG_BARNES_HUT_OPTIMIZE,
    ARG_BARNES_HUT_THETA,
    ARG_SCALING_RATIO,
    ARG_STRONG_GRAVITY_MODE,
    ARG_GRAVITY,
    ARG_VERBOSE,
    ARG_SEED,
];
const SIMILARITY_ARGUMENTS: &[CugraphArgumentDefinition] =
    &[ARG_EDGES, ARG_DIRECTED_FALSE, ARG_RENUMBER, ARG_CONSTRUCTION_POLICY, ARG_VERTEX_PAIRS];

pub const CUGRAPH_ARGUMENT_DEFINITIONS: &[CugraphFunctionDefinition] = &[
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::PageRank,
        sql_function_name: CUGRAPH_PAGERANK_FUNCTION,
        arguments: PAGERANK_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Bfs,
        sql_function_name: CUGRAPH_BFS_FUNCTION,
        arguments: BFS_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Louvain,
        sql_function_name: CUGRAPH_LOUVAIN_FUNCTION,
        arguments: LOUVAIN_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::EigenvectorCentrality,
        sql_function_name: CUGRAPH_EIGENVECTOR_CENTRALITY_FUNCTION,
        arguments: EIGENVECTOR_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::KatzCentrality,
        sql_function_name: CUGRAPH_KATZ_CENTRALITY_FUNCTION,
        arguments: KATZ_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Hits,
        sql_function_name: CUGRAPH_HITS_FUNCTION,
        arguments: HITS_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::BetweennessCentrality,
        sql_function_name: CUGRAPH_BETWEENNESS_CENTRALITY_FUNCTION,
        arguments: BETWEENNESS_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::EdgeBetweennessCentrality,
        sql_function_name: CUGRAPH_EDGE_BETWEENNESS_CENTRALITY_FUNCTION,
        arguments: EDGE_BETWEENNESS_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Sssp,
        sql_function_name: CUGRAPH_SSSP_FUNCTION,
        arguments: SSSP_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::DegreesAll,
        sql_function_name: CUGRAPH_DEGREES_ALL_FUNCTION,
        arguments: DEGREES_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::InDegreesAll,
        sql_function_name: CUGRAPH_IN_DEGREES_ALL_FUNCTION,
        arguments: IN_DEGREES_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::OutDegreesAll,
        sql_function_name: CUGRAPH_OUT_DEGREES_ALL_FUNCTION,
        arguments: OUT_DEGREES_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::WeaklyConnectedComponents,
        sql_function_name: CUGRAPH_WEAKLY_CONNECTED_COMPONENTS_FUNCTION,
        arguments: WCC_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::StronglyConnectedComponents,
        sql_function_name: CUGRAPH_STRONGLY_CONNECTED_COMPONENTS_FUNCTION,
        arguments: SCC_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::TriangleCountAll,
        sql_function_name: CUGRAPH_TRIANGLE_COUNT_ALL_FUNCTION,
        arguments: TRIANGLE_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::CoreNumber,
        sql_function_name: CUGRAPH_CORE_NUMBER_FUNCTION,
        arguments: CORE_NUMBER_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::KCore,
        sql_function_name: CUGRAPH_K_CORE_FUNCTION,
        arguments: K_CORE_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::MinimumSpanningTree,
        sql_function_name: CUGRAPH_MINIMUM_SPANNING_TREE_FUNCTION,
        arguments: MST_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::PersonalizedPageRank,
        sql_function_name: CUGRAPH_PERSONALIZED_PAGERANK_FUNCTION,
        arguments: PERSONALIZED_PAGERANK_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Leiden,
        sql_function_name: CUGRAPH_LEIDEN_FUNCTION,
        arguments: LEIDEN_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Ecg,
        sql_function_name: CUGRAPH_ECG_FUNCTION,
        arguments: ECG_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::SpectralModularityMaximization,
        sql_function_name: CUGRAPH_SPECTRAL_MODULARITY_MAXIMIZATION_FUNCTION,
        arguments: SPECTRAL_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::ForceAtlas2,
        sql_function_name: CUGRAPH_FORCE_ATLAS2_FUNCTION,
        arguments: FORCE_ATLAS2_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Jaccard,
        sql_function_name: CUGRAPH_JACCARD_FUNCTION,
        arguments: SIMILARITY_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Sorensen,
        sql_function_name: CUGRAPH_SORENSEN_FUNCTION,
        arguments: SIMILARITY_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Overlap,
        sql_function_name: CUGRAPH_OVERLAP_FUNCTION,
        arguments: SIMILARITY_ARGUMENTS,
    },
    CugraphFunctionDefinition {
        algorithm: CugraphAlgorithm::Cosine,
        sql_function_name: CUGRAPH_COSINE_FUNCTION,
        arguments: SIMILARITY_ARGUMENTS,
    },
];

pub fn definition_for_algorithm(algorithm: CugraphAlgorithm) -> &'static CugraphFunctionDefinition {
    CUGRAPH_ARGUMENT_DEFINITIONS
        .iter()
        .find(|definition| definition.algorithm == algorithm)
        .expect("cuGraph argument definitions are missing an algorithm")
}

pub fn arguments_for_algorithm(algorithm: CugraphAlgorithm) -> &'static [CugraphArgumentDefinition] {
    definition_for_algorithm(algorithm).arguments
}

fn native_default(field: CugraphNativeDefault, algorithm: CugraphAlgorithm) -> Result<Value> {
    let params = CugraphAlgorithmParams::from_native_defaults(algorithm, None)?;
    Ok(match (field, params) {
        (CugraphNativeDefault::Alpha, CugraphAlgorithmParams::PageRank(params)) => json!(params.alpha),
        (CugraphNativeDefault::Alpha, CugraphAlgorithmParams::PersonalizedPageRank(params)) => {
            json!(params.pagerank.alpha)
        },
        (CugraphNativeDefault::Alpha, CugraphAlgorithmParams::KatzCentrality(params)) => json!(params.alpha),
        (CugraphNativeDefault::Epsilon, CugraphAlgorithmParams::PageRank(params)) => json!(params.epsilon),
        (CugraphNativeDefault::Epsilon, CugraphAlgorithmParams::PersonalizedPageRank(params)) => {
            json!(params.pagerank.epsilon)
        },
        (CugraphNativeDefault::Epsilon, CugraphAlgorithmParams::EigenvectorCentrality(params)) => json!(params.epsilon),
        (CugraphNativeDefault::Epsilon, CugraphAlgorithmParams::KatzCentrality(params)) => json!(params.epsilon),
        (CugraphNativeDefault::Epsilon, CugraphAlgorithmParams::Hits(params)) => json!(params.epsilon),
        (CugraphNativeDefault::MaxIterations, CugraphAlgorithmParams::PageRank(params)) => json!(params.max_iterations),
        (CugraphNativeDefault::MaxIterations, CugraphAlgorithmParams::PersonalizedPageRank(params)) => {
            json!(params.pagerank.max_iterations)
        },
        (CugraphNativeDefault::MaxIterations, CugraphAlgorithmParams::EigenvectorCentrality(params)) => {
            json!(params.max_iterations)
        },
        (CugraphNativeDefault::MaxIterations, CugraphAlgorithmParams::KatzCentrality(params)) => {
            json!(params.max_iterations)
        },
        (CugraphNativeDefault::MaxIterations, CugraphAlgorithmParams::Hits(params)) => json!(params.max_iterations),
        (CugraphNativeDefault::MaxLevel, CugraphAlgorithmParams::Louvain(params)) => json!(params.max_level),
        (CugraphNativeDefault::MaxLevel, CugraphAlgorithmParams::Leiden(params)) => json!(params.max_level),
        (CugraphNativeDefault::MaxLevel, CugraphAlgorithmParams::Ecg(params)) => json!(params.max_level),
        (CugraphNativeDefault::DepthLimit, CugraphAlgorithmParams::Bfs(params)) => json!(params.depth_limit),
        (CugraphNativeDefault::OutputMode, CugraphAlgorithmParams::Bfs(params)) => json!(params.output.mode.as_str()),
        (CugraphNativeDefault::Threshold, CugraphAlgorithmParams::Louvain(params)) => json!(params.threshold),
        (CugraphNativeDefault::Threshold, CugraphAlgorithmParams::Ecg(params)) => json!(params.threshold),
        (CugraphNativeDefault::Resolution, CugraphAlgorithmParams::Louvain(params)) => json!(params.resolution),
        (CugraphNativeDefault::Resolution, CugraphAlgorithmParams::Leiden(params)) => json!(params.resolution),
        (CugraphNativeDefault::Resolution, CugraphAlgorithmParams::Ecg(params)) => json!(params.resolution),
        (CugraphNativeDefault::Beta, CugraphAlgorithmParams::KatzCentrality(params)) => json!(params.beta),
        (CugraphNativeDefault::Normalize, CugraphAlgorithmParams::Hits(params)) => json!(params.normalize),
        (CugraphNativeDefault::Normalized, CugraphAlgorithmParams::BetweennessCentrality(params)) => {
            json!(params.normalized)
        },
        (CugraphNativeDefault::Normalized, CugraphAlgorithmParams::EdgeBetweennessCentrality(params)) => {
            json!(params.normalized)
        },
        (CugraphNativeDefault::Seeds, CugraphAlgorithmParams::BetweennessCentrality(_)) => Value::Null,
        (CugraphNativeDefault::Seeds, CugraphAlgorithmParams::EdgeBetweennessCentrality(_)) => Value::Null,
        (CugraphNativeDefault::K, CugraphAlgorithmParams::BetweennessCentrality(params)) => json!(params.k),
        (CugraphNativeDefault::K, CugraphAlgorithmParams::EdgeBetweennessCentrality(params)) => json!(params.k),
        (CugraphNativeDefault::K, CugraphAlgorithmParams::KCore(params)) => json!(params.k),
        (CugraphNativeDefault::IncludeEndpoints, CugraphAlgorithmParams::BetweennessCentrality(params)) => {
            json!(params.include_endpoints)
        },
        (CugraphNativeDefault::ExactVertexThreshold, CugraphAlgorithmParams::BetweennessCentrality(params)) => {
            json!(params.exact_vertex_threshold)
        },
        (CugraphNativeDefault::ExactVertexThreshold, CugraphAlgorithmParams::EdgeBetweennessCentrality(params)) => {
            json!(params.exact_vertex_threshold)
        },
        (CugraphNativeDefault::Cutoff, CugraphAlgorithmParams::Sssp(params)) => json!(params.cutoff),
        (CugraphNativeDefault::DegreeType, CugraphAlgorithmParams::CoreNumber(params)) => {
            json!(params.degree_type.as_str())
        },
        (CugraphNativeDefault::DegreeType, CugraphAlgorithmParams::KCore(params)) => json!(params.degree_type.as_str()),
        (CugraphNativeDefault::Seed, CugraphAlgorithmParams::Leiden(params)) => json!(params.seed),
        (CugraphNativeDefault::Seed, CugraphAlgorithmParams::Ecg(params)) => json!(params.seed),
        (CugraphNativeDefault::Seed, CugraphAlgorithmParams::SpectralModularityMaximization(params)) => {
            json!(params.seed)
        },
        (CugraphNativeDefault::Seed, CugraphAlgorithmParams::ForceAtlas2(params)) => json!(params.seed),
        (CugraphNativeDefault::MinWeight, CugraphAlgorithmParams::Ecg(params)) => json!(params.min_weight),
        (CugraphNativeDefault::EnsembleSize, CugraphAlgorithmParams::Ecg(params)) => json!(params.ensemble_size),
        (CugraphNativeDefault::NClusters, CugraphAlgorithmParams::SpectralModularityMaximization(params)) => {
            json!(params.n_clusters)
        },
        (CugraphNativeDefault::NEigenvectors, CugraphAlgorithmParams::SpectralModularityMaximization(params)) => {
            json!(params.n_eigenvectors)
        },
        (CugraphNativeDefault::EvsTolerance, CugraphAlgorithmParams::SpectralModularityMaximization(params)) => {
            json!(params.evs_tolerance)
        },
        (CugraphNativeDefault::MaxIter, CugraphAlgorithmParams::ForceAtlas2(params)) => json!(params.max_iter),
        (CugraphNativeDefault::OutboundAttractionDistribution, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.outbound_attraction_distribution)
        },
        (CugraphNativeDefault::LinLogMode, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.lin_log_mode)
        },
        (CugraphNativeDefault::PreventOverlapping, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.prevent_overlapping)
        },
        (CugraphNativeDefault::OverlapScalingRatio, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.overlap_scaling_ratio)
        },
        (CugraphNativeDefault::EdgeWeightInfluence, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.edge_weight_influence)
        },
        (CugraphNativeDefault::JitterTolerance, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.jitter_tolerance)
        },
        (CugraphNativeDefault::BarnesHutOptimize, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.barnes_hut_optimize)
        },
        (CugraphNativeDefault::BarnesHutTheta, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.barnes_hut_theta)
        },
        (CugraphNativeDefault::ScalingRatio, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.scaling_ratio)
        },
        (CugraphNativeDefault::StrongGravityMode, CugraphAlgorithmParams::ForceAtlas2(params)) => {
            json!(params.strong_gravity_mode)
        },
        (CugraphNativeDefault::Gravity, CugraphAlgorithmParams::ForceAtlas2(params)) => json!(params.gravity),
        (CugraphNativeDefault::Verbose, CugraphAlgorithmParams::ForceAtlas2(params)) => json!(params.verbose),
        (field, params) => panic!("cuGraph argument default {field:?} is not defined for {params:?}"),
    })
}