From 109031043ea6f4c10964cc17aa89921be780659b Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 28 Apr 2021 09:05:59 +0900 Subject: [PATCH] feat: add the option to skip header https://github.com/abetomo/Convert-Athena-QueryResults-to-Array/issues/8 --- src/ConvertAthenaQueryResultstoArray.php | 6 +- .../ConvertAthenaQueryResultstoArrayTest.php | 90 ++++++++++++++++++- 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/ConvertAthenaQueryResultstoArray.php b/src/ConvertAthenaQueryResultstoArray.php index d30de70..ece99b9 100644 --- a/src/ConvertAthenaQueryResultstoArray.php +++ b/src/ConvertAthenaQueryResultstoArray.php @@ -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) { diff --git a/tests/ConvertAthenaQueryResultstoArrayTest.php b/tests/ConvertAthenaQueryResultstoArrayTest.php index 512cf8d..8b9daca 100644 --- a/tests/ConvertAthenaQueryResultstoArrayTest.php +++ b/tests/ConvertAthenaQueryResultstoArrayTest.php @@ -35,7 +35,7 @@ public function testCast() } } - public function testConvert() + public function testConvertSkipHeader() { $resultSet = [ 'Rows' => [ @@ -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() @@ -187,7 +273,7 @@ public function testConvertWithoutVarCharValue() ]; $this->assertSame( $expected, - ConvertAthenaQueryResultstoArray::convert($resultSet) + ConvertAthenaQueryResultstoArray::convert($resultSet, true) ); } }