Search in sources :

Example 6 with SqlString

use of org.apache.calcite.sql.util.SqlString in project calcite by apache.

the class JdbcTable method scan.

public Enumerable<Object[]> scan(DataContext root) {
    final JavaTypeFactory typeFactory = root.getTypeFactory();
    final SqlString sql = generateSql();
    return ResultSetEnumerable.of(jdbcSchema.getDataSource(), sql.getSql(), JdbcUtils.ObjectArrayRowBuilder.factory(fieldClasses(typeFactory)));
}
Also used : JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) SqlString(org.apache.calcite.sql.util.SqlString)

Example 7 with SqlString

use of org.apache.calcite.sql.util.SqlString in project calcite by apache.

the class SqlOperatorBaseTest method testLiteralBeyondLimit.

/**
 * Tests that CAST fails when given a value just outside the valid range for
 * that type. For example,
 *
 * <ul>
 * <li>CAST(-200 AS TINYINT) fails because the value is less than -128;
 * <li>CAST(1E-999 AS FLOAT) fails because the value underflows;
 * <li>CAST(123.4567891234567 AS FLOAT) fails because the value loses
 * precision.
 * </ul>
 */
@Test
public void testLiteralBeyondLimit() {
    tester.setFor(SqlStdOperatorTable.CAST);
    final List<RelDataType> types = SqlLimitsTest.getTypes(tester.getValidator().getTypeFactory());
    for (RelDataType type : types) {
        for (Object o : getValues((BasicSqlType) type, false)) {
            SqlLiteral literal = type.getSqlTypeName().createLiteral(o, SqlParserPos.ZERO);
            SqlString literalString = literal.toSqlString(AnsiSqlDialect.DEFAULT);
            if ((type.getSqlTypeName() == SqlTypeName.BIGINT) || ((type.getSqlTypeName() == SqlTypeName.DECIMAL) && (type.getPrecision() == 19))) {
                // Values which are too large to be literals fail at
                // validate time.
                tester.checkFails("CAST(^" + literalString + "^ AS " + type + ")", "Numeric literal '.*' out of range", false);
            } else if ((type.getSqlTypeName() == SqlTypeName.CHAR) || (type.getSqlTypeName() == SqlTypeName.VARCHAR) || (type.getSqlTypeName() == SqlTypeName.BINARY) || (type.getSqlTypeName() == SqlTypeName.VARBINARY)) {
            // Casting overlarge string/binary values do not fail -
            // they are truncated. See testCastTruncates().
            } else {
                // Value outside legal bound should fail at runtime (not
                // validate time).
                // 
                // NOTE: Because Java and Fennel calcs give
                // different errors, the pattern hedges its bets.
                tester.checkFails("CAST(" + literalString + " AS " + type + ")", "(?s).*(Overflow during calculation or cast\\.|Code=22003).*", true);
            }
        }
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlString(org.apache.calcite.sql.util.SqlString) SqlLimitsTest(org.apache.calcite.test.SqlLimitsTest) Test(org.junit.Test)

Aggregations

SqlString (org.apache.calcite.sql.util.SqlString)7 Test (org.junit.Test)3 RelDataType (org.apache.calcite.rel.type.RelDataType)2 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)2 SqlLimitsTest (org.apache.calcite.test.SqlLimitsTest)2 CreateViewPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateViewPlan)1 Timestamp (java.sql.Timestamp)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 SqlDialect (org.apache.calcite.sql.SqlDialect)1 AnsiSqlDialect (org.apache.calcite.sql.dialect.AnsiSqlDialect)1 SqlBuilder (org.apache.calcite.sql.util.SqlBuilder)1 TimestampString (org.apache.calcite.util.TimestampString)1