use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class QueryRewriter method rewriteCreateTableAsSelect.
private Query rewriteCreateTableAsSelect(Connection connection, Query query, CreateTableAsSelect statement) throws SQLException {
List<String> parts = new ArrayList<>();
int originalSize = statement.getName().getOriginalParts().size();
int prefixSize = rewritePrefix.getOriginalParts().size();
if (originalSize > prefixSize) {
parts.addAll(statement.getName().getOriginalParts().subList(0, originalSize - prefixSize));
}
parts.addAll(rewritePrefix.getOriginalParts());
parts.set(parts.size() - 1, createTemporaryTableName());
QualifiedName temporaryTableName = QualifiedName.of(parts);
Statement rewritten = new CreateTableAsSelect(temporaryTableName, statement.getQuery(), statement.isNotExists(), statement.getProperties(), statement.isWithData());
String createTableAsSql = formatSql(rewritten, Optional.empty());
String checksumSql = checksumSql(getColumns(connection, statement), temporaryTableName);
String dropTableSql = dropTableSql(temporaryTableName);
return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTableAsSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties());
}
use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class TestStatementBuilder method printStatement.
private static void printStatement(String sql) {
println(sql.trim());
println("");
ParsingOptions parsingOptions = new ParsingOptions(AS_DOUBLE);
Statement statement = SQL_PARSER.createStatement(sql, parsingOptions);
println(statement.toString());
println("");
println(SqlFormatter.formatSql(statement, Optional.empty()));
println("");
assertFormattedSql(SQL_PARSER, statement);
println(repeat("=", 60));
println("");
}
use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class TreeAssertions method assertFormattedSql.
public static void assertFormattedSql(SqlParser sqlParser, ParsingOptions parsingOptions, Node expected) {
String formatted = formatSql(expected, Optional.empty());
// verify round-trip of formatting already-formatted SQL
Statement actual = parseFormatted(sqlParser, parsingOptions, formatted, expected);
assertEquals(formatSql(actual, Optional.empty()), formatted);
// compare parsed tree with parsed tree of formatted SQL
if (!actual.equals(expected)) {
// simplify finding the non-equal part of the tree
assertListEquals(linearizeTree(actual), linearizeTree(expected));
}
assertEquals(actual, expected);
}
use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class VerificationManager method filterQueryType.
private List<SourceQuery> filterQueryType(List<SourceQuery> sourceQueries) {
if (explain) {
return sourceQueries;
}
ImmutableList.Builder<SourceQuery> selected = ImmutableList.builder();
for (SourceQuery sourceQuery : sourceQueries) {
try {
Statement controlStatement = sqlParser.createStatement(sourceQuery.getQuery(CONTROL), PARSING_OPTIONS);
QueryType controlQueryType = QueryType.of(controlStatement);
QueryType testQueryType = QueryType.of(sqlParser.createStatement(sourceQuery.getQuery(TEST), PARSING_OPTIONS));
if (controlQueryType == UNSUPPORTED || testQueryType == UNSUPPORTED) {
postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, UNSUPPORTED_QUERY_TYPE, skipControl));
} else if (controlQueryType != testQueryType) {
postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, MISMATCHED_QUERY_TYPE, skipControl));
} else if (isLimitWithoutOrderBy(controlStatement, sourceQuery.getName())) {
log.debug("LimitWithoutOrderByChecker Skipped %s", sourceQuery.getName());
postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, NON_DETERMINISTIC, skipControl));
} else {
selected.add(sourceQuery);
}
} catch (ParsingException e) {
log.warn("Failed to parse query: %s", sourceQuery.getName());
postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, SYNTAX_ERROR, skipControl));
}
}
List<SourceQuery> selectQueries = selected.build();
log.info("Filtering query type... Remaining queries: %s", selectQueries.size());
return selectQueries;
}
use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class TestChecksumValidator method testChecksumQuery.
@Test
public void testChecksumQuery() {
Query checksumQuery = checksumValidator.generateChecksumQuery(QualifiedName.of("test:di"), ImmutableList.of(BIGINT_COLUMN, VARCHAR_COLUMN, DOUBLE_COLUMN, REAL_COLUMN, INT_ARRAY_COLUMN, ROW_ARRAY_COLUMN, MAP_ARRAY_COLUMN, MAP_COLUMN, MAP_NON_ORDERABLE_COLUMN, ROW_COLUMN));
Statement expectedChecksumQuery = sqlParser.createStatement("SELECT\n" + " \"count\"(*)\n" + ", \"checksum\"(\"bigint\") \"bigint$checksum\"\n" + ", \"checksum\"(\"varchar\") \"varchar$checksum\"\n" + ", \"sum\"(\"double\") FILTER (WHERE \"is_finite\"(\"double\")) \"double$sum\"\n" + ", \"count\"(\"double\") FILTER (WHERE \"is_nan\"(\"double\")) \"double$nan_count\"\n" + ", \"count\"(\"double\") FILTER (WHERE (\"double\" = \"infinity\"())) \"double$pos_inf_count\"\n" + ", \"count\"(\"double\") FILTER (WHERE (\"double\" = -\"infinity\"())) \"double$neg_inf_count\"\n" + ", \"sum\"(CAST(\"real\" AS double)) FILTER (WHERE \"is_finite\"(\"real\")) \"real$sum\"\n" + ", \"count\"(\"real\") FILTER (WHERE \"is_nan\"(\"real\")) \"real$nan_count\"\n" + ", \"count\"(\"real\") FILTER (WHERE (\"real\" = \"infinity\"())) \"real$pos_inf_count\"\n" + ", \"count\"(\"real\") FILTER (WHERE (\"real\" = -\"infinity\"())) \"real$neg_inf_count\"\n" + ", \"checksum\"(\"array_sort\"(\"int_array\")) \"int_array$checksum\"\n" + ", \"checksum\"(\"cardinality\"(\"int_array\")) \"int_array$cardinality_checksum\"\n" + ", COALESCE(\"sum\"(\"cardinality\"(\"int_array\")), 0) \"int_array$cardinality_sum\"\n" + ", COALESCE(\"checksum\"(TRY(\"array_sort\"(\"row_array\"))), \"checksum\"(\"row_array\")) \"row_array$checksum\"\n" + ", \"checksum\"(\"cardinality\"(\"row_array\")) \"row_array$cardinality_checksum\"\n" + ", COALESCE(\"sum\"(\"cardinality\"(\"row_array\")), 0) \"row_array$cardinality_sum\"\n" + ", \"checksum\"(\"map_array\") \"map_array$checksum\"\n" + ", \"checksum\"(\"cardinality\"(\"map_array\")) \"map_array$cardinality_checksum\"\n" + ", COALESCE(\"sum\"(\"cardinality\"(\"map_array\")), 0) \"map_array$cardinality_sum\"\n" + ", \"checksum\"(\"map\") \"map$checksum\"\n" + ", \"checksum\"(\"array_sort\"(\"map_keys\"(\"map\"))) \"map$keys_checksum\"\n" + ", COALESCE(\"checksum\"(TRY(\"array_sort\"(\"map_values\"(\"map\")))), \"checksum\"(\"map_values\"(\"map\"))) \"map$values_checksum\"\n" + ", \"checksum\"(\"cardinality\"(\"map\")) \"map$cardinality_checksum\"\n" + ", COALESCE(\"sum\"(\"cardinality\"(\"map\")), 0) \"map$cardinality_sum\"\n" + ", \"checksum\"(\"map_non_orderable\") \"map_non_orderable$checksum\"\n" + ", \"checksum\"(\"map_keys\"(\"map_non_orderable\")) \"map_non_orderable$keys_checksum\"\n" + ", \"checksum\"(\"map_values\"(\"map_non_orderable\")) \"map_non_orderable$values_checksum\"\n" + ", \"checksum\"(\"cardinality\"(\"map_non_orderable\")) \"map_non_orderable$cardinality_checksum\"\n" + ", COALESCE(\"sum\"(\"cardinality\"(\"map_non_orderable\")), 0) \"map_non_orderable$cardinality_sum\"\n" + ", \"checksum\"(\"row\".\"i\") \"row.i$checksum\"\n" + ", \"checksum\"(\"row\"[2]) \"row._col2$checksum\"\n" + ", \"sum\"(\"row\".\"d\") FILTER (WHERE \"is_finite\"(\"row\".\"d\")) \"row.d$sum\"\n" + ", \"count\"(\"row\".\"d\") FILTER (WHERE \"is_nan\"(\"row\".\"d\")) \"row.d$nan_count\"\n" + ", \"count\"(\"row\".\"d\") FILTER (WHERE (\"row\".\"d\" = \"infinity\"())) \"row.d$pos_inf_count\"\n" + ", \"count\"(\"row\".\"d\") FILTER (WHERE (\"row\".\"d\" = -\"infinity\"())) \"row.d$neg_inf_count\"\n" + ", \"checksum\"(\"array_sort\"(\"row\".\"a\")) \"row.a$checksum\"\n" + ", \"checksum\"(\"cardinality\"(\"row\".\"a\")) \"row.a$cardinality_checksum\"\n" + ", COALESCE(\"sum\"(\"cardinality\"(\"row\".\"a\")), 0) \"row.a$cardinality_sum\"\n" + ", \"sum\"(\"row\".\"r\"[1]) FILTER (WHERE \"is_finite\"(\"row\".\"r\"[1])) \"row.r._col1$sum\"\n" + ", \"count\"(\"row\".\"r\"[1]) FILTER (WHERE \"is_nan\"(\"row\".\"r\"[1])) \"row.r._col1$nan_count\"\n" + ", \"count\"(\"row\".\"r\"[1]) FILTER (WHERE (\"row\".\"r\"[1] = \"infinity\"())) \"row.r._col1$pos_inf_count\"\n" + ", \"count\"(\"row\".\"r\"[1]) FILTER (WHERE (\"row\".\"r\"[1] = -\"infinity\"())) \"row.r._col1$neg_inf_count\"\n" + ", \"checksum\"(\"row\".\"r\".\"b\") \"row.r.b$checksum\"\n" + "FROM\n" + " \"test:di\"\n", PARSING_OPTIONS);
assertEquals(formatSql(checksumQuery, Optional.empty()), formatSql(expectedChecksumQuery, Optional.empty()));
}
Aggregations