-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquery-site.bench.ts
More file actions
106 lines (102 loc) · 3.06 KB
/
Copy pathquery-site.bench.ts
File metadata and controls
106 lines (102 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { bench, describe } from "vitest";
import {
recognizeSqlRelationQuerySite,
type SqlQuerySiteDialect,
} from "../query-site.js";
import { createIdentitySqlSource } from "../source.js";
import {
buildSqlStatementIndex,
DUCKDB_SQL_LEXICAL_PROFILE,
findSqlStatementSlot,
} from "../statement-index.js";
const dialect: SqlQuerySiteDialect = {
classifyIdentifierToken: (rawIdentifier) => ({
status: "identifier",
value: rawIdentifier,
}),
decodeRelationPath: (rawPath, cursorOffset) => ({
finalSegment: { from: 0, to: rawPath.length },
prefix: {
quoted: false,
value: rawPath.slice(0, cursorOffset),
},
qualifier: [],
quality: "exact",
status: "decoded",
}),
lexicalProfile: DUCKDB_SQL_LEXICAL_PROFILE,
maximumPathDepth: 16,
supportsNaturalJoin: true,
};
const TEN_KIBIBYTES = 10 * 1_024;
const queryPrefix = "SELECT ";
const querySuffix = " FROM schema_prefix";
const projectedListLength =
TEN_KIBIBYTES - queryPrefix.length - querySuffix.length;
const projectedList = `${"x,".repeat(
Math.floor((projectedListLength - 1) / 2),
)}x`;
const tenKilobyteQuery = `${queryPrefix}${projectedList}${" ".repeat(
projectedListLength - projectedList.length,
)}${querySuffix}`;
if (tenKilobyteQuery.length !== TEN_KIBIBYTES) {
throw new Error("Query benchmark fixture must be exactly 10 KiB");
}
const source = createIdentitySqlSource(tenKilobyteQuery);
const index = buildSqlStatementIndex(
source.analysisText,
dialect.lexicalProfile,
);
const position = tenKilobyteQuery.length;
const slot = findSqlStatementSlot(index, position, "left");
const aliasHeavyQuery = `SELECT * FROM ${Array.from(
{ length: 1_000 },
(_, aliasIndex) => `table_name alias_${aliasIndex}`,
).join(", ")}, `;
const aliasHeavySource = createIdentitySqlSource(aliasHeavyQuery);
const aliasHeavyIndex = buildSqlStatementIndex(
aliasHeavySource.analysisText,
dialect.lexicalProfile,
);
const aliasHeavyPosition = aliasHeavyQuery.length;
const aliasHeavySlot = findSqlStatementSlot(
aliasHeavyIndex,
aliasHeavyPosition,
"left",
);
const usingHeavyQuery = `SELECT * FROM first_table JOIN second_table USING(${Array.from(
{ length: 1_000 },
(_, columnIndex) => `column_${columnIndex}`,
).join(", ")}) JOIN target`;
const usingHeavySource = createIdentitySqlSource(usingHeavyQuery);
const usingHeavyIndex = buildSqlStatementIndex(
usingHeavySource.analysisText,
dialect.lexicalProfile,
);
const usingHeavyPosition = usingHeavyQuery.length;
const usingHeavySlot = findSqlStatementSlot(
usingHeavyIndex,
usingHeavyPosition,
"left",
);
describe("query-site recognizer", () => {
bench("10 KiB active statement", () => {
recognizeSqlRelationQuerySite(source, slot, position, dialect);
});
bench("1,000 classified aliases", () => {
recognizeSqlRelationQuerySite(
aliasHeavySource,
aliasHeavySlot,
aliasHeavyPosition,
dialect,
);
});
bench("1,000 authenticated USING columns", () => {
recognizeSqlRelationQuerySite(
usingHeavySource,
usingHeavySlot,
usingHeavyPosition,
dialect,
);
});
});