Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Everettss
Created September 16, 2017 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Everettss/31cc275246f2ff4dddf55a51049b2676 to your computer and use it in GitHub Desktop.
Save Everettss/31cc275246f2ff4dddf55a51049b2676 to your computer and use it in GitHub Desktop.
// The UDF
function getTasks(row, emit) {
if (row.jsonScripts) {
x = JSON.parse(row.jsonScripts)
if (x !== null && typeof x === 'object') {
tasksCounter = 0
Object.keys(x).forEach(function(entry) {
if (validTask(x, entry)) {
tasksCounter++;
}
});
Object.keys(x).forEach(function(entry) {
if (validTask(x, entry)) {
emit({
taskName: entry,
task: decodeTaskHelper(x, entry),
scripts: row.jsonScripts,
projectID: row.id,
tasksCounter: tasksCounter
});
}
});
}
}
}
function validTask(x, entry) {
return decodeTaskHelper(x, entry) != '' && decodeTaskHelper(x, entry) != 'echo "Error: no test specified" && exit 1';
}
function decodeTaskHelper(x, entry) {
var returnVal = x[entry];
return typeof returnVal === 'string' ? returnVal : '' + returnVal;
}
bigquery.defineFunction(
'getTasks', // Name used to call the function from SQL
['id', 'jsonScripts'], // Input column names
// JSON representation of the output schema
[
{name: 'task', type: 'string'},
{name: 'scripts', type: 'string'},
{name: 'taskName', type: 'string'},
{name: 'projectID', type: 'string'},
{name: 'tasksCounter', type: 'integer'},
],
getTasks // The function reference
);
SELECT taskName, task, tasksCounter, projectID, scripts
FROM getTasks(
SELECT id, JSON_EXTRACT(content, '$.scripts') AS jsonScripts
FROM [package-json-files]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment