Skip to content

Commit 9186ad7

Browse files
authored
chore: any refactor (#12)
1 parent 0341263 commit 9186ad7

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

lib/athena-query.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ export class AthenaQuery {
1616
async *query(
1717
sql: string,
1818
options?: { executionParameters?: string[]; maxResults: number }
19-
): AsyncGenerator<
20-
Record<string, string | number | BigInt | null>,
21-
void,
22-
undefined
23-
> {
19+
): AsyncGenerator<helpers.AtheneRecordData, void, undefined> {
2420
const QueryExecutionId = await helpers.startQueryExecution({
2521
athena: this.athena,
2622
sql,

lib/helper.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import {
1+
import type {
22
Athena,
3-
Datum,
43
GetQueryResultsCommandOutput,
54
} from "@aws-sdk/client-athena";
65

6+
export type AtheneRecordData = Record<string, string | number | BigInt | null>;
7+
type AtheneRecord = AtheneRecordData[];
8+
79
async function startQueryExecution(params: {
810
athena: Athena;
911
sql: string;
@@ -55,14 +57,14 @@ async function getQueryResults(params: {
5557
MaxResults?: number;
5658
NextToken?: string;
5759
QueryExecutionId: string;
58-
}) {
60+
}): Promise<{ items: AtheneRecord; nextToken?: string }> {
5961
const queryResults = await params.athena.getQueryResults({
6062
QueryExecutionId: params.QueryExecutionId,
6163
MaxResults: params.MaxResults,
6264
NextToken: params.NextToken,
6365
});
6466
return {
65-
items: await cleanUpPaginatedDML(
67+
items: cleanUpPaginatedDML(
6668
queryResults,
6769
// If NextToken is not given, ignore first data.
6870
// Because the first data is header info.
@@ -72,42 +74,39 @@ async function getQueryResults(params: {
7274
};
7375
}
7476

75-
async function cleanUpPaginatedDML(
77+
function cleanUpPaginatedDML(
7678
queryResults: GetQueryResultsCommandOutput,
7779
ignoreFirstData: boolean
78-
) {
79-
const dataTypes = await getDataTypes(queryResults);
80+
): AtheneRecord {
81+
const dataTypes = getDataTypes(queryResults);
8082
if (!dataTypes) return [];
8183

8284
const columnNames = Object.keys(dataTypes);
83-
let unformattedS3RowArray: Datum[] | null = null;
84-
let formattedArray: Record<string, string | number | BigInt | null>[] = [];
85-
86-
for (
87-
let i = ignoreFirstData ? 1 : 0;
88-
i < (queryResults.ResultSet?.Rows?.length ?? 0);
89-
i++
90-
) {
91-
unformattedS3RowArray = queryResults.ResultSet?.Rows?.[i].Data ?? null;
9285

93-
if (!unformattedS3RowArray) continue;
86+
const items = queryResults.ResultSet?.Rows?.reduce((acc, { Data }, index) => {
87+
if (ignoreFirstData && index === 0) return acc;
88+
if (!Data) return acc;
9489

95-
const rowObject = unformattedS3RowArray?.reduce((acc, row, index) => {
90+
const rowObject = Data?.reduce((acc, row, index) => {
9691
if (row.VarCharValue) {
92+
// use mutable operation for performance
9793
acc[columnNames[index]] = row.VarCharValue;
9894
}
9995
return acc;
10096
}, {} as Record<string, string>);
10197

102-
formattedArray.push(addDataType(rowObject, dataTypes));
103-
}
104-
return formattedArray;
98+
// use mutable operation for performance
99+
acc.push(addDataType(rowObject, dataTypes));
100+
return acc;
101+
}, [] as AtheneRecord);
102+
103+
return items ?? [];
105104
}
106105

107106
function addDataType(
108107
input: Record<string, string>,
109108
dataTypes: Record<string, string>
110-
): Record<string, null | string | number | BigInt> {
109+
): AtheneRecordData {
111110
const updatedObjectWithDataType: Record<
112111
string,
113112
null | string | number | BigInt
@@ -143,9 +142,9 @@ function addDataType(
143142
return updatedObjectWithDataType;
144143
}
145144

146-
async function getDataTypes(
145+
function getDataTypes(
147146
queryResults: GetQueryResultsCommandOutput
148-
): Promise<Record<string, string> | undefined> {
147+
): Record<string, string> | undefined {
149148
const columnInfoArray = queryResults.ResultSet?.ResultSetMetadata?.ColumnInfo;
150149

151150
const columnInfoObject = columnInfoArray?.reduce((acc, columnInfo) => {

0 commit comments

Comments
 (0)