Skip to content

Commit 834e363

Browse files
authored
Merge pull request #9 from abetomo/feature/add_skip_header_option
feat: add the option to skip header
2 parents 4e1084b + 1090310 commit 834e363

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

src/ConvertAthenaQueryResultstoArray.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ private static function cast(array $metadata, string $value)
2424
* convert
2525
*
2626
* @param array $resultSet
27+
* @param bool $isSkipHeader
2728
* @return array
2829
*/
29-
public static function convert(array $resultSet): array
30+
public static function convert(array $resultSet, bool $isSkipHeader = false): array
3031
{
31-
$rows = array_slice($resultSet['Rows'], 1);
32+
$offset = $isSkipHeader ? 1 : 0;
33+
$rows = array_slice($resultSet['Rows'], $offset);
3234
$metadata = $resultSet['ResultSetMetadata']['ColumnInfo'];
3335

3436
return array_map(function ($row) use ($metadata) {

tests/ConvertAthenaQueryResultstoArrayTest.php

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testCast()
3535
}
3636
}
3737

38-
public function testConvert()
38+
public function testConvertSkipHeader()
3939
{
4040
$resultSet = [
4141
'Rows' => [
@@ -107,10 +107,96 @@ public function testConvert()
107107
'column_name2' => 3
108108
]
109109
];
110+
$this->assertSame(
111+
$expected,
112+
ConvertAthenaQueryResultstoArray::convert($resultSet, true)
113+
);
114+
}
115+
116+
public function testConvertNotSkipHeader()
117+
{
118+
$resultSet = [
119+
'Rows' => [
120+
[
121+
'Data' => [
122+
['VarCharValue' => 'column_name1'],
123+
['VarCharValue' => 'column_name2']
124+
]
125+
],
126+
[
127+
'Data' => [
128+
['VarCharValue' => 'value1'],
129+
['VarCharValue' => '1']
130+
]
131+
],
132+
[
133+
'Data' => [
134+
['VarCharValue' => 'value2'],
135+
['VarCharValue' => '2']
136+
]
137+
],
138+
[
139+
'Data' => [
140+
['VarCharValue' => 'value3'],
141+
['VarCharValue' => '3']
142+
]
143+
]
144+
],
145+
'ResultSetMetadata' => [
146+
'ColumnInfo' => [
147+
[
148+
'CatalogName' => 'hive',
149+
'SchemaName' => '',
150+
'TableName' => '',
151+
'Name' => 'column_name1',
152+
'Label' => 'column_name1',
153+
'Type' => 'varchar',
154+
'Precision' => 2147483647,
155+
'Scale' => 0,
156+
'Nullable' => 'UNKNOWN',
157+
'CaseSensitive' => true
158+
],
159+
[
160+
'CatalogName' => 'hive',
161+
'SchemaName' => '',
162+
'TableName' => '',
163+
'Name' => 'column_name2',
164+
'Label' => 'column_name2',
165+
'Type' => 'integer',
166+
'Precision' => 10,
167+
'Scale' => 0,
168+
'Nullable' => 'UNKNOWN',
169+
'CaseSensitive' => false
170+
]
171+
]
172+
]
173+
];
174+
$expected = [
175+
[
176+
'column_name1' => 'column_name1',
177+
'column_name2' => 0
178+
],
179+
[
180+
'column_name1' => 'value1',
181+
'column_name2' => 1
182+
],
183+
[
184+
'column_name1' => 'value2',
185+
'column_name2' => 2
186+
],
187+
[
188+
'column_name1' => 'value3',
189+
'column_name2' => 3
190+
]
191+
];
110192
$this->assertSame(
111193
$expected,
112194
ConvertAthenaQueryResultstoArray::convert($resultSet)
113195
);
196+
$this->assertSame(
197+
$expected,
198+
ConvertAthenaQueryResultstoArray::convert($resultSet, false)
199+
);
114200
}
115201

116202
public function testConvertWithoutVarCharValue()
@@ -187,7 +273,7 @@ public function testConvertWithoutVarCharValue()
187273
];
188274
$this->assertSame(
189275
$expected,
190-
ConvertAthenaQueryResultstoArray::convert($resultSet)
276+
ConvertAthenaQueryResultstoArray::convert($resultSet, true)
191277
);
192278
}
193279
}

0 commit comments

Comments
 (0)