Skip to content

Commit

Permalink
[fix][dingo-calcite] Enrich and improve the syntax for creating indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
guojn1 committed Dec 31, 2024
1 parent dd607ea commit fd5fa01
Show file tree
Hide file tree
Showing 21 changed files with 786 additions and 57 deletions.
14 changes: 14 additions & 0 deletions dingo-calcite/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ data: {
"io.dingodb.calcite.grammar.ddl.SqlAnalyze"
"io.dingodb.calcite.grammar.ddl.SqlCreateTenant"
"io.dingodb.calcite.grammar.ddl.SqlCreateSchema"
"io.dingodb.calcite.grammar.ddl.DingoSqlCreateView"
"io.dingodb.calcite.grammar.ddl.SqlCreateUser"
"io.dingodb.calcite.grammar.ddl.SqlCreateIndex"
"io.dingodb.calcite.grammar.ddl.SqlCreateVectorIndex"
Expand Down Expand Up @@ -161,7 +162,10 @@ data: {
# List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
# not a reserved keyword, add it to the 'nonReservedKeywords' section.
keywords: [
"ALGORITHM"
"INPLACE"
"IF"
"COPY"
"MATERIALIZED"
"STORED"
"VIRTUAL"
Expand All @@ -178,6 +182,7 @@ data: {
"ENGINE"
"ENGINES"
"EXCHANGE"
"EXCLUSIVE"
"EXECUTOR"
"EXECUTORS"
"ENFORCED"
Expand Down Expand Up @@ -208,6 +213,7 @@ data: {
"SCAN"
"SSL"
"STARTING"
"SHARED"
"STATUS"
"TABLES"
"STARTTS"
Expand All @@ -228,6 +234,7 @@ data: {
"SAMPLES"
"SAMPLERATE"
"PESSIMISTIC"
"PARSER"
"OPTIMISTIC"
"BLOCKS"
"HASH"
Expand Down Expand Up @@ -260,6 +267,13 @@ data: {
"CACHE"
"MODIFY"
"CHANGE"
"SPATIAL"
"FULLTEXT"
"KEY_BLOCK_SIZE"
"BTREE"
"RTREE"
"UNDEFINED"
"TEMPTABLE"
]

# List of non-reserved keywords to add;
Expand Down
109 changes: 97 additions & 12 deletions dingo-calcite/src/main/codegen/includes/AlterTable.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ SqlAlterSchema SqlAlterSchema(Span s, String scope): {
}
}

SqlAlterTable SqlAlterIgnoreTable(Span s, String scope): {
SqlAlterTable alterTable = null;
} {
<IGNORE>
alterTable = SqlAlterTable(s, scope)
{
return alterTable;
}
}

SqlAlterTable SqlAlterTable(Span s, String scope): {
SqlIdentifier id;
SqlAlterTable alterTable = null;
Expand All @@ -81,6 +91,10 @@ SqlAlterTable SqlAlterTable(Span s, String scope): {
alterTable = addColumn(s, scope, id)
|
alterTable = addConstraint(s, scope, id)
|
<FULLTEXT> alterTable = addIndexByMode(s, scope, id, "fulltext")
|
<SPATIAL> alterTable = addIndexByMode(s, scope, id, "spetail")
)
|
<DROP>
Expand Down Expand Up @@ -166,6 +180,7 @@ SqlAlterTable addColumn(Span s, String scope, SqlIdentifier id): {
boolean primary = false;
String comment = "";
boolean autoInc = false;
String collate = "utf8_bin";
} {
<COLUMN>
columnId = SimpleIdentifier()
Expand All @@ -188,6 +203,8 @@ SqlAlterTable addColumn(Span s, String scope, SqlIdentifier id): {
<PRIMARY> <KEY> { primary=true; }
|
<COMMENT> comment = dingoIdentifier()
|
<COLLATE> { collate = this.getNextToken().image;}
|
<AUTO_INCREMENT> { autoInc = true;}
|
Expand All @@ -199,7 +216,7 @@ SqlAlterTable addColumn(Span s, String scope, SqlIdentifier id): {
: ColumnStrategy.NOT_NULLABLE;
}
return new SqlAlterAddColumn(s.end(this), id, DingoSqlDdlNodes.createColumn(
s.end(this), columnId, type.withNullable(nullable), e, strategy, autoInc, comment, primary
s.end(this), columnId, type.withNullable(nullable), e, strategy, autoInc, comment, primary, collate
));
}
}
Expand Down Expand Up @@ -233,17 +250,20 @@ SqlAlterTable addIndex(Span s, String scope, SqlIdentifier id): {
int replica = 0;
String indexType = "scalar";
SqlNodeList withColumnList = null;
final SqlNodeList columnList;
List<SqlNode> columnList;
String engine = null;
Properties prop = new Properties();
String indexAlg = null;
String indexLockOpt = null;
} {
<INDEX> { s.add(this); }
{ index = getNextToken().image; }
(
<VECTOR> { indexType = "vector"; } columnList = ParenthesizedSimpleIdentifierList()
<VECTOR> { indexType = "vector"; } columnList = indexColumns()
|
<TEXT> { indexType = "text"; } columnList = ParenthesizedSimpleIdentifierList()
<TEXT> { indexType = "text"; } columnList = indexColumns()
|
[<SCALAR>] columnList = ParenthesizedSimpleIdentifierList()
[<SCALAR>] columnList = indexColumns()
)
(
<WITH> withColumnList = ParenthesizedSimpleIdentifierList()
Expand All @@ -261,11 +281,14 @@ SqlAlterTable addIndex(Span s, String scope, SqlIdentifier id): {
|
<PARAMETERS> properties = readProperties()
)*
prop = indexOption()
[ indexAlg = indexAlg()]
[ indexLockOpt = indexLockOpt()]
{
return new SqlAlterAddIndex(
s.end(this), id,
new SqlIndexDeclaration(
s.end(this), index, columnList, withColumnList, properties,partitionDefinition, replica, indexType, engine, false
s.end(this), index, columnList, withColumnList, properties,partitionDefinition, replica, indexType, engine, "", prop, indexAlg, indexLockOpt
)
);
}
Expand All @@ -279,12 +302,15 @@ SqlAlterTable addUniqueIndex(Span s, String scope, SqlIdentifier id): {
int replica = 0;
String indexType = "scalar";
SqlNodeList withColumnList = null;
final SqlNodeList columnList;
List<SqlNode> columnList;
String engine = null;
Properties prop = null;
String indexAlg = null;
String indexLockOpt = null;
} {
<UNIQUE> [<INDEX>][<KEY>] { s.add(this); }
{ index = getNextToken().image; }
[<SCALAR>] columnList = ParenthesizedSimpleIdentifierList()
[<SCALAR>] columnList = indexColumns()
(
<WITH> withColumnList = ParenthesizedSimpleIdentifierList()
|
Expand All @@ -301,16 +327,66 @@ SqlAlterTable addUniqueIndex(Span s, String scope, SqlIdentifier id): {
|
<PARAMETERS> properties = readProperties()
)*
prop = indexOption()
[ indexAlg = indexAlg()]
[ indexLockOpt = indexLockOpt()]
{
return new SqlAlterAddIndex(
s.end(this), id,
new SqlIndexDeclaration(
s.end(this), index, columnList, withColumnList, properties,partitionDefinition, replica, indexType, engine, true
s.end(this), index, columnList, withColumnList, properties,partitionDefinition, replica, indexType, engine, "unique", prop, indexAlg, indexLockOpt
)
);
}
}

SqlAlterTable addIndexByMode(Span s, String scope, SqlIdentifier id, String mode): {
final String index;
Boolean autoIncrement = false;
Properties properties = null;
PartitionDefinition partitionDefinition = null;
int replica = 0;
String indexType = "scalar";
SqlNodeList withColumnList = null;
List<SqlNode> columnList;
String engine = null;
Properties prop = null;
String indexAlg = null;
String indexLockOpt = null;
} {
<INDEX> { s.add(this); }
{ index = getNextToken().image; }
columnList = indexColumns()
(
<WITH> withColumnList = ParenthesizedSimpleIdentifierList()
|
<ENGINE> <EQ> engine = dingoIdentifier() { if (engine.equalsIgnoreCase("innodb")) { engine = "TXN_LSM";} }
|
<PARTITION> <BY>
{
partitionDefinition = new PartitionDefinition();
partitionDefinition.setFuncName(getNextToken().image);
partitionDefinition.setDetails(readPartitionDetails());
}
|
<REPLICA> <EQ> {replica = Integer.parseInt(getNextToken().image);}
|
<PARAMETERS> properties = readProperties()
)*
prop = indexOption()
[ indexAlg = indexAlg()]
[ indexLockOpt = indexLockOpt()]
{
return new SqlAlterAddIndex(
s.end(this), id,
new SqlIndexDeclaration(
s.end(this), index, columnList, withColumnList, properties,partitionDefinition, replica, indexType, engine, mode, prop, indexAlg, indexLockOpt
)
);
}
}


SqlAlterTable alterIndex(Span s, String scope, SqlIdentifier id): {
final String index;
Properties properties = new Properties();
Expand Down Expand Up @@ -501,13 +577,15 @@ SqlAlterTable modifyColumn(Span s, String scope, SqlIdentifier id, SqlAlterTable
PartitionDefinition partitionDefinition = null;
int replica = 0;
String engine = null;
String collate = "utf8_bin";
String indexType = "scalar";
Boolean primaryKey = false;
String comment = "";
SqlNodeList refColumnList = null;
SqlIdentifier refTable = null;
String updateRefOpt = null;
String deleteRefOpt = null;
SqlIdentifier afterCol = null;
} {
<COLUMN> name = SimpleIdentifier()
(
Expand Down Expand Up @@ -540,6 +618,8 @@ SqlAlterTable modifyColumn(Span s, String scope, SqlIdentifier id, SqlAlterTable
<COMMENT> (<IDENTIFIER>|<QUOTED_STRING>) { comment = token.image; }
|
<ON> <UPDATE> <CURRENT_TIMESTAMP>
|
<COLLATE> { collate = this.getNextToken().image; }
|
<CONSTRAINT> { s.add(this); } [name = SimpleIdentifier()] <CHECK> <LPAREN>
checkExpr = Expression(ExprContext.ACCEPT_SUB_QUERY)
Expand All @@ -553,20 +633,22 @@ SqlAlterTable modifyColumn(Span s, String scope, SqlIdentifier id, SqlAlterTable
)
)*
)*
[ <AFTER> afterCol = SimpleIdentifier() ]
{
if (e == null) {
strategy = nullable ? ColumnStrategy.NULLABLE
: ColumnStrategy.NOT_NULLABLE;
}
columnDec = DingoSqlDdlNodes.createColumn(s.add(id).end(this), name, type.withNullable(nullable), e, strategy, autoIncrement, comment, primaryKey);
columnDec = DingoSqlDdlNodes.createColumn(s.add(id).end(this), name, type.withNullable(nullable), e, strategy, autoIncrement, comment, primaryKey, collate);
}
)
{
if (alterTable == null) {
return new SqlAlterModifyColumn(s.end(this), id, columnDec);
return new SqlAlterModifyColumn(s.end(this), id, columnDec, afterCol);
} else {
SqlAlterModifyColumn alterModifyCol = (SqlAlterModifyColumn)alterTable;
alterModifyCol.addSqlColumn(columnDec);
alterModifyCol.addAlterColumn(afterCol);
return alterModifyCol;
}
}
Expand Down Expand Up @@ -617,6 +699,7 @@ SqlAlterTable changeColumn(Span s, String scope, SqlIdentifier id): {
SqlIdentifier refTable = null;
String updateRefOpt = null;
String deleteRefOpt = null;
String collate = "utf8_bin";
} {
<COLUMN> name = SimpleIdentifier() newName = SimpleIdentifier()
(
Expand Down Expand Up @@ -649,6 +732,8 @@ SqlAlterTable changeColumn(Span s, String scope, SqlIdentifier id): {
<COMMENT> (<IDENTIFIER>|<QUOTED_STRING>) { comment = token.image; }
|
<ON> <UPDATE> <CURRENT_TIMESTAMP>
|
<COLLATE> { collate = this.getNextToken().image;}
|
<CONSTRAINT> { s.add(this); } [name = SimpleIdentifier()] <CHECK> <LPAREN>
checkExpr = Expression(ExprContext.ACCEPT_SUB_QUERY)
Expand All @@ -667,7 +752,7 @@ SqlAlterTable changeColumn(Span s, String scope, SqlIdentifier id): {
strategy = nullable ? ColumnStrategy.NULLABLE
: ColumnStrategy.NOT_NULLABLE;
}
columnDec = DingoSqlDdlNodes.createColumn(s.add(id).end(this), name, type.withNullable(nullable), e, strategy, autoIncrement, comment, primaryKey);
columnDec = DingoSqlDdlNodes.createColumn(s.add(id).end(this), name, type.withNullable(nullable), e, strategy, autoIncrement, comment, primaryKey, collate);
}
)?
{
Expand Down
Loading

0 comments on commit fd5fa01

Please sign in to comment.