diff --git a/packages/php-datatypes/src/Definition/Bigquery.php b/packages/php-datatypes/src/Definition/Bigquery.php index e0fd6ce30..00f955ef8 100644 --- a/packages/php-datatypes/src/Definition/Bigquery.php +++ b/packages/php-datatypes/src/Definition/Bigquery.php @@ -76,7 +76,17 @@ class Bigquery extends Common public const TYPE_STRING = 'STRING'; // STRING(L) L is a positive INT64 value public const TYPE_STRUCT = 'STRUCT'; - + // Map of types available in SQL api but not supported by REST API + // Most of them are aliases for other types + public const REST_API_TYPES_MAP = [ + self::TYPE_INT => self::TYPE_INT64, + self::TYPE_SMALLINT => self::TYPE_INT64, + self::TYPE_BIGINT => self::TYPE_INT64, + self::TYPE_TINYINT => self::TYPE_INT64, + self::TYPE_BYTEINT => self::TYPE_INT64, + self::TYPE_DECIMAL => self::TYPE_NUMERIC, + self::TYPE_BIGDECIMAL => self::TYPE_BIGNUMERIC, + ]; public const TYPES = [ self::TYPE_ARRAY, self::TYPE_BOOL, diff --git a/packages/php-table-backend-utils/src/Column/Bigquery/Parser/SQLtoRestDatatypeConverter.php b/packages/php-table-backend-utils/src/Column/Bigquery/Parser/SQLtoRestDatatypeConverter.php index 4e65d1139..9b68c50f5 100644 --- a/packages/php-table-backend-utils/src/Column/Bigquery/Parser/SQLtoRestDatatypeConverter.php +++ b/packages/php-table-backend-utils/src/Column/Bigquery/Parser/SQLtoRestDatatypeConverter.php @@ -191,7 +191,7 @@ public static function convertColumnToRestFormat(BigqueryColumn $column): array // others $schema = [ 'name' => $column->getColumnName(), - 'type' => $definition->getType(), + 'type' => self::getRestTypeForType($definition->getType()), ]; $schema = self::updateSchemaLength($definition->getLength(), $schema); if ($definition->isNullable() === false) { @@ -204,4 +204,12 @@ public static function convertColumnToRestFormat(BigqueryColumn $column): array /** @phpstan-var BigqueryTableFieldSchema */ return $schema; } + + private static function getRestTypeForType(string $type): string + { + if (array_key_exists($type, Bigquery::REST_API_TYPES_MAP)) { + return Bigquery::REST_API_TYPES_MAP[$type]; + } + return $type; + } } diff --git a/packages/php-table-backend-utils/tests/Unit/Column/Bigquery/Parser/SQLtoRestDatatypeConverterTest.php b/packages/php-table-backend-utils/tests/Unit/Column/Bigquery/Parser/SQLtoRestDatatypeConverterTest.php index 4aaf32132..907bba9c4 100644 --- a/packages/php-table-backend-utils/tests/Unit/Column/Bigquery/Parser/SQLtoRestDatatypeConverterTest.php +++ b/packages/php-table-backend-utils/tests/Unit/Column/Bigquery/Parser/SQLtoRestDatatypeConverterTest.php @@ -388,6 +388,77 @@ public function definitions(): Generator ], ], ]; + // aliases + yield 'INT' => [ + 'type' => Bigquery::TYPE_INT, + 'length' => null, + 'default' => null, + 'nullable' => true, + 'expected' => [ + 'name' => 'myCol', + 'type' => 'INT64', + ], + ]; + yield 'SMALLINT' => [ + 'type' => Bigquery::TYPE_SMALLINT, + 'length' => null, + 'default' => null, + 'nullable' => true, + 'expected' => [ + 'name' => 'myCol', + 'type' => 'INT64', + ], + ]; + yield 'BIGINT' => [ + 'type' => Bigquery::TYPE_BIGINT, + 'length' => null, + 'default' => null, + 'nullable' => true, + 'expected' => [ + 'name' => 'myCol', + 'type' => 'INT64', + ], + ]; + yield 'TINYINT' => [ + 'type' => Bigquery::TYPE_TINYINT, + 'length' => null, + 'default' => null, + 'nullable' => true, + 'expected' => [ + 'name' => 'myCol', + 'type' => 'INT64', + ], + ]; + yield 'BYTEINT' => [ + 'type' => Bigquery::TYPE_BYTEINT, + 'length' => null, + 'default' => null, + 'nullable' => true, + 'expected' => [ + 'name' => 'myCol', + 'type' => 'INT64', + ], + ]; + yield 'DECIMAL' => [ + 'type' => Bigquery::TYPE_DECIMAL, + 'length' => null, + 'default' => null, + 'nullable' => true, + 'expected' => [ + 'name' => 'myCol', + 'type' => 'NUMERIC', + ], + ]; + yield 'BIGDECIMAL' => [ + 'type' => Bigquery::TYPE_BIGDECIMAL, + 'length' => null, + 'default' => null, + 'nullable' => true, + 'expected' => [ + 'name' => 'myCol', + 'type' => 'BIGNUMERIC', + ], + ]; } /**