Search in sources :

Example 96 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class TableApiIdentifierParsingTest method testTableApiIdentifierParsing.

@Test
public void testTableApiIdentifierParsing() throws ParseException {
    FlinkSqlParserImpl parser = createFlinkParser(stringIdentifier);
    SqlIdentifier sqlIdentifier = parser.TableApiIdentifier();
    assertThat(sqlIdentifier.names, equalTo(expectedParsedIdentifier));
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) FlinkSqlParserImpl(org.apache.flink.sql.parser.impl.FlinkSqlParserImpl) Test(org.junit.Test)

Example 97 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class SqlValidatorImpl method checkConstraint.

/**
 * Validates updates against the constraint of a modifiable view.
 *
 * @param validatorTable A {@link SqlValidatorTable} that may wrap a ModifiableViewTable
 * @param update The UPDATE parse tree node
 * @param targetRowType The target type
 */
private void checkConstraint(SqlValidatorTable validatorTable, SqlUpdate update, RelDataType targetRowType) {
    final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
    if (modifiableViewTable != null) {
        final Table table = modifiableViewTable.unwrap(Table.class);
        final RelDataType tableRowType = table.getRowType(typeFactory);
        final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
        final Map<String, Integer> nameToIndex = SqlValidatorUtil.mapNameToIndex(tableRowType.getFieldList());
        // Validate update values against the view constraint.
        final List<SqlNode> targets = update.getTargetColumnList().getList();
        final List<SqlNode> sources = update.getSourceExpressionList().getList();
        for (final Pair<SqlNode, SqlNode> column : Pair.zip(targets, sources)) {
            final String columnName = ((SqlIdentifier) column.left).getSimple();
            final Integer columnIndex = nameToIndex.get(columnName);
            if (projectMap.containsKey(columnIndex)) {
                final RexNode columnConstraint = projectMap.get(columnIndex);
                final ValidationError validationError = new ValidationError(column.right, RESOURCE.viewConstraintNotSatisfied(columnName, Util.last(validatorTable.getQualifiedName())));
                RelOptUtil.validateValueAgainstConstraint(column.right, columnConstraint, validationError);
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) BigInteger(java.math.BigInteger) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 98 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink 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>> fields = new ArrayList<>();
    if (append) {
        for (RelDataTypeField targetField : targetFields) {
            fields.add(Pair.of(SqlUtil.deriveAliasFromOrdinal(fields.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()));
        }
        fields.add(targetField);
    }
    return typeFactory.createStructType(fields);
}
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) HashSet(java.util.HashSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 99 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class AliasNamespace method validateImpl.

// ~ Methods ----------------------------------------------------------------
protected RelDataType validateImpl(RelDataType targetRowType) {
    final List<String> nameList = new ArrayList<>();
    final List<SqlNode> operands = call.getOperandList();
    final SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
    final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
    final List<SqlNode> columnNames = Util.skip(operands, 2);
    for (final SqlNode operand : columnNames) {
        String name = ((SqlIdentifier) operand).getSimple();
        if (nameList.contains(name)) {
            throw validator.newValidationError(operand, RESOURCE.aliasListDuplicate(name));
        }
        nameList.add(name);
    }
    if (nameList.size() != rowType.getFieldCount()) {
        // Position error over all column names
        final SqlNode node = operands.size() == 3 ? operands.get(2) : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
        throw validator.newValidationError(node, RESOURCE.aliasListDegree(rowType.getFieldCount(), getString(rowType), nameList.size()));
    }
    final List<RelDataType> typeList = new ArrayList<>();
    for (RelDataTypeField field : rowType.getFieldList()) {
        typeList.add(field.getType());
    }
    RelDataType aliasedType = validator.getTypeFactory().createStructType(rowType.getStructKind(), typeList, nameList);
    return validator.getTypeFactory().createTypeWithNullability(aliasedType, rowType.isNullable());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 100 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class Expander method expanded.

/**
 * Expands identifiers in a given SQL string, returning a {@link Expanded}.
 */
public Expanded expanded(String ori) {
    final Map<SqlParserPos, SqlIdentifier> identifiers = new HashMap<>();
    final Map<String, SqlIdentifier> funcNameToId = new HashMap<>();
    final SqlNode oriNode = planner.parser().parse(ori);
    // parse again because validation is stateful, that means the node tree was probably
    // mutated.
    final SqlNode validated = planner.validate(planner.parser().parse(ori));
    validated.accept(new SqlBasicVisitor<Void>() {

        @Override
        public Void visit(SqlCall call) {
            SqlOperator operator = call.getOperator();
            if (operator instanceof BridgingSqlFunction) {
                final SqlIdentifier functionID = ((BridgingSqlFunction) operator).getSqlIdentifier();
                if (!functionID.isSimple()) {
                    funcNameToId.put(Util.last(functionID.names), functionID);
                }
            }
            return super.visit(call);
        }

        @Override
        public Void visit(SqlIdentifier identifier) {
            // and we stop expanding all of them.
            if (!identifier.names.get(0).startsWith("EXPR$")) {
                identifiers.putIfAbsent(identifier.getParserPosition(), identifier);
            }
            return null;
        }
    });
    return new Expanded(oriNode, identifiers, funcNameToId);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) HashMap(java.util.HashMap) SqlCall(org.apache.calcite.sql.SqlCall) SqlOperator(org.apache.calcite.sql.SqlOperator) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)131 SqlNode (org.apache.calcite.sql.SqlNode)84 RelDataType (org.apache.calcite.rel.type.RelDataType)46 SqlNodeList (org.apache.calcite.sql.SqlNodeList)43 ArrayList (java.util.ArrayList)41 SqlCall (org.apache.calcite.sql.SqlCall)32 BitString (org.apache.calcite.util.BitString)28 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)21 SqlSelect (org.apache.calcite.sql.SqlSelect)21 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)12 SchemaPlus (org.apache.calcite.schema.SchemaPlus)11 SqlOperator (org.apache.calcite.sql.SqlOperator)11 NlsString (org.apache.calcite.util.NlsString)11 List (java.util.List)9 Map (java.util.Map)9 RelOptTable (org.apache.calcite.plan.RelOptTable)9 RexNode (org.apache.calcite.rex.RexNode)9 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)9 ImmutableList (com.google.common.collect.ImmutableList)8 RelNode (org.apache.calcite.rel.RelNode)7