In the following metaprogramming script:
在以下元编程脚本中:
def generateReport(tbl, colNames, colFormat): sql(sqlColAlias(each(makeCall{format},sqlCol(colNames),colFormat),colNames), tbl).eval()
t = table(1..100 as id, (1..100 + 2018.01.01) as date, rand(100.0, 100) as price, rand(10000, 100) as qty, rand(`A`B`C`D`E`F`G, 100) as city);
generateReport(t, ["id", "date","price","qty"], ["0", "MM/dd/yyyy", "0.00", "#,###"]);
I'd like to add the "city" column as part of the query result without changing the internal logic of the format
function. Could you please provide guidance on how to achieve this?
我想添加“City”列作为查询结果的一部分,而不更改Format函数的内部逻辑。您能就如何实现这一点提供指导吗?
更多回答
Method 1:
方法一:
You can use the join
(<-) function. Refer to the following script:
您可以使用Join(<-)函数。请参阅以下脚本:
def generateReport(tbl, colNames, colFormat): sql(sqlColAlias(each(makeCall{format},sqlCol(colNames),colFormat),colNames)<-sqlCol("city"), tbl).eval();
t=table(1..100 as id, (1..100 + 2018.01.01) as date, rand(100.0, 100) as price, rand(10000, 100) as qty, rand(`A`B`C`D`E`F`G, 100) as city);
generateReport(t, ["id", "date","price","qty"], ["0", "MM/dd/yyyy", "0.00", "#,###"]);
Output:
产出:
id date price qty city
1 01/02/2018 70.86 518 E
2 01/03/2018 81.32 8,132 A
3 01/04/2018 88.08 9,599 C
4 01/05/2018 77.98 4,101 G
5 01/06/2018 16.44 9,191 F
6 01/07/2018 48.81 8,953 A
7 01/08/2018 36.65 459 E
8 01/09/2018 35.80 4,012 C
……
Method 2:
方法二:
For the “city” column without any specific formatting, you can define a UDF with the logic to return it unchanged. For specific details, refer to the following script:
对于没有任何特定格式的“City”列,您可以定义一个UDF,其逻辑是不变地返回它。有关具体细节,请参阅以下脚本:
def myf(colName, ft){
if(isValid(ft)){
return makeCall(format, sqlCol(colName), ft)
}else{
return sqlCol(colName)
}
}
def generateReport(tbl, colNames, colFormat): sql(sqlColAlias(each(myf, colNames, colFormat),colNames), tbl).eval()
colNames=["id", "date","price","qty", "city"]
colFormat=["0", "MM/dd/yyyy", "0.00", "#,###", ""]
t = table(1..100 as id, (1..100 + 2018.01.01) as date, rand(100.0, 100) as price, rand(10000, 100) as qty, rand(`A`B`C`D`E`F`G, 100) as city);
generateReport(t, ["id", "date","price","qty", "city"], ["0", "MM/dd/yyyy", "0.00", "#,###", ""]);
更多回答
我是一名优秀的程序员,十分优秀!