How to fix the error 'API call to bigquery.jobs.query failed with error: Encountered " "FROM" "from "" at .... Was expecting: ")"' - google-apps-script

I am trying to run a bigquery api call using the query string(attached in the code) from appscript; The query runs perfectly fine in bigquery UI
I have tried the following with no success:
1. include parentheses in orders.creation_date in the query string
2. replace orders with the actual table table i.e [my-project:theservices.theservice_order_item]
/**
* Runs a BigQuery query and logs the results in a spreadsheet.
*/
function runQuery() {
var projectId = 'my-project';
var request = {
query:
"SELECT extract(date from orders.creation_date) as the_date \n FROM [my-project:theservices.theservice_order_item] AS orders LIMIT 10;"
};
};
Following is the error I get:
API call to bigquery.jobs.query failed with error: Encountered " "FROM" "from "" at line 1, column 22. Was expecting: ")" ... (line 23, file "Code")

Quoting App Script BigQuery Standard SQL insert or update statement:
You need to set the useLegacySql flag/parameter to false, to indicate that you want to use standard SQL, like so:
var job = {
configuration: {
query: {
query: 'SELECT ....',
useLegacySql: false
}
Additionally, when a table looks like this - that's #legacySQL:
FROM [my-project:theservices.theservice_order_item]
In #standardSQL the table should be enclosed in tilde '`' and has a '.' between the project and dataset name:
FROM `my-project.theservices.theservice_order_item`

Related

Trying to execute a BiqQuery Stored Procedure from a Google Cloud Function

I have the following code in Google Cloud Function. The cloud function is written in.NET 6 C#
var testsku = 1414;
var parameters = new BigQueryParameter[]
{
new BigQueryParameter("skuId", BigQueryDbType.Int64, testsku),
};
BigQueryClient client = BigQueryClient.Create(projectID);
string query = #"CALL 'xxxxxxx.xxxxxx.fraudskus'()";
BigQueryJob job = client.CreateQueryJob(
sql: query,
parameters: parameters,
options: new QueryOptions { UseQueryCache = false });
// Wait for the job to complete.
job.PollUntilCompleted();
// Display the results
int numOrders = 0;
foreach (BigQueryRow row in client.GetQueryResults(job.Reference))
{
numOrders++;
}
message = "Num of rows = " + numOrders;
The stored procedure runs fine in BIGQUERY and outputs the right results, but I am getting errors on the call to the stored procedure as follows.
"The service bigquery has thrown an exception.
No HttpStatusCode was specified.
Google.Apis.Requests.RequestError
Job unique-moon-366800/US/job_24e29215_e011_4d66_a2c4_bbf5640d5cc6 contained 2 error(s). First error message: Syntax error: Unexpected string literal 'unique-moon-366800.ecommerce_fraud_ds.fraudskus' at [1:6] [0]
Errors [
Message[Syntax error: Unexpected string literal 'xxxx.xxxxx.fraudskus' at [1:6]] Location[query - ] Reason[invalidQuery] Domain[]
Message[Syntax error: Unexpected string literal 'xxxx.xxxxx.fraudskus' at [1:6]] Location[query - ] Reason[invalidQuery] Domain[]
]
Google.GoogleApiException: The service bigquery has thrown an exception. No HttpStatusCode was specified. Job xxxxxxx/US/job_24e29215_e011_4d66_a2c4_bbf5640d5cc6 contained 2 error(s). First error message: Syntax error: Unexpected string literal 'unique-moon-366800.ecommerce_fraud_ds.fraudskus' at [1:6]
at Google.Cloud.BigQuery.V2.BigQueryJob.ThrowOnFatalError()
at Google.Cloud.BigQuery.V2.BigQueryJob.GetQueryDestinationTable()
at Google.Cloud.BigQuery.V2.BigQueryJob.GetQueryResults(GetQueryResultsOptions options)
at Google.Cloud.BigQuery.V2.BigQueryClientImpl.GetQueryResults(JobReference jobReference, GetQueryResultsOptions options)
at SimpleHttpFunction.Function.HandleAsync(HttpContext context) in /workspace/Function.cs:line 196
at Google.Cloud.Functions.Hosting.HostingInternals.Execute(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)"
I can get the results I expect if I use sql SELECT, but not if I try to call the stored procedure.

Error: '1054 (42S22): Unknown column 'Jo' in 'field list''

I'm attempting to execute a simple query that inserts two names, "self.player1", and "self.player2" as VARCHAR datatypes into a table on a MySQL server version 8.0.31. When I call a custom-built function called "execute_query()" provided below, I receive the following error in my terminal:
Error: '1054 (42S22): Unknown column 'Jo' in 'field list''
Function Definition of "execute_query()":
def execute_query(self, query):
''' This function takes SQL queries stored
in Python as strings and passes them
to the "cursor.execute()" method to
execute them on the server'''
cursor = self.connection.cursor()
try:
cursor.execute(query)
self.connection.commit() # Implements commands detailed in SQL queries
print(query + "Query successful")
except Error as err:
print(f"Error: '{err}'")
The names of the attributes are "player_1" and "player_2", and the values to be inserted are assigned to variable names "self.player1", and "self.player2" as shown below.
query = "USE " + self.db_obj.db_name + ";"
self.db_obj.execute_query(query)
query = "INSERT INTO `match`(player_1, player_2) VALUES (" + self.player1 + "," + self.player2 + ");"
It is important to mention that the issue is not with the "execute_query()" function as I have been able to successfully execute other queries. Additionally, I have run through numerous other similar issues on this discussion forum and have tried dropping triggers, changing secure file priviliges, and hardcoding the value of my entries into the query as opposed to concatenating the values to other strings to produce the resultant query, and have not had any luck.
The design of the table, match is provided below (Note: I was not able to write out the contents of this query to a text file using the following command:
SELECT * FROM `match`
INTO OUTFILE "test.txt";
as I received this error:
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
The query that I used to generated the table in the image below is:
DESCRIBE `match`;
Your error says : Error: '1054 (42S22): Unknown column 'Jo' in 'field list''.
The word 'Jo' in the error is not a field name but a player name. You need to modify your query to :
query = "INSERT INTO `match`(player_1, player_2) VALUES ('" + self.player1 + "','" + self.player2 + "');"
Since string values need to be between single quotes.

BigQuery errs with DELETE from AppsScript? [duplicate]

This question already has answers here:
Insert data into BigQuery from a Google Script : Encountered ""
(2 answers)
Closed 1 year ago.
I am attempting to issue a delete to BigQuery from AppsScript:
var req = {
// gotta place the where clause else BQ abends
query: "delete from `" + projectId + "." + datasetId + "." + tableName + "` where 1=1;"
};
var queryResults = BigQuery.Jobs.query(req,projectId);
Alas, I get the following error:
GoogleJsonResponseException: API call to bigquery.jobs.query failed with error: 1.1 - 1.53: Unrecognized token delete. [Try using standard SQL (https://cloud.google.com/bigquery/docs/reference/standard-sql/enabling-standard-sql)] (line 61, file "Code")
Obviously, the problem is not "standard" syntax here, rather it appears as though the API is pre-validating the SQL statement; said validator likely does not realize deletes are now supported.
What mechanism is to be used then to delete table data from AppsScript?
You need to add #standardSQL to your sql in order to run BigQuery from an appsscript
var req = {
// gotta place the where clause else BQ abends
query: "#standardSQL \n delete from `" + projectId + "." + datasetId + "." + tableName + "` where 1=1;"
};
var queryResults = BigQuery.Jobs.query(req,projectId);
The \n isn't technically required, it is a formatting preference for me. When/if you look up the call in BigQuery's "Query History" it will show the commands on separate lines

MySQL query within a function in R

I have created a function within r which generates a variable called value.
This value now needs to be inserted into the database for which I am using RMySQL package
but When I try to run the function I get the following error
Error in .local(conn, statement, ...) :
unused arguments ("Aryan", ");")
The function is very long and works well until the point where the variable generated is inserted into the database which is as under:
pedd<- function(breed, thattrait) {
library(DBI)
library(RMySQL)
...
...
if (thattrait == "weight"){
rs<- fn$dbSendQuery(con,
"INSERT INTO mytable
(weight, race)
VALUES ('$value', ", race, ");")
} else {
inser <- paste0("Update mytable Set ",wean," = '$value' where race = ", race, "")
rswean<- fn$dbSendQuery(con, inser)
}
I am calling the function thus :
pedd("Aryan", "weight")
I cannot understand where I am wrong.

Can't access object properties

I am using NodeJS and node-mysql, I've created a DB layer to allow easy integration with other DBs (example MongoDB) for later use. At this point in time, the syntax is essentially the same as MySQL.
I am placing a call to the query object of MySQL, and it's returning the results to my callback function, which I can send to the console with console.log, and it shows the following:
[ { username: 'test', total: 1 } ]
Yet, when I try to access the total, it says undefined. Here's my code:
db.query("SELECT username, COUNT(user_id) AS total FROM users WHERE username = '" + message.username + "'", function(err, info) {
console.log(info);
self.sendRequest(client, '{ "command" : "CALLBACK", "result" : ' + info.total + ' }');
});
when using console.log(info), it returns valid JSON syntax, however I cannot access the total via info.total, as it returns undefined....
Any ideas on why I'm having this issue? If you need to see more code I will be happy to provide it..
Figured it out;
info[0].total returns what I need (d'oh)...