Search in sources :

Example 16 with Identifier

use of com.facebook.presto.sql.tree.Identifier in project presto by prestodb.

the class TestSqlParser method testCreateSchema.

@Test
public void testCreateSchema() {
    assertStatement("CREATE SCHEMA test", new CreateSchema(QualifiedName.of("test"), false, ImmutableList.of()));
    assertStatement("CREATE SCHEMA IF NOT EXISTS test", new CreateSchema(QualifiedName.of("test"), true, ImmutableList.of()));
    assertStatement("CREATE SCHEMA test WITH (a = 'apple', b = 123)", new CreateSchema(QualifiedName.of("test"), false, ImmutableList.of(new Property(new Identifier("a"), new StringLiteral("apple")), new Property(new Identifier("b"), new LongLiteral("123")))));
    assertStatement("CREATE SCHEMA \"some name that contains space\"", new CreateSchema(QualifiedName.of("some name that contains space"), false, ImmutableList.of()));
}
Also used : Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) CreateSchema(com.facebook.presto.sql.tree.CreateSchema) Property(com.facebook.presto.sql.tree.Property) Test(org.testng.annotations.Test)

Example 17 with Identifier

use of com.facebook.presto.sql.tree.Identifier in project presto by prestodb.

the class TestSqlParser method testAnalyze.

@Test
public void testAnalyze() {
    QualifiedName table = QualifiedName.of("foo");
    assertStatement("ANALYZE foo", new Analyze(table, ImmutableList.of()));
    assertStatement("ANALYZE foo WITH ( \"string\" = 'bar', \"long\" = 42, computed = concat('ban', 'ana'), a = ARRAY[ 'v1', 'v2' ] )", new Analyze(table, ImmutableList.of(new Property(new Identifier("string"), new StringLiteral("bar")), new Property(new Identifier("long"), new LongLiteral("42")), new Property(new Identifier("computed"), new FunctionCall(QualifiedName.of("concat"), ImmutableList.of(new StringLiteral("ban"), new StringLiteral("ana")))), new Property(new Identifier("a"), new ArrayConstructor(ImmutableList.of(new StringLiteral("v1"), new StringLiteral("v2")))))));
    assertStatement("EXPLAIN ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), false, false, ImmutableList.of()));
    assertStatement("EXPLAIN ANALYZE ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), true, false, ImmutableList.of()));
}
Also used : Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) Explain(com.facebook.presto.sql.tree.Explain) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Property(com.facebook.presto.sql.tree.Property) Analyze(com.facebook.presto.sql.tree.Analyze) Test(org.testng.annotations.Test)

Example 18 with Identifier

use of com.facebook.presto.sql.tree.Identifier in project presto by prestodb.

the class TestSqlParser method testCreateMaterializedView.

@Test
public void testCreateMaterializedView() {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    assertStatement("CREATE MATERIALIZED VIEW mv" + " AS SELECT * FROM t", new CreateMaterializedView(Optional.empty(), QualifiedName.of("mv"), query, false, ImmutableList.of(), Optional.empty()));
    assertStatement("CREATE MATERIALIZED VIEW mv COMMENT 'A simple materialized view'" + " AS SELECT * FROM t", new CreateMaterializedView(Optional.empty(), QualifiedName.of("mv"), query, false, ImmutableList.of(), Optional.of("A simple materialized view")));
    assertStatement("CREATE MATERIALIZED VIEW IF NOT EXISTS mv COMMENT 'A simple materialized view'" + " AS SELECT * FROM t", new CreateMaterializedView(Optional.empty(), QualifiedName.of("mv"), query, true, ImmutableList.of(), Optional.of("A simple materialized view")));
    List<Property> properties = ImmutableList.of(new Property(new Identifier("partitioned_by"), new ArrayConstructor(ImmutableList.of(new StringLiteral("ds")))));
    assertStatement("CREATE MATERIALIZED VIEW IF NOT EXISTS mv COMMENT 'A simple materialized view'" + " WITH (partitioned_by = ARRAY ['ds'])" + " AS SELECT * FROM t", new CreateMaterializedView(Optional.empty(), QualifiedName.of("mv"), query, true, properties, Optional.of("A simple materialized view")));
    List<Property> properties1 = ImmutableList.of(new Property(new Identifier("partitioned_by"), new ArrayConstructor(ImmutableList.of(new StringLiteral("ds")))), new Property(new Identifier("retention_days"), new LongLiteral("90")));
    assertStatement("CREATE MATERIALIZED VIEW IF NOT EXISTS mv COMMENT 'A simple materialized view'" + " WITH (partitioned_by = ARRAY ['ds'], retention_days = 90)" + " AS SELECT * FROM t", new CreateMaterializedView(Optional.empty(), QualifiedName.of("mv"), query, true, properties1, Optional.of("A simple materialized view")));
}
Also used : CreateMaterializedView(com.facebook.presto.sql.tree.CreateMaterializedView) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) AllColumns(com.facebook.presto.sql.tree.AllColumns) Property(com.facebook.presto.sql.tree.Property) Test(org.testng.annotations.Test)

Example 19 with Identifier

use of com.facebook.presto.sql.tree.Identifier in project presto by prestodb.

the class TestSqlParser method testRefreshMaterializedView.

@Test
public void testRefreshMaterializedView() {
    assertStatement("REFRESH MATERIALIZED VIEW a WHERE p = 'x'", new RefreshMaterializedView(table(QualifiedName.of("a")), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new Identifier("p"), new StringLiteral("x"))));
    assertStatement("REFRESH MATERIALIZED VIEW a.b WHERE p = 'x'", new RefreshMaterializedView(table(QualifiedName.of("a", "b")), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new Identifier("p"), new StringLiteral("x"))));
}
Also used : QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) RefreshMaterializedView(com.facebook.presto.sql.tree.RefreshMaterializedView) Test(org.testng.annotations.Test)

Example 20 with Identifier

use of com.facebook.presto.sql.tree.Identifier 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);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Identifier(com.facebook.presto.sql.tree.Identifier) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) SingleColumn(com.facebook.presto.sql.tree.SingleColumn)

Aggregations

Identifier (com.facebook.presto.sql.tree.Identifier)46 Test (org.testng.annotations.Test)31 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)27 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)15 Query (com.facebook.presto.sql.tree.Query)11 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)10 AllColumns (com.facebook.presto.sql.tree.AllColumns)9 DropTable (com.facebook.presto.sql.tree.DropTable)9 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)9 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)9 Table (com.facebook.presto.sql.tree.Table)9 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)8 CreateTable (com.facebook.presto.sql.tree.CreateTable)8 Expression (com.facebook.presto.sql.tree.Expression)8 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)7 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)7 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)7 WithQuery (com.facebook.presto.sql.tree.WithQuery)7 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)6 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)6