Skip to content

Commit 103f43f

Browse files
committed
Better support for LATERAL
Signed-off-by: worksofliam <[email protected]>
1 parent 71a75b6 commit 103f43f

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/language/sql/statement.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ export default class Statement {
512512

513513
let endIndex = i;
514514

515-
let isUDTF = tokenIs(nextToken, `function`, `TABLE`);
516-
515+
const isUDTF = tokenIs(nextToken, `function`, `TABLE`);
516+
const isLateral = tokenIs(nextToken, `function`, `LATERAL`);
517517

518518
if (isUDTF) {
519519
sqlObj = this.getRefAtToken(i+2);
@@ -528,7 +528,16 @@ export default class Statement {
528528
nextIndex = -1;
529529
nextToken = undefined;
530530
}
531-
531+
} else if (isLateral) {
532+
console.log(`HI`);
533+
const blockTokens = this.getBlockAt(nextToken.range.end+1);
534+
console.log(blockTokens);
535+
const newStatement = new Statement(blockTokens, {start: nextToken.range.start, end: blockTokens[blockTokens.length-1].range.end});
536+
[sqlObj] = newStatement.getObjectReferences();
537+
538+
nextIndex = i + 2 + blockTokens.length;
539+
nextToken = this.tokens[nextIndex];
540+
532541
} else {
533542
if (nextToken && NameTypes.includes(nextToken.type)) {
534543
nextIndex = i;

src/language/sql/tests/statements.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,32 @@ describe(`Object references`, () => {
971971
expect(statement.type).toBe(StatementType.With);
972972

973973
const refs = statement.getObjectReferences();
974+
expect(refs.length).toBe(3);
975+
976+
expect(refs[0].object.name).toBe(`object_ownership`);
977+
expect(refs[0].object.schema).toBe(`qsys2`);
978+
979+
expect(refs[1].object.name).toBe(`qsysobjs`);
980+
expect(refs[1].object.schema).toBeUndefined();
981+
expect(refs[1].alias).toBe(`q`);
982+
983+
expect(refs[2].object.name).toBe(`object_statistics`);
984+
expect(refs[2].object.schema).toBe(`qsys2`);
985+
expect(refs[2].alias).toBe(`z`);
974986
});
987+
988+
test(`Multiple UDTFs`, () => {
989+
const lines = [
990+
`SELECT b.objlongschema, b.objname, b.objtype, b.objattribute, b.objcreated, b.objsize, b.objtext, b.days_used_count, b.last_used_timestamp,b.* FROM `,
991+
` TABLE (QSYS2.OBJECT_STATISTICS('*ALLUSRAVL ', '*LIB') ) as a, `,
992+
` TABLE (QSYS2.OBJECT_STATISTICS(a.objname, 'ALL') ) AS b`,
993+
`WHERE b.OBJOWNER = 'user-name'`,
994+
`ORDER BY b.OBJSIZE DESC`,
995+
`FETCH FIRST 100 ROWS ONLY;`,
996+
].join(`\n`);
997+
998+
const document = new Document(lines);
999+
})
9751000
});
9761001

9771002
describe(`Offset reference tests`, () => {
@@ -1167,7 +1192,6 @@ describe(`PL body tests`, () => {
11671192
const refs = statement.getObjectReferences();
11681193
const ctes = statement.getCTEReferences();
11691194

1170-
console.log(refs);
11711195
expect(refs.length).toBe(7);
11721196
expect(refs[0].object.name).toBe(`shipments`);
11731197
expect(refs[0].alias).toBe(`s`);

0 commit comments

Comments
 (0)