1
- import {
1
+ import type {
2
2
Athena ,
3
- Datum ,
4
3
GetQueryResultsCommandOutput ,
5
4
} from "@aws-sdk/client-athena" ;
6
5
6
+ export type AtheneRecordData = Record < string , string | number | BigInt | null > ;
7
+ type AtheneRecord = AtheneRecordData [ ] ;
8
+
7
9
async function startQueryExecution ( params : {
8
10
athena : Athena ;
9
11
sql : string ;
@@ -55,14 +57,14 @@ async function getQueryResults(params: {
55
57
MaxResults ?: number ;
56
58
NextToken ?: string ;
57
59
QueryExecutionId : string ;
58
- } ) {
60
+ } ) : Promise < { items : AtheneRecord ; nextToken ?: string } > {
59
61
const queryResults = await params . athena . getQueryResults ( {
60
62
QueryExecutionId : params . QueryExecutionId ,
61
63
MaxResults : params . MaxResults ,
62
64
NextToken : params . NextToken ,
63
65
} ) ;
64
66
return {
65
- items : await cleanUpPaginatedDML (
67
+ items : cleanUpPaginatedDML (
66
68
queryResults ,
67
69
// If NextToken is not given, ignore first data.
68
70
// Because the first data is header info.
@@ -72,42 +74,39 @@ async function getQueryResults(params: {
72
74
} ;
73
75
}
74
76
75
- async function cleanUpPaginatedDML (
77
+ function cleanUpPaginatedDML (
76
78
queryResults : GetQueryResultsCommandOutput ,
77
79
ignoreFirstData : boolean
78
- ) {
79
- const dataTypes = await getDataTypes ( queryResults ) ;
80
+ ) : AtheneRecord {
81
+ const dataTypes = getDataTypes ( queryResults ) ;
80
82
if ( ! dataTypes ) return [ ] ;
81
83
82
84
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 ;
92
85
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 ;
94
89
95
- const rowObject = unformattedS3RowArray ?. reduce ( ( acc , row , index ) => {
90
+ const rowObject = Data ?. reduce ( ( acc , row , index ) => {
96
91
if ( row . VarCharValue ) {
92
+ // use mutable operation for performance
97
93
acc [ columnNames [ index ] ] = row . VarCharValue ;
98
94
}
99
95
return acc ;
100
96
} , { } as Record < string , string > ) ;
101
97
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 ?? [ ] ;
105
104
}
106
105
107
106
function addDataType (
108
107
input : Record < string , string > ,
109
108
dataTypes : Record < string , string >
110
- ) : Record < string , null | string | number | BigInt > {
109
+ ) : AtheneRecordData {
111
110
const updatedObjectWithDataType : Record <
112
111
string ,
113
112
null | string | number | BigInt
@@ -143,9 +142,9 @@ function addDataType(
143
142
return updatedObjectWithDataType ;
144
143
}
145
144
146
- async function getDataTypes (
145
+ function getDataTypes (
147
146
queryResults : GetQueryResultsCommandOutput
148
- ) : Promise < Record < string , string > | undefined > {
147
+ ) : Record < string , string > | undefined {
149
148
const columnInfoArray = queryResults . ResultSet ?. ResultSetMetadata ?. ColumnInfo ;
150
149
151
150
const columnInfoObject = columnInfoArray ?. reduce ( ( acc , columnInfo ) => {
0 commit comments