Skip to content

Commit 2e54a88

Browse files
committed
udf inline assignment
1 parent 4708d19 commit 2e54a88

File tree

6 files changed

+183
-2
lines changed

6 files changed

+183
-2
lines changed

modules/compiler/lib/compiler.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ function walk(line, symbols) {
246246
break;
247247
case 'define':
248248
introspectObject(line.object, symbols, line.dependsOn, line);
249+
if(line.udf && line.udf != 'require'){
250+
_.each(line.args, function(arg){
251+
dependency = symbols[arg.name];
252+
if(dependency){
253+
addDep(line, line.dependsOn, dependency, symbols);
254+
}
255+
});
256+
}
249257
break;
250258
case 'return':
251259
walk(line.rhs, symbols);

modules/engine/lib/engine.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var configLoader = require('./engine/config.js'),
4242
winston = require('winston'),
4343
compiler = require('ql.io-compiler'),
4444
visualization = require('./engine/visualization'),
45+
udf = require('./engine/udf'),
4546
_ = require('underscore'),
4647
EventEmitter = require('events').EventEmitter,
4748
util = require('util'),
@@ -712,6 +713,7 @@ function _execOne(opts, statement, parentEvent, cb) {
712713
obj = jsonfill.fill(statement.object, params);
713714
}
714715
else if(statement.udf) {
716+
if (statement.udf === 'require'){
715717
args = [];
716718
_.each(_.pluck(statement.args, 'value'), function(arg) {
717719
args.push(jsonfill.lookup(arg, opts.context));
@@ -723,6 +725,18 @@ function _execOne(opts, statement, parentEvent, cb) {
723725
console.log(e.stack || e);
724726
return cb(e);
725727
}
728+
}else{//assign variable using udf.
729+
udf.applyAssign(opts, statement, function(err, results) {
730+
if(err) {
731+
cb('udf assignment failed.')
732+
}
733+
else {
734+
if(statement.assign) {
735+
obj = results;
736+
}
737+
}
738+
});
739+
}
726740
}
727741

728742
opts.context[statement.assign] = obj;

modules/engine/lib/engine/udf.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ exports.applyWhere = function(opts, statement, results, cb, tempNames, tempIndic
127127
}
128128
}
129129

130+
exports.applyAssign = function(opts, statement, cb){
131+
var fn = resolveUdf(opts, statement),
132+
context = opts.context,
133+
args = _.map(statement.args, function(arg){
134+
if(arg.name){
135+
return context[arg.name];
136+
}else{
137+
return arg.value;
138+
}
139+
}),
140+
wrapper = {
141+
__proto__: context
142+
},
143+
result, err;
144+
try{
145+
result = fn.apply(wrapper, args);;
146+
}catch(e){
147+
err = e;
148+
}finally{
149+
cb(null, result);
150+
}
151+
//return result;
152+
}
130153
function resolve(opts, columns, extras, udf, tempNames, tempIndices) {
131154
var fn = resolveUdf(opts, udf);
132155
if(fn) {

modules/engine/lib/udfs/standard.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,17 @@ exports.require = function() {
3232
return module.require.apply(null, args);
3333
}
3434
catch(e) {
35-
args[0] = __dirname + '/' + name;
36-
return module.require.apply(null, args);
35+
try {
36+
var splitpath = process.cwd().split('/');
37+
splitpath[splitpath.length-1] = name;
38+
args[0] = splitpath.join('/');
39+
return module.require.apply(null, args);
40+
41+
} catch(e) {
42+
args[0] = __dirname + '/' + name;
43+
return module.require.apply(null, args);
44+
}
3745
}
46+
3847
}
3948
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012 eBay Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
var _ = require('underscore'),
18+
util = require('util'),
19+
Engine = require('../lib/engine');
20+
21+
var engine = new Engine();
22+
23+
process.on('uncaughtException', function(error) {
24+
console.log(error.stack || error);
25+
});
26+
module.exports = {
27+
'with-args': function(test) {
28+
var script = 'u = require("./test/udfs/addone.js");\
29+
x = 1;\
30+
y = 2;\
31+
b = u.add(x, y);\
32+
return b';
33+
engine.execute(script, function (emitter) {
34+
emitter.on('end', function (err, results) {
35+
if(err) {
36+
console.log(err.stack || err);
37+
test.ok(false);
38+
}
39+
else {
40+
test.equals(results.body, 3);
41+
}
42+
});
43+
test.done();
44+
});
45+
},
46+
'no-arg': function(test) {
47+
var script = 'u = require("./test/udfs/addone.js");\
48+
x = 1;\
49+
b = u.addonex();\
50+
return b';
51+
engine.execute(script, function (emitter) {
52+
emitter.on('end', function (err, results) {
53+
if(err) {
54+
console.log(err.stack || err);
55+
test.ok(false);
56+
}
57+
else {
58+
test.equals(results.body, 2);
59+
}
60+
});
61+
test.done();
62+
});
63+
},
64+
'dependency-check': function(test) {
65+
var script = 'u = require("./test/udfs/addone.js");\
66+
b = u.add(x_beta, y_beta);\
67+
x = 1;\
68+
x_beta = select * from x;\
69+
y = 2;\
70+
y_beta = select * from y;\
71+
return b';
72+
engine.execute(script, function (emitter) {
73+
emitter.on('end', function (err, results) {
74+
if(err) {
75+
console.log(err.stack || err);
76+
test.ok(false);
77+
}
78+
else {
79+
test.equals(results.body, 3);
80+
}
81+
});
82+
test.done();
83+
});
84+
},
85+
'direct-pass': function(test) {
86+
var script = 'u = require("./test/udfs/addone.js");\
87+
x = 1;\
88+
b = u.add(x, 2);\
89+
return b';
90+
engine.execute(script, function (emitter) {
91+
emitter.on('end', function (err, results) {
92+
if(err) {
93+
console.log(err.stack || err);
94+
test.ok(false);
95+
}
96+
else {
97+
test.equals(results.body, 3);
98+
}
99+
});
100+
test.done();
101+
});
102+
}
103+
}

modules/engine/test/udfs/addone.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012 eBay Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
exports.addonex = function() {
17+
var b = this.x+1;
18+
return b;
19+
}
20+
21+
exports.add = function(a, b){
22+
var c = a + b;
23+
return c;
24+
}

0 commit comments

Comments
 (0)