Skip to content

Commit

Permalink
refactor top parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TherChenYang committed Dec 26, 2023
1 parent f3c1788 commit d771681
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ duplicateSpecification
;

projections
: (unqualifiedShorthand | projection | top (unqualifiedShorthand | projection)) (COMMA_ (unqualifiedShorthand | projection))*
: (unqualifiedShorthand | projection | top (unqualifiedShorthand | projection)?) (COMMA_ (unqualifiedShorthand | projection))*
;

projection
Expand All @@ -98,7 +98,7 @@ projection
;

top
: TOP LP_? topNum RP_? PERCENT? (WITH TIES)? (ROW_NUMBER LP_ RP_ OVER LP_ orderByClause RP_)?
: TOP LP_? topNum RP_? PERCENT? (WITH TIES)? (ROW_NUMBER LP_ RP_ OVER LP_ orderByClause RP_ (AS? alias)?)?
;

topNum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,7 @@ private boolean isDistinct(final SelectClauseContext ctx) {
public ASTNode visitProjections(final ProjectionsContext ctx) {
Collection<ProjectionSegment> projections = new LinkedList<>();
if (null != ctx.top()) {
RowNumberValueSegment rowNumber = (RowNumberValueSegment) visit(ctx.top());
projections.add(new TopProjectionSegment(ctx.top().getStart().getStartIndex(), ctx.top().getStop().getStopIndex(), rowNumber, null));
projections.add((ProjectionSegment) visit(ctx.top()));
}
for (UnqualifiedShorthandContext each : ctx.unqualifiedShorthand()) {
projections.add(new ShorthandProjectionSegment(each.getStart().getStartIndex(), each.getStop().getStopIndex()));
Expand Down Expand Up @@ -1154,11 +1153,12 @@ public ASTNode visitTop(final TopContext ctx) {
int stopIndex = ctx.topNum().getStop().getStopIndex();
ASTNode topNum = visit(ctx.topNum());
if (topNum instanceof NumberLiteralValue) {
return new NumberLiteralRowNumberValueSegment(startIndex, stopIndex, ((NumberLiteralValue) topNum).getValue().longValue(), false);
NumberLiteralRowNumberValueSegment rowNumberSegment = new NumberLiteralRowNumberValueSegment(startIndex, stopIndex, ((NumberLiteralValue) topNum).getValue().longValue(), false);
return new TopProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), rowNumberSegment, null != ctx.alias() ? ctx.alias().getText() : null);
}
ParameterMarkerSegment result = new ParameterMarkerRowNumberValueSegment(startIndex, stopIndex, ((ParameterMarkerValue) topNum).getValue(), false);
parameterMarkerSegments.add(result);
return result;
ParameterMarkerSegment parameterSegment = new ParameterMarkerRowNumberValueSegment(startIndex, stopIndex, ((ParameterMarkerValue) topNum).getValue(), false);
parameterMarkerSegments.add(parameterSegment);
return new TopProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (RowNumberValueSegment) parameterSegment, null != ctx.alias() ? ctx.alias().getText() : null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="158">
<top-projection start-index="22" stop-index="26">
<top-projection alias="rownum_" start-index="22" stop-index="81">
<top-value value="3" parameter-index="0" start-index="26" stop-index="26" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="28" stop-index="81" alias="rownum_"/>
<column-projection name="item_id" start-index="84" stop-index="92">
<owner name="i" start-index="84" stop-index="84" />
</column-projection>
Expand Down Expand Up @@ -398,10 +397,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="176">
<top-projection start-index="22" stop-index="44">
<top-projection alias="rownum_" start-index="22" stop-index="99">
<top-value value="3" parameter-index="0" start-index="26" stop-index="26" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="46" stop-index="99" alias="rownum_"/>
<column-projection name="item_id" start-index="102" stop-index="110">
<owner name="i" start-index="102" stop-index="102" />
</column-projection>
Expand Down Expand Up @@ -543,10 +541,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="160">
<top-projection start-index="22" stop-index="28">
<top-projection alias="rownum_" start-index="22" stop-index="83">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="30" stop-index="83" alias="rownum_"/>
<column-projection name="item_id" start-index="86" stop-index="94">
<owner name="i" start-index="86" stop-index="86" />
</column-projection>
Expand Down Expand Up @@ -688,10 +685,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="178">
<top-projection start-index="22" stop-index="46">
<top-projection alias="rownum_" start-index="22" stop-index="101">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="48" stop-index="101" alias="rownum_"/>
<column-projection name="item_id" start-index="104" stop-index="112">
<owner name="i" start-index="104" stop-index="104" />
</column-projection>
Expand Down Expand Up @@ -833,10 +829,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="158">
<top-projection start-index="22" stop-index="26">
<top-projection alias="rownum_" start-index="22" stop-index="81">
<top-value value="3" parameter-index="0" start-index="26" stop-index="26" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="28" stop-index="81" alias="rownum_"/>
<column-projection name="item_id" start-index="84" stop-index="92">
<owner name="i" start-index="84" stop-index="84" />
</column-projection>
Expand Down Expand Up @@ -978,10 +973,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="176">
<top-projection start-index="22" stop-index="44">
<top-projection alias="rownum_" start-index="22" stop-index="99">
<top-value value="3" parameter-index="0" start-index="26" stop-index="26" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="46" stop-index="99" alias="rownum_"/>
<column-projection name="item_id" start-index="102" stop-index="110">
<owner name="i" start-index="102" stop-index="102" />
</column-projection>
Expand Down Expand Up @@ -1123,10 +1117,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="160">
<top-projection start-index="22" stop-index="28">
<top-projection alias="rownum_" start-index="22" stop-index="83">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="30" stop-index="83" alias="rownum_"/>
<column-projection name="item_id" start-index="86" stop-index="94">
<owner name="i" start-index="86" stop-index="86" />
</column-projection>
Expand Down Expand Up @@ -1268,10 +1261,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="178">
<top-projection start-index="22" stop-index="46">
<top-projection alias="rownum_" start-index="22" stop-index="101">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="48" stop-index="101" alias="rownum_"/>
<column-projection name="item_id" start-index="104" stop-index="112">
<owner name="i" start-index="104" stop-index="104" />
</column-projection>
Expand Down
18 changes: 6 additions & 12 deletions test/it/parser/src/main/resources/case/dml/select-pagination.xml
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="160">
<top-projection start-index="22" stop-index="28">
<top-projection alias="rownum_" start-index="22" stop-index="83">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="30" stop-index="83" alias="rownum_"/>
<column-projection name="item_id" start-index="86" stop-index="94">
<owner name="i" start-index="86" stop-index="86" />
</column-projection>
Expand Down Expand Up @@ -584,10 +583,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="178">
<top-projection start-index="22" stop-index="46">
<top-projection alias="rownum_" start-index="22" stop-index="101">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="48" stop-index="101" alias="rownum_"/>
<column-projection name="item_id" start-index="104" stop-index="112">
<owner name="i" start-index="104" stop-index="104" />
</column-projection>
Expand Down Expand Up @@ -925,10 +923,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="160">
<top-projection start-index="22" stop-index="28">
<top-projection alias="rownum_" start-index="22" stop-index="83">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="30" stop-index="83" alias="rownum_"/>
<column-projection name="item_id" start-index="86" stop-index="94">
<owner name="i" start-index="86" stop-index="86" />
</column-projection>
Expand Down Expand Up @@ -1065,10 +1062,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="178">
<top-projection start-index="22" stop-index="46">
<top-projection alias="rownum_" start-index="22" stop-index="101">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="48" stop-index="101" alias="rownum_"/>
<column-projection name="item_id" start-index="104" stop-index="112">
<owner name="i" start-index="104" stop-index="104" />
</column-projection>
Expand Down Expand Up @@ -1205,10 +1201,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="160">
<top-projection start-index="22" stop-index="28">
<top-projection alias="rownum_" start-index="22" stop-index="83">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="30" stop-index="83" alias="rownum_"/>
<column-projection name="item_id" start-index="86" stop-index="94">
<owner name="i" start-index="86" stop-index="86" />
</column-projection>
Expand Down Expand Up @@ -1345,10 +1340,9 @@
<subquery>
<select>
<projections start-index="22" stop-index="178">
<top-projection start-index="22" stop-index="46">
<top-projection alias="rownum_" start-index="22" stop-index="101">
<top-value value="3" parameter-index="0" start-index="27" stop-index="27" />
</top-projection>
<expression-projection text="row_number() OVER (ORDER BY i.item_id DESC)" start-index="48" stop-index="101" alias="rownum_"/>
<column-projection name="item_id" start-index="104" stop-index="112">
<owner name="i" start-index="104" stop-index="104" />
</column-projection>
Expand Down
Loading

0 comments on commit d771681

Please sign in to comment.