Search in sources :

Example 41 with SqlNode

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);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 42 with SqlNode

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;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 43 with SqlNode

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);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlNode(org.apache.calcite.sql.SqlNode)

Example 44 with SqlNode

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;
}
Also used : SqlNode(org.apache.calcite.sql.SqlNode)

Example 45 with SqlNode

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));
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlUpdate(org.apache.calcite.sql.SqlUpdate) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)510 RelDataType (org.apache.calcite.rel.type.RelDataType)141 SqlNodeList (org.apache.calcite.sql.SqlNodeList)98 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)84 SqlCall (org.apache.calcite.sql.SqlCall)81 ArrayList (java.util.ArrayList)78 RelNode (org.apache.calcite.rel.RelNode)60 Test (org.junit.Test)59 BitString (org.apache.calcite.util.BitString)46 SqlSelect (org.apache.calcite.sql.SqlSelect)45 SqlWriter (org.apache.calcite.sql.SqlWriter)42 RexNode (org.apache.calcite.rex.RexNode)40 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)33 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)25 SqlLiteral (org.apache.calcite.sql.SqlLiteral)23 SqlOperator (org.apache.calcite.sql.SqlOperator)23 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 ImmutableList (com.google.common.collect.ImmutableList)20 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)20