Skip to content

Commit 2c3cbd4

Browse files
author
zhaoge
committed
feat(flinksql): collect table alias
1 parent cb9a37a commit 2c3cbd4

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

src/grammar/flink/FlinkSqlParser.g4

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ tableReference
512512
;
513513

514514
tablePrimary
515-
: KW_TABLE? tablePath systemTimePeriod? (KW_AS? correlationName)?
516-
| viewPath systemTimePeriod? (KW_AS? correlationName)?
515+
: KW_TABLE? tablePath systemTimePeriod?
516+
| viewPath systemTimePeriod?
517517
| KW_LATERAL KW_TABLE LR_BRACKET functionName LR_BRACKET functionParam (COMMA functionParam)* RR_BRACKET RR_BRACKET
518518
| KW_LATERAL? LR_BRACKET queryStatement RR_BRACKET
519519
| KW_UNNEST LR_BRACKET expression RR_BRACKET
@@ -833,7 +833,7 @@ intervalValue
833833
;
834834

835835
tableAlias
836-
: KW_AS? identifier identifierList?
836+
: KW_AS? tbAlias=identifier identifierList?
837837
;
838838

839839
errorCapturingIdentifier

src/parser/common/entityCollector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ const baseAlias: BaseAliasContext = {
6565
export enum attrName {
6666
comment = '_comment',
6767
colType = '_colType',
68+
alias = '_tbAlias',
6869
}
6970

7071
export const attrNameInRule = {
7172
[attrName.comment]: 'comment',
7273
[attrName.colType]: 'colType',
74+
[attrName.alias]: 'alias',
7375
} as const;
7476

7577
type ParserRuleContextWithAttr = ParserRuleContext & {
@@ -83,6 +85,7 @@ export interface EntityContext extends BaseAliasContext {
8385
relatedEntities: EntityContext[] | null;
8486
columns: ColumnEntityContext[] | null;
8587
comment?: string;
88+
alias?: string;
8689
}
8790

8891
export interface FuncEntityContext extends EntityContext {
@@ -126,6 +129,7 @@ export function toEntityContext(
126129
for (let k = 0; k < attrInfo?.attrList?.length; k++) {
127130
const attributeName: attrName = attrInfo?.attrList[k];
128131
const attrToken = findAttribute(ctx, attributeName, attrInfo?.endContext);
132+
console.log(attrToken, attributeName);
129133
if (attrToken) {
130134
const attrVal: string = tokenToWord(attrToken, input)?.text;
131135
extraInfo[attrNameInRule[attributeName]] = attrVal;

src/parser/flink/flinkEntityCollector.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export class FlinkEntityCollector extends EntityCollector implements FlinkSqlPar
4545
}
4646

4747
exitTablePath(ctx: TablePathContext) {
48-
this.pushEntity(ctx, EntityContextType.TABLE);
48+
this.pushEntity(ctx, EntityContextType.TABLE, {
49+
needCollectAttr: true,
50+
attrList: [attrName.alias],
51+
endContext: 'TableReferenceContext',
52+
});
4953
}
5054

5155
exitTablePathCreate(ctx: TablePathCreateContext) {

test/parser/flink/contextCollect/entityCollector.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ describe('Flink entity collector tests', () => {
167167
expect(allEntities.length).toBe(1);
168168

169169
const tableEntity = allEntities[0];
170-
170+
console.log(tableEntity, '---tableEntity');
171171
expect(tableEntity.entityContextType).toBe(EntityContextType.TABLE);
172172
expect(tableEntity.text).toBe('Orders');
173173
expect(tableEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);

test/parser/flink/contextCollect/fixtures/common.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CREATE TABLE Orders_with_watermark (
2020
INCLUDING GENERATED
2121
);
2222

23-
SELECT order_id, price + tax FROM Orders;
23+
SELECT order_id, price + tax FROM Orders AS o1;
2424

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

0 commit comments

Comments
 (0)