Search in sources :

Example 6 with Parameter

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

the class TestParameterExtractor method testParameterCount.

@Test
public void testParameterCount() {
    Statement statement = sqlParser.createStatement("SELECT c1, c2 FROM test_table WHERE c1 = ? AND c2 > ?", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 41), 0), new Parameter(new NodeLocation(1, 52), 1));
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(2);
}
Also used : NodeLocation(io.trino.sql.tree.NodeLocation) ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) Parameter(io.trino.sql.tree.Parameter) Test(org.testng.annotations.Test)

Example 7 with Parameter

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

the class AstBuilder method visitParameter.

@Override
public Node visitParameter(SqlBaseParser.ParameterContext context) {
    io.trino.sql.tree.Parameter parameter = new io.trino.sql.tree.Parameter(getLocation(context), parameterPosition);
    parameterPosition++;
    return parameter;
}
Also used : Parameter(io.trino.sql.tree.Parameter) NumericParameter(io.trino.sql.tree.NumericParameter) DataTypeParameter(io.trino.sql.tree.DataTypeParameter) TypeParameter(io.trino.sql.tree.TypeParameter) Parameter(io.trino.sql.tree.Parameter)

Example 8 with Parameter

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

the class AstBuilder method visitQueryNoWith.

@Override
public Node visitQueryNoWith(SqlBaseParser.QueryNoWithContext context) {
    QueryBody term = (QueryBody) visit(context.queryTerm());
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(getLocation(context.ORDER()), visit(context.sortItem(), SortItem.class)));
    }
    Optional<Offset> offset = Optional.empty();
    if (context.OFFSET() != null) {
        Expression rowCount;
        if (context.offset.INTEGER_VALUE() != null) {
            rowCount = new LongLiteral(getLocation(context.offset.INTEGER_VALUE()), context.offset.getText());
        } else {
            rowCount = new Parameter(getLocation(context.offset.QUESTION_MARK()), parameterPosition);
            parameterPosition++;
        }
        offset = Optional.of(new Offset(Optional.of(getLocation(context.OFFSET())), rowCount));
    }
    Optional<Node> limit = Optional.empty();
    if (context.FETCH() != null) {
        Optional<Expression> rowCount = Optional.empty();
        if (context.fetchFirst != null) {
            if (context.fetchFirst.INTEGER_VALUE() != null) {
                rowCount = Optional.of(new LongLiteral(getLocation(context.fetchFirst.INTEGER_VALUE()), context.fetchFirst.getText()));
            } else {
                rowCount = Optional.of(new Parameter(getLocation(context.fetchFirst.QUESTION_MARK()), parameterPosition));
                parameterPosition++;
            }
        }
        limit = Optional.of(new FetchFirst(Optional.of(getLocation(context.FETCH())), rowCount, context.TIES() != null));
    } else if (context.LIMIT() != null) {
        if (context.limit == null) {
            throw new IllegalStateException("Missing LIMIT value");
        }
        Expression rowCount;
        if (context.limit.ALL() != null) {
            rowCount = new AllRows(getLocation(context.limit.ALL()));
        } else if (context.limit.rowCount().INTEGER_VALUE() != null) {
            rowCount = new LongLiteral(getLocation(context.limit.rowCount().INTEGER_VALUE()), context.limit.getText());
        } else {
            rowCount = new Parameter(getLocation(context.limit.rowCount().QUESTION_MARK()), parameterPosition);
            parameterPosition++;
        }
        limit = Optional.of(new Limit(Optional.of(getLocation(context.LIMIT())), rowCount));
    }
    if (term instanceof QuerySpecification) {
        // When we have a simple query specification
        // followed by order by, offset, limit or fetch,
        // fold the order by, limit, offset or fetch clauses
        // into the query specification (analyzer/planner
        // expects this structure to resolve references with respect
        // to columns defined in the query specification)
        QuerySpecification query = (QuerySpecification) term;
        return new Query(getLocation(context), Optional.empty(), new QuerySpecification(getLocation(context), query.getSelect(), query.getFrom(), query.getWhere(), query.getGroupBy(), query.getHaving(), query.getWindows(), orderBy, offset, limit), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, limit);
}
Also used : OrderBy(io.trino.sql.tree.OrderBy) AllRows(io.trino.sql.tree.AllRows) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) LongLiteral(io.trino.sql.tree.LongLiteral) Node(io.trino.sql.tree.Node) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Offset(io.trino.sql.tree.Offset) QuerySpecification(io.trino.sql.tree.QuerySpecification) SortItem(io.trino.sql.tree.SortItem) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TryExpression(io.trino.sql.tree.TryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) NumericParameter(io.trino.sql.tree.NumericParameter) DataTypeParameter(io.trino.sql.tree.DataTypeParameter) TypeParameter(io.trino.sql.tree.TypeParameter) Parameter(io.trino.sql.tree.Parameter) Limit(io.trino.sql.tree.Limit) QueryBody(io.trino.sql.tree.QueryBody) FetchFirst(io.trino.sql.tree.FetchFirst)

Example 9 with Parameter

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

the class TestParameterExtractor method testLambda.

@Test
public void testLambda() {
    Statement statement = sqlParser.createStatement("SELECT * FROM test_table WHERE any_match(items, x -> x > ?)", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 58), 0));
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(1);
}
Also used : NodeLocation(io.trino.sql.tree.NodeLocation) ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) Parameter(io.trino.sql.tree.Parameter) Test(org.testng.annotations.Test)

Example 10 with Parameter

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

the class TableProceduresPropertyManager method getProperties.

public Map<String, Object> getProperties(CatalogName catalog, String procedureName, Map<String, Expression> sqlPropertyValues, Session session, PlannerContext plannerContext, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters) {
    Map<String, PropertyMetadata<?>> supportedProperties = connectorProperties.get(new Key(catalog, procedureName));
    if (supportedProperties == null) {
        throw new TrinoException(NOT_FOUND, format("Catalog '%s' table procedure '%s' property not found", catalog, procedureName));
    }
    Map<String, Optional<Object>> propertyValues = evaluateProperties(sqlPropertyValues.entrySet().stream().map(entry -> new Property(new Identifier(entry.getKey()), entry.getValue())).collect(toImmutableList()), session, plannerContext, accessControl, parameters, true, supportedProperties, INVALID_PROCEDURE_ARGUMENT, format("catalog '%s' table procedure '%s' property", catalog, procedureName));
    return propertyValues.entrySet().stream().filter(entry -> entry.getValue().isPresent()).collect(toImmutableMap(Entry::getKey, entry -> entry.getValue().orElseThrow()));
}
Also used : PropertyUtil.evaluateProperties(io.trino.metadata.PropertyUtil.evaluateProperties) ConcurrentMap(java.util.concurrent.ConcurrentMap) INVALID_PROCEDURE_ARGUMENT(io.trino.spi.StandardErrorCode.INVALID_PROCEDURE_ARGUMENT) CatalogName(io.trino.connector.CatalogName) NodeRef(io.trino.sql.tree.NodeRef) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Property(io.trino.sql.tree.Property) Identifier(io.trino.sql.tree.Identifier) NOT_FOUND(io.trino.spi.StandardErrorCode.NOT_FOUND) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) PropertyMetadata(io.trino.spi.session.PropertyMetadata) Maps(com.google.common.collect.Maps) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) AccessControl(io.trino.security.AccessControl) Parameter(io.trino.sql.tree.Parameter) Entry(java.util.Map.Entry) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) Session(io.trino.Session) PlannerContext(io.trino.sql.PlannerContext) Identifier(io.trino.sql.tree.Identifier) Optional(java.util.Optional) PropertyMetadata(io.trino.spi.session.PropertyMetadata) TrinoException(io.trino.spi.TrinoException) Property(io.trino.sql.tree.Property)

Aggregations

Parameter (io.trino.sql.tree.Parameter)12 Expression (io.trino.sql.tree.Expression)6 NodeRef (io.trino.sql.tree.NodeRef)5 Session (io.trino.Session)4 CatalogName (io.trino.connector.CatalogName)4 AccessControl (io.trino.security.AccessControl)4 PlannerContext (io.trino.sql.PlannerContext)4 List (java.util.List)4 Map (java.util.Map)4 Objects.requireNonNull (java.util.Objects.requireNonNull)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Futures.immediateVoidFuture (com.google.common.util.concurrent.Futures.immediateVoidFuture)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 WarningCollector (io.trino.execution.warnings.WarningCollector)3 MetadataUtil.createQualifiedObjectName (io.trino.metadata.MetadataUtil.createQualifiedObjectName)3 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)3 TrinoException (io.trino.spi.TrinoException)3 ParameterUtils.parameterExtractor (io.trino.sql.ParameterUtils.parameterExtractor)3 ParsingOptions (io.trino.sql.parser.ParsingOptions)3 NodeLocation (io.trino.sql.tree.NodeLocation)3