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;
}
}
}
}
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());
}
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));
}
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);
}
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);
}
Aggregations