Translate queries with the interactive SQL translator

The BigQuery interactive SQL translator lets you translate queries from other SQL dialects into GoogleSQL queries by using the Google Cloud console, the Google Cloud CLI, or the BigQuery Migration Service MCP server. Using the interactive SQL translator helps reduce the time and effort required to migrate workloads to BigQuery, and you can customize translations by using the translation rule feature.

To build a custom software integration, an automated CI/CD pipeline, or another programmatic workflow, see the BigQuery Migration API guide. For information about supported dialects and processing locations, see Supported SQL dialects and Locations.

Before you begin

Before you submit a translation job, do the following steps.

Enable SQL translations

Enable the required API, and get the permissions needed to use a BigQuery SQL translator. For more information, see Enable SQL translations.

Required permissions

To get the permissions that you need to create translation jobs with the interactive translator, the translation API, or the batch SQL translator, ask your administrator to grant you the following IAM roles on the parent resource:

  • Viewing and monitoring migration jobs: MigrationWorkflow Viewer (roles/bigquerymigration.viewer)
  • Submitting migration jobs: MigrationWorkflow Editor (roles/bigquerymigration.editor)
  • Access the Cloud Storage buckets for input and files: Storage Object Admin (roles/storage.objectAdmin) - on the source and destination Cloud Storage bucket.

For more information about granting roles, see Manage access to projects, folders, and organizations.

These predefined roles contain the permissions required to create translation jobs with the interactive translator, the translation API, or the batch SQL translator. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to create translation jobs with the interactive translator, the translation API, or the batch SQL translator:

  • bigquerymigration.workflows.create
  • bigquerymigration.workflows.get
  • bigquerymigration.workflows.list
  • bigquerymigration.workflows.delete
  • bigquerymigration.subtasks.get
  • bigquerymigration.subtasks.list
  • storage.objects.get
  • storage.objects.list
  • storage.objects.create

You might also be able to get these permissions with custom roles or other predefined roles.

To restrict specific translation workflow configurations in your organization, such as disallowing Gemini AI suggestions or restricting translation output types, configure Custom Org Policies for BigQuery Migration Service.

Handling unsupported SQL functions with helper UDFs

When translating SQL from a source dialect to BigQuery, some functions might not have a direct equivalent. To address this, the BigQuery Migration Service (and the broader BigQuery community) provide helper user-defined functions (UDFs) that replicate the behavior of these unsupported source dialect functions.

These UDFs are often found in the bqutil public dataset, allowing translated queries to initially reference them using the format bqutil.<dataset>.<function>(). For example, bqutil.fn.cw_count().

Important considerations for production environments:

While bqutil offers convenient access to these helper UDFs for initial translation and testing, direct reliance on bqutil for production workloads is not recommended for several reasons:

  1. Version control: The bqutil project hosts the latest version of these UDFs, which means their definitions can change over time. Relying directly on bqutil could lead to unexpected behavior or breaking changes in your production queries if a UDF's logic is updated.
  2. Dependency isolation: Deploying UDFs to your own project isolates your production environment from external changes.
  3. Customization: You might need to modify or optimize these UDFs to better suit your specific business logic or performance requirements. This is only possible if they are within your own project.
  4. Security and governance: Your organization's security policies might restrict direct access to public datasets like bqutil for production data processing. Copying UDFs to your controlled environment aligns with such policies.

Deploying helper UDFs to your project:

For reliable and stable production use, you should deploy these helper UDFs into your own project and dataset. This gives you full control over their version, customization, and access. For detailed instructions on how to deploy these UDFs, refer to the UDFs deployment guide on GitHub. This guide provides the necessary scripts and steps to copy the UDFs into your environment.

Locations

The interactive SQL translator is only available in select processing locations. For more information, see Locations.

Gemini-based translation configurations are only available in specific processing locations. For more information, see Google model endpoint locations

Translate a query into GoogleSQL

You can translate a query using the Google Cloud console, the gcloud CLI, or the BigQuery Migration Service MCP server. Select one of the following options:

Console

Follow these steps to translate a query into GoogleSQL:

  1. In the Google Cloud console, go to the BigQuery page.

    Go to BigQuery

  2. In the Editor pane, click Tools > Translation settings.

  3. For Source dialect, select the SQL dialect that you want to translate.

  4. Optional. For Processing location, select the location where you want the translation job to run. For example, if you are in Europe and you don't want your data to cross any location boundaries, select the eu region.

  5. Click Save.

  6. In the Editor pane, click Tools > Enable SQL translation.

    The Editor pane splits into two panes.

  7. In the left pane, enter the query you want to translate.

  8. Click Translate.

    BigQuery translates your query into GoogleSQL and displays it in the right pane. For example, the following screenshot shows translated Teradata SQL:

    Displays a Teradata SQL query translated into GoogleSQL

  9. Optional: To run the translated GoogleSQL query, click Run.

  10. Optional: To return to the SQL editor, click More > Disable SQL translation.

    The Editor pane returns to a single pane.

gcloud

To translate a query from the Google Cloud CLI, use the gcloud alpha bq migration translate command:

gcloud alpha bq migration translate \
  --source-dialect=SOURCE_DIALECT \
  --target-dialect=TARGET_DIALECT \
  --location=LOCATION \
  --input-file=INPUT_FILE

Replace the following:

  • SOURCE_DIALECT: the SQL dialect that you want to translate, such as teradata. The SparkSQL dialect value works for both Databricks SQL and Apache Spark SQL workloads. For the supported values, see Supported SQL dialects.
  • TARGET_DIALECT: the dialect to translate the query into. For example, bigquery.
  • LOCATION: the location where you want the translation job to run. For example, if you are in Europe and you don't want your data to cross any location boundaries, specify the eu region.
  • INPUT_FILE: the local file that contains the query that you want to translate.

The command prints the translated query to the gcloud CLI. To write the translated query to a file instead, add the --output-file flag.

To translate a query without creating a file, pipe it through standard input and omit the --input-file flag:

echo 'SELECT 1;' | gcloud alpha bq migration translate \
  --source-dialect=teradata \
  --target-dialect=bigquery \
  --location=us

To apply the customizations that you would otherwise set in Translation settings, use the following flags:

  • --translation-config-files: a comma-separated list of configuration YAML files. Filenames must end with .config.yaml or .ai_config.yaml.
  • --metadata-gcs-uri: the Cloud Storage URI of a metadata ZIP file.

MCP

To translate a query from an AI application or agent, connect your MCP client to the BigQuery Migration Service MCP server. For setup instructions and required roles, see Configure an MCP client to use the BigQuery Migration Service MCP server.

Prompt your MCP client to translate a query:

Translate this query from DIALECT: QUERY. Use PROJECT_NUMBER and LOCATION.

Replace the following:

  • DIALECT: the dialect of the SQL query that you're translating, such as Teradata. For the supported values, see Supported SQL dialects.
  • QUERY: the SQL query that you're translating.
  • PROJECT_NUMBER: your Google Cloud project number.
  • LOCATION: the location where you want the translation job to run, such as us or eu.

When you use this prompt, the MCP client calls the translate_query tool, and then periodically calls the get_translation tool until the translation state is SUCCEEDED or FAILED and displays the translated query and any translation logs.

You can also ask your MCP client to include a metadata ZIP file or configuration YAML files in the translation, explain the translation with the explain_translation tool, or infer source DDL statements with the generate_ddl_suggestion and fetch_ddl_suggestion tools. For more sample prompts, see Use the BigQuery Migration Service MCP server.

Explore the translation output

Depending on the configuration files that you provide, translating a query can produce the translated SQL, rewritten source SQL from SOURCE_SQL rules, rewritten target SQL from TARGET_SQL rules, and a translation explanation. For more information, see Transform SQL translations with YAML.

To find each translation output, select one of the following options:

Console

In the Google Cloud console, find each output in the following locations of the SQL editor:

  • Translated SQL: the right pane of the SQL editor after you click Translate.
  • Rewritten source SQL: the Preview suggested changes dialog that appears after you run the translation, or click Assist > View suggestions in the left pane. For more information, see Create and apply Gemini-enhanced translation rules.
  • Rewritten target SQL: click Assist > View suggestions in the right pane. For more information, see Create and apply Gemini-enhanced translation rules.
  • Translation explanation: click Assist > Explain this translation. For more information, see Explain a translation.
  • Query optimization suggestions (Preview): in the Translation results pane, click the Log messages tab, and then click View optimization in the Action column. For more information, see Optimize a GoogleSQL query.

gcloud

By default, the gcloud alpha bq migration translate command prints the translated SQL, any rewritten source SQL, and any rewritten target SQL to standard output. To write each output to a local file instead, use the following flags:

  • --output-file: a local file that receives the translated SQL that the translation engine produces after applying any rules in *.config.yaml files.
  • --source-sql-suggestion-output-file: a local file that receives the rewritten source SQL that Gemini produces before translation by applying SOURCE_SQL rules in *.ai_config.yaml files to the input SQL.
  • --target-sql-suggestion-output-file: a local file that receives the rewritten target SQL that Gemini produces by applying TARGET_SQL rules in *.ai_config.yaml files to the translated SQL.
  • --explanation-output-file: a local file that receives a Gemini-generated explanation of the translation. The command generates an explanation only when you specify this flag, and it doesn't print the explanation to standard output.

Optimize a GoogleSQL query

You can use the interactive SQL translator in the Google Cloud console to optimize existing GoogleSQL queries in BigQuery. When you run an interactive translation on a GoogleSQL query, the interactive SQL translator suggests optional, deterministic SQL transformations that can reduce slot consumption and bytes scanned. Examples of supported optimizations include the following:

  • Materializing repeated common table expressions (CTEs) into temporary tables.
  • Extracting independent scalar subqueries or complex constants into DECLARE variables to improve partition and cluster pruning.
  • Rewriting function-wrapped date or timestamp filters into contiguous range filters for partition pruning.

The performance effect of each transformation depends on your data distribution and workload. Evaluate the proposed SQL against your original query before you deploy it to production.

Prerequisites and catalog metadata

If you haven't used the interactive SQL translator before, then enable SQL translations and get the required permissions.

Query optimization suggestions target queries where you select GoogleSQL as the source dialect. If you don't configure a translation metadata ZIP file, then the interactive SQL translator automatically loads catalog metadata from BigQuery.

To load catalog metadata automatically, you need the bigquery.tables.get and bigquery.routines.get permissions on the referenced datasets, tables, or routines. The predefined BigQuery Metadata Viewer role (roles/bigquery.metadataViewer) includes these permissions.

Review and apply optimization suggestions

To review and apply query optimization suggestions in the Google Cloud console, do the following:

  1. If SQL translation isn't already enabled, then in the Editor pane, click Tools > Enable SQL translation.

  2. In the Translating from list, select GoogleSQL.

  3. In the left pane, enter your GoogleSQL query, and then click Translate. For full interactive translation instructions, see Translate a query into GoogleSQL.

  4. In the Translation results pane, click the Log messages tab.

  5. Find a row that has OptimizationTransform in the Category column, a message that starts with Potential optimization available: RULE_NAME, and View optimization in the Action column.

    In the message, RULE_NAME is the optimization name, such as REWRITE_CTE_TO_TEMP_TABLE. If no matching row appears, then no supported optimizations apply to your query. You can grant the translator access to catalog metadata to increase optimization opportunities.

  6. In that row, click View optimization.

    The Query optimization suggestions dialog opens with that rule preselected and all other detected rules listed in the Available Optimizations pane.

  7. In the Available Optimizations pane, select or clear optimization rules.

    The side-by-side comparison between the Translated query (Current) and Optimized query (Proposed) panes updates automatically. To read a rule description, hold the pointer over Show optimization description next to the rule. You can also edit the query in the Optimized query (Proposed) pane directly before you click Apply.

  8. To replace the translated query in the editor with the optimized GoogleSQL query, click Apply.

    If you reopen the dialog for the same translation, then previously applied rules remain selected and can't be cleared, and you can select additional rules to apply.

  9. Optional: To test the optimized query, click Run.

    To compare slot usage with the original query, see Query plan and timeline.

Limitations

Query optimization suggestions in the Google Cloud console have the following limitations:

  • The Query optimization suggestions dialog loads metadata only from the BigQuery catalog and doesn't use configured translation metadata from Cloud Storage.
  • For other source dialects, if referenced tables don't exist in BigQuery, then schema-dependent rules might not apply, and unknown column types can reduce output quality.
  • Suggestions apply only selected query-level transformations and exclude DDL transformations, such as the REWRITE_ZERO_SCALE_NUMERIC_AS_INTEGER and APPROXIMATE_RANGE_PARTITIONS rules.
  • Suggestions don't accept custom parameters and always run with default parameter values.

For the full set of optimization capabilities, including other source dialects, DDL transformations, and custom parameters, you can use optimizer rules in a configuration YAML file during translation. For more information, see Optimize and improve the performance of translated SQL.

Use Gemini with the interactive SQL translator

You can configure the interactive SQL translator to adjust how the interactive SQL translator translates your source SQL. You can do so by providing your own rules for use with Gemini in a YAML configuration file, or by providing a configuration YAML file containing SQL object metadata or object mapping information.

Create and apply Gemini-enhanced translation rules

You can customize the way the interactive SQL translator translates SQL by creating translation rules. The interactive SQL translator adjusts its translations based on any Gemini-enhanced SQL translation rules that you assign to it, letting you customize the translation results based on your migration needs.

To create a Gemini-enhanced SQL translation rule, you can either create it in the console, or create a configuration YAML file and upload it to Cloud Storage.

Console

To create a Gemini-enhanced SQL translation rule for the input SQL, write an input SQL query in the query editor, then click ASSIST > Customize. (Preview)

Customize translation input

Similarly, to create a Gemini-enhanced SQL translation rule for the output SQL, run an interactive translation, then click ASSIST > Customize this translation.

Customize translation output

When the Customize menu appears, continue with the following steps.

  1. Use one or both of the following prompts to create a translation rule:

    • In the Find and replace a pattern prompt, specify a SQL pattern that you want to replace in the Replace field, and a SQL pattern to replace it in the With field.

      A SQL pattern can contain any number of statements, clauses, or functions in a SQL script. When you create a rule using this prompt, the Gemini enhanced SQL translation identifies any instances of that SQL pattern in the SQL query and dynamically replaces it with another SQL pattern. For example, you can use this prompt to create a rule that replaces all occurrences of months_between (X,Y) with date_diff(X,Y,MONTH).

    • In the Describe a change to the output field, type a change to the SQL translation output in natural language.

      When you create a rule using this prompt, the Gemini-enhanced SQL translation identifies the request and makes the specified change to the SQL query.

  2. Click Preview.

  3. In the Suggestions generated by Gemini dialog, review the changes made by the Gemini-enhanced SQL translation to the SQL query based on your rule.

    Apply changes from Gemini-based configuration YAML file

  4. Optional: To add this rule for use with future translations, select the Save this prompt... checkbox.

    Rules are saved in the default configuration YAML file, or __default.ai_config.yaml. This configuration YAML file is saved to the Cloud Storage folder as specified in the Translation Configuration Source Location field in the translation settings. If the Translation Configuration Source Location isn't already set, a folder browser appears and lets you select one. A configuration YAML file is subject to configuration file size limitations.

  5. To apply the suggested changes to the SQL query, click Apply.

YAML

To create a Gemini-enhanced SQL translation rule, you can create a Gemini-based configuration YAML file and upload it to Cloud Storage. For more information, see Create a Gemini-based configuration YAML file.

Once you have uploaded a Gemini-enhanced SQL translation rule and uploaded it to Cloud Storage, you can apply the rule by doing the following:

  1. In the Google Cloud console, go to the BigQuery page.

    Go to BigQuery

  2. In the query editor, click Tools > Translation settings.

  3. In the Translation Configuration Source Location field, specify the path to the Gemini-based YAML file stored in a Cloud Storage folder.

  4. Click Save.

    Once saved, run an interactive translation. The interactive translator suggests changes to your translations based on the rules in your configuration YAML file if one is available.

If a Gemini suggestion is available for the input based on your rule, then the Preview suggested changes dialog appears and shows possible changes to the translation input. (Preview)

If a Gemini suggestion is available for the output based on your rule, a notification banner appears in the code editor. To review and apply these suggestions, do the following:

  1. Click Assist > View suggestions on either side of the code editor to revisit the suggested changes to the corresponding query.

    Apply changes from Gemini-based configuration YAML file

  2. In the Suggestions generated by Gemini dialog, review the changes made by Gemini to the SQL query based on your translation rule.

  3. To apply the suggested changes to the translation output, click Apply.

Update Gemini-based configuration YAML file

To update an existing configuration YAML file, do the following:

  1. On the Suggestions generated in Gemini dialog, click View Gemini rule config file.

  2. When the configuration editor appears, select the configuration YAML file that you want to edit.

  3. Make the change and click Save.

  4. Close the YAML editor by clicking Done.

  5. Run an interactive translation to apply the updated rule.

Explain a translation

After running an interactive translation, you can request a Gemini-generated text explanation. The generated text includes a summary of the translated SQL query. Gemini also identifies translation differences and inconsistencies between the source SQL query and the translated GoogleSQL query.

To get Gemini-generate SQL translation explanation, do the following:

  1. To create a Gemini-generated SQL translation explanation, click Assist, and then click Explain this translation.

    Explain translation button.

Translate with a batch translation configuration ID

You can run an interactive query with the same translation configurations as a batch translation job by providing a batch translation configuration ID.

  1. In the query editor, click Tools > Translation settings.
  2. In the Translation Configuration ID field, provide a batch translation configuration ID to apply the same translation configuration from a completed BigQuery batch migration job.

    To find a job's batch translation configuration ID, select a batch translation job from the SQL translation page, and then click the Translation Configuration tab. The batch translation configuration ID is listed as Resource name.

  3. Click Save.

Translate with additional configurations

You can run an interactive query with additional translation configurations by specifying configuration YAML files stored on a Cloud Storage folder. Translation configurations might include SQL object metadata or object mapping information from the source database that can improve translation quality. For example, include DDL information or schemas from the source database to improve interactive SQL translation quality.

To specify translation configurations by providing a location to the translation configuration source files, do the following:

  1. In the query editor, click Tools > Translation settings.
  2. In the Translation Configuration Source Location field, specify the path to the translation configuration files stored in a Cloud Storage folder.

    The BigQuery interactive SQL translator supports metadata ZIP files containing translation metadata and object name mapping. For information on how to upload files to Cloud Storage, see Upload objects from a file system.

  3. Click Save.

Configuration file size limitations

When you use a translation configuration file with the BigQuery interactive SQL translator, the compressed metadata file or YAML config file must be smaller than 50 MB. If the file size exceeds 50 MB, the interactive translator skips that configuration file during translation and produces an error message similar to the following:

CONFIG ERROR: Skip reading file "gs://metadata-file.zip". File size (150,000,000 bytes) exceeds limit (50 MB).

One method to reduce metadata file size is to use the --database or --schema flags to only extract metadata for databases or schemas that are relevant for the translation input queries. For more information about using these flags when you generate metadata files, see Global flags.

Troubleshoot translation errors

The following sections describe commonly encountered errors when using the interactive SQL translator.

RelationNotFound or AttributeNotFound translation issues

After translating a query using the interactive SQL translator, you might encounter a failed translation with the RelationNotFound or AttributeNotFound error.

You can find failed translations by going to the Translation details page in BigQuery in the Google Cloud console and opening the Log Messages tab.

To ensure the most accurate translation, you can enter the data definition language (DDL) statements for any tables used in a query prior to the query itself. For example, if you want to translate the Amazon Redshift query select table1.field1, table2.field1 from table1, table2 where table1.id = table2.id;, enter the following SQL statements into the interactive SQL translator:

create table schema1.table1 (id int, field1 int, field2 varchar(16));
create table schema1.table2 (id int, field1 varchar(30), field2 date);

select table1.field1, table2.field1
from table1, table2
where table1.id = table2.id;

Fix translation issues with Gemini

To fix failed translation jobs with the RelationNotFound or AttributeNotFound errors, you can also use Gemini to resolve these issues:

  1. In BigQuery in the Google Cloud console, go to the Translation details page and open the Log Messages tab.
  2. Click the query that has the message RelationNotFound or AttributeNotFound in the Category column.
  3. Click Suggested fix.
  4. Click Apply.
  5. To retranslate the query, click Translate.

Pricing

There is no charge to use the interactive SQL translator. However, storage used to store input and output files incurs the normal fees. For more information, see Storage pricing.

What's next

Learn more about the following steps in data warehouse migration: