Skip to content

Commit

Permalink
feat: add the option to skip header
Browse files Browse the repository at this point in the history
  • Loading branch information
abetomo committed Apr 28, 2021
1 parent 0b221a3 commit 1090310
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/ConvertAthenaQueryResultstoArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ private static function cast(array $metadata, string $value)
* convert
*
* @param array $resultSet
* @param bool $isSkipHeader
* @return array
*/
public static function convert(array $resultSet): array
public static function convert(array $resultSet, bool $isSkipHeader = false): array
{
$rows = array_slice($resultSet['Rows'], 1);
$offset = $isSkipHeader ? 1 : 0;
$rows = array_slice($resultSet['Rows'], $offset);
$metadata = $resultSet['ResultSetMetadata']['ColumnInfo'];

return array_map(function ($row) use ($metadata) {
Expand Down
90 changes: 88 additions & 2 deletions tests/ConvertAthenaQueryResultstoArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testCast()
}
}

public function testConvert()
public function testConvertSkipHeader()
{
$resultSet = [
'Rows' => [
Expand Down Expand Up @@ -107,10 +107,96 @@ public function testConvert()
'column_name2' => 3
]
];
$this->assertSame(
$expected,
ConvertAthenaQueryResultstoArray::convert($resultSet, true)
);
}

public function testConvertNotSkipHeader()
{
$resultSet = [
'Rows' => [
[
'Data' => [
['VarCharValue' => 'column_name1'],
['VarCharValue' => 'column_name2']
]
],
[
'Data' => [
['VarCharValue' => 'value1'],
['VarCharValue' => '1']
]
],
[
'Data' => [
['VarCharValue' => 'value2'],
['VarCharValue' => '2']
]
],
[
'Data' => [
['VarCharValue' => 'value3'],
['VarCharValue' => '3']
]
]
],
'ResultSetMetadata' => [
'ColumnInfo' => [
[
'CatalogName' => 'hive',
'SchemaName' => '',
'TableName' => '',
'Name' => 'column_name1',
'Label' => 'column_name1',
'Type' => 'varchar',
'Precision' => 2147483647,
'Scale' => 0,
'Nullable' => 'UNKNOWN',
'CaseSensitive' => true
],
[
'CatalogName' => 'hive',
'SchemaName' => '',
'TableName' => '',
'Name' => 'column_name2',
'Label' => 'column_name2',
'Type' => 'integer',
'Precision' => 10,
'Scale' => 0,
'Nullable' => 'UNKNOWN',
'CaseSensitive' => false
]
]
]
];
$expected = [
[
'column_name1' => 'column_name1',
'column_name2' => 0
],
[
'column_name1' => 'value1',
'column_name2' => 1
],
[
'column_name1' => 'value2',
'column_name2' => 2
],
[
'column_name1' => 'value3',
'column_name2' => 3
]
];
$this->assertSame(
$expected,
ConvertAthenaQueryResultstoArray::convert($resultSet)
);
$this->assertSame(
$expected,
ConvertAthenaQueryResultstoArray::convert($resultSet, false)
);
}

public function testConvertWithoutVarCharValue()
Expand Down Expand Up @@ -187,7 +273,7 @@ public function testConvertWithoutVarCharValue()
];
$this->assertSame(
$expected,
ConvertAthenaQueryResultstoArray::convert($resultSet)
ConvertAthenaQueryResultstoArray::convert($resultSet, true)
);
}
}

0 comments on commit 1090310

Please sign in to comment.