Search in sources :

Example 86 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 validateMerge.

public void validateMerge(SqlMerge call) {
    SqlSelect sqlSelect = call.getSourceSelect();
    // REVIEW zfong 5/25/06 - Does an actual type have to be passed into
    // validateSelect()?
    // REVIEW jvs 6-June-2006:  In general, passing unknownType like
    // this means we won't be able to correctly infer the types
    // for dynamic parameter markers (SET x = ?).  But
    // maybe validateUpdate and validateInsert below will do
    // the job?
    // REVIEW ksecretan 15-July-2011: They didn't get a chance to
    // since validateSelect() would bail.
    // Let's use the update/insert targetRowType when available.
    IdentifierNamespace targetNamespace = (IdentifierNamespace) getNamespace(call.getTargetTable());
    validateNamespace(targetNamespace, unknownType);
    SqlValidatorTable table = targetNamespace.getTable();
    validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE);
    RelDataType targetRowType = unknownType;
    if (call.getUpdateCall() != null) {
        targetRowType = createTargetRowType(table, call.getUpdateCall().getTargetColumnList(), true);
    }
    if (call.getInsertCall() != null) {
        targetRowType = createTargetRowType(table, call.getInsertCall().getTargetColumnList(), false);
    }
    validateSelect(sqlSelect, targetRowType);
    if (call.getUpdateCall() != null) {
        validateUpdate(call.getUpdateCall());
    }
    if (call.getInsertCall() != null) {
        validateInsert(call.getInsertCall());
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 87 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 validate.

public SqlNode validate(SqlNode topNode) {
    SqlValidatorScope scope = new EmptyScope(this);
    scope = new CatalogScope(scope, ImmutableList.of("CATALOG"));
    final SqlNode topNode2 = validateScopedExpression(topNode, scope);
    final RelDataType type = getValidatedNodeType(topNode2);
    Util.discard(type);
    return topNode2;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 88 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 createTargetRowType.

/**
 * Derives a row-type for INSERT and UPDATE operations.
 *
 * @param table            Target table for INSERT/UPDATE
 * @param targetColumnList List of target columns, or null if not specified
 * @param append           Whether to append fields to those in <code>
 *                         baseRowType</code>
 * @return Rowtype
 */
protected RelDataType createTargetRowType(SqlValidatorTable table, SqlNodeList targetColumnList, boolean append) {
    RelDataType baseRowType = table.getRowType();
    if (targetColumnList == null) {
        return baseRowType;
    }
    List<RelDataTypeField> targetFields = baseRowType.getFieldList();
    final List<Map.Entry<String, RelDataType>> types = new ArrayList<>();
    if (append) {
        for (RelDataTypeField targetField : targetFields) {
            types.add(Pair.of(SqlUtil.deriveAliasFromOrdinal(types.size()), targetField.getType()));
        }
    }
    final Set<Integer> assignedFields = new HashSet<>();
    final RelOptTable relOptTable = table instanceof RelOptTable ? ((RelOptTable) table) : null;
    for (SqlNode node : targetColumnList) {
        SqlIdentifier id = (SqlIdentifier) node;
        RelDataTypeField targetField = SqlValidatorUtil.getTargetField(baseRowType, typeFactory, id, catalogReader, relOptTable);
        if (targetField == null) {
            throw newValidationError(id, RESOURCE.unknownTargetColumn(id.toString()));
        }
        if (!assignedFields.add(targetField.getIndex())) {
            throw newValidationError(id, RESOURCE.duplicateTargetColumn(targetField.getName()));
        }
        types.add(targetField);
    }
    return typeFactory.createStructType(types);
}
Also used : BigInteger(java.math.BigInteger) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 89 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 deriveConstructorType.

public RelDataType deriveConstructorType(SqlValidatorScope scope, SqlCall call, SqlFunction unresolvedConstructor, SqlFunction resolvedConstructor, List<RelDataType> argTypes) {
    SqlIdentifier sqlIdentifier = unresolvedConstructor.getSqlIdentifier();
    assert sqlIdentifier != null;
    RelDataType type = catalogReader.getNamedType(sqlIdentifier);
    if (type == null) {
        // TODO jvs 12-Feb-2005:  proper type name formatting
        throw newValidationError(sqlIdentifier, RESOURCE.unknownDatatypeName(sqlIdentifier.toString()));
    }
    if (resolvedConstructor == null) {
        if (call.operandCount() > 0) {
            // no user-defined constructor could be found
            throw handleUnresolvedFunction(call, unresolvedConstructor, argTypes, null);
        }
    } else {
        SqlCall testCall = resolvedConstructor.createCall(call.getParserPosition(), call.getOperandList());
        RelDataType returnType = resolvedConstructor.validateOperands(this, scope, testCall);
        assert type == returnType;
    }
    if (shouldExpandIdentifiers()) {
        if (resolvedConstructor != null) {
            ((SqlBasicCall) call).setOperator(resolvedConstructor);
        } else {
            // fake a fully-qualified call to the default constructor
            ((SqlBasicCall) call).setOperator(new SqlFunction(type.getSqlIdentifier(), ReturnTypes.explicit(type), null, null, null, SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR));
        }
    }
    return type;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 90 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 validateDefinitions.

private void validateDefinitions(SqlMatchRecognize mr, MatchRecognizeScope scope) {
    final Set<String> aliases = catalogReader.nameMatcher().isCaseSensitive() ? new LinkedHashSet<String>() : new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    for (SqlNode item : mr.getPatternDefList().getList()) {
        final String alias = alias(item);
        if (!aliases.add(alias)) {
            throw newValidationError(item, Static.RESOURCE.patternVarAlreadyDefined(alias));
        }
        scope.addPatternVar(alias);
    }
    final List<SqlNode> sqlNodes = new ArrayList<>();
    for (SqlNode item : mr.getPatternDefList().getList()) {
        final String alias = alias(item);
        SqlNode expand = expand(item, scope);
        expand = navigationInDefine(expand, alias);
        setOriginal(expand, item);
        inferUnknownTypes(booleanType, scope, expand);
        expand.validate(this, scope);
        // Some extra work need required here.
        // In PREV, NEXT, FINAL and LAST, only one pattern variable is allowed.
        sqlNodes.add(SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO, expand, new SqlIdentifier(alias, SqlParserPos.ZERO)));
        final RelDataType type = deriveType(scope, expand);
        if (!SqlTypeUtil.inBooleanFamily(type)) {
            throw newValidationError(expand, RESOURCE.condMustBeBoolean("DEFINE"));
        }
        setValidatedNodeType(item, type);
    }
    SqlNodeList list = new SqlNodeList(sqlNodes, mr.getPatternDefList().getParserPosition());
    inferUnknownTypes(unknownType, scope, list);
    for (SqlNode node : list) {
        validateExpr(node, scope);
    }
    mr.setOperand(SqlMatchRecognize.OPERAND_PATTERN_DEFINES, list);
}
Also used : ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) 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)

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