Search in sources :

Example 86 with SqlIdentifier

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

the class RichSqlHiveInsert method unparse.

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.startList(SqlWriter.FrameTypeEnum.SELECT);
    String insertKeyword = "INSERT INTO";
    if (isUpsert()) {
        insertKeyword = "UPSERT INTO";
    } else if (isOverwrite()) {
        insertKeyword = "INSERT OVERWRITE";
    }
    writer.sep(insertKeyword);
    final int opLeft = getOperator().getLeftPrec();
    final int opRight = getOperator().getRightPrec();
    getTargetTable().unparse(writer, opLeft, opRight);
    if (getTargetColumnList() != null) {
        getTargetColumnList().unparse(writer, opLeft, opRight);
    }
    writer.newlineAndIndent();
    if (allPartKeys != null && allPartKeys.size() > 0) {
        writer.keyword("PARTITION");
        SqlWriter.Frame frame = writer.startList("(", ")");
        for (SqlNode node : allPartKeys) {
            writer.sep(",", false);
            SqlIdentifier partKey = (SqlIdentifier) node;
            SqlProperty spec = partKeyToSpec.get(partKey);
            if (spec != null) {
                spec.unparse(writer, leftPrec, rightPrec);
            } else {
                partKey.unparse(writer, leftPrec, rightPrec);
            }
        }
        writer.endList(frame);
        writer.newlineAndIndent();
    }
    getSource().unparse(writer, 0, 0);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlProperty(org.apache.flink.sql.parser.SqlProperty) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 87 with SqlIdentifier

use of 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 88 with SqlIdentifier

use of 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 89 with SqlIdentifier

use of 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 90 with SqlIdentifier

use of 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)

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