use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.
the class TestShadowing method testCreateTableAsSelect.
@Test
public void testCreateTableAsSelect() throws Exception {
handle.execute("CREATE TABLE \"my_test_table\" (column1 BIGINT, column2 DOUBLE)");
SqlParser parser = new SqlParser();
Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE my_test_table AS SELECT 1 column1, CAST(2.0 AS DOUBLE) column2 LIMIT 1", ImmutableList.of(), null, null, ImmutableMap.of());
QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
Query rewrittenQuery = rewriter.shadowQuery(query);
assertEquals(rewrittenQuery.getPreQueries().size(), 1);
assertEquals(rewrittenQuery.getPostQueries().size(), 1);
CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0));
assertEquals(createTableAs.getName().getParts().size(), 1);
assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_"));
assertFalse(createTableAs.getName().getSuffix().contains("my_test_table"));
assertEquals(PrestoVerifier.statementToQueryType(parser, rewrittenQuery.getQuery()), READ);
Table table = new Table(createTableAs.getName());
SingleColumn column1 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("column1"))));
SingleColumn column2 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("column2"), new LongLiteral("1"))))));
Select select = new Select(false, ImmutableList.of(column1, column2));
QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new com.facebook.presto.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty()));
assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTableAs.getName(), true));
}
use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.
the class MaterializedViewQueryOptimizer method visitSingleColumn.
@Override
protected Node visitSingleColumn(SingleColumn node, Void context) {
// For a single table, without sub-queries, the column prefix is unnecessary. Here It is removed so that it can be mapped to the view column properly.
// For relations other than single table, it needs to be reserved to differentiate columns from different tables.
// One way to do so is to process the prefix within `visitDereferenceExpression()` since the prefix information is saved as `base` in `DereferenceExpression` node.
node = removeSingleColumnPrefix(node, removablePrefix);
Expression expression = node.getExpression();
Optional<Set<Expression>> groupByOfMaterializedView = materializedViewInfo.getGroupBy();
// TODO: Replace this logic with rule-based validation framework.
if (groupByOfMaterializedView.isPresent() && validateExpressionForGroupBy(groupByOfMaterializedView.get(), expression) && (!expressionsInGroupBy.isPresent() || !expressionsInGroupBy.get().contains(expression))) {
throw new IllegalStateException("Query a column presents in materialized view group by: " + expression.toString());
}
Expression processedColumn = (Expression) process(expression, context);
Optional<Identifier> alias = node.getAlias();
// If a column name was rewritten, make sure we re-alias to same name as base query
if (!alias.isPresent() && processedColumn instanceof Identifier && !processedColumn.equals(node.getExpression())) {
alias = Optional.of((Identifier) node.getExpression());
}
return new SingleColumn(processedColumn, alias);
}
use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.
the class RowColumnValidator method generateChecksumColumns.
@Override
public List<SingleColumn> generateChecksumColumns(Column column) {
checkColumnType(column);
ImmutableList.Builder<SingleColumn> columnsBuilder = ImmutableList.builder();
List<Field> fields = getFields(column);
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
Column fieldColumn = getFieldAsColumn(column, field, i);
List<SingleColumn> fieldColumns = columnValidators.get(fieldColumn.getCategory()).get().generateChecksumColumns(fieldColumn);
columnsBuilder.addAll(fieldColumns);
}
return columnsBuilder.build();
}
use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.
the class ArrayColumnValidator method generateChecksumColumns.
@Override
public List<SingleColumn> generateChecksumColumns(Column column) {
Expression checksum = generateArrayChecksum(column.getExpression(), column.getType());
Expression arrayCardinalityChecksum = functionCall("checksum", functionCall("cardinality", column.getExpression()));
Expression arrayCardinalitySum = new CoalesceExpression(functionCall("sum", functionCall("cardinality", column.getExpression())), new LongLiteral("0"));
return ImmutableList.of(new SingleColumn(checksum, Optional.of(delimitedIdentifier(getChecksumColumnAlias(column)))), new SingleColumn(arrayCardinalityChecksum, Optional.of(delimitedIdentifier(getCardinalityChecksumColumnAlias(column)))), new SingleColumn(arrayCardinalitySum, Optional.of(delimitedIdentifier(getCardinalitySumColumnAlias(column)))));
}
use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.
the class ChecksumValidator method generateChecksumQuery.
public Query generateChecksumQuery(QualifiedName tableName, List<Column> columns) {
ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder();
selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("count"), ImmutableList.of())));
for (Column column : columns) {
selectItems.addAll(columnValidators.get(column.getCategory()).get().generateChecksumColumns(column));
}
return simpleQuery(new Select(false, selectItems.build()), new Table(tableName));
}
Aggregations