Skip to content

Commit a85c04b

Browse files
committed
[fix][dingo-calcite] Changes made for Aliyun DTS adaptation
1 parent 04353d3 commit a85c04b

File tree

11 files changed

+618
-34
lines changed

11 files changed

+618
-34
lines changed

dingo-calcite/src/main/codegen/config.fmpp

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ data: {
303303
"JAR"
304304
"FILE"
305305
"ARCHIVE"
306+
"CARDINALITY"
306307
]
307308

308309
# List of methods for parsing extensions to "CREATE [OR REPLACE]" calls.

dingo-calcite/src/main/codegen/templates/Parser.jj

+1
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,7 @@ SqlNode SqlInsert() :
19121912
|
19131913
<UPSERT> { keywords.add(SqlInsertKeyword.UPSERT.symbol(getPos())); }
19141914
)
1915+
[<IGNORE>]
19151916
{
19161917
s = span();
19171918
targetColumnList = new SqlNodeList(s.pos());

dingo-calcite/src/main/java/io/dingodb/calcite/DingoParser.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ public DingoParser(final @NonNull DingoParserContext context) {
236236
Objects.requireNonNull(cluster.getMetadataProvider())
237237
)
238238
));
239-
240239
// Create SqlValidator
241240
sqlValidator = context.getSqlValidator();
242241

@@ -451,6 +450,17 @@ private static String processKeyWords(String sql) {
451450
sql = sql.replace(comment, "");
452451
}
453452
// for dump test
453+
// for dts test
454+
int i;
455+
if ((i = sql.indexOf("where grantee")) >= 0) {
456+
sql = sql.substring(0, i);
457+
sql = sql + " where 1=1";
458+
} else if ((i = sql.indexOf("where grantee")) >= 0) {
459+
sql = sql.substring(0, i);
460+
sql = sql + " where 1=1";
461+
} else if (sql.equalsIgnoreCase("set time_zone=\"+08:00\"")) {
462+
sql = "set time_zone='+08:00'";
463+
}
454464

455465
for (Map.Entry<String, String> entry : sensitiveKey.entrySet()) {
456466
if (sql.contains(entry.getKey())) {

dingo-calcite/src/main/java/io/dingodb/calcite/DingoSqlValidator.java

+101
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import lombok.Setter;
2222
import org.apache.calcite.rel.type.RelDataType;
2323
import org.apache.calcite.rel.type.RelDataTypeFactory;
24+
import org.apache.calcite.rel.type.RelDataTypeField;
2425
import org.apache.calcite.sql.SqlBasicCall;
2526
import org.apache.calcite.sql.SqlCall;
27+
import org.apache.calcite.sql.SqlKind;
2628
import org.apache.calcite.sql.SqlNode;
29+
import org.apache.calcite.sql.SqlUtil;
2730
import org.apache.calcite.sql.fun.SqlMapValueConstructor;
2831
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
2932
import org.apache.calcite.sql.util.SqlOperatorTables;
@@ -38,11 +41,17 @@
3841
import org.apache.calcite.sql2rel.SqlFunctionScanOperator;
3942
import org.apache.calcite.sql2rel.SqlHybridSearchOperator;
4043
import org.apache.calcite.sql2rel.SqlVectorOperator;
44+
import org.apache.calcite.util.Pair;
45+
import org.apache.calcite.util.Util;
4146
import org.checkerframework.checker.nullness.qual.Nullable;
4247

48+
import java.util.AbstractList;
49+
import java.util.List;
4350
import java.util.Map;
4451
import java.util.concurrent.ConcurrentHashMap;
4552

53+
import static org.apache.calcite.util.Static.RESOURCE;
54+
4655
public class DingoSqlValidator extends SqlValidatorImpl {
4756

4857
@Getter
@@ -148,4 +157,96 @@ protected void inferUnknownTypes(RelDataType inferredType, SqlValidatorScope sco
148157
super.inferUnknownTypes(inferredType, scope, node);
149158
}
150159

160+
protected void validateValues(
161+
SqlCall node,
162+
RelDataType targetRowType,
163+
final SqlValidatorScope scope) {
164+
assert node.getKind() == SqlKind.VALUES;
165+
166+
final List<SqlNode> operands = node.getOperandList();
167+
for (SqlNode operand : operands) {
168+
if (!(operand.getKind() == SqlKind.ROW)) {
169+
throw Util.needToImplement(
170+
"Values function where operands are scalars");
171+
}
172+
173+
SqlCall rowConstructor = (SqlCall) operand;
174+
if (false
175+
&& targetRowType.isStruct()
176+
&& rowConstructor.operandCount() < targetRowType.getFieldCount()) {
177+
targetRowType =
178+
typeFactory.createStructType(
179+
targetRowType.getFieldList()
180+
.subList(0, rowConstructor.operandCount()));
181+
} else if (targetRowType.isStruct()
182+
&& rowConstructor.operandCount() != targetRowType.getFieldCount()) {
183+
return;
184+
}
185+
186+
inferUnknownTypes(
187+
targetRowType,
188+
scope,
189+
rowConstructor);
190+
191+
if (targetRowType.isStruct()) {
192+
for (Pair<SqlNode, RelDataTypeField> pair
193+
: Pair.zip(rowConstructor.getOperandList(),
194+
targetRowType.getFieldList())) {
195+
if (!pair.right.getType().isNullable()
196+
&& SqlUtil.isNullLiteral(pair.left, false)) {
197+
throw newValidationError(node,
198+
RESOURCE.columnNotNullable(pair.right.getName()));
199+
}
200+
}
201+
}
202+
}
203+
204+
for (SqlNode operand : operands) {
205+
operand.validate(this, scope);
206+
}
207+
208+
// validate that all row types have the same number of columns
209+
// and that expressions in each column are compatible.
210+
// A values expression is turned into something that looks like
211+
// ROW(type00, type01,...), ROW(type11,...),...
212+
final int rowCount = operands.size();
213+
if (rowCount >= 2) {
214+
SqlCall firstRow = (SqlCall) operands.get(0);
215+
final int columnCount = firstRow.operandCount();
216+
217+
// 1. check that all rows have the same cols length
218+
for (SqlNode operand : operands) {
219+
SqlCall thisRow = (SqlCall) operand;
220+
if (columnCount != thisRow.operandCount()) {
221+
throw newValidationError(node,
222+
RESOURCE.incompatibleValueType(
223+
SqlStdOperatorTable.VALUES.getName()));
224+
}
225+
}
226+
227+
// 2. check if types at i:th position in each row are compatible
228+
for (int col = 0; col < columnCount; col++) {
229+
final int c = col;
230+
final RelDataType type =
231+
typeFactory.leastRestrictive(
232+
new AbstractList<RelDataType>() {
233+
@Override public RelDataType get(int row) {
234+
SqlCall thisRow = (SqlCall) operands.get(row);
235+
return deriveType(scope, thisRow.operand(c));
236+
}
237+
238+
@Override public int size() {
239+
return rowCount;
240+
}
241+
});
242+
243+
if (null == type) {
244+
throw newValidationError(node,
245+
RESOURCE.incompatibleValueType(
246+
SqlStdOperatorTable.VALUES.getName()));
247+
}
248+
}
249+
}
250+
}
251+
151252
}

dingo-calcite/src/main/java/io/dingodb/calcite/executor/SetOptionExecutor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ public void execute() {
8989
&& "READ-COMMITTED".equalsIgnoreCase(value)
9090
&& connection.getClientInfo("txn_mode").equalsIgnoreCase("optimistic")
9191
) {
92-
throw new RuntimeException("Optimistic transaction mode cannot be changed"
93-
+ " to read committed transaction isolation level");
92+
return;
93+
//throw new RuntimeException("Optimistic transaction mode cannot be changed"
94+
// + " to read committed transaction isolation level");
9495
}
9596
if ("SESSION".equals(scope) || "USER".equals(scope)) {
9697
if (!setCharacter(name, value)) {

dingo-calcite/src/main/java/io/dingodb/calcite/executor/VariableValidator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public static String validator(String name, String value, String scope) {
8989
}
9090

9191
if ("SYSTEM".equals(scope)) {
92-
if (!ScopeVariables.containsGlobalVarKey(name)) {
93-
throw new RuntimeException(String.format(ErrorCode.ER_UNKNOWN_VARIABLES.message, name));
94-
}
92+
//if (!ScopeVariables.containsGlobalVarKey(name)) {
93+
// throw new RuntimeException(String.format(ErrorCode.ER_UNKNOWN_VARIABLES.message, name));
94+
//}
9595
} else {
9696
if (value.contains("'")) {
9797
value = value.replace("'", "");

0 commit comments

Comments
 (0)