Search in sources :

Example 1 with SqlString

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

the class SqlOperatorBaseTest method testLiteralAtLimit.

/**
 * 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 testLiteralAtLimit() {
    tester.setFor(SqlStdOperatorTable.CAST);
    if (!enable) {
        return;
    }
    final List<RelDataType> types = SqlLimitsTest.getTypes(tester.getValidator().getTypeFactory());
    for (RelDataType type : types) {
        for (Object o : getValues((BasicSqlType) type, true)) {
            SqlLiteral literal = type.getSqlTypeName().createLiteral(o, SqlParserPos.ZERO);
            SqlString literalString = literal.toSqlString(AnsiSqlDialect.DEFAULT);
            final String expr = "CAST(" + literalString + " AS " + type + ")";
            try {
                tester.checkType(expr, type.getFullTypeString());
                if (type.getSqlTypeName() == SqlTypeName.BINARY) {
                // Casting a string/binary values may change the value.
                // For example, CAST(X'AB' AS BINARY(2)) yields
                // X'AB00'.
                } else {
                    tester.checkScalar(expr + " = " + literalString, true, "BOOLEAN NOT NULL");
                }
            } catch (Error e) {
                System.out.println("Failed for expr=[" + expr + "]");
                throw e;
            } catch (RuntimeException e) {
                System.out.println("Failed for expr=[" + expr + "]");
                throw e;
            }
        }
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlString(org.apache.calcite.sql.util.SqlString) SqlLimitsTest(org.apache.calcite.test.SqlLimitsTest) Test(org.junit.Test)

Example 2 with SqlString

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

the class IntervalSqlType method generateTypeString.

// ~ Methods ----------------------------------------------------------------
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
    sb.append("INTERVAL ");
    final SqlDialect dialect = AnsiSqlDialect.DEFAULT;
    final SqlPrettyWriter writer = new SqlPrettyWriter(dialect);
    writer.setAlwaysUseParentheses(false);
    writer.setSelectListItemsOnSeparateLines(false);
    writer.setIndentation(0);
    intervalQualifier.unparse(writer, 0, 0);
    final String sql = writer.toString();
    sb.append(new SqlString(dialect, sql).getSql());
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) AnsiSqlDialect(org.apache.calcite.sql.dialect.AnsiSqlDialect) SqlDialect(org.apache.calcite.sql.SqlDialect) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString)

Example 3 with SqlString

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

the class UtilTest method testSqlBuilder.

/**
 * Tests SQL builders.
 */
@Test
public void testSqlBuilder() {
    final SqlBuilder buf = new SqlBuilder(CalciteSqlDialect.DEFAULT);
    assertEquals(0, buf.length());
    buf.append("select ");
    assertEquals("select ", buf.getSql());
    buf.identifier("x");
    assertEquals("select \"x\"", buf.getSql());
    buf.append(", ");
    buf.identifier("y", "a b");
    assertEquals("select \"x\", \"y\".\"a b\"", buf.getSql());
    final SqlString sqlString = buf.toSqlString();
    assertEquals(CalciteSqlDialect.DEFAULT, sqlString.getDialect());
    assertEquals(buf.getSql(), sqlString.getSql());
    assertTrue(buf.getSql().length() > 0);
    assertEquals(buf.getSqlAndClear(), sqlString.getSql());
    assertEquals(0, buf.length());
    buf.clear();
    assertEquals(0, buf.length());
    buf.literal("can't get no satisfaction");
    assertEquals("'can''t get no satisfaction'", buf.getSqlAndClear());
    buf.literal(new Timestamp(0));
    assertEquals("TIMESTAMP '1970-01-01 00:00:00'", buf.getSqlAndClear());
    buf.clear();
    assertEquals(0, buf.length());
    buf.append("hello world");
    assertEquals(2, buf.indexOf("l"));
    assertEquals(-1, buf.indexOf("z"));
    assertEquals(9, buf.indexOf("l", 5));
}
Also used : SqlBuilder(org.apache.calcite.sql.util.SqlBuilder) Timestamp(java.sql.Timestamp) SqlString(org.apache.calcite.sql.util.SqlString) Test(org.junit.Test)

Example 4 with SqlString

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

the class SqlNode method toSqlString.

/**
 * Returns the SQL text of the tree of which this <code>SqlNode</code> is
 * the root.
 *
 * @param dialect     Dialect
 * @param forceParens wraps all expressions in parentheses; good for parse
 *                    test, but false by default.
 *
 *                    <p>Typical return values are:</p>
 *                    <ul>
 *                    <li>'It''s a bird!'</li>
 *                    <li>NULL</li>
 *                    <li>12.3</li>
 *                    <li>DATE '1969-04-29'</li>
 *                    </ul>
 */
public SqlString toSqlString(SqlDialect dialect, boolean forceParens) {
    if (dialect == null) {
        dialect = AnsiSqlDialect.DEFAULT;
    }
    SqlPrettyWriter writer = new SqlPrettyWriter(dialect);
    writer.setAlwaysUseParentheses(forceParens);
    writer.setSelectListItemsOnSeparateLines(false);
    writer.setIndentation(0);
    unparse(writer, 0, 0);
    final String sql = writer.toString();
    return new SqlString(dialect, sql);
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString)

Example 5 with SqlString

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

the class CalciteSqlOptimizer method toCreateViewPlan.

private SqlPlan toCreateViewPlan(PlanKey planKey, OptimizerContext context, SqlCreateView sqlNode) {
    SqlString sqlString = sqlNode.getQuery().toSqlString(PostgresqlSqlDialect.DEFAULT);
    String sql = sqlString.getSql();
    boolean replace = sqlNode.getReplace();
    boolean ifNotExists = sqlNode.ifNotExists;
    return new CreateViewPlan(planKey, context, sqlNode.name(), sql, sqlNode.isStream(), replace, ifNotExists, planExecutor);
}
Also used : CreateViewPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateViewPlan) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString)

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