You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been working on trying to create PRQL functions to replace some of the boilerplate operations in our project. One of the operations is a very simple "mapping" operation which uses a key-value lookup table to transform a column from keys to values. The function currently looks like this:
let postcodes = [
{ key = "35032", value = "Bon Air" },
{ key = "35046", value = "Clanton" },
{ key = "35070", value = "Garden City" },
]
from employees
select { last_name, first_name, location }
map_into postcodes location { location = that.value }
Resulting SQL, as expected (click to expand)
WITH table_0 AS (
SELECT'35032'AS"key",
'Bon Air'AS value
UNION
ALL
SELECT'35046'AS"key",
'Clanton'AS value
UNION
ALL
SELECT'35070'AS"key",
'Garden City'AS value
),
postcodes AS (
SELECT"key",
value
FROM
table_0
)
SELECTemployees.last_name,
employees.first_name,
map.valueAS location
FROM
employees
LEFT JOIN postcodes AS map ONemployees.location= map."key"-- Generated by PRQL compiler version:0.11.2 (https://prql-lang.org)
However, I'm interested in reducing the duplication of the location term in the map_into function call. I would like to be able to simplify it to just map_into postcodes location. However, when I move the definition of the last tuple into the function, as below:
-- snip...SELECTemployees.last_name,
employees.first_name,
employees.location, -- this should not be presentmap.valueAS"column"-- this should be `map.value AS location`FROM
employees
LEFT JOIN postcodes AS map ONemployees.location= map."key"-- Generated by PRQL compiler version:0.11.2 (https://prql-lang.org)
This is because the word column has now become the name of the new column being defined through the mapping operation -- not the name of the column from the call of the function.
Is there any way to define the map_into function to avoid the double-reference to the mapped column? For example, would it possible to pass the name of the original column through the function parameter, and use it on the left-hand side of a tuple definition inside the function? I hope this question is clear, I apologize if I'm not explaining myself well.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have been working on trying to create PRQL functions to replace some of the boilerplate operations in our project. One of the operations is a very simple "mapping" operation which uses a key-value lookup table to transform a column from keys to values. The function currently looks like this:
and it can be used like this:
Resulting SQL, as expected (click to expand)
However, I'm interested in reducing the duplication of the
location
term in themap_into
function call. I would like to be able to simplify it to justmap_into postcodes location
. However, when I move the definition of the last tuple into the function, as below:This results in the wrong SQL:
This is because the word
column
has now become the name of the new column being defined through the mapping operation -- not the name of the column from the call of the function.Is there any way to define the
map_into
function to avoid the double-reference to the mapped column? For example, would it possible to pass the name of the original column through the function parameter, and use it on the left-hand side of a tuple definition inside the function? I hope this question is clear, I apologize if I'm not explaining myself well.Beta Was this translation helpful? Give feedback.
All reactions