Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing SQL Server SELECT COUNT(*) sql #29433

Merged
merged 4 commits into from
Dec 19, 2023
Merged

Support parsing SQL Server SELECT COUNT(*) sql #29433

merged 4 commits into from
Dec 19, 2023

Conversation

TherChenYang
Copy link
Collaborator

Fixes #29170.

Changes proposed in this pull request:
Support parsing SQL Server

SELECT COUNT(*) AS [Number of rows] FROM #Test
SELECT obj1.name AS [XEvent-name],
    col2.name AS [XEvent-column],
    obj1.description AS [Descr-name],
    col2.description AS [Descr-column]
FROM sys.dm_xe_objects AS obj1
INNER JOIN sys.dm_xe_object_columns AS col2
    ON col2.object_name = obj1.name
ORDER BY obj1.name,
    col2.name

Related document

  • sys.dm_exec_sql_text function link
INSERT INTO TestSchema.Employees (Name, Location) VALUES
  (N'Jared',  N'Australia'),
  (N'Nikita', N'India'),
  (N'Tom',    N'Germany')
SELECT query = a.text, start_time, percent_complete,
    eta = dateadd(second,estimated_completion_time/1000, getdate())
FROM sys.dm_exec_requests r
    CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a
WHERE r.command = 'RESTORE DATABASE'

Related document

image
  • Use equal signs to set aliases link
image

Before committing this PR, I'm sure that I have checked the following options:

  • My code follows the code of conduct of this project.
  • I have self-reviewed the commit code.
  • I have (or in comment I request) added corresponding labels for the pull request.
  • I have passed maven check locally : ./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e.
  • I have made corresponding changes to the documentation.
  • I have added corresponding unit tests for my changes.

@@ -125,12 +126,16 @@ tableReference
;

tableFactor
: tableName (AS? alias)? | subquery AS? alias columnNames? | LP_ tableReferences RP_
: tableName (AS? alias)?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @TherChenYang Can you refer this doc for g4 code conduct - https://shardingsphere.apache.org/community/en/involved/conduct/code/#contributor-covenant-g4-of-conduct

If a rule’s branch is over than 5, every branch take a new line.

According to g4 code conduct, we should keep these sub rule in one line when the branch is less than 5.

@@ -58,3 +56,5 @@ QUESTION_: '?';
AT_: '@';
SEMI_: ';';
DOLLAR_: '$';
fragment LBT_: '[';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change it to fragment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was writing g4, LBT_ would be parsed separately, affecting normal syntax rules. I merged the master code and found that the problem had been solved. I would roll back the code

result.setAlias(alias);
return result;
}

private int getStartIndexWithAlias(final SQLSegment sqlSegment, final AliasSegment alias) {
return alias != null && alias.getStartIndex() < sqlSegment.getStartIndex() ? alias.getStartIndex() : sqlSegment.getStartIndex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put null on the left condition.

}

private int getStopIndexWithAlias(final SQLSegment sqlSegment, final AliasSegment alias) {
return alias != null && alias.getStopIndex() > sqlSegment.getStopIndex() ? alias.getStopIndex() : sqlSegment.getStopIndex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put null on the left condition.

@@ -60,12 +60,12 @@ public Optional<IdentifierValue> getAlias() {

@Override
public int getStartIndex() {
return column.getStartIndex();
return alias != null && alias.getStartIndex() < column.getStartIndex() ? alias.getStartIndex() : column.getStartIndex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put null on the left condition.

}

@Override
public int getStopIndex() {
return null == alias ? column.getStopIndex() : alias.getStopIndex();
return alias != null && alias.getStopIndex() > column.getStopIndex() ? alias.getStopIndex() : column.getStopIndex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put null on the left condition.

@@ -83,8 +83,13 @@ public AliasSegment getAliasSegment() {
return alias;
}

@Override
public int getStartIndex() {
return alias != null && alias.getStartIndex() < startIndex ? alias.getStartIndex() : startIndex;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put null on the left condition.

@Override
public int getStopIndex() {
return null == alias ? stopIndex : alias.getStopIndex();
return alias != null && alias.getStopIndex() > stopIndex ? alias.getStopIndex() : stopIndex;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put null on the left condition.

@TherChenYang
Copy link
Collaborator Author

@strongduanmu Thank you, the modifications have been made according to your CR requirements.

Copy link
Member

@strongduanmu strongduanmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @TherChenYang for your contribution. This pr looks better now.

@strongduanmu strongduanmu merged commit 37f2185 into apache:master Dec 19, 2023
133 checks passed
@TherChenYang TherChenYang deleted the 29170-local branch December 19, 2023 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support parsing SQL Server SELECT COUNT(*) sql
2 participants