简体   繁体   English

如何使用 App Script 将变量插入 BigQuery 查询字符串?

[英]How can I insert variables into BigQuery query strings using App Script?

I have ran into an issue that I cannot figure out or find a solution for anywhere on google.我遇到了一个问题,我无法在谷歌上的任何地方找出或找到解决方案。

I have a table in BigQuery with 150k+ unique rows and 29 columns.我在 BigQuery 中有一个表,其中包含 150k+ 唯一行和 29 列。
Each row has a REQUIRED column dateX type DATE .每行都有一个REQUIREDdateX类型DATE

In my App Script, I am taking user input from a spreadsheet (most importantly the date range), creating a new spreadsheet in a specified folder, inserting data & setting formatting, and returning the download link to the user.在我的应用程序脚本中,我从电子表格中获取用户输入(最重要的是日期范围),在指定文件夹中创建新电子表格,插入数据和设置格式,然后将下载链接返回给用户。

The 'final' feature I need is the ability to take the date range values provided by the user and insert them into my BigQuery query string so that I may write the data from the BigQuery table for those dates ONLY.我需要的“最终”功能是能够获取用户提供的日期范围值并将它们插入到我的 BigQuery 查询字符串中,这样我就可以只为这些日期写入 BigQuery 表中的数据。

THE PROBLEM:问题:
No matter how I construct my query string in App Script, the query does not return any rows.无论我如何在 App Script 中构建查询字符串,查询都不会返回任何行。 Just headers.只是标题。
So far, I have determined that the WHERE statement in the query is the issue.到目前为止,我已经确定查询中的WHERE语句是问题所在。 I've tried the following:我试过以下方法:

var request = {
    query: `SELECT * FROM le.table.name WHERE dateX = '2020-10-5'`
};
//
// Some of the variations I've tried (among countless others w/ different formatting):
// 'SELECT * FROM le.table.name WHERE dateX = "2020-10-5"'
// "SELECT * FROM le.table.name WHERE dateX = '2020-10-5'"
// 'SELECT * FROM le.table.name WHERE dateX between "2020-10-5" and "2020-10-5"'
// 'SELECT * FROM le.table.name WHERE cast(dateX as date) between "2020-10-5" and "2020-10-5"'
// `SELECT * FROM le.table.name WHERE dateX = ${dateFrom}`
// `SELECT * FROM le.table.name WHERE dateX between ${dateFrom} and ${dateTo}`
//
// I've also tried setting the query string as a variable & passing with no success. 
// var queryString = 'SELECT * FROM le.table.name WHERE dateX = "2020-10-5"';
// var queryString = 'SELECT * FROM le.table.name WHERE dateX = ' + '2020-10-5';

ALL of these run just fine.所有这些都运行得很好。 Job executes with no errors.作业执行无错误。 But it only returns the headers, no rows:(但它只返回标题,没有行:(
The same query runs just fine and returns the desired rows when I run in BigQuery directly.当我直接在 BigQuery 中运行时,相同的查询运行得很好并返回所需的行。

FURTHERMORE, if I remove everything from WHERE onward, it runs the query WITH ALL my 150k+ rows.此外,如果我从WHERE开始删除所有内容,它将使用我所有的 150k+ 行运行查询。
If I select my columns, it returns the desired columns.如果我 select 我的列,它会返回所需的列。 But won't return ALL (*).但不会返回所有 (*)。

I am beyond confused & truly lost.我非常困惑,真的迷路了。 Perhaps I have spent so much time on this I've developed a tunnel vision & NEED a second set of eyes to spot the issue.也许我在这上面花了太多时间,以至于我形成了狭隘的视野并且需要第二只眼睛来发现问题。 Any help is GREATLY appreciated.任何帮助是极大的赞赏。 Thank you in advance.先感谢您。

Not sure I can say anything more without a sample data.如果没有样本数据,我不确定我还能说些什么。 If possible - perhaps you could share some anonymized version of your dataset?如果可能 - 也许您可以共享一些匿名版本的数据集?

My guess would be a simple typo:我的猜测是一个简单的错字:

var request = {
    query: `SELECT * FROM le.table.name WHERE dateX = '2020-10-5'`
};

Shouldn't it be '2020-10- 0 5'?不应该是' 2020-10-0 5'吗?

I've slept on the issue and found a solution to my problem:我已经解决了这个问题并找到了解决我的问题的方法:

When running the query directly in BigQuery, it is smart enough to know that '2020-10-05' is actually a date, and so it reads it in DATE format.当直接在 BigQuery 中运行查询时,它很聪明地知道“2020-10-05”实际上是一个日期,因此它以DATE格式读取它。

When constructing the query string in App Script, the whole this is a String , therefore the '2020-10-05' is also a string.在 App Script 中构造查询字符串时,整个 this 是一个String ,因此 '2020-10-05' 也是一个字符串。 This obviously causes WHERE to return false as it is comparing dateX , a DATE , to a String .这显然会导致WHERE返回false ,因为它正在将dateX (一个DATE )与一个String进行比较。
This is solved by using cast() on the date variables:这是通过在日期变量上使用cast()来解决的:

var request = {
    query: `SELECT * FROM le.table.name WHERE dateX between ` + 
           `cast(${dateFrom} as date) and cast(${dateTo} as date);`
}  
//
//Note: dateFrom & dateTo variables need to include ' ' in them.
//  Ex: var dateFrom = "'2020-10-05'";
//

I hope this can help anyone else dealing with App Script & BigQuery!我希望这可以帮助其他处理 App Script 和 BigQuery 的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM