Skip to content

Commit

Permalink
feat: collect alias in select statement
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoge committed Jul 4, 2024
1 parent 22a9454 commit 009effc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/parser/common/entityCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ export abstract class EntityCollector {

exitEveryRule() {}

getRootStmt() {
return this._rootStmt;
}

getEntities() {
return Array.from(this._entitiesSet) as EntityContext[];
}
Expand Down
12 changes: 11 additions & 1 deletion src/parser/flink/flinkEntityCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ export class FlinkEntityCollector extends EntityCollector implements FlinkSqlPar
}

exitTablePath(ctx: TablePathContext) {
this.pushEntity(ctx, EntityContextType.TABLE);
const needCollectAttr = this.getRootStmt()?.stmtContextType === StmtContextType.SELECT_STMT;
this.pushEntity(
ctx,
EntityContextType.TABLE,
needCollectAttr
? {
attrNameList: [AttrName.alias],
endContext: QueryStatementContext.name,
}
: undefined
);
}

exitTablePathCreate(ctx: TablePathCreateContext) {
Expand Down
32 changes: 20 additions & 12 deletions test/parser/flink/contextCollect/entityCollector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ describe('Flink entity collector tests', () => {

expect(tableEntity.entityContextType).toBe(EntityContextType.TABLE);
expect(tableEntity.text).toBe('Orders');
expect(tableEntity[AttrName.alias]).toEqual({
text: 'o1',
startIndex: 607,
endIndex: 608,
line: 23,
startColumn: 45,
endColumn: 47,
});
expect(tableEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
if (isCommonEntityContext(tableEntity)) {
expect(tableEntity.columns).toBeUndefined();
Expand Down Expand Up @@ -360,8 +368,8 @@ describe('Flink entity collector tests', () => {
expect(allEntities[0].text).toBe('view1');
expect(allEntities[0][AttrName.comment]).toEqual({
text: "'this is a view'",
startIndex: 1199,
endIndex: 1214,
startIndex: 1205,
endIndex: 1220,
line: 42,
startColumn: 39,
endColumn: 55,
Expand Down Expand Up @@ -389,27 +397,27 @@ describe('Flink entity collector tests', () => {
expect(dbEntity.text).toBe('db1');
expect(dbEntity[AttrName.comment]).toEqual({
text: "'this is a created database'",
startIndex: 1290,
endIndex: 1317,
startIndex: 1296,
endIndex: 1323,
line: 44,
startColumn: 43,
endColumn: 71,
});
expect(dbEntity.position).toEqual({
endColumn: 34,
endIndex: 1280,
endIndex: 1286,
line: 44,
startColumn: 31,
startIndex: 1278,
startIndex: 1284,
});

expect(dbEntity.belongStmt.stmtContextType).toBe(StmtContextType.CREATE_DATABASE_STMT);
expect(dbEntity.belongStmt.position).toEqual({
endColumn: 119,
endIndex: 1365,
endIndex: 1371,
endLine: 44,
startColumn: 1,
startIndex: 1248,
startIndex: 1254,
startLine: 44,
});
if (isCommonEntityContext(dbEntity)) {
Expand All @@ -434,21 +442,21 @@ describe('Flink entity collector tests', () => {
expect(functionEntity.text).toBe('tempFunction');
expect(functionEntity.position).toEqual({
endColumn: 43,
endIndex: 1410,
endIndex: 1416,
line: 46,
startColumn: 31,
startIndex: 1399,
startIndex: 1405,
});

expect(functionEntity.belongStmt.stmtContextType).toBe(
StmtContextType.CREATE_FUNCTION_STMT
);
expect(functionEntity.belongStmt.position).toEqual({
endColumn: 58,
endIndex: 1425,
endIndex: 1431,
endLine: 46,
startColumn: 1,
startIndex: 1369,
startIndex: 1375,
startLine: 46,
});
if (isFuncEntityContext(functionEntity)) {
Expand Down
2 changes: 1 addition & 1 deletion test/parser/flink/contextCollect/fixtures/common.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CREATE TABLE Orders_with_watermark (
INCLUDING GENERATED
);

SELECT order_id, price + tax FROM Orders;
SELECT order_id, price + tax FROM Orders AS o1;

SELECT * FROM Orders LEFT JOIN Product ON Orders.product_id = Product.id;

Expand Down

0 comments on commit 009effc

Please sign in to comment.