diff --git a/java/google/registry/tools/MakeBillingTablesCommand.java b/java/google/registry/tools/MakeBillingTablesCommand.java index 66a213808..08339f2dd 100644 --- a/java/google/registry/tools/MakeBillingTablesCommand.java +++ b/java/google/registry/tools/MakeBillingTablesCommand.java @@ -46,6 +46,8 @@ final class MakeBillingTablesCommand extends BigqueryCommand { private static final SqlTemplate CURRENCY_TABLE_SQL = getSql("currency_table.sql"); private static final SqlTemplate REGISTRAR_DATA_SQL = getSql("registrar_data_view.sql"); + private static final SqlTemplate REGISTRAR_ACCOUNT_DATA_SQL = + getSql("registrar_account_data_view.sql"); private static final SqlTemplate REGISTRY_DATA_SQL = getSql("registry_data_view.sql"); private static final SqlTemplate BILLING_DATA_SQL = getSql("billing_data_view.sql"); @@ -59,6 +61,7 @@ final class MakeBillingTablesCommand extends BigqueryCommand { try { makeCurrencyTable(); makeRegistrarView(); + makeRegistrarAccountView(); makeRegistryView(); makeBillingView(); } catch (TableCreationException e) { @@ -99,6 +102,28 @@ final class MakeBillingTablesCommand extends BigqueryCommand { .build())); } + /** + * Generates a view of registrar account data, which includes the billing account id and the + * currency used. + * + *
The generated view is similar to the one generated by {@link #makeRegistrarView()}, but it + * uses currency-specific billing account id and does not include allowed TLDs. + */ + private void makeRegistrarAccountView() throws Exception { + handleTableCreation( + "registrar account data view", + bigquery() + .query( + REGISTRAR_ACCOUNT_DATA_SQL.put("SOURCE_DATASET", sourceDatasetId).build(), + bigquery() + .buildDestinationTable("RegistrarAccountData") + .description( + "Synthetic view of registrar information " + + "with currency-specific billing account id.") + .type(TableType.VIEW) + .build())); + } + /** Generates a view of registry data to feed into later views. */ private void makeRegistryView() throws Exception { handleTableCreation( diff --git a/java/google/registry/tools/sql/registrar_account_data_view.sql b/java/google/registry/tools/sql/registrar_account_data_view.sql new file mode 100644 index 000000000..d8a58fa4f --- /dev/null +++ b/java/google/registry/tools/sql/registrar_account_data_view.sql @@ -0,0 +1,50 @@ +-- Copyright 2017 The Nomulus Authors. All Rights Reserved. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +-- Registrar Account Data View SQL +-- +-- This query lists registrar IDs with billing account IDs and corresponding +-- currencies. +-- +-- The table that contains both billing account IDs and currencies as repeated +-- fields are first flattened to two separate tables, with IDs and currencies +-- in each table, and the corresponding row numbers, partitioned over registrar. +-- The row numbers are used to join the two tables together, restoring the +-- original mapping between IDs and currencies. +SELECT + I.registrarId AS registrarId, + -- Apply no-op STRING() function to keep BigQuery schema transformation logic + -- from wanting different names in direct query vs save-as-view. + STRING(C.billingAccountMap.currency) AS currency, + STRING(I.billingAccountMap.accountId) AS billingAccountId, +FROM ( + SELECT + __key__.name AS registrarId, + billingAccountMap.accountId, + ROW_NUMBER() OVER (PARTITION BY registrarId) AS pos + FROM + FLATTEN([latest_snapshot.Registrar], billingAccountMap.accountId)) AS I +JOIN ( + SELECT + __key__.name AS registrarId, + billingAccountMap.currency, + ROW_NUMBER() OVER (PARTITION BY registrarId) AS pos + FROM + FLATTEN([%SOURCE_DATASET%.Registrar], billingAccountMap.currency)) AS C +ON + I.registrarId == C.registrarId + AND I.pos == C.pos +ORDER BY + registrarId, + I.pos