Skip to content

Commit

Permalink
Add Doris grammar rules and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamilv committed Dec 26, 2024
1 parent 99b0097 commit 60fdf5e
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,6 @@ databaseName
: identifier
;

newDatabaseName
: identifier
;

databaseId
: identifier
;
Expand Down Expand Up @@ -855,11 +851,6 @@ partitionName
: identifier
;

newPartitionName
: identifier
;


partitionId
: identifier
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ restart
;

recoverDatabase
: RECOVER DATABASE databaseName (databaseId | AS newDatabaseName)?
: RECOVER DATABASE databaseName (databaseId | AS newDatabaseName=identifier)?
;

recoverPartition
: RECOVER PARTITION partitionName partitionId? (AS newPartitionName)? FROM tableName
: RECOVER PARTITION partitionName partitionId? (AS newPartitionName=identifier)? FROM tableName
;

recoverTable
: RECOVER TABLE tableName tableId? (AS newDatabaseName)?
: RECOVER TABLE tableName tableId? (AS newTableName=identifier)?
;

shutdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.LoadTableIndexSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.PartitionDefinitionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.PartitionIdSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.PartitionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ResetMasterOptionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ResetOptionSegment;
Expand All @@ -137,8 +138,12 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseIdSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableIdSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.value.collection.CollectionValue;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
Expand Down Expand Up @@ -935,17 +940,17 @@ private VariableAssignSegment getVariableAssignSegment(final OptionValueContext
ctx.internalVariableName().start.getStartIndex(), ctx.internalVariableName().stop.getStopIndex(), ctx.internalVariableName().getText(), ctx.optionType().getText());
return new VariableAssignSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), variable, ctx.setExprOrDefault().getText());
}

private VariableAssignSegment getVariableAssignSegment(final OptionValueListContext ctx) {
VariableSegment variable = new VariableSegment(
ctx.internalVariableName().start.getStartIndex(), ctx.internalVariableName().stop.getStopIndex(), ctx.internalVariableName().getText(), ctx.optionType().getText());
return new VariableAssignSegment(ctx.start.getStartIndex(), ctx.setExprOrDefault().stop.getStopIndex(), variable, ctx.setExprOrDefault().getText());
}

private VariableAssignSegment getVariableAssignSegment(final OptionValueNoOptionTypeContext ctx) {
return new VariableAssignSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), getVariableSegment(ctx), getAssignValue(ctx));
}

private VariableSegment getVariableSegment(final OptionValueNoOptionTypeContext ctx) {
if (null != ctx.NAMES()) {
// TODO Consider setting all three system variables: character_set_client, character_set_results, character_set_connection
Expand Down Expand Up @@ -1080,44 +1085,89 @@ public ASTNode visitHelp(final HelpContext ctx) {
result.setSearchString(ctx.textOrIdentifier().getText());
return result;
}

@Override
public ASTNode visitRecoverDatabase(final RecoverDatabaseContext ctx) {
DorisRecoverDatabaseStatement result = new DorisRecoverDatabaseStatement();
result.setDatabaseName(new IdentifierValue(ctx.databaseName().getText()).getValue());
DatabaseSegment databaseName = new DatabaseSegment(
ctx.databaseName().start.getStartIndex(),
ctx.databaseName().stop.getStopIndex(),
new IdentifierValue(ctx.databaseName().identifier().getText()));
result.setDatabaseName(databaseName);

if (null != ctx.databaseId()) {
result.setDatabaseId(new IdentifierValue(ctx.databaseId().getText()).getValue());
DatabaseIdSegment databaseIdSegment = new DatabaseIdSegment(
ctx.databaseId().start.getStartIndex(),
ctx.databaseId().stop.getStopIndex(),
new IdentifierValue(ctx.databaseId().identifier().getText()));
result.setDatabaseId(databaseIdSegment);
}
if (null != ctx.newDatabaseName()) {
result.setDatabaseName(new IdentifierValue(ctx.newDatabaseName().getText()).getValue());
if (null != ctx.newDatabaseName) {
DatabaseSegment newDatabaseName = new DatabaseSegment(
ctx.newDatabaseName.start.getStartIndex(),
ctx.newDatabaseName.stop.getStopIndex(),
new IdentifierValue(ctx.newDatabaseName.getText()));
result.setDatabaseName(newDatabaseName);
}
return result;
}

@Override
public ASTNode visitRecoverPartition(final RecoverPartitionContext ctx) {
DorisRecoverPartitionStatement result = new DorisRecoverPartitionStatement();
result.setPartitionName(new IdentifierValue(ctx.partitionName().getText()).getValue());
PartitionSegment partitionSegment = new PartitionSegment(
ctx.partitionName().start.getStartIndex(),
ctx.partitionName().stop.getStopIndex(),
new IdentifierValue(ctx.partitionName().identifier().getText()));
result.setPartitionName(partitionSegment);
if (null != ctx.partitionId()) {
result.setPartitionId(new IdentifierValue(ctx.partitionId().getText()).getValue());
PartitionIdSegment partitionId = new PartitionIdSegment(
ctx.partitionId().start.getStartIndex(),
ctx.partitionId().stop.getStopIndex(),
new IdentifierValue(ctx.partitionId().identifier().getText()));
result.setPartitionId(partitionId);
}
if (null != ctx.newPartitionName()) {
result.setNewPartitionName(new IdentifierValue(ctx.newPartitionName().getText()).getValue());
if (null != ctx.newPartitionName) {
final PartitionSegment newPartitionName = new PartitionSegment(
ctx.newPartitionName.start.getStartIndex(),
ctx.newPartitionName.stop.getStopIndex(),
new IdentifierValue(ctx.newPartitionName.getText()));
result.setNewPartitionName(newPartitionName);
}
if (null != ctx.tableName().owner()) {
result.setOwner(new IdentifierValue(ctx.tableName().owner().getText()).getValue());
}
result.setTableName(new IdentifierValue(ctx.tableName().name().getText()).getValue());
SimpleTableSegment simpleTableSegment = createSimpleTableSegment(ctx.tableName());
result.setTableName(simpleTableSegment);

return result;
}

@Override
public ASTNode visitRecoverTable(final RecoverTableContext ctx) {
DorisRecoverTableStatement result = new DorisRecoverTableStatement();
result.setTableName(new IdentifierValue(ctx.tableName().getText()).getValue());
SimpleTableSegment simpleTableSegment = createSimpleTableSegment(ctx.tableName());
result.setTableName(simpleTableSegment);
if (null != ctx.tableId()) {
result.setTableName(new IdentifierValue(ctx.tableId().getText()).getValue());
final TableIdSegment tableIdSegment = new TableIdSegment(
ctx.tableId().start.getStartIndex(),
ctx.tableId().stop.getStopIndex(),
new IdentifierValue(ctx.tableId().getText()));
result.setTableId(tableIdSegment);
}
return result;
}

private SimpleTableSegment createSimpleTableSegment(final TableNameContext tableNameContext) {
final TableNameSegment tableNameSegment = new TableNameSegment(
tableNameContext.start.getStartIndex(),
tableNameContext.stop.getStopIndex(),
new IdentifierValue(tableNameContext.getText()));
SimpleTableSegment simpleTableSegment = new SimpleTableSegment(tableNameSegment);
if (null != tableNameContext.owner()) {
OwnerSegment ownerSegment = new OwnerSegment(
tableNameContext.owner().start.getStartIndex(),
tableNameContext.owner().stop.getStopIndex(),
new IdentifierValue(tableNameContext.owner().getText()));
simpleTableSegment.setOwner(ownerSegment);
}
return simpleTableSegment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.statement.core.statement.dal;
package org.apache.shardingsphere.sql.parser.statement.core.segment.dal;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

/**
* Create database statement.
* Partition Id segment.
*/
@RequiredArgsConstructor
@Getter
@Setter
public abstract class RecoverDatabaseStatement extends AbstractSQLStatement implements DDLStatement {
public final class PartitionIdSegment implements SQLSegment {

private String databaseName;
private final int startIndex;

private String databaseId;
private final int stopIndex;

private final IdentifierValue name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.statement.core.statement.dal;
package org.apache.shardingsphere.sql.parser.statement.core.segment.generic;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.statement.core.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

/**
* Create database statement.
* Database Id segment.
*/
@RequiredArgsConstructor
@Getter
@Setter
public abstract class RecoverTableStatement extends AbstractSQLStatement implements DDLStatement {
public final class DatabaseIdSegment implements SQLSegment {

private String tableName;
private final int startIndex;

private String tableId;
private final int stopIndex;

private final IdentifierValue identifier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,23 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.statement.core.statement.dal;
package org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.statement.core.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

/**
* Create database statement.
* Table Id segment.
*/
@RequiredArgsConstructor
@Getter
@Setter
public abstract class RecoverPartitionStatement extends AbstractSQLStatement implements DDLStatement {
public final class TableIdSegment implements SQLSegment {

private String partitionName;
private final int startIndex;

private String partitionId;

private String databaseName;

private String newPartitionName;

private String owner;

private String tableName;
private final int stopIndex;

private final IdentifierValue identifier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@

package org.apache.shardingsphere.sql.parser.statement.doris.dal;

import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.RecoverDatabaseStatement;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseIdSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.statement.doris.DorisStatement;

/**
* Doris create database statement.
* Doris recover database statement.
*/
public final class DorisRecoverDatabaseStatement extends RecoverDatabaseStatement implements DorisStatement {

@Setter
@Getter
public final class DorisRecoverDatabaseStatement extends AbstractSQLStatement implements DorisStatement {

private DatabaseSegment databaseName;

private DatabaseIdSegment databaseId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@

package org.apache.shardingsphere.sql.parser.statement.doris.dal;

import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.RecoverPartitionStatement;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.PartitionIdSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.PartitionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.statement.doris.DorisStatement;

/**
* Doris create database statement.
* Doris recover partition statement.
*/
public final class DorisRecoverPartitionStatement extends RecoverPartitionStatement implements DorisStatement {
@Setter
@Getter
public final class DorisRecoverPartitionStatement extends AbstractSQLStatement implements DorisStatement {

private PartitionSegment partitionName;

private PartitionSegment newPartitionName;

private PartitionIdSegment partitionId;

private DatabaseSegment databaseName;

private SimpleTableSegment tableName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@

package org.apache.shardingsphere.sql.parser.statement.doris.dal;

import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.RecoverTableStatement;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableIdSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.statement.doris.DorisStatement;

/**
* Doris create database statement.
* Doris recover table statement.
*/
public final class DorisRecoverTableStatement extends RecoverTableStatement implements DorisStatement {

@Getter
@Setter
public final class DorisRecoverTableStatement extends AbstractSQLStatement implements DorisStatement {

private SimpleTableSegment tableName;

private TableIdSegment tableId;
}

0 comments on commit 60fdf5e

Please sign in to comment.