【发布时间】:2019-03-20 23:32:54
【问题描述】:
我必须查询 github 内容才能找到特定函数 add 的实现。我使用以下 SQL 查询提取的内容:
(function(f){if(typeof exports==="object"&&typeof module!=="undefined")
{module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ngContextmenu = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
'use strict';
angular.module('io.dennis.contextmenu')
.directive('contextmenuContainer', Container);
function Container() {
return {
scope: {
contextmenu: '=contextmenuContainer'
},
restrict: 'A',
controller: ['$scope', ContainerCtrl]
};
.
.
.
.
function add(entry) {
if (!isSelected(entry)) {
selected.unshift(entry);
toggleSelected(entry.element, true);
}
pub.item = selected[0].item;
}
function remove(entry) {
var index = selected.indexOf(entry);
if (index > -1) {
selected.splice(index, 1);
}
toggleSelected(entry.element, false);
}
.
.
.
function toggle(entry, multi) {
multi = multi || false;
var isEntrySelected = isSelected(entry);
if (isEntrySelected) {
remove(entry);
} else {
if (!multi) { clear(); }
add(entry);
}
}
function clear() {
angular.forEach(selected, function(entry) {
toggleSelected(entry.element, false);
});
selected = [];
}
function getListOfIds(limit, path) {
path = path || 'item.id';
limit = Math.min(limit || selected.length, selected.length);
var list = selected.slice(0, limit).map(function(entry) {
return safeGet(entry, path, '');
});
var asString = list.join(', ');
return (limit < selected.length) ? asString + '..' : asString;
}
function toggleSelected(element, forceState) {
element.toggleClass('selected', forceState);
}
function safeGet(obj, path, _default) {
if (!obj) {
return _default;
}
if (!path || !String(path).length) {
return obj;
}
var keys = (angular.isArray(path)) ? path : path.split('.');
var next = keys.shift();
return get(obj[next], keys, _default);
}
}
},{}]},{},[4])(4)
});
还有更多这样的行。我正在使用的查询是:
SELECT
content,
id
FROM
[bigquery-public-data:github_repos.sample_contents]
WHERE
content CONTAINS 'function add('
AND sample_path LIKE '%.js'
LIMIT 40;
如何修改上述查询,以便我只能提取具有输出的内容以实现函数“add”,即
function add(entry) {
if (!isSelected(entry)) {
selected.unshift(entry);
toggleSelected(entry.element, true);
}
pub.item = selected[0].item;
}
提前致谢!
【问题讨论】:
-
我认为仅使用正则表达式会很困难。也许如果你写了一个JavaScript UDF 你可以解析内容。请注意,BigQuery 支持两种 SQL 方言,并且您在示例中使用的是旧版 SQL,而上面的链接适用于标准 SQL。
-
我已将查询修改为 SELECT REGEXP_EXTRACT(content,r'add(.*{(.|\n)*}\n{2,}'), id FROM ( SELECT content, id FROM [bigquery-public-data:github_repos.sample_contents] WHERE content CONTAINS 'function add(' AND sample_path LIKE '%.js' LIMIT 40 ) 但也许我错过了什么。有人可以帮忙吗?
-
@ElliottBrossard,你能给 JS udf 吗?
-
您能否在输出中提供与您的需求不符并且您想过滤掉的数据,您的问题不是很清楚到底是什么问题
标签: sql regex github google-bigquery