Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

udf inline assignment

  • Loading branch information...
commit 2e54a88db04480c19d9cf88ebe6a16cc10627d74 1 parent 4708d19
@hochang hochang authored
View
8 modules/compiler/lib/compiler.js
@@ -246,6 +246,14 @@ function walk(line, symbols) {
break;
case 'define':
introspectObject(line.object, symbols, line.dependsOn, line);
+ if(line.udf && line.udf != 'require'){
+ _.each(line.args, function(arg){
+ dependency = symbols[arg.name];
+ if(dependency){
+ addDep(line, line.dependsOn, dependency, symbols);
+ }
+ });
+ }
break;
case 'return':
walk(line.rhs, symbols);
View
14 modules/engine/lib/engine.js
@@ -42,6 +42,7 @@ var configLoader = require('./engine/config.js'),
winston = require('winston'),
compiler = require('ql.io-compiler'),
visualization = require('./engine/visualization'),
+ udf = require('./engine/udf'),
_ = require('underscore'),
EventEmitter = require('events').EventEmitter,
util = require('util'),
@@ -712,6 +713,7 @@ function _execOne(opts, statement, parentEvent, cb) {
obj = jsonfill.fill(statement.object, params);
}
else if(statement.udf) {
+ if (statement.udf === 'require'){
args = [];
_.each(_.pluck(statement.args, 'value'), function(arg) {
args.push(jsonfill.lookup(arg, opts.context));
@@ -723,6 +725,18 @@ function _execOne(opts, statement, parentEvent, cb) {
console.log(e.stack || e);
return cb(e);
}
+ }else{//assign variable using udf.
+ udf.applyAssign(opts, statement, function(err, results) {
+ if(err) {
+ cb('udf assignment failed.')
+ }
+ else {
+ if(statement.assign) {
+ obj = results;
+ }
+ }
+ });
+ }
}
opts.context[statement.assign] = obj;
View
23 modules/engine/lib/engine/udf.js
@@ -127,6 +127,29 @@ exports.applyWhere = function(opts, statement, results, cb, tempNames, tempIndic
}
}
+exports.applyAssign = function(opts, statement, cb){
+ var fn = resolveUdf(opts, statement),
+ context = opts.context,
+ args = _.map(statement.args, function(arg){
+ if(arg.name){
+ return context[arg.name];
+ }else{
+ return arg.value;
+ }
+ }),
+ wrapper = {
+ __proto__: context
+ },
+ result, err;
+ try{
+ result = fn.apply(wrapper, args);;
+ }catch(e){
+ err = e;
+ }finally{
+ cb(null, result);
+ }
+ //return result;
+}
function resolve(opts, columns, extras, udf, tempNames, tempIndices) {
var fn = resolveUdf(opts, udf);
if(fn) {
View
13 modules/engine/lib/udfs/standard.js
@@ -32,8 +32,17 @@ exports.require = function() {
return module.require.apply(null, args);
}
catch(e) {
- args[0] = __dirname + '/' + name;
- return module.require.apply(null, args);
+ try {
+ var splitpath = process.cwd().split('/');
+ splitpath[splitpath.length-1] = name;
+ args[0] = splitpath.join('/');
+ return module.require.apply(null, args);
+
+ } catch(e) {
+ args[0] = __dirname + '/' + name;
+ return module.require.apply(null, args);
+ }
}
+
}
}
View
103 modules/engine/test/assign-udf-test.js
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2012 eBay Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var _ = require('underscore'),
+ util = require('util'),
+ Engine = require('../lib/engine');
+
+var engine = new Engine();
+
+process.on('uncaughtException', function(error) {
+ console.log(error.stack || error);
+});
+module.exports = {
+ 'with-args': function(test) {
+ var script = 'u = require("./test/udfs/addone.js");\
+ x = 1;\
+ y = 2;\
+ b = u.add(x, y);\
+ return b';
+ engine.execute(script, function (emitter) {
+ emitter.on('end', function (err, results) {
+ if(err) {
+ console.log(err.stack || err);
+ test.ok(false);
+ }
+ else {
+ test.equals(results.body, 3);
+ }
+ });
+ test.done();
+ });
+ },
+ 'no-arg': function(test) {
+ var script = 'u = require("./test/udfs/addone.js");\
+ x = 1;\
+ b = u.addonex();\
+ return b';
+ engine.execute(script, function (emitter) {
+ emitter.on('end', function (err, results) {
+ if(err) {
+ console.log(err.stack || err);
+ test.ok(false);
+ }
+ else {
+ test.equals(results.body, 2);
+ }
+ });
+ test.done();
+ });
+ },
+ 'dependency-check': function(test) {
+ var script = 'u = require("./test/udfs/addone.js");\
+ b = u.add(x_beta, y_beta);\
+ x = 1;\
+ x_beta = select * from x;\
+ y = 2;\
+ y_beta = select * from y;\
+ return b';
+ engine.execute(script, function (emitter) {
+ emitter.on('end', function (err, results) {
+ if(err) {
+ console.log(err.stack || err);
+ test.ok(false);
+ }
+ else {
+ test.equals(results.body, 3);
+ }
+ });
+ test.done();
+ });
+ },
+ 'direct-pass': function(test) {
+ var script = 'u = require("./test/udfs/addone.js");\
+ x = 1;\
+ b = u.add(x, 2);\
+ return b';
+ engine.execute(script, function (emitter) {
+ emitter.on('end', function (err, results) {
+ if(err) {
+ console.log(err.stack || err);
+ test.ok(false);
+ }
+ else {
+ test.equals(results.body, 3);
+ }
+ });
+ test.done();
+ });
+ }
+}
View
24 modules/engine/test/udfs/addone.js
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2012 eBay Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+exports.addonex = function() {
+ var b = this.x+1;
+ return b;
+}
+
+exports.add = function(a, b){
+ var c = a + b;
+ return c;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.