The ML.UNDERSTAND_TEXT function
This document describes the ML.UNDERSTAND_TEXT function, which lets you
analyze text that's stored in BigQuery tables by using the Cloud Natural Language API.
Syntax
ML.UNDERSTAND_TEXT(
MODEL `PROJECT_ID.DATASET.MODEL`,
{ TABLE `PROJECT_ID.DATASET.TABLE` | (QUERY_STATEMENT) },
STRUCT('OPTION_NAME' AS nlu_option
[, FLATTEN_JSON_OUTPUT AS flatten_json_output]
[, ENCODING_TYPE AS encoding_type])
)
Arguments
ML.UNDERSTAND_TEXT takes the following arguments:
PROJECT_ID: the project that contains the resource.DATASET: the dataset that contains the resource.MODEL: the name of a remote model with aREMOTE_SERVICE_TYPEofCLOUD_AI_NATURAL_LANGUAGE_V1.TABLE: the name of the BigQuery table that contains text data. The text analysis is applied on the column with nametext_contentin this table. If your table does not havetext_contentcolumn, use aSELECTstatement for this argument to provide an alias for an existing table column, as shown in the following example:SELECT * from ML.UNDERSTAND_TEXT( mydataset.mymodel, (SELECT comment AS text_content from mydataset.mytable), STRUCT('ANALYZE_SYNTAX' AS nlu_option) );An error occurs if no
text_contentcolumn is available.QUERY_STATEMENT: a query whose result contains the text data. The text analysis is applied on the column in the query namedtext_content. You can alias an existing table column astext_contentif necessary. For information about the supported SQL syntax of theQUERY_STATEMENTclause, see GoogleSQL query syntax.OPTION_NAME: aSTRINGvalue that specifies the feature name of a supported Natural Language API feature. The supported features are as follows:FLATTEN_JSON_OUTPUT: aBOOLvalue that determines whether the JSON content returned by the function is parsed into separate columns. The default isFALSE.ENCODING_TYPE: aSTRINGvalue that specifies the encoding that the Cloud Natural Language API uses to determine encoding-dependent information such as thebeginOffsetvalue. For more information, seeEncodingType. You can specify this option for any NLU option except forCLASSIFY_TEXT. The default value isNONE. The supported types are as follows:NONEUTF8UTF16UTF32
Output
ML.UNDERSTAND_TEXT returns the input table plus the following columns:
ml_understand_text_result: aJSONvalue that contains the text analysis result from Natural Language API. This column is returned whenflatten_json_outputisFALSE.entities: aJSONvalue that contains the recognized entities in the input document. This column is returned whenflattened_json_outputisTRUEandoption_nameisANALYZE_ENTITIESorANALYZE_ENTITY_SENTIMENT.language: aSTRINGvalue that gives the language of the text. This column is returned whenflattened_json_outputisTRUEandoption_nameisANALYZE_ENTITIES,ANALYZE_ENTITY_SENTIMENT,ANALYZE_SENTIMENT, orANALYZE_SYNTAX.sentiment: aJSONvalue that contains the overall sentiment of the input document. This column is returned whenflattened_json_outputisTRUEandoption_nameisANALYZE_SENTIMENT.sentences: aJSONvalue that contains the sentiment for all sentences in the document. This column is returned whenflattened_json_outputisTRUEandoption_nameisANALYZE_SENTIMENTorANALYZE_SYNTAX.tokens: aJSONvalue that contains the tokens, along with their syntactic information, in the input document. This column is returned whenflattened_json_outputisTRUEandoption_nameisANALYZE_SYNTAX.categories: aJSONvalue that contains the categories representing the input document. This column is returned whenflattened_json_outputisTRUEandoption_nameisCLASSIFY_TEXT.ml_understand_text_status: aSTRINGvalue that contains the API response status for the corresponding row. This value is empty if the operation was successful.
Quotas
See Cloud AI service functions quotas and limits.
Known issues
Sometimes after a query job that uses this function finishes successfully, some returned rows contain the following error message:
Aretryableerroroccurred:RESOURCEEXHAUSTEDerrorfrom<remoteendpoint>
This issue occurs because BigQuery query jobs finish successfully
even if the function fails for some of the rows. The function fails when the
volume of API calls to the remote endpoint exceeds the quota limits for that
service. This issue occurs most often when you are running multiple parallel
batch queries. BigQuery retries these calls, but if the retries
fail, the resource exhausted error message is returned.
To iterate through inference calls until all rows are successfully processed, you can use the BigQuery remote inference SQL scripts or the BigQuery remote inference pipeline Dataform package.
Locations
ML.UNDERSTAND_TEXT must run in the same region as the remote model that the
function references. For more information about supported locations for models
based on the Natural Language API, see Locations for remote models.
Examples
Example 1
The following example applies classify_text on the bq table mybqtable in
mydataset.
#CreateModel CREATEORREPLACEMODEL `myproject.mydataset.mynlpmodel` REMOTEWITHCONNECTION`myproject.myregion.myconnection` OPTIONS(remote_service_type='cloud_ai_natural_language_v1');
#UnderstandText SELECT*FROMML.UNDERSTAND_TEXT( MODEL`mydataset.mynlpmodel`, TABLE`mydataset.mybqtable`, STRUCT('classify_text'ASnlu_option) );
The output is similar to the following:
ml_understand_text_result|ml_understand_text_status|text_content|
-------|--------|--------
{"categories":[{"confidence":0.51999998,"name":"/Arts & Entertainment/TV & Video/TV Shows & Programs"}]}||ThatactoronTVmakesmoviesinHollywoodandalsostarsinavarietyofpopularnewTVshows.
Example 2
The following example classify the text in the column text_content in the
table mybqtable, selects the rows where confidence is higher than 0.5, and
then returns the results in separate columns.
CREATETABLE `mydataset.classfied_result`AS( SELECT text_contentAS`OriginalInput`, STRING(ml_understand_text_result.categories[0].name)AS`ClassifiedName`, FLOAT64(ml_understand_text_result.categories[0].confidence)AS`Confidence`, ml_understand_text_statusAS`Status` FROM ML.UNDERSTAND_TEXT(MODEL`mydataset.mynlpmodel`, TABLE`mydataset.mybqtable`, STRUCT('classify_text'ASnlu_option)) ); SELECT * FROM `mydataset.classfied_result` WHERE confidence>0.5;
The output is similar to the following:
OriginalInput|ClassifiedName|Confidence|Status|
-------|--------|--------|--------
ThatactoronTVmakesmoviesinHollywoodandalsostarsinavarietyofpopularnewTVshows.|/Arts & Entertainment/TV & Video/TVShows & Programs|0.51999998||
If you get an error like query limit exceeded, you might have exceeded the
quota for this function, which
can leave you with unprocessed rows. Use the following query to complete
processing the unprocessed rows:
CREATETABLE `mydataset.classfied_result_next`AS( SELECT text_contentAS`OriginalInput`, STRING(ml_understand_text_result.categories[0].name)AS`ClassifiedName`, FLOAT64(ml_understand_text_result.categories[0].confidence)AS`Confidence`, ml_understand_text_statusAS`Status` FROM ML.UNDERSTAND_TEXT(MODEL`mydataset.mynlpmodel`, (SELECT`OriginalInput`astext_contentFROM`mydataset.classfied_result` WHEREStatus!=''), STRUCT('classify_text'ASnlu_option)) ); SELECT*FROM`mydataset.classfied_result_next`;
What's next
- Get step-by-step instructions on how to
analyze text in a BigQuery table
using the
ML.UNDERSTAND_TEXTfunction. - Learn more about other functions you can use to analyze BigQuery data.
- For information about model inference, see Model inference overview.
- For more information about supported SQL statements and functions for generative AI models, see End-to-end user journeys for generative AI models.