Search in sources :

Example 81 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class SqlValidatorImpl method validateHavingClause.

protected void validateHavingClause(SqlSelect select) {
    // HAVING is validated in the scope after groups have been created.
    // For example, in "SELECT empno FROM emp WHERE empno = 10 GROUP BY
    // deptno HAVING empno = 10", the reference to 'empno' in the HAVING
    // clause is illegal.
    SqlNode having = select.getHaving();
    if (having == null) {
        return;
    }
    final AggregatingScope havingScope = (AggregatingScope) getSelectScope(select);
    if (getConformance().isHavingAlias()) {
        SqlNode newExpr = expandGroupByOrHavingExpr(having, havingScope, select, true);
        if (having != newExpr) {
            having = newExpr;
            select.setHaving(newExpr);
        }
    }
    havingScope.checkAggregateExpr(having, true);
    inferUnknownTypes(booleanType, havingScope, having);
    having.validate(this, havingScope);
    final RelDataType type = deriveType(havingScope, having);
    if (!SqlTypeUtil.inBooleanFamily(type)) {
        throw newValidationError(having, RESOURCE.havingMustBeBoolean());
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 82 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType 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 83 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType 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 84 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType 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 85 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType 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

RelDataType (org.apache.calcite.rel.type.RelDataType)834 RexNode (org.apache.calcite.rex.RexNode)268 ArrayList (java.util.ArrayList)214 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)209 RelNode (org.apache.calcite.rel.RelNode)153 SqlNode (org.apache.calcite.sql.SqlNode)143 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)123 RexBuilder (org.apache.calcite.rex.RexBuilder)118 Test (org.junit.Test)62 ImmutableList (com.google.common.collect.ImmutableList)58 RexInputRef (org.apache.calcite.rex.RexInputRef)57 List (java.util.List)51 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)45 RexLiteral (org.apache.calcite.rex.RexLiteral)44 SqlNodeList (org.apache.calcite.sql.SqlNodeList)42 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)40 AggregateCall (org.apache.calcite.rel.core.AggregateCall)39 BitString (org.apache.calcite.util.BitString)38 BigDecimal (java.math.BigDecimal)35 RelBuilder (org.apache.calcite.tools.RelBuilder)34