Skip to content

Commit

Permalink
add Bigquery::REST_API_TYPES_MAP and use it to convert unsupported ty…
Browse files Browse the repository at this point in the history
…pes in BQ REST API
  • Loading branch information
zajca committed Jan 23, 2024
1 parent 8bb3925 commit 7c0fc35
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/php-datatypes/src/Definition/Bigquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
}

/**
Expand Down

0 comments on commit 7c0fc35

Please sign in to comment.