zachjsh commented on code in PR #15962:
URL: https://github.com/apache/druid/pull/15962#discussion_r1523822702
##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidSqlValidator.java:
##########
@@ -449,6 +480,94 @@ private RelDataType validateTargetType(
return targetType;
}
+ @Override
+ protected void checkTypeAssignment(
+ @Nullable SqlValidatorScope sourceScope,
+ SqlValidatorTable table,
+ RelDataType sourceRowType,
+ RelDataType targetRowType,
+ final SqlNode query
+ )
+ {
+ if (SqlTypeUtil.equalAsStructSansNullability(typeFactory,
+ sourceRowType, targetRowType, null)) {
+ // Returns early if source and target row type equals sans nullability.
+ return;
+ }
+ final List<RelDataTypeField> sourceFields = sourceRowType.getFieldList();
+ List<RelDataTypeField> targetFields = targetRowType.getFieldList();
+ final int sourceCount = sourceFields.size();
+ for (int i = 0; i < sourceCount; ++i) {
+ RelDataType sourceFielRelDataType = sourceFields.get(i).getType();
+ RelDataType targetFieldRelDataType = targetFields.get(i).getType();
+ ColumnType sourceFieldColumnType =
Calcites.getColumnTypeForRelDataType(sourceFielRelDataType);
+ ColumnType targetFieldColumnType =
Calcites.getColumnTypeForRelDataType(targetFieldRelDataType);
+
+ if (targetFieldColumnType !=
ColumnType.leastRestrictiveType(targetFieldColumnType, sourceFieldColumnType)) {
+ SqlNode node = getNthExpr(query, i, sourceCount);
+ String targetTypeString;
+ String sourceTypeString;
+ if (SqlTypeUtil.areCharacterSetsMismatched(
+ sourceFielRelDataType,
+ targetFieldRelDataType)) {
+ sourceTypeString = sourceFielRelDataType.getFullTypeString();
+ targetTypeString = targetFieldRelDataType.getFullTypeString();
+ } else {
+ sourceTypeString = sourceFielRelDataType.toString();
+ targetTypeString = targetFieldRelDataType.toString();
+ }
+ throw newValidationError(node,
+ Static.RESOURCE.typeNotAssignable(
+ targetFields.get(i).getName(), targetTypeString,
+ sourceFields.get(i).getName(), sourceTypeString));
+ }
+ }
+ // the call to base class definition will insert implicit casts /
coercions where needed.
+ super.checkTypeAssignment(sourceScope, table, sourceRowType,
targetRowType, query);
+ }
+
+ /**
+ * Locates the n'th expression in an INSERT or UPDATE query.
+ *
+ * @param query Query
+ * @param ordinal Ordinal of expression
+ * @param sourceCount Number of expressions
+ * @return Ordinal'th expression, never null
+ */
+ private static SqlNode getNthExpr(SqlNode query, int ordinal, int
sourceCount)
Review Comment:
This was copied from the base class, unfortunately its a private method 😢
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]