| #!/usr/bin/env python3 | |
| """Stage 4 planner: build work/graphs/plan.parquet (one row per public code_sample row, with code), ordered by batch_id. | |
| Order: pilot (stratified ~20k sample: all languages, C/C++ first, vuln/fix pairs included) first, then the rest by priority group: | |
| p1_vuln_pairs every vulnerable function + the paired fixed version(s) from real-world sources (Juliet/Draper are later groups) | |
| p2_realworld remaining PrimeVul, CVEfixes, MoreFixes, CrossVul functions | |
| p3_bigvul remaining Big-Vul | |
| p4_juliet remaining Juliet | |
| p5_draper remaining Draper VDISC | |
| Within a group: C/C++ first, then other languages by size. Only public code_sample rows (restricted sources have no code here). | |
| Languages without a Joern frontend get status 'unsupported_language' (batch_id = -1).""" | |
| import duckdb, os, sys | |
| ROOT = "/workspace/security-dataset"; W = f"{ROOT}/work/graphs"; os.makedirs(W, exist_ok=True) | |
| OUT = f"{W}/plan.parquet" | |
| if os.path.exists(OUT) and "--force" not in sys.argv: print("plan exists"); sys.exit(0) | |
| c = duckdb.connect(); c.execute("SET memory_limit='5GB'; SET threads=6; SET preserve_insertion_order=true") | |
| FRONT = {"c_cpp": "c", "java": "javasrc", "javascript": "jssrc", "typescript": "jssrc", "python": "pythonsrc", "php": "php", "go": "golang", | |
| "ruby": "rubysrc", "csharp": "csharpsrc", "kotlin": "kotlin", "swift": "swiftsrc", "rust": "rust"} | |
| LORD = ["c_cpp", "java", "javascript", "php", "python", "go", "ruby", "typescript", "rust", "csharp", "kotlin", "swift"] | |
| PILOT_N = {"c_cpp": 11000, "javascript": 1500, "php": 1500, "java": 1500, "python": 1500, "go": 1000, "ruby": 700, "typescript": 400, | |
| "rust": 300, "csharp": 300, "kotlin": 150, "swift": 150} | |
| fr = "CASE language " + " ".join(f"WHEN '{k}' THEN '{v}'" for k, v in FRONT.items()) + " END" | |
| lo = "CASE language " + " ".join(f"WHEN '{k}' THEN {i}" for i, k in enumerate(LORD)) + " ELSE 99 END" | |
| c.execute(f"""CREATE TABLE s AS SELECT sample_id, language, language_detail, granularity, split, primary_source, is_vulnerable, pair_id, pair_role, | |
| length(code) AS code_len, code, {fr} AS frontend, {lo} AS lang_ord, hash(sample_id || ':pilot') AS h | |
| FROM read_parquet('{ROOT}/data/code/code_sample/*/*.parquet', hive_partitioning=true)""") | |
| # pair partners of vulnerable functions | |
| c.execute("CREATE TABLE vp AS SELECT DISTINCT pair_id FROM s WHERE is_vulnerable AND pair_id IS NOT NULL") | |
| SYN = "primary_source IN ('juliet_c', 'draper_vdisc')" # synthetic / static-analyser sources come later per the priority order | |
| c.execute(f"""ALTER TABLE s ADD COLUMN priority_group VARCHAR; UPDATE s SET priority_group = CASE | |
| WHEN NOT ({SYN}) AND (is_vulnerable OR (pair_id IN (SELECT pair_id FROM vp) AND pair_role = 'fix')) THEN 'p1_vuln_pairs' | |
| WHEN primary_source IN ('primevul', 'cvefixes', 'morefixes', 'crossvul') THEN 'p2_realworld' | |
| WHEN primary_source = 'bigvul' THEN 'p3_bigvul' WHEN primary_source = 'juliet_c' THEN 'p4_juliet' ELSE 'p5_draper' END""") | |
| # pilot: per language, ~25 % from complete vuln/fix pairs, rest stratified by (primary_source, is_vulnerable) via hash order | |
| pil = [] | |
| for lang, n in PILOT_N.items(): | |
| npair = n // 4 | |
| c.execute(f"""CREATE OR REPLACE TABLE pp AS SELECT pair_id FROM s WHERE language = '{lang}' AND pair_id IN (SELECT pair_id FROM vp) | |
| GROUP BY 1 HAVING count(*) FILTER (WHERE pair_role = 'fix') > 0 AND count(*) FILTER (WHERE is_vulnerable) > 0 ORDER BY hash(pair_id || ':p') LIMIT {npair // 2}""") | |
| c.execute(f"""INSERT INTO pilot_ids SELECT sample_id FROM s WHERE language = '{lang}' AND pair_id IN (SELECT pair_id FROM pp) AND pair_role IN ('vuln', 'fix')""") if pil else \ | |
| c.execute(f"""CREATE TABLE pilot_ids AS SELECT sample_id FROM s WHERE language = '{lang}' AND pair_id IN (SELECT pair_id FROM pp) AND pair_role IN ('vuln', 'fix')""") | |
| pil.append(lang) | |
| have = c.execute(f"select count(*) from pilot_ids join s using (sample_id) where language = '{lang}'").fetchone()[0] | |
| rest = max(0, n - have) | |
| c.execute(f"""INSERT INTO pilot_ids SELECT sample_id FROM ( | |
| SELECT sample_id, row_number() OVER (PARTITION BY primary_source, is_vulnerable ORDER BY h) AS r, | |
| count(*) OVER (PARTITION BY primary_source, is_vulnerable) AS g, count(*) OVER () AS tot | |
| FROM s WHERE language = '{lang}' AND sample_id NOT IN (SELECT sample_id FROM pilot_ids)) | |
| WHERE r <= greatest(1, ceil({rest} * g / tot)) ORDER BY hash(sample_id) LIMIT {rest}""") | |
| c.execute("ALTER TABLE s ADD COLUMN pilot BOOLEAN; UPDATE s SET pilot = sample_id IN (SELECT sample_id FROM pilot_ids)") | |
| print(c.execute("select language, count(*), sum(is_vulnerable::int), count(*) filter (where pair_role in ('vuln','fix') and pair_id in (select pair_id from vp)) from s where pilot group by 1 order by 2 desc").fetchall()) | |
| # batches: (pilot desc, group, lang_ord, big-function flag, sample order); size 500 (C/C++ 1000), 20 for code_len > 20k | |
| c.execute("""CREATE TABLE o AS SELECT *, row_number() OVER (ORDER BY pilot DESC, CASE WHEN pilot THEN '' ELSE priority_group END, lang_ord, frontend, | |
| code_len > 20000, primary_source, sample_id) AS ord FROM s WHERE frontend IS NOT NULL""") | |
| rows = c.execute("select ord, pilot, priority_group, frontend, code_len > 20000 from o order by ord").fetchall() | |
| bid, cur, n, ids = -1, None, 0, [] | |
| for ord_, pilot, grp, frt, big in rows: | |
| key = (pilot, None if pilot else grp, frt, big); cap = 20 if big else (1000 if frt == "c" else 500) | |
| if key != cur or n >= cap: bid += 1; cur = key; n = 0 | |
| ids.append((ord_, bid)); n += 1 | |
| import pyarrow as pa | |
| c.register("bm", pa.table({"ord": [x[0] for x in ids], "batch_id": [x[1] for x in ids]})) | |
| c.execute(f"""COPY (SELECT o.* EXCLUDE (h, lang_ord), bm.batch_id FROM o JOIN bm USING (ord) | |
| UNION ALL BY NAME SELECT s.* EXCLUDE (h, lang_ord), NULL::BIGINT AS ord, -1 AS batch_id FROM s WHERE frontend IS NULL | |
| ORDER BY batch_id, ord) TO '{OUT}' (FORMAT parquet, COMPRESSION zstd, ROW_GROUP_SIZE 1000)""") | |
| print(c.execute(f"select priority_group, pilot, count(*), count(distinct batch_id) from '{OUT}' group by all order by 2 desc, 1").fetchall()) | |
| print("batches", bid + 1, "unsupported", c.execute(f"select count(*) from '{OUT}' where batch_id = -1").fetchone()[0]) | |
Xet Storage Details
- Size:
- 6.29 kB
- Xet hash:
- 8a2fe2e073af4441aba737974e16838a68a9ac5df0d9305dd676644577a26cbf
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.