use of org.apache.calcite.sql.SqlDynamicParam in project flink by splunk.
the class SqlValidatorImpl method checkTypeAssignment.
/**
* Checks the type assignment of an INSERT or UPDATE query.
*
* <p>Skip the virtual columns(can not insert into) type assignment check if the source fields
* count equals with the real target table fields count, see how #checkFieldCount was used.
*
* @param sourceScope Scope of query source which is used to infer node type
* @param table Target table
* @param sourceRowType Source row type
* @param targetRowType Target row type, it should either contain all the virtual columns (can
* not insert into) or exclude all the virtual columns
* @param query The query
*/
protected void checkTypeAssignment(SqlValidatorScope sourceScope, SqlValidatorTable table, RelDataType sourceRowType, RelDataType targetRowType, final SqlNode query) {
// NOTE jvs 23-Feb-2006: subclasses may allow for extra targets
// representing system-maintained columns, so stop after all sources
// matched
boolean isUpdateModifiableViewTable = false;
if (query instanceof SqlUpdate) {
final SqlNodeList targetColumnList = ((SqlUpdate) query).getTargetColumnList();
if (targetColumnList != null) {
final int targetColumnCnt = targetColumnList.size();
targetRowType = SqlTypeUtil.extractLastNFields(typeFactory, targetRowType, targetColumnCnt);
sourceRowType = SqlTypeUtil.extractLastNFields(typeFactory, sourceRowType, targetColumnCnt);
}
isUpdateModifiableViewTable = table.unwrap(ModifiableViewTable.class) != null;
}
if (SqlTypeUtil.equalAsStructSansNullability(typeFactory, sourceRowType, targetRowType, null)) {
// Returns early if source and target row type equals sans nullability.
return;
}
if (config.typeCoercionEnabled() && !isUpdateModifiableViewTable) {
// Try type coercion first if implicit type coercion is allowed.
boolean coerced = typeCoercion.querySourceCoercion(sourceScope, sourceRowType, targetRowType, query);
if (coerced) {
return;
}
}
// Fall back to default behavior: compare the type families.
List<RelDataTypeField> sourceFields = sourceRowType.getFieldList();
List<RelDataTypeField> targetFields = targetRowType.getFieldList();
final int sourceCount = sourceFields.size();
for (int i = 0; i < sourceCount; ++i) {
RelDataType sourceType = sourceFields.get(i).getType();
RelDataType targetType = targetFields.get(i).getType();
if (!SqlTypeUtil.canAssignFrom(targetType, sourceType)) {
SqlNode node = getNthExpr(query, i, sourceCount);
if (node instanceof SqlDynamicParam) {
continue;
}
String targetTypeString;
String sourceTypeString;
if (SqlTypeUtil.areCharacterSetsMismatched(sourceType, targetType)) {
sourceTypeString = sourceType.getFullTypeString();
targetTypeString = targetType.getFullTypeString();
} else {
sourceTypeString = sourceType.toString();
targetTypeString = targetType.toString();
}
throw newValidationError(node, RESOURCE.typeNotAssignable(targetFields.get(i).getName(), targetTypeString, sourceFields.get(i).getName(), sourceTypeString));
}
}
}
use of org.apache.calcite.sql.SqlDynamicParam in project flink-mirror by flink-ci.
the class SqlValidatorImpl method getParameterRowType.
public RelDataType getParameterRowType(SqlNode sqlQuery) {
// NOTE: We assume that bind variables occur in depth-first tree
// traversal in the same order that they occurred in the SQL text.
final List<RelDataType> types = new ArrayList<>();
// NOTE: but parameters on fetch/offset would be counted twice
// as they are counted in the SqlOrderBy call and the inner SqlSelect call
final Set<SqlNode> alreadyVisited = new HashSet<>();
sqlQuery.accept(new SqlShuttle() {
@Override
public SqlNode visit(SqlDynamicParam param) {
if (alreadyVisited.add(param)) {
RelDataType type = getValidatedNodeType(param);
types.add(type);
}
return param;
}
});
return typeFactory.createStructType(types, new AbstractList<String>() {
@Override
public String get(int index) {
return "?" + index;
}
@Override
public int size() {
return types.size();
}
});
}
use of org.apache.calcite.sql.SqlDynamicParam in project flink-mirror by flink-ci.
the class SqlValidatorImpl method checkTypeAssignment.
/**
* Checks the type assignment of an INSERT or UPDATE query.
*
* <p>Skip the virtual columns(can not insert into) type assignment check if the source fields
* count equals with the real target table fields count, see how #checkFieldCount was used.
*
* @param sourceScope Scope of query source which is used to infer node type
* @param table Target table
* @param sourceRowType Source row type
* @param targetRowType Target row type, it should either contain all the virtual columns (can
* not insert into) or exclude all the virtual columns
* @param query The query
*/
protected void checkTypeAssignment(SqlValidatorScope sourceScope, SqlValidatorTable table, RelDataType sourceRowType, RelDataType targetRowType, final SqlNode query) {
// NOTE jvs 23-Feb-2006: subclasses may allow for extra targets
// representing system-maintained columns, so stop after all sources
// matched
boolean isUpdateModifiableViewTable = false;
if (query instanceof SqlUpdate) {
final SqlNodeList targetColumnList = ((SqlUpdate) query).getTargetColumnList();
if (targetColumnList != null) {
final int targetColumnCnt = targetColumnList.size();
targetRowType = SqlTypeUtil.extractLastNFields(typeFactory, targetRowType, targetColumnCnt);
sourceRowType = SqlTypeUtil.extractLastNFields(typeFactory, sourceRowType, targetColumnCnt);
}
isUpdateModifiableViewTable = table.unwrap(ModifiableViewTable.class) != null;
}
if (SqlTypeUtil.equalAsStructSansNullability(typeFactory, sourceRowType, targetRowType, null)) {
// Returns early if source and target row type equals sans nullability.
return;
}
if (config.typeCoercionEnabled() && !isUpdateModifiableViewTable) {
// Try type coercion first if implicit type coercion is allowed.
boolean coerced = typeCoercion.querySourceCoercion(sourceScope, sourceRowType, targetRowType, query);
if (coerced) {
return;
}
}
// Fall back to default behavior: compare the type families.
List<RelDataTypeField> sourceFields = sourceRowType.getFieldList();
List<RelDataTypeField> targetFields = targetRowType.getFieldList();
final int sourceCount = sourceFields.size();
for (int i = 0; i < sourceCount; ++i) {
RelDataType sourceType = sourceFields.get(i).getType();
RelDataType targetType = targetFields.get(i).getType();
if (!SqlTypeUtil.canAssignFrom(targetType, sourceType)) {
SqlNode node = getNthExpr(query, i, sourceCount);
if (node instanceof SqlDynamicParam) {
continue;
}
String targetTypeString;
String sourceTypeString;
if (SqlTypeUtil.areCharacterSetsMismatched(sourceType, targetType)) {
sourceTypeString = sourceType.getFullTypeString();
targetTypeString = targetType.getFullTypeString();
} else {
sourceTypeString = sourceType.toString();
targetTypeString = targetType.toString();
}
throw newValidationError(node, RESOURCE.typeNotAssignable(targetFields.get(i).getName(), targetTypeString, sourceFields.get(i).getName(), sourceTypeString));
}
}
}
use of org.apache.calcite.sql.SqlDynamicParam in project flink-mirror by flink-ci.
the class SqlCastFunction method inferReturnType.
// ~ Methods ----------------------------------------------------------------
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
assert opBinding.getOperandCount() == 2;
RelDataType ret = opBinding.getOperandType(1);
RelDataType firstType = opBinding.getOperandType(0);
ret = opBinding.getTypeFactory().createTypeWithNullability(ret, firstType.isNullable());
if (opBinding instanceof SqlCallBinding) {
SqlCallBinding callBinding = (SqlCallBinding) opBinding;
SqlNode operand0 = callBinding.operand(0);
// to them using the type they are casted to.
if (((operand0 instanceof SqlLiteral) && (((SqlLiteral) operand0).getValue() == null)) || (operand0 instanceof SqlDynamicParam)) {
final SqlValidatorImpl validator = (SqlValidatorImpl) callBinding.getValidator();
validator.setValidatedNodeType(operand0, ret);
}
}
return ret;
}
use of org.apache.calcite.sql.SqlDynamicParam in project Mycat2 by MyCATApache.
the class SqlValidatorImpl method getParameterRowType.
public RelDataType getParameterRowType(SqlNode sqlQuery) {
// NOTE: We assume that bind variables occur in depth-first tree
// traversal in the same order that they occurred in the SQL text.
final List<RelDataType> types = new ArrayList<>();
// NOTE: but parameters on fetch/offset would be counted twice
// as they are counted in the SqlOrderBy call and the inner SqlSelect call
final Set<SqlNode> alreadyVisited = new HashSet<>();
sqlQuery.accept(new SqlShuttle() {
@Override
public SqlNode visit(SqlDynamicParam param) {
if (alreadyVisited.add(param)) {
RelDataType type = getValidatedNodeType(param);
types.add(type);
}
return param;
}
});
return typeFactory.createStructType(types, new AbstractList<String>() {
@Override
public String get(int index) {
return "?" + index;
}
@Override
public int size() {
return types.size();
}
});
}
Aggregations