Skip to content

Commit

Permalink
Support for SQLServer parse update statistics sql (#29655)
Browse files Browse the repository at this point in the history
* add update statistics Segment

* Add support for update statistics

* Add test rules for update statistics

* replace import collection

* add java doc in segment

* Adjust spotless appley

* Adjust spotless appley
  • Loading branch information
TherChenYang authored Jan 7, 2024
1 parent 518f352 commit d30870a
Show file tree
Hide file tree
Showing 28 changed files with 1,220 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ unreservedWord
| DATA_RETENTION | TEMPORAL_HISTORY_RETENTION | EDITION | MIXED_PAGE_ALLOCATION | DISABLED | ALLOWED | HADR | MULTI_USER | RESTRICTED_USER | SINGLE_USER | OFFLINE | EMERGENCY | SUSPEND | DATE_CORRELATION_OPTIMIZATION
| ELASTIC_POOL | SERVICE_OBJECTIVE | DATABASE_NAME | ALLOW_CONNECTIONS | GEO | NAMED | DATEFIRST | BACKUP_STORAGE_REDUNDANCY | FORCE_FAILOVER_ALLOW_DATA_LOSS | SECONDARY | FAILOVER | DEFAULT_FULLTEXT_LANGUAGE
| DEFAULT_LANGUAGE | INLINE | NESTED_TRIGGERS | TRANSFORM_NOISE_WORDS | TWO_DIGIT_YEAR_CUTOFF | PERSISTENT_LOG_BUFFER | DIRECTORY_NAME | DATEFORMAT | DELAYED_DURABILITY | TRANSFER | SCHEMA | PASSWORD | AUTHORIZATION
| MEMBER | SEARCH | TEXT | SECOND | PRECISION | VIEWS | PROVIDER
| MEMBER | SEARCH | TEXT | SECOND | PRECISION | VIEWS | PROVIDER | COLUMNS
;

databaseName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ truncateTable
: TRUNCATE TABLE tableName
;

updateStatistics
: UPDATE STATISTICS tableName (LP_? indexName (COMMA_ indexName)* RP_?)? statisticsWithClause?
;

statisticsWithClause
: WITH sampleOption? statisticsOptions?
;

sampleOption
: (FULLSCAN | (SAMPLE NUMBER_ (PERCENT | ROWS))) (PERSIST_SAMPLE_PERCENT EQ_ (ON | OFF))?
| RESAMPLE (ON PARTITIONS LP_ NUMBER_ (COMMA_ NUMBER_)* RP_)?
;

statisticsOptions
: (COMMA_? statisticsOption)+
;

statisticsOption
: ALL | COLUMNS | INDEX
| NORECOMPUTE
| INCREMENTAL EQ_ (ON | OFF)
| MAXDOP EQ_ NUMBER_
| AUTO_DROP EQ_ (ON | OFF)
;

fileTableClause
: (AS FILETABLE)?
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ COLUMN
: C O L U M N
;

COLUMNS
: C O L U M N S
;

INDEX
: I N D E X
;
Expand Down Expand Up @@ -227,6 +231,10 @@ ON
: O N
;

OFF
: O F F
;

IF
: I F
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1914,3 +1914,31 @@ XMLNAMESPACES
APPLY
: A P P L Y
;

STATISTICS
: S T A T I S T I C S
;

FULLSCAN
: F U L L S C A N
;

SAMPLE
: S A M P L E
;

RESAMPLE
: R E S A M P L E
;

NORECOMPUTE
: N O R E C O M P U T E
;

AUTO_DROP
: A U T O UL_ D R O P
;

PERSIST_SAMPLE_PERCENT
: P E R S I S T UL_ S A M P L E UL_ P E R C E N T
;
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@ execute
| explain
| setUser
| revert
| updateStatistics
) SEMI_?
;
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ViewNameContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.WhereClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.WithClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.UpdateStatisticsContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.StatisticsWithClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.StatisticsOptionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.StatisticsOptionsContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SampleOptionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertExecClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ExecContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ProcedureNameContext;
Expand Down Expand Up @@ -187,10 +192,17 @@
import org.apache.shardingsphere.sql.parser.sql.common.value.parametermarker.ParameterMarkerValue;
import org.apache.shardingsphere.sql.parser.sql.dialect.segment.sqlserver.exec.ExecSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerCreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerUpdateStatisticsStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerDeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerInsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerSelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerUpdateStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment.SampleOptionSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment.SampleStrategy;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment.ScanUnit;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment.StatisticsDimension;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment.StatisticsOptionSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment.StatisticsStrategySegment;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -1378,4 +1390,99 @@ public ASTNode visitCreateTableAsSelectClause(final CreateTableAsSelectClauseCon
}
return result;
}

@Override
public ASTNode visitUpdateStatistics(final UpdateStatisticsContext ctx) {
SQLServerUpdateStatisticsStatement result = new SQLServerUpdateStatisticsStatement();
if (null != ctx.tableName()) {
result.setTable((SimpleTableSegment) visit(ctx.tableName()));
}
if (null != ctx.indexName() && ctx.indexName().size() > 0) {
List<IndexSegment> indexSegments = new LinkedList<>();
for (IndexNameContext indexNameContext : ctx.indexName()) {
indexSegments.add((IndexSegment) visit(indexNameContext));
}
result.setIndexes(indexSegments);
}
if (null != ctx.statisticsWithClause()) {
result.setStrategy((StatisticsStrategySegment) visit(ctx.statisticsWithClause()));
}
return result;
}

@Override
public ASTNode visitStatisticsWithClause(final StatisticsWithClauseContext ctx) {
StatisticsStrategySegment result = new StatisticsStrategySegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
if (null != ctx.sampleOption()) {
result.setSampleOption((SampleOptionSegment) visit(ctx.sampleOption()));
}
if (null != ctx.statisticsOptions()) {
result.setStatisticsOptions((StatisticsOptionSegment) visit(ctx.statisticsOptions()));
}
return result;
}

@Override
public ASTNode visitStatisticsOption(final StatisticsOptionContext ctx) {
return super.visitStatisticsOption(ctx);
}

@Override
public ASTNode visitSampleOption(final SampleOptionContext ctx) {
SampleOptionSegment result = new SampleOptionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
if (null != ctx.FULLSCAN()) {
result.setStrategy(SampleStrategy.FULLSCAN);
} else if (null != ctx.SAMPLE()) {
result.setStrategy(SampleStrategy.SAMPLE);
if (null != ctx.NUMBER_()) {
List<TerminalNode> number = ctx.NUMBER_();
result.setSampleNumber(number.get(0).getText());
}
if (null != ctx.PERCENT()) {
result.setScanUnit(ScanUnit.PERCENT);
} else if (null != ctx.ROWS()) {
result.setScanUnit(ScanUnit.ROWS);
}
} else if (null != ctx.RESAMPLE()) {
result.setStrategy(SampleStrategy.RESAMPLE);
if (null != ctx.NUMBER_()) {
List<String> partitions = new LinkedList<>();
for (TerminalNode terminalNode : ctx.NUMBER_()) {
partitions.add(terminalNode.getText());
}
result.setPartitions(partitions);
}
}
if (null != ctx.PERSIST_SAMPLE_PERCENT()) {
result.setPersistSamplePercent(null != ctx.ON());
}
return result;
}

@Override
public ASTNode visitStatisticsOptions(final StatisticsOptionsContext ctx) {
StatisticsOptionSegment result = new StatisticsOptionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
for (StatisticsOptionContext option : ctx.statisticsOption()) {
if (null != option.ALL()) {
result.setStatisticsDimension(StatisticsDimension.ALL);
} else if (null != option.COLUMNS()) {
result.setStatisticsDimension(StatisticsDimension.COLUMNS);
} else if (null != option.INDEX()) {
result.setStatisticsDimension(StatisticsDimension.INDEX);
}
if (null != option.NORECOMPUTE()) {
result.setNoRecompute(true);
}
if (null != option.INCREMENTAL()) {
result.setIncremental(null != option.ON());
}
if (null != option.MAXDOP()) {
result.setMaxDegreeOfParallelism(option.NUMBER_().getText());
}
if (null != option.AUTO_DROP()) {
result.setAutoDrop(null != option.ON());
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,9 @@ public enum SQLVisitorRule {

SWITCH("Switch", SQLStatementType.DDL),

CREATE_PROFILE("CreateProfile", SQLStatementType.DDL);
CREATE_PROFILE("CreateProfile", SQLStatementType.DDL),

UPDATE_STATISTICS("UpdateStatistics", SQLStatementType.DDL);

private final String name;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment.StatisticsStrategySegment;

import java.util.List;

/**
* SQLServer update statistics statement.
*/
@Getter
public final class SQLServerUpdateStatisticsStatement extends AbstractSQLStatement implements DDLStatement, SQLServerStatement {

@Setter
private List<IndexSegment> indexes;

@Setter
private SimpleTableSegment table;

@Setter
private StatisticsStrategySegment strategy;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;

import java.util.List;

/**
* Update statistics sample option segment.
*/
@Getter
@Setter
@NoArgsConstructor
public final class SampleOptionSegment implements SQLSegment {

private int startIndex;

private int stopIndex;

private boolean persistSamplePercent;

private String sampleNumber;

private SampleStrategy strategy;

private ScanUnit scanUnit;

private List<String> partitions;

public SampleOptionSegment(final int startIndex, final int stopIndex) {
this.startIndex = startIndex;
this.stopIndex = stopIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.segment;

/**
* Update statistics sample strategy enum.
*/
public enum SampleStrategy {

FULLSCAN,

SAMPLE,

RESAMPLE
}
Loading

0 comments on commit d30870a

Please sign in to comment.