Search in sources :

Example 6 with Identifier

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

the class TestSetTimeZoneTask method testSetTimeZoneVarcharFunctionCall.

@Test
public void testSetTimeZoneVarcharFunctionCall() {
    QueryStateMachine stateMachine = createQueryStateMachine("SET TIME ZONE concat_ws('/', 'America', 'Los_Angeles')");
    SetTimeZone setTimeZone = new SetTimeZone(new NodeLocation(1, 1), Optional.of(new FunctionCall(new NodeLocation(1, 15), QualifiedName.of(ImmutableList.of(new Identifier(new NodeLocation(1, 15), "concat_ws", false))), ImmutableList.of(new StringLiteral(new NodeLocation(1, 25), "/"), new StringLiteral(new NodeLocation(1, 30), "America"), new StringLiteral(new NodeLocation(1, 41), "Los_Angeles")))));
    executeSetTimeZone(setTimeZone, stateMachine);
    Map<String, String> setSessionProperties = stateMachine.getSetSessionProperties();
    assertThat(setSessionProperties).hasSize(1);
    assertEquals(setSessionProperties.get(TIME_ZONE_ID), "America/Los_Angeles");
}
Also used : Identifier(io.trino.sql.tree.Identifier) NodeLocation(io.trino.sql.tree.NodeLocation) StringLiteral(io.trino.sql.tree.StringLiteral) SetTimeZone(io.trino.sql.tree.SetTimeZone) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 7 with Identifier

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

the class TestSetTimeZoneTask method testSetTimeZoneIntervalDayTimeTypeFunctionCall.

@Test
public void testSetTimeZoneIntervalDayTimeTypeFunctionCall() {
    QueryStateMachine stateMachine = createQueryStateMachine("SET TIME ZONE parse_duration('8h')");
    SetTimeZone setTimeZone = new SetTimeZone(new NodeLocation(1, 1), Optional.of(new FunctionCall(new NodeLocation(1, 24), QualifiedName.of(ImmutableList.of(new Identifier(new NodeLocation(1, 24), "parse_duration", false))), ImmutableList.of(new StringLiteral(new NodeLocation(1, 39), "8h")))));
    executeSetTimeZone(setTimeZone, stateMachine);
    Map<String, String> setSessionProperties = stateMachine.getSetSessionProperties();
    assertThat(setSessionProperties).hasSize(1);
    assertEquals(setSessionProperties.get(TIME_ZONE_ID), "+08:00");
}
Also used : Identifier(io.trino.sql.tree.Identifier) NodeLocation(io.trino.sql.tree.NodeLocation) StringLiteral(io.trino.sql.tree.StringLiteral) SetTimeZone(io.trino.sql.tree.SetTimeZone) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 8 with Identifier

use of io.trino.sql.tree.Identifier 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 9 with Identifier

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

the class GrantRolesTask method execute.

@Override
public ListenableFuture<Void> execute(GrantRoles statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    Set<String> roles = statement.getRoles().stream().map(role -> role.getValue().toLowerCase(Locale.ENGLISH)).collect(toImmutableSet());
    Set<TrinoPrincipal> grantees = statement.getGrantees().stream().map(MetadataUtil::createPrincipal).collect(toImmutableSet());
    boolean adminOption = statement.isAdminOption();
    Optional<TrinoPrincipal> grantor = statement.getGrantor().map(specification -> createPrincipal(session, specification));
    Optional<String> catalog = processRoleCommandCatalog(metadata, session, statement, statement.getCatalog().map(Identifier::getValue));
    Set<String> specifiedRoles = new LinkedHashSet<>();
    specifiedRoles.addAll(roles);
    grantees.stream().filter(principal -> principal.getType() == ROLE).map(TrinoPrincipal::getName).forEach(specifiedRoles::add);
    if (grantor.isPresent() && grantor.get().getType() == ROLE) {
        specifiedRoles.add(grantor.get().getName());
    }
    for (String role : specifiedRoles) {
        checkRoleExists(session, statement, metadata, role, catalog);
    }
    accessControl.checkCanGrantRoles(session.toSecurityContext(), roles, grantees, adminOption, grantor, catalog);
    metadata.grantRoles(session, roles, grantees, adminOption, grantor, catalog);
    return immediateVoidFuture();
}
Also used : Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) MetadataUtil.checkRoleExists(io.trino.metadata.MetadataUtil.checkRoleExists) MetadataUtil(io.trino.metadata.MetadataUtil) Set(java.util.Set) ROLE(io.trino.spi.security.PrincipalType.ROLE) GrantRoles(io.trino.sql.tree.GrantRoles) Inject(javax.inject.Inject) List(java.util.List) AccessControl(io.trino.security.AccessControl) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) MetadataUtil.createPrincipal(io.trino.metadata.MetadataUtil.createPrincipal) Locale(java.util.Locale) Objects.requireNonNull(java.util.Objects.requireNonNull) WarningCollector(io.trino.execution.warnings.WarningCollector) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Expression(io.trino.sql.tree.Expression) MetadataUtil.processRoleCommandCatalog(io.trino.metadata.MetadataUtil.processRoleCommandCatalog) Identifier(io.trino.sql.tree.Identifier) LinkedHashSet(java.util.LinkedHashSet) Session(io.trino.Session) LinkedHashSet(java.util.LinkedHashSet) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) Session(io.trino.Session)

Example 10 with Identifier

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

the class ScopeAware method scopeAwareHash.

private OptionalInt scopeAwareHash(Node node) {
    if (node instanceof Expression) {
        Expression expression = (Expression) node;
        if (analysis.isColumnReference(expression)) {
            ResolvedField field = analysis.getResolvedField(expression);
            Scope resolvedScope = field.getScope();
            if (resolvedScope.hasOuterParent(queryScope)) {
                return OptionalInt.of(treeHash(expression, CanonicalizationAware::canonicalizationAwareHash));
            }
            return OptionalInt.of(field.getFieldId().hashCode());
        } else if (expression instanceof Identifier) {
            return OptionalInt.of(treeHash(expression, CanonicalizationAware::canonicalizationAwareHash));
        } else if (node.getChildren().isEmpty()) {
            // Calculate shallow hash since node doesn't have any children
            return OptionalInt.of(expression.hashCode());
        }
    }
    return OptionalInt.empty();
}
Also used : ResolvedField(io.trino.sql.analyzer.ResolvedField) Identifier(io.trino.sql.tree.Identifier) CanonicalizationAware(io.trino.sql.analyzer.CanonicalizationAware) Scope(io.trino.sql.analyzer.Scope) Expression(io.trino.sql.tree.Expression)

Aggregations

Identifier (io.trino.sql.tree.Identifier)71 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)37 Test (org.junit.jupiter.api.Test)37 FunctionCall (io.trino.sql.tree.FunctionCall)19 StringLiteral (io.trino.sql.tree.StringLiteral)19 LongLiteral (io.trino.sql.tree.LongLiteral)18 Test (org.testng.annotations.Test)15 CreateTable (io.trino.sql.tree.CreateTable)14 Property (io.trino.sql.tree.Property)14 Expression (io.trino.sql.tree.Expression)13 Table (io.trino.sql.tree.Table)12 DropTable (io.trino.sql.tree.DropTable)11 AllColumns (io.trino.sql.tree.AllColumns)9 QualifiedName (io.trino.sql.tree.QualifiedName)9 PrincipalSpecification (io.trino.sql.tree.PrincipalSpecification)8 List (java.util.List)8 ImmutableList (com.google.common.collect.ImmutableList)6 Session (io.trino.Session)6 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)6 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)5