Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/ifct2017/ifct2017
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfram77 committed Apr 26, 2018
2 parents f4769f2 + fe93acd commit 2e9c098
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ NLP:
= show total vitamins
- SELECT FROM <table> WHERE <condition>
= show food where toxic minerals is more than catechin and catechin is less than or equal to epigallo catechin plus aluminium minus available carbohydrate
= show food where toxic minerals is more than catechin and catechin is less than or equal to epigallo catechin plus aluminium minus available carbohydrate or vitamin b3 into vitamin b2 greater than total oxalates
= show food where (absolute of (coalesce open vitamin a and vitamin b and vitamin c close) is more than catechin) and (catechin is less than or equal to epigallo catechin plus aluminium minus available carbohydrate) or ((vitamin b3 plus two) into (vitamin b2 plus vitamin b6)) greater than total oxalates order by absolute of open total oxalates plus toxic minerals close in descending order
= show maximum of vitamin a group by food group having char length of food group higher than ten
= show maximum of vitamin a group by food group order by char length of food group
```
Expand Down
7 changes: 5 additions & 2 deletions inp/aql.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ async function orderBy(db, ast) {
return z;
};

function groupBy(db, ast) {
return Promise.all(ast.map((exp) => expressions(db, exp)));
async function groupBy(db, ast) {
var y = await Promise.all(ast.map((exp) => expressions(db, exp)));
for(var i=0, I=y.length, z=[]; i<I; i++)
z.push.apply(z, y[i]);
return z;
};

function from(db, ast) {
Expand Down
40 changes: 23 additions & 17 deletions inp/nlp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,35 @@ const VALUE = [
{t: [T.KEYWORD], v: [/NULL/], f: (s, t, i) => token(T.VALUE, t[i].value)},
{t: [T.KEYWORD], v: [/TRUE|FALSE/], f: (s, t, i) => token(T.BOOLEAN, t[i].value)},
];
// T.VALUE: t[i].type
const EXPRESSION = [
{t: [T.OPERATOR, T.BINARY, T.EXPRESSION], v: [null, /\+|\-/, null], f: (s, t, i) => [t[i], token(T.VALUE, `${t[i+1].value}${t[i+2].value}`)]},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /\^/, null], f: (s, t, i) => token(T.VALUE, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /[\*\/%]/, null], f: (s, t, i) => token(T.VALUE, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /[\+\-]/, null], f: (s, t, i) => token(T.VALUE, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.UNARY, T.EXPRESSION], v: [/[^(NOT)]/, null], f: (s, t, i) => token(T.VALUE, `${t[i].value} ${t[i+1].value}`)},
{t: [T.OPEN, T.CLOSE], v: [null, null], f: (s, t, i) => null},
{t: [T.EXPRESSION, T.EXPRESSION, T.CLOSE], v: [null, null, null], f: (s, t, i) => [token(T.EXPRESSION, `${t[i].value}, ${t[i+1].value}`), t[i+2]]},
{t: [T.FUNCTION], v: [/pi|random/], f: (s, t, i) => token(T.EXPRESSION, `${t[i].value}()`)},
{t: [T.FUNCTION, T.OPEN, T.EXPRESSION, T.CLOSE], v: [null, null, null, null], f: (s, t, i) => token(T.EXPRESSION, `${t[i].value}(${t[i+2].value})`)},
{t: [T.FUNCTION, T.EXPRESSION], v: [null, null], f: (s, t, i) => token(T.EXPRESSION, `${t[i].value}(${t[i+1].value})`)},
{t: [T.OPEN, T.EXPRESSION, T.CLOSE], v: [null, null, null], f: (s, t, i) => token(T.EXPRESSION, `(${t[i+1].value})`)},
{t: [T.OPERATOR, T.BINARY, T.EXPRESSION], v: [null, /\+|\-/, null], f: (s, t, i) => [t[i], token(t[i+2].type, `${t[i+1].value}${t[i+2].value}`)]},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /\^/, null], f: (s, t, i) => token(t[i].type & t[i+2].type, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /[\*\/%]/, null], f: (s, t, i) => token(t[i].type & t[i+2].type, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /[\+\-]/, null], f: (s, t, i) => token(t[i].type & t[i+2].type, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.UNARY, T.EXPRESSION], v: [/[^(NOT)]/, null], f: (s, t, i) => token(t[i+1].type, `${t[i].value} ${t[i+1].value}`)},
{t: [T.EXPRESSION, T.UNARY], v: [null, /IS.*/], f: (s, t, i) => token(T.BOOLEAN, `${t[i].value} ${t[i+1].value}`)},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /[^\w\s=!<>]+/, null], f: (s, t, i) => token(T.VALUE, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.EXPRESSION, T.TERNARY, T.EXPRESSION, T.OPERATOR, T.EXPRESSION], v: [null, null, null, /AND/, null], f: (s, t, i) => token(T.BOOLEAN, `${t[i].value} ${t[i+1].value} ${t[i+2].value} AND ${t[i+4].value}`)},
{t: [T.EXPRESSION, T.TERNARY, T.EXPRESSION, T.EXPRESSION], v: [null, null, null, null], f: (s, t, i) => token(T.BOOLEAN, `${t[i].value} ${t[i+1].value} ${t[i+2].value} AND ${t[i+3].value}`)},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION, T.OPERATOR, T.EXPRESSION], v: [null, null, null, /ESCAPE/, null], f: (s, t, i) => token(T.BOOLEAN, `${t[i].value} ${t[i+1].value} ${t[i+2].value} ESCAPE ${t[i+4].value}`)},
// {t: [T.VALUE, T.BINARY, T.VALUE, T.OPERATOR, T.VALUE, T.OPERATOR], v: [null, null, null, /OR|AND/, null, /OR|AND/], f: (s, t, i) => [token(T.BOOLEAN, `${t[i].value} ${t[i+1].value} ${t[i+2].value} AND ${t[i].value} ${t[i+1].value} ${t[i+4].value}`), t[i+5]]},
// {t: [T.VALUE, T.OPERATOR, T.VALUE, T.BINARY, T.VALUE, T.OPERATOR], v: [null, /OR|AND/, null, null, null, /OR|AND/], f: (s, t, i) => [token(T.BOOLEAN, `${t[i].value} ${t[i+3].value} ${t[i+4].value} AND ${t[i+2].value} ${t[i+3].value} ${t[i+4].value}`), t[i+5]]},
{t: [T.KEYWORD, T.VALUE, T.BINARY, T.VALUE, T.OPERATOR, T.VALUE], v: [null, null, null, null, /OR|AND/, null], f: (s, t, i) => i+6>=t.length? [t[i], token(T.BOOLEAN, `${t[i+1].value} ${t[i+2].value} ${t[i+3].value} AND ${t[i+1].value} ${t[i+2].value} ${t[i+5].value}`)]:t.slice(i, i+6)},
{t: [T.KEYWORD, T.VALUE, T.OPERATOR, T.VALUE, T.BINARY, T.VALUE], v: [null, null, /OR|AND/, null, null, null], f: (s, t, i) => i+6>=t.length? [t[i], token(T.BOOLEAN, `${t[i+1].value} ${t[i+4].value} ${t[i+5].value} AND ${t[i+3].value} ${t[i+4].value} ${t[i+4].value}`)]:t.slice(i, i+6)},
{t: [T.KEYWORD, T.VALUE, T.BINARY, T.VALUE, T.OPERATOR, T.VALUE], v: [null, null, /[^(OR)|(AND)]/, null, /OR|AND/, null], f: (s, t, i) => i+6>=t.length? [t[i], token(T.BOOLEAN, `${t[i+1].value} ${t[i+2].value} ${t[i+3].value} AND ${t[i+1].value} ${t[i+2].value} ${t[i+5].value}`)]:t.slice(i, i+6)},
{t: [T.KEYWORD, T.VALUE, T.OPERATOR, T.VALUE, T.BINARY, T.VALUE], v: [null, null, /OR|AND/, null, /[^(OR)|(AND)]/, null], f: (s, t, i) => i+6>=t.length? [t[i], token(T.BOOLEAN, `${t[i+1].value} ${t[i+4].value} ${t[i+5].value} AND ${t[i+3].value} ${t[i+4].value} ${t[i+4].value}`)]:t.slice(i, i+6)},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, /[^(OR)(AND)]/, null], f: (s, t, i) => token(T.BOOLEAN, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.UNARY, T.EXPRESSION], v: [null, null], f: (s, t, i) => token(T.BOOLEAN, `${t[i].value} ${t[i+1].value}`)},
{t: [T.VALUE, T.BINARY, T.VALUE], v: [null, null, null], f: (s, t, i) => { s.columns.push(t[i].value); s.columns.push(t[i+2].value); return null; }},
{t: [T.BINARY, T.VALUE], v: [null, null], f: (s, t, i) => { s.columns.push(t[i+1].value); return null; }},
{t: [T.VALUE, T.BINARY, T.VALUE], v: [null, /AND/, null], f: (s, t, i) => { s.columnsUsed.push(t[i].value, t[i+2].value); return [t[i], t[i+2]]; }},
{t: [T.BINARY, T.VALUE], v: [/AND/, null], f: (s, t, i) => { s.columnsUsed.push(t[i+1].value); return t[i+1]; }},
{t: [T.EXPRESSION, T.BINARY, T.EXPRESSION], v: [null, null, null], f: (s, t, i) => token(T.BOOLEAN, `${t[i].value} ${t[i+1].value} ${t[i+2].value}`)},
{t: [T.EXPRESSION, T.EXPRESSION, T.CLOSE], v: [null, null, null], f: (s, t, i) => [token(T.EXPRESSION, `${t[i].value}, ${t[i+1].value}`), t[i+2]]},
{t: [T.FUNCTION, T.OPEN, T.EXPRESSION, T.CLOSE], v: [null, null, null, null], f: (s, t, i) => token(T.EXPRESSION, `${t[i].value}(${t[i+2].value})`)},
{t: [T.FUNCTION, T.EXPRESSION], v: [null, null], f: (s, t, i) => token(T.EXPRESSION, `${t[i].value}(${t[i+1].value})`)},
{t: [T.OPEN, T.EXPRESSION, T.CLOSE], v: [null, null, null], f: (s, t, i) => token(T.EXPRESSION, `(${t[i+1].value})`)},
];
const ORDERBY = [
{t: [T.KEYWORD, T.EXPRESSION], v: [/ORDER BY/, null], f: (s, t, i) => { s.orderBy.push(`${t[i+1].value} ${s.reverse? 'DESC':'ASC'}`); return t[i]; }},
{t: [T.EXPRESSION, T.KEYWORD, T.KEYWORD], v: [null, /DESC/, /NULLS (FIRST|LAST)/], f: (s, t, i) => { s.orderBy.push(`${t[i].value} ${s.reverse? 'ASC':'DESC'} ${t[i+2].value}`); return null; }},
{t: [T.EXPRESSION, T.KEYWORD, T.KEYWORD], v: [null, /ASC/, /NULLS (FIRST|LAST)/], f: (s, t, i) => { s.orderBy.push(`${t[i].value} ${s.reverse? 'DESC':'ASC'} ${t[i+2].value}`); return null; }},
{t: [T.KEYWORD, T.EXPRESSION, T.KEYWORD], v: [/DESC/, null, /NULLS (FIRST|LAST)/], f: (s, t, i) => { s.orderBy.push(`${t[i+1].value} ${s.reverse? 'ASC':'DESC'} ${t[i+2].value}`); return null; }},
Expand All @@ -76,6 +78,7 @@ const ORDERBY = [
{t: [T.OPERATOR, T.EXPRESSION], v: [/<|<=/, null], f: (s, t, i) => { s.orderBy.push(`${t[i+1].value} ${s.reverse? 'DESC':'ASC'}`); return null; }},
{t: [T.EXPRESSION, T.OPERATOR], v: [null, />|>=/], f: (s, t, i) => { s.orderBy.push(`${t[i].value} ${s.reverse? 'ASC':'DESC'}`); return null; }},
{t: [T.EXPRESSION, T.OPERATOR], v: [null, /<|<=/], f: (s, t, i) => { s.orderBy.push(`${t[i].value} ${s.reverse? 'DESC':'ASC'}`); return null; }},
{t: [T.KEYWORD, T.EXPRESSION], v: [/ORDER BY/, null], f: (s, t, i) => { s.orderBy.push(`${t[i+1].value} ${s.reverse? 'DESC':'ASC'}`); return t[i]; }},
];
const GROUPBY = [
{t: [T.KEYWORD, T.EXPRESSION], v: [/GROUP BY/, null], f: (s, t, i) => { s.groupBy.push(`${t[i+1].value}`); return t[i]; }},
Expand All @@ -94,7 +97,7 @@ const WHERE = [
];
const FROM = [
{t: [T.OPERATOR, T.ENTITY, T.OPERATOR], v: [/ALL/, /(field|column)s?/i, null], f: (s, t, i) => { s.columns.push('*'); return null; }},
{t: [T.KEYWORD], v: [/GROUP BY/], f: (s, t, i) => { if(i!==t.length-1 /* && s.groupBy.length===0 */) return t[i]; s.from.push('"groups"'); return null; }},
{t: [T.KEYWORD], v: [/GROUP BY/], f: (s, t, i) => { if(i!==t.length-1 || s.groupBy.length!==0) return t[i]; s.from.push('"groups"'); return null; }},
{t: [T.TABLE], v: [null], f: (s, t, i) => { s.from.push(`"${t[i].value}"`); return null; }},
{t: [T.ROW], v: [null], f: (s, t, i) => { s.from.push(`"${t[i].value}"`); return null; }},
];
Expand Down Expand Up @@ -152,6 +155,7 @@ function stageRun(stg, sta, tkns, rpt0=false, rpt1=false) {
function process(tkns) {
var sta = {columns: [], from: [], groupBy: [], orderBy: [], where: '', having: '', limit: 0, columnsUsed: [], reverse: false};
tkns = tkns.filter((t) => t.type!==T.SEPARATOR);
if(tkns[0].value!=='SELECT') tkns.unshift(token(T.KEYWORD, 'SELECT'));
tkns = stageRun(NULLORDER, sta, tkns);
tkns = stageRun(NUMBER, sta, tkns);
tkns = stageRun(LIMIT, sta, tkns);
Expand All @@ -173,17 +177,19 @@ function process(tkns) {
if(!sta.columns.includes(exp)) sta.columns.push(exp);
}
}
if(sta.columns.length===0 || !sta.columns.includes('*')) {
if(sta.groupBy.length===0 && (sta.columns.length===0 || !sta.columns.includes('*'))) {
for(var col of sta.columnsUsed)
if(!sta.columns.includes(col)) sta.columns.push(col);
}
for(var i=sta.groupBy.length-1; i>=0; i--)
sta.columns.unshift(sta.groupBy[i]);
if(sta.from.length===0) sta.from.push(`"food"`);
if(data.table(sta.from[0].replace(/\"/g, ''))!=='compositions_tsvector') { if(sta.columns.length===0) sta.columns.push('*'); }
else if(!sta.columns.includes('*') && !sta.columns.includes(`"name"`)) sta.columns.unshift(`"name"`);
var z = `SELECT ${sta.columns.join(', ')} FROM ${sta.from.join(', ')}`;
if(sta.where.length>0) z += ` WHERE ${sta.where}`;
if(sta.orderBy.length>0) z += ` ORDER BY ${sta.orderBy.join(', ')}`;
if(sta.groupBy.length>0) z += ` GROUP BY ${sta.groupBy.join(', ')}`;
if(sta.orderBy.length>0) z += ` ORDER BY ${sta.orderBy.join(', ')}`;
if(sta.having.length>0) z += ` HAVING ${sta.having}`;
if(sta.limit>0) z += ` LIMIT ${sta.limit}`;
return z;
Expand Down
7 changes: 4 additions & 3 deletions inp/nlp/reserved.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ const UNARYOPERATOR = new Map([
['factori', '!'],
['! !', '!'],
['!', '!'],
['absolut valu', '@'],
['absolut', '@'],
['abs', '@'],
// ['absolut valu', '@'],
// ['absolut', '@'],
// ['abs', '@'],
['@', '@'],
['bitwis not', '~'],
['bit not', '~'],
Expand Down Expand Up @@ -755,6 +755,7 @@ const KEYWORD = new Map([
['by grouped', 'GROUP BY'],
['by classify', 'GROUP BY'],
['by classified', 'GROUP BY'],
['having', 'HAVING'],
['on uniqu', 'DISTINCT'],
['distinct on', 'DISTINCT'],
['uniqu', 'DISTINCT'],
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"license": "MIT",
"dependencies": {
"@ifct2017/abbreviations": "^0.1.3",
"@ifct2017/columns": "^0.1.20",
"@ifct2017/columns": "^0.1.21",
"@ifct2017/compositingcentres": "^0.1.1",
"@ifct2017/compositions": "^0.2.2",
"@ifct2017/descriptions": "^0.1.10",
Expand Down

0 comments on commit 2e9c098

Please sign in to comment.