Skip to content

Commit

Permalink
udf inline assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
hochang committed Nov 6, 2012
1 parent 4708d19 commit 2e54a88
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 2 deletions.
8 changes: 8 additions & 0 deletions modules/compiler/lib/compiler.js
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions modules/engine/lib/engine.js
Expand Up @@ -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'),
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions modules/engine/lib/engine/udf.js
Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions modules/engine/lib/udfs/standard.js
Expand Up @@ -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);
}
}

}
}
103 changes: 103 additions & 0 deletions 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();
});
}
}
24 changes: 24 additions & 0 deletions 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;
}

0 comments on commit 2e54a88

Please sign in to comment.