use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlValidatorImpl method validateInsert.
public void validateInsert(SqlInsert insert) {
final SqlValidatorNamespace targetNamespace = getNamespace(insert);
validateNamespace(targetNamespace, unknownType);
final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable(targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null);
final SqlValidatorTable table = relOptTable == null ? targetNamespace.getTable() : relOptTable.unwrap(SqlValidatorTable.class);
// INSERT has an optional column name list. If present then
// reduce the rowtype to the columns specified. If not present
// then the entire target rowtype is used.
final RelDataType targetRowType = createTargetRowType(table, insert.getTargetColumnList(), false);
final SqlNode source = insert.getSource();
if (source instanceof SqlSelect) {
final SqlSelect sqlSelect = (SqlSelect) source;
validateSelect(sqlSelect, targetRowType);
} else {
final SqlValidatorScope scope = scopes.get(source);
validateQuery(source, scope, targetRowType);
}
// REVIEW jvs 4-Dec-2008: In FRG-365, this namespace row type is
// discarding the type inferred by inferUnknownTypes (which was invoked
// from validateSelect above). It would be better if that information
// were used here so that we never saw any untyped nulls during
// checkTypeAssignment.
final RelDataType sourceRowType = getNamespace(source).getRowType();
final RelDataType logicalTargetRowType = getLogicalTargetRowType(targetRowType, insert);
setValidatedNodeType(insert, logicalTargetRowType);
final RelDataType logicalSourceRowType = getLogicalSourceRowType(sourceRowType, insert);
checkFieldCount(insert.getTargetTable(), table, source, logicalSourceRowType, logicalTargetRowType);
checkTypeAssignment(logicalSourceRowType, logicalTargetRowType, insert);
checkConstraint(table, source, logicalTargetRowType);
validateAccess(insert.getTargetTable(), table, SqlAccessEnum.INSERT);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlValidatorImpl method expandSelectItem.
/**
* If <code>selectItem</code> is "*" or "TABLE.*", expands it and returns
* true; otherwise writes the unexpanded item.
*
* @param selectItem Select-list item
* @param select Containing select clause
* @param selectItems List that expanded items are written to
* @param aliases Set of aliases
* @param types List of data types in alias order
* @param includeSystemVars If true include system vars in lists
* @return Whether the node was expanded
*/
private boolean expandSelectItem(final SqlNode selectItem, SqlSelect select, RelDataType targetType, List<SqlNode> selectItems, Set<String> aliases, List<Map.Entry<String, RelDataType>> types, final boolean includeSystemVars) {
final SelectScope scope = (SelectScope) getWhereScope(select);
if (expandStar(selectItems, aliases, types, includeSystemVars, scope, selectItem)) {
return true;
}
// Expand the select item: fully-qualify columns, and convert
// parentheses-free functions such as LOCALTIME into explicit function
// calls.
SqlNode expanded = expand(selectItem, scope);
final String alias = deriveAlias(selectItem, aliases.size());
// If expansion has altered the natural alias, supply an explicit 'AS'.
final SqlValidatorScope selectScope = getSelectScope(select);
if (expanded != selectItem) {
String newAlias = deriveAlias(expanded, aliases.size());
if (!newAlias.equals(alias)) {
expanded = SqlStdOperatorTable.AS.createCall(selectItem.getParserPosition(), expanded, new SqlIdentifier(alias, SqlParserPos.ZERO));
deriveTypeImpl(selectScope, expanded);
}
}
selectItems.add(expanded);
aliases.add(alias);
inferUnknownTypes(targetType, scope, expanded);
final RelDataType type = deriveType(selectScope, expanded);
setValidatedNodeType(expanded, type);
types.add(Pair.of(alias, type));
return false;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlValidatorImpl method getTableConstructorRowType.
/**
* Returns null if there is no common type. E.g. if the rows have a
* different number of columns.
*/
RelDataType getTableConstructorRowType(SqlCall values, SqlValidatorScope scope) {
final List<SqlNode> rows = values.getOperandList();
assert rows.size() >= 1;
final List<RelDataType> rowTypes = new ArrayList<>();
for (final SqlNode row : rows) {
assert row.getKind() == SqlKind.ROW;
SqlCall rowConstructor = (SqlCall) row;
// REVIEW jvs 10-Sept-2003: Once we support single-row queries as
// rows, need to infer aliases from there.
final List<String> aliasList = new ArrayList<>();
final List<RelDataType> typeList = new ArrayList<>();
for (Ord<SqlNode> column : Ord.zip(rowConstructor.getOperandList())) {
final String alias = deriveAlias(column.e, column.i);
aliasList.add(alias);
final RelDataType type = deriveType(scope, column.e);
typeList.add(type);
}
rowTypes.add(typeFactory.createStructType(typeList, aliasList));
}
if (rows.size() == 1) {
// leastRestrictive can handle all cases
return rowTypes.get(0);
}
return typeFactory.leastRestrictive(rowTypes);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlValidatorImpl method addOrExpandField.
private boolean addOrExpandField(List<SqlNode> selectItems, Set<String> aliases, List<Map.Entry<String, RelDataType>> types, boolean includeSystemVars, SelectScope scope, SqlIdentifier id, RelDataTypeField field) {
switch(field.getType().getStructKind()) {
case PEEK_FIELDS:
case PEEK_FIELDS_DEFAULT:
final SqlNode starExp = id.plusStar();
expandStar(selectItems, aliases, types, includeSystemVars, scope, starExp);
return true;
default:
addToSelectList(selectItems, aliases, types, id, scope, includeSystemVars);
}
return false;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlValidatorImpl method checkTypeAssignment.
protected void checkTypeAssignment(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
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)) {
// FRG-255: account for UPDATE rewrite; there's
// probably a better way to do this.
int iAdjusted = i;
if (query instanceof SqlUpdate) {
int nUpdateColumns = ((SqlUpdate) query).getTargetColumnList().size();
assert sourceFields.size() >= nUpdateColumns;
iAdjusted -= sourceFields.size() - nUpdateColumns;
}
SqlNode node = getNthExpr(query, iAdjusted, sourceCount);
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));
}
}
}
Aggregations