Search in sources :

Example 1 with RelDataTypeSystem

use of org.apache.calcite.rel.type.RelDataTypeSystem in project calcite by apache.

the class SqlValidatorImpl method validateIntervalQualifier.

public void validateIntervalQualifier(SqlIntervalQualifier qualifier) {
    assert qualifier != null;
    boolean startPrecisionOutOfRange = false;
    boolean fractionalSecondPrecisionOutOfRange = false;
    final RelDataTypeSystem typeSystem = typeFactory.getTypeSystem();
    final int startPrecision = qualifier.getStartPrecision(typeSystem);
    final int fracPrecision = qualifier.getFractionalSecondPrecision(typeSystem);
    final int maxPrecision = typeSystem.getMaxPrecision(qualifier.typeName());
    final int minPrecision = qualifier.typeName().getMinPrecision();
    final int minScale = qualifier.typeName().getMinScale();
    final int maxScale = typeSystem.getMaxScale(qualifier.typeName());
    if (qualifier.isYearMonth()) {
        if (startPrecision < minPrecision || startPrecision > maxPrecision) {
            startPrecisionOutOfRange = true;
        } else {
            if (fracPrecision < minScale || fracPrecision > maxScale) {
                fractionalSecondPrecisionOutOfRange = true;
            }
        }
    } else {
        if (startPrecision < minPrecision || startPrecision > maxPrecision) {
            startPrecisionOutOfRange = true;
        } else {
            if (fracPrecision < minScale || fracPrecision > maxScale) {
                fractionalSecondPrecisionOutOfRange = true;
            }
        }
    }
    if (startPrecisionOutOfRange) {
        throw newValidationError(qualifier, RESOURCE.intervalStartPrecisionOutOfRange(startPrecision, "INTERVAL " + qualifier));
    } else if (fractionalSecondPrecisionOutOfRange) {
        throw newValidationError(qualifier, RESOURCE.intervalFractionalSecondPrecisionOutOfRange(fracPrecision, "INTERVAL " + qualifier));
    }
}
Also used : RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem)

Example 2 with RelDataTypeSystem

use of org.apache.calcite.rel.type.RelDataTypeSystem in project calcite by apache.

the class RelOptUtil method projectJoinInputs.

// to be removed before 2.0
@Deprecated
public static void projectJoinInputs(RelNode[] inputRels, List<RexNode> leftJoinKeys, List<RexNode> rightJoinKeys, int systemColCount, List<Integer> leftKeys, List<Integer> rightKeys, List<Integer> outputProj) {
    RelNode leftRel = inputRels[0];
    RelNode rightRel = inputRels[1];
    final RelOptCluster cluster = leftRel.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RelDataTypeSystem typeSystem = cluster.getTypeFactory().getTypeSystem();
    int origLeftInputSize = leftRel.getRowType().getFieldCount();
    int origRightInputSize = rightRel.getRowType().getFieldCount();
    final List<RexNode> newLeftFields = new ArrayList<>();
    final List<String> newLeftFieldNames = new ArrayList<>();
    final List<RexNode> newRightFields = new ArrayList<>();
    final List<String> newRightFieldNames = new ArrayList<>();
    int leftKeyCount = leftJoinKeys.size();
    int rightKeyCount = rightJoinKeys.size();
    int i;
    for (i = 0; i < systemColCount; i++) {
        outputProj.add(i);
    }
    for (i = 0; i < origLeftInputSize; i++) {
        final RelDataTypeField field = leftRel.getRowType().getFieldList().get(i);
        newLeftFields.add(rexBuilder.makeInputRef(field.getType(), i));
        newLeftFieldNames.add(field.getName());
        outputProj.add(systemColCount + i);
    }
    int newLeftKeyCount = 0;
    for (i = 0; i < leftKeyCount; i++) {
        RexNode leftKey = leftJoinKeys.get(i);
        if (leftKey instanceof RexInputRef) {
            // already added to the projected left fields
            // only need to remember the index in the join key list
            leftKeys.add(((RexInputRef) leftKey).getIndex());
        } else {
            newLeftFields.add(leftKey);
            newLeftFieldNames.add(null);
            leftKeys.add(origLeftInputSize + newLeftKeyCount);
            newLeftKeyCount++;
        }
    }
    int leftFieldCount = origLeftInputSize + newLeftKeyCount;
    for (i = 0; i < origRightInputSize; i++) {
        final RelDataTypeField field = rightRel.getRowType().getFieldList().get(i);
        newRightFields.add(rexBuilder.makeInputRef(field.getType(), i));
        newRightFieldNames.add(field.getName());
        outputProj.add(systemColCount + leftFieldCount + i);
    }
    int newRightKeyCount = 0;
    for (i = 0; i < rightKeyCount; i++) {
        RexNode rightKey = rightJoinKeys.get(i);
        if (rightKey instanceof RexInputRef) {
            // already added to the projected left fields
            // only need to remember the index in the join key list
            rightKeys.add(((RexInputRef) rightKey).getIndex());
        } else {
            newRightFields.add(rightKey);
            newRightFieldNames.add(null);
            rightKeys.add(origRightInputSize + newRightKeyCount);
            newRightKeyCount++;
        }
    }
    final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
    // fields
    if (newLeftKeyCount > 0) {
        leftRel = relBuilder.push(leftRel).project(newLeftFields, newLeftFieldNames, true).build();
    }
    if (newRightKeyCount > 0) {
        rightRel = relBuilder.push(rightRel).project(newRightFields, newRightFieldNames).build();
    }
    inputRels[0] = leftRel;
    inputRels[1] = rightRel;
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) ArrayList(java.util.ArrayList) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with RelDataTypeSystem

use of org.apache.calcite.rel.type.RelDataTypeSystem in project flink by apache.

the class SqlValidatorImpl method validateIntervalQualifier.

public void validateIntervalQualifier(SqlIntervalQualifier qualifier) {
    assert qualifier != null;
    boolean startPrecisionOutOfRange = false;
    boolean fractionalSecondPrecisionOutOfRange = false;
    final RelDataTypeSystem typeSystem = typeFactory.getTypeSystem();
    final int startPrecision = qualifier.getStartPrecision(typeSystem);
    final int fracPrecision = qualifier.getFractionalSecondPrecision(typeSystem);
    final int maxPrecision = typeSystem.getMaxPrecision(qualifier.typeName());
    final int minPrecision = qualifier.typeName().getMinPrecision();
    final int minScale = qualifier.typeName().getMinScale();
    final int maxScale = typeSystem.getMaxScale(qualifier.typeName());
    if (startPrecision < minPrecision || startPrecision > maxPrecision) {
        startPrecisionOutOfRange = true;
    } else {
        if (fracPrecision < minScale || fracPrecision > maxScale) {
            fractionalSecondPrecisionOutOfRange = true;
        }
    }
    if (startPrecisionOutOfRange) {
        throw newValidationError(qualifier, RESOURCE.intervalStartPrecisionOutOfRange(startPrecision, "INTERVAL " + qualifier));
    } else if (fractionalSecondPrecisionOutOfRange) {
        throw newValidationError(qualifier, RESOURCE.intervalFractionalSecondPrecisionOutOfRange(fracPrecision, "INTERVAL " + qualifier));
    }
}
Also used : RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem)

Example 4 with RelDataTypeSystem

use of org.apache.calcite.rel.type.RelDataTypeSystem in project calcite by apache.

the class CalciteMetaImpl method getAllDefaultType.

private ImmutableList<MetaTypeInfo> getAllDefaultType() {
    final ImmutableList.Builder<MetaTypeInfo> allTypeList = ImmutableList.builder();
    final CalciteConnectionImpl conn = (CalciteConnectionImpl) connection;
    final RelDataTypeSystem typeSystem = conn.typeFactory.getTypeSystem();
    for (SqlTypeName sqlTypeName : SqlTypeName.values()) {
        allTypeList.add(new MetaTypeInfo(sqlTypeName.getName(), sqlTypeName.getJdbcOrdinal(), typeSystem.getMaxPrecision(sqlTypeName), typeSystem.getLiteral(sqlTypeName, true), typeSystem.getLiteral(sqlTypeName, false), // All types are nullable
        (short) DatabaseMetaData.typeNullable, typeSystem.isCaseSensitive(sqlTypeName), // be specific and declare under SqlTypeName
        (short) DatabaseMetaData.typeSearchable, false, false, typeSystem.isAutoincrement(sqlTypeName), (short) sqlTypeName.getMinScale(), (short) typeSystem.getMaxScale(sqlTypeName), typeSystem.getNumTypeRadix(sqlTypeName)));
    }
    return allTypeList.build();
}
Also used : RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ImmutableList(com.google.common.collect.ImmutableList)

Example 5 with RelDataTypeSystem

use of org.apache.calcite.rel.type.RelDataTypeSystem in project calcite by apache.

the class SqlValidatorTest method testIntervalLiterals.

@Test
public void testIntervalLiterals() {
    // First check that min, max, and defaults are what we expect
    // (values used in subtests depend on these being true to
    // accurately test bounds)
    final RelDataTypeSystem typeSystem = getTester().getValidator().getTypeFactory().getTypeSystem();
    final RelDataTypeSystem defTypeSystem = RelDataTypeSystem.DEFAULT;
    for (SqlTypeName typeName : SqlTypeName.INTERVAL_TYPES) {
        assertThat(typeName.getMinPrecision(), is(1));
        assertThat(typeSystem.getMaxPrecision(typeName), is(10));
        assertThat(typeSystem.getDefaultPrecision(typeName), is(2));
        assertThat(typeName.getMinScale(), is(1));
        assertThat(typeSystem.getMaxScale(typeName), is(9));
        assertThat(typeName.getDefaultScale(), is(6));
    }
    // Tests that should pass both parser and validator
    subTestIntervalYearPositive();
    subTestIntervalYearToMonthPositive();
    subTestIntervalMonthPositive();
    subTestIntervalDayPositive();
    subTestIntervalDayToHourPositive();
    subTestIntervalDayToMinutePositive();
    subTestIntervalDayToSecondPositive();
    subTestIntervalHourPositive();
    subTestIntervalHourToMinutePositive();
    subTestIntervalHourToSecondPositive();
    subTestIntervalMinutePositive();
    subTestIntervalMinuteToSecondPositive();
    subTestIntervalSecondPositive();
    // Tests that should pass parser but fail validator
    subTestIntervalYearNegative();
    subTestIntervalYearToMonthNegative();
    subTestIntervalMonthNegative();
    subTestIntervalDayNegative();
    subTestIntervalDayToHourNegative();
    subTestIntervalDayToMinuteNegative();
    subTestIntervalDayToSecondNegative();
    subTestIntervalHourNegative();
    subTestIntervalHourToMinuteNegative();
    subTestIntervalHourToSecondNegative();
    subTestIntervalMinuteNegative();
    subTestIntervalMinuteToSecondNegative();
    subTestIntervalSecondNegative();
    // Miscellaneous
    // fractional value is not OK, even if it is 0
    checkWholeExpFails("INTERVAL '1.0' HOUR", "Illegal interval literal format '1.0' for INTERVAL HOUR");
    // only seconds are allowed to have a fractional part
    checkExpType("INTERVAL '1.0' SECOND", "INTERVAL SECOND NOT NULL");
    // leading zeroes do not cause precision to be exceeded
    checkExpType("INTERVAL '0999' MONTH(3)", "INTERVAL MONTH(3) NOT NULL");
}
Also used : RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Aggregations

RelDataTypeSystem (org.apache.calcite.rel.type.RelDataTypeSystem)5 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 RelNode (org.apache.calcite.rel.RelNode)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexInputRef (org.apache.calcite.rex.RexInputRef)1 RexNode (org.apache.calcite.rex.RexNode)1 RelBuilder (org.apache.calcite.tools.RelBuilder)1 Test (org.junit.Test)1