use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class SqlPrettyWriterTest method testDamiansSubQueryStyle.
@Test
public void testDamiansSubQueryStyle() throws Exception {
// Note that ( is at the indent, SELECT is on the same line, and ) is
// below it.
final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
prettyWriter.setSubQueryStyle(SqlWriter.SubQueryStyle.BLACK);
checkSimple(prettyWriter, "${desc}", "${formatted}");
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class SqlOperatorTest method testArgumentBounds.
/**
* Test that calls all operators with all possible argument types, and for
* each type, with a set of tricky values.
*
* <p>This is not really a unit test since there are no assertions;
* it either succeeds or fails in the preparation of the operator case
* and not when actually testing (validating/executing) the call.
*
* <p>Nevertheless the log messages conceal many problems which potentially
* need to be fixed especially cases where the query passes from the
* validation stage and fails at runtime.
*/
@Disabled("Too slow and not really a unit test")
@Tag("slow")
@Test
void testArgumentBounds() {
final SqlOperatorFixture f = fixture();
final SqlValidatorImpl validator = (SqlValidatorImpl) f.getFactory().createValidator();
final SqlValidatorScope scope = validator.getEmptyScope();
final RelDataTypeFactory typeFactory = validator.getTypeFactory();
final Builder builder = new Builder(typeFactory);
builder.add0(SqlTypeName.BOOLEAN, true, false);
builder.add0(SqlTypeName.TINYINT, 0, 1, -3, Byte.MAX_VALUE, Byte.MIN_VALUE);
builder.add0(SqlTypeName.SMALLINT, 0, 1, -4, Short.MAX_VALUE, Short.MIN_VALUE);
builder.add0(SqlTypeName.INTEGER, 0, 1, -2, Integer.MIN_VALUE, Integer.MAX_VALUE);
builder.add0(SqlTypeName.BIGINT, 0, 1, -5, Integer.MAX_VALUE, Long.MAX_VALUE, Long.MIN_VALUE);
builder.add1(SqlTypeName.VARCHAR, 11, "", " ", "hello world");
builder.add1(SqlTypeName.CHAR, 5, "", "e", "hello");
builder.add0(SqlTypeName.TIMESTAMP, 0L, DateTimeUtils.MILLIS_PER_DAY);
Set<SqlOperator> operatorsToSkip = new HashSet<>();
if (!Bug.CALCITE_3243_FIXED) {
// TODO: Remove entirely the if block when the bug is fixed
// REVIEW zabetak 12-August-2019: It may still make sense to avoid the
// JSON functions since for most of the values above they are expected
// to raise an error and due to the big number of operands they accept
// they increase significantly the running time of the method.
operatorsToSkip.add(SqlStdOperatorTable.JSON_VALUE);
operatorsToSkip.add(SqlStdOperatorTable.JSON_QUERY);
}
// Skip since ClassCastException is raised in SqlOperator#unparse
// since the operands of the call do not have the expected type.
// Moreover, the values above do not make much sense for this operator.
operatorsToSkip.add(SqlStdOperatorTable.WITHIN_GROUP);
// can't handle the flag argument
operatorsToSkip.add(SqlStdOperatorTable.TRIM);
operatorsToSkip.add(SqlStdOperatorTable.EXISTS);
for (SqlOperator op : SqlStdOperatorTable.instance().getOperatorList()) {
if (operatorsToSkip.contains(op)) {
continue;
}
if (op.getSyntax() == SqlSyntax.SPECIAL) {
continue;
}
final SqlOperandTypeChecker typeChecker = op.getOperandTypeChecker();
if (typeChecker == null) {
continue;
}
final SqlOperandCountRange range = typeChecker.getOperandCountRange();
for (int n = range.getMin(), max = range.getMax(); n <= max; n++) {
final List<List<ValueType>> argValues = Collections.nCopies(n, builder.values);
for (final List<ValueType> args : Linq4j.product(argValues)) {
SqlNodeList nodeList = new SqlNodeList(SqlParserPos.ZERO);
int nullCount = 0;
for (ValueType arg : args) {
if (arg.value == null) {
++nullCount;
}
nodeList.add(arg.node);
}
final SqlCall call = op.createCall(nodeList);
final SqlCallBinding binding = new SqlCallBinding(validator, scope, call);
if (!typeChecker.checkOperandTypes(binding, false)) {
continue;
}
final SqlPrettyWriter writer = new SqlPrettyWriter();
op.unparse(writer, call, 0, 0);
final String s = writer.toSqlString().toString();
if (s.startsWith("OVERLAY(") || s.contains(" / 0") || s.matches("MOD\\(.*, 0\\)")) {
continue;
}
final Strong.Policy policy = Strong.policy(op);
try {
if (nullCount > 0 && policy == Strong.Policy.ANY) {
f.checkNull(s);
} else {
final String query;
if (op instanceof SqlAggFunction) {
if (op.requiresOrder()) {
query = "SELECT " + s + " OVER () FROM (VALUES (1))";
} else {
query = "SELECT " + s + " FROM (VALUES (1))";
}
} else {
query = AbstractSqlTester.buildQuery(s);
}
f.check(query, SqlTests.ANY_TYPE_CHECKER, SqlTests.ANY_PARAMETER_CHECKER, result -> {
});
}
} catch (Throwable e) {
// Logging the top-level throwable directly makes the message
// difficult to read since it either contains too much information
// or very few details.
Throwable cause = findMostDescriptiveCause(e);
LOGGER.info("Failed: " + s + ": " + cause);
}
}
}
}
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class ServerDdlExecutor method populate.
/**
* Populates the table called {@code name} by executing {@code query}.
*/
static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
// Generate, prepare and execute an "INSERT INTO table query" statement.
// (It's a bit inefficient that we convert from SqlNode to SQL and back
// again.)
final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(context.getRootSchema().plus()).build();
final Planner planner = Frameworks.getPlanner(config);
try {
final StringBuilder buf = new StringBuilder();
final SqlWriterConfig writerConfig = SqlPrettyWriter.config().withAlwaysUseParentheses(false);
final SqlPrettyWriter w = new SqlPrettyWriter(writerConfig, buf);
buf.append("INSERT INTO ");
name.unparse(w, 0, 0);
buf.append(' ');
query.unparse(w, 0, 0);
final String sql = buf.toString();
final SqlNode query1 = planner.parse(sql);
final SqlNode query2 = planner.validate(query1);
final RelRoot r = planner.rel(query2);
final PreparedStatement prepare = context.getRelRunner().prepareStatement(r.rel);
int rowCount = prepare.executeUpdate();
Util.discard(rowCount);
prepare.close();
} catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
throw Util.throwAsRuntime(e);
}
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class SqlParserTest method testSqlOptions.
@Test
void testSqlOptions() {
SqlNode node = sql("alter system set schema = true").node();
SqlSetOption opt = (SqlSetOption) node;
assertThat(opt.getScope(), equalTo("SYSTEM"));
SqlPrettyWriter writer = new SqlPrettyWriter();
assertThat(writer.format(opt.getName()), equalTo("\"SCHEMA\""));
writer = new SqlPrettyWriter();
assertThat(writer.format(opt.getValue()), equalTo("TRUE"));
writer = new SqlPrettyWriter();
assertThat(writer.format(opt), equalTo("ALTER SYSTEM SET \"SCHEMA\" = TRUE"));
sql("alter system set \"a number\" = 1").ok("ALTER SYSTEM SET `a number` = 1").node(isDdl());
sql("alter system set flag = false").ok("ALTER SYSTEM SET `FLAG` = FALSE");
sql("alter system set approx = -12.3450").ok("ALTER SYSTEM SET `APPROX` = -12.3450");
sql("alter system set onOff = on").ok("ALTER SYSTEM SET `ONOFF` = `ON`");
sql("alter system set onOff = off").ok("ALTER SYSTEM SET `ONOFF` = `OFF`");
sql("alter system set baz = foo").ok("ALTER SYSTEM SET `BAZ` = `FOO`");
sql("alter system set \"a\".\"number\" = 1").ok("ALTER SYSTEM SET `a`.`number` = 1");
sql("set approx = -12.3450").ok("SET `APPROX` = -12.3450").node(isDdl());
node = sql("reset schema").node();
opt = (SqlSetOption) node;
assertThat(opt.getScope(), equalTo(null));
writer = new SqlPrettyWriter();
assertThat(writer.format(opt.getName()), equalTo("\"SCHEMA\""));
assertThat(opt.getValue(), equalTo(null));
writer = new SqlPrettyWriter();
assertThat(writer.format(opt), equalTo("RESET \"SCHEMA\""));
sql("alter system RESET flag").ok("ALTER SYSTEM RESET `FLAG`");
sql("reset onOff").ok("RESET `ONOFF`").node(isDdl());
sql("reset \"this\".\"is\".\"sparta\"").ok("RESET `this`.`is`.`sparta`");
sql("alter system reset all").ok("ALTER SYSTEM RESET `ALL`");
sql("reset all").ok("RESET `ALL`");
// expressions not allowed
sql("alter system set aString = 'abc' ^||^ 'def' ").fails("(?s)Encountered \"\\|\\|\" at line 1, column 34\\..*");
// multiple assignments not allowed
sql("alter system set x = 1^,^ y = 2").fails("(?s)Encountered \",\" at line 1, column 23\\..*");
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class CassandraSchema method addMaterializedViews.
/**
* Adds all materialized views defined in the schema to this column family.
*/
private void addMaterializedViews() {
// Close the hook used to get us here
hook.close();
for (ViewMetadata view : getKeyspace().getViews().values()) {
String tableName = view.getBaseTable().asInternal();
StringBuilder queryBuilder = new StringBuilder("SELECT ");
// Add all the selected columns to the query
String columnsList = view.getColumns().values().stream().map(c -> c.getName().asInternal()).collect(Collectors.joining(", "));
queryBuilder.append(columnsList);
queryBuilder.append(" FROM ").append(tableName);
// Get the where clause from the system schema
String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName().asInternal() + "'";
Row whereClauseRow = Objects.requireNonNull(session.execute(whereQuery).one());
queryBuilder.append(" WHERE ").append(whereClauseRow.getString(0));
// Parse and unparse the view query to get properly quoted field names
String query = queryBuilder.toString();
SqlParser.Config parserConfig = SqlParser.config().withUnquotedCasing(Casing.UNCHANGED);
SqlSelect parsedQuery;
try {
parsedQuery = (SqlSelect) SqlParser.create(query, parserConfig).parseQuery();
} catch (SqlParseException e) {
LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName().asInternal());
continue;
}
final StringBuilder buf = new StringBuilder(query.length());
final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(true);
final SqlWriter writer = new SqlPrettyWriter(config, buf);
parsedQuery.unparse(writer, 0, 0);
query = buf.toString();
// Add the view for this query
String viewName = "$" + getTableNames().size();
SchemaPlus schema = parentSchema.getSubSchema(name);
if (schema == null) {
throw new IllegalStateException("Cannot find schema " + name + " in parent schema " + parentSchema.getName());
}
CalciteSchema calciteSchema = CalciteSchema.from(schema);
List<String> viewPath = calciteSchema.path(viewName);
schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName().asInternal(), true));
}
}
Aggregations