Search in sources :

Example 1 with Property

use of io.trino.sql.tree.Property in project trino by trinodb.

the class PropertyUtil method evaluateProperties.

public static Map<String, Optional<Object>> evaluateProperties(Iterable<Property> setProperties, Session session, PlannerContext plannerContext, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters, boolean includeAllProperties, Map<String, PropertyMetadata<?>> metadata, ErrorCodeSupplier errorCode, String propertyTypeDescription) {
    Map<String, Optional<Object>> propertyValues = new LinkedHashMap<>();
    // Fill in user-specified properties
    for (Property property : setProperties) {
        // property names are case-insensitive and normalized to lower case
        String propertyName = property.getName().getValue().toLowerCase(ENGLISH);
        PropertyMetadata<?> propertyMetadata = metadata.get(propertyName);
        if (propertyMetadata == null) {
            throw new TrinoException(errorCode, format("%s '%s' does not exist", capitalize(propertyTypeDescription), propertyName));
        }
        Optional<Object> value;
        if (property.isSetToDefault()) {
            value = Optional.ofNullable(propertyMetadata.getDefaultValue());
        } else {
            value = Optional.of(evaluateProperty(property.getNonDefaultValue(), propertyMetadata, session, plannerContext, accessControl, parameters, errorCode, propertyTypeDescription));
        }
        propertyValues.put(propertyMetadata.getName(), value);
    }
    if (includeAllProperties) {
        for (PropertyMetadata<?> propertyMetadata : metadata.values()) {
            if (!propertyValues.containsKey(propertyMetadata.getName())) {
                propertyValues.put(propertyMetadata.getName(), Optional.ofNullable(propertyMetadata.getDefaultValue()));
            }
        }
    }
    return ImmutableMap.copyOf(propertyValues);
}
Also used : Optional(java.util.Optional) TrinoException(io.trino.spi.TrinoException) Property(io.trino.sql.tree.Property) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Property

use of io.trino.sql.tree.Property in project trino by trinodb.

the class TestSetPropertiesTask method testSetMaterializedViewProperties.

@Test
public void testSetMaterializedViewProperties() {
    QualifiedObjectName materializedViewName = qualifiedObjectName("test_materialized_view");
    metadata.createMaterializedView(testSession, materializedViewName, someMaterializedView(), false, false);
    // set all properties to non-DEFAULT values and check the results
    executeSetProperties(new SetProperties(MATERIALIZED_VIEW, asQualifiedName(materializedViewName), ImmutableList.of(new Property(new Identifier(MATERIALIZED_VIEW_PROPERTY_1_NAME), new LongLiteral("111")), new Property(new Identifier(MATERIALIZED_VIEW_PROPERTY_2_NAME), new StringLiteral("abc")))));
    assertThat(metadata.getMaterializedView(testSession, materializedViewName).get().getProperties()).isEqualTo(ImmutableMap.of(MATERIALIZED_VIEW_PROPERTY_1_NAME, 111L, MATERIALIZED_VIEW_PROPERTY_2_NAME, "abc"));
    // set all properties to DEFAULT and check the results
    executeSetProperties(new SetProperties(MATERIALIZED_VIEW, asQualifiedName(materializedViewName), ImmutableList.of(new Property(new Identifier(MATERIALIZED_VIEW_PROPERTY_1_NAME)), new Property(new Identifier(MATERIALIZED_VIEW_PROPERTY_2_NAME)))));
    // since the default value of property 1 is null, property 1 should not appear in the result, whereas property 2 should appear in
    // the result with its (non-null) default value
    assertThat(metadata.getMaterializedView(testSession, materializedViewName).get().getProperties()).isEqualTo(ImmutableMap.of(MATERIALIZED_VIEW_PROPERTY_2_NAME, MATERIALIZED_VIEW_PROPERTY_2_DEFAULT_VALUE));
}
Also used : Identifier(io.trino.sql.tree.Identifier) SetProperties(io.trino.sql.tree.SetProperties) StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) Property(io.trino.sql.tree.Property) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 3 with Property

use of io.trino.sql.tree.Property in project trino by trinodb.

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()), ImmutableList.of()));
    assertStatement("EXPLAIN ANALYZE ANALYZE foo", new ExplainAnalyze(new Analyze(table, ImmutableList.of()), false));
}
Also used : QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) ExplainAnalyze(io.trino.sql.tree.ExplainAnalyze) QualifiedName(io.trino.sql.tree.QualifiedName) Explain(io.trino.sql.tree.Explain) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) FunctionCall(io.trino.sql.tree.FunctionCall) Property(io.trino.sql.tree.Property) ExplainAnalyze(io.trino.sql.tree.ExplainAnalyze) Analyze(io.trino.sql.tree.Analyze) Test(org.junit.jupiter.api.Test)

Example 4 with Property

use of io.trino.sql.tree.Property in project trino by trinodb.

the class TestSqlParser method testCreateMaterializedView.

@Test
public void testCreateMaterializedView() {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    Optional<NodeLocation> location = Optional.empty();
    assertStatement("CREATE MATERIALIZED VIEW a AS SELECT * FROM t", new CreateMaterializedView(location, QualifiedName.of("a"), query, false, false, new ArrayList<>(), Optional.empty()));
    Query query2 = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("catalog2", "schema2", "tab")));
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A simple materialized view'" + " AS SELECT * FROM catalog2.schema2.tab", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query2, true, false, new ArrayList<>(), Optional.of("A simple materialized view")));
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A simple materialized view'" + " AS SELECT * FROM catalog2.schema2.tab", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query2, true, false, new ArrayList<>(), Optional.of("A simple materialized view")));
    List<Property> properties = ImmutableList.of(new Property(new Identifier("partitioned_by"), new ArrayConstructor(ImmutableList.of(new StringLiteral("dateint")))));
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A simple materialized view'" + "WITH (partitioned_by = ARRAY ['dateint'])" + " AS SELECT * FROM catalog2.schema2.tab", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query2, true, false, properties, Optional.of("A simple materialized view")));
    Query query3 = new Query(Optional.of(new With(false, ImmutableList.of(new WithQuery(identifier("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("x"))), Optional.of(ImmutableList.of(identifier("t"), identifier("u")))), new WithQuery(identifier("b"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("a"))), Optional.empty())))), new Table(QualifiedName.of("b")), Optional.empty(), Optional.empty(), Optional.empty());
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A partitioned materialized view' " + "WITH (partitioned_by = ARRAY ['dateint'])" + " AS WITH a (t, u) AS (SELECT * FROM x), b AS (SELECT * FROM a) TABLE b", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query3, true, false, properties, Optional.of("A partitioned materialized view")));
}
Also used : CreateTable(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) ArrayList(java.util.ArrayList) AllColumns(io.trino.sql.tree.AllColumns) With(io.trino.sql.tree.With) CreateMaterializedView(io.trino.sql.tree.CreateMaterializedView) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) NodeLocation(io.trino.sql.tree.NodeLocation) StringLiteral(io.trino.sql.tree.StringLiteral) WithQuery(io.trino.sql.tree.WithQuery) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) Property(io.trino.sql.tree.Property) Test(org.junit.jupiter.api.Test)

Example 5 with Property

use of io.trino.sql.tree.Property in project trino by trinodb.

the class TestSqlParser method testCreateTableAsSelect.

@Test
public void testCreateTableAsSelect() {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    Query querySelectColumn = simpleQuery(selectList(new Identifier("a")), table(QualifiedName.of("t")));
    Query querySelectColumns = simpleQuery(selectList(new Identifier("a"), new Identifier("b")), table(QualifiedName.of("t")));
    QualifiedName table = QualifiedName.of("foo");
    assertStatement("CREATE TABLE foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, false, ImmutableList.of(), true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, true, ImmutableList.of(), true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo AS SELECT * FROM t WITH NO DATA", new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    List<Property> properties = 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("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t", new CreateTableAsSelect(table, query, false, properties, true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false, Optional.empty(), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x) COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x,y) COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x,y) COMMENT 'test'" + "WITH ( \"string\" = 'bar', \"long\" = 42, computed = 'ban' || 'ana', a = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.of("test")));
}
Also used : QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) QualifiedName(io.trino.sql.tree.QualifiedName) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) AllColumns(io.trino.sql.tree.AllColumns) FunctionCall(io.trino.sql.tree.FunctionCall) Property(io.trino.sql.tree.Property) Test(org.junit.jupiter.api.Test)

Aggregations

Property (io.trino.sql.tree.Property)13 Identifier (io.trino.sql.tree.Identifier)12 StringLiteral (io.trino.sql.tree.StringLiteral)9 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)6 LongLiteral (io.trino.sql.tree.LongLiteral)6 Test (org.junit.jupiter.api.Test)6 AllColumns (io.trino.sql.tree.AllColumns)4 Test (org.testng.annotations.Test)4 AllowAllAccessControl (io.trino.security.AllowAllAccessControl)3 PropertyMetadata.stringProperty (io.trino.spi.session.PropertyMetadata.stringProperty)3 ArrayConstructor (io.trino.sql.tree.ArrayConstructor)3 CreateMaterializedView (io.trino.sql.tree.CreateMaterializedView)3 SetProperties (io.trino.sql.tree.SetProperties)3 FeaturesConfig (io.trino.FeaturesConfig)2 TrinoException (io.trino.spi.TrinoException)2 PropertyMetadata.integerProperty (io.trino.spi.session.PropertyMetadata.integerProperty)2 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)2 BooleanLiteral (io.trino.sql.tree.BooleanLiteral)2 CreateTable (io.trino.sql.tree.CreateTable)2 Expression (io.trino.sql.tree.Expression)2