Search in sources :

Example 26 with FunctionCall

use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.

the class AstBuilder method visitNormalize.

@Override
public Node visitNormalize(SqlBaseParser.NormalizeContext context) {
    Expression str = (Expression) visit(context.valueExpression());
    String normalForm = Optional.ofNullable(context.normalForm()).map(ParserRuleContext::getText).orElse("NFC");
    return new FunctionCall(getLocation(context), QualifiedName.of("normalize"), ImmutableList.of(str, new StringLiteral(getLocation(context), normalForm)));
}
Also used : StringLiteral(io.prestosql.sql.tree.StringLiteral) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) TryExpression(io.prestosql.sql.tree.TryExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall)

Example 27 with FunctionCall

use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.

the class AstBuilder method visitCreateCube.

@Override
public Node visitCreateCube(SqlBaseParser.CreateCubeContext context) {
    if (context.cubeProperties() == null) {
        throw new IllegalArgumentException("Missing properties: AGGREGATIONS, GROUP");
    }
    QualifiedName cubeName = getQualifiedName(context.cubeName);
    QualifiedName sourceTableName = getQualifiedName(context.tableName);
    List<Identifier> groupingSet = ImmutableList.of();
    List<FunctionCall> aggregations = ImmutableList.of();
    List<Property> properties = new ArrayList<>();
    Optional<Expression> optionalExpression = visitIfPresent(context.expression(), Expression.class);
    boolean cubeGroupProvided = false;
    boolean aggregationsProvided = false;
    boolean sourceFilterProvided = false;
    Optional<Expression> sourceFilterPredicate = Optional.empty();
    for (SqlBaseParser.CubePropertyContext propertyContext : context.cubeProperties().cubeProperty()) {
        if (propertyContext.cubeGroup() != null) {
            if (cubeGroupProvided) {
                throw new IllegalArgumentException("Duplicate property: GROUP");
            }
            groupingSet = visit(propertyContext.cubeGroup().identifier(), Identifier.class);
            cubeGroupProvided = true;
        } else if (propertyContext.aggregations() != null) {
            if (aggregationsProvided) {
                throw new IllegalArgumentException("Duplicate property: AGGREGATIONS");
            }
            aggregations = visit(propertyContext.aggregations().expression(), FunctionCall.class);
            aggregationsProvided = true;
        } else if (propertyContext.sourceFilter() != null) {
            if (sourceFilterProvided) {
                throw new IllegalArgumentException("Duplicate Property: FILTER");
            }
            sourceFilterPredicate = visitIfPresent(propertyContext.sourceFilter().expression(), Expression.class);
            sourceFilterProvided = true;
        } else if (propertyContext.property() != null) {
            properties.add((Property) visitProperty(propertyContext.property()));
        }
    }
    if (!cubeGroupProvided) {
        throw new IllegalArgumentException("Missing property: GROUP");
    }
    if (!aggregationsProvided) {
        throw new IllegalArgumentException("Missing property: AGGREGATIONS");
    }
    List<Identifier> decomposedGroupingSet = new ArrayList<>();
    groupingSet.forEach(groupItem -> {
        decomposedGroupingSet.add(new Identifier(groupItem.getLocation().get(), groupItem.getValue().toLowerCase(Locale.ENGLISH), groupItem.isDelimited()));
    });
    Set<FunctionCall> decomposedAggregations = new LinkedHashSet<>();
    aggregations.forEach(aggItem -> {
        List<Expression> listArguments = aggItem.getArguments();
        List<Expression> newArguments = new ArrayList<>();
        for (Expression argument : listArguments) {
            if (argument instanceof Identifier) {
                newArguments.add(new Identifier(argument.getLocation().get(), ((Identifier) argument).getValue().toLowerCase(Locale.ENGLISH), ((Identifier) argument).isDelimited()));
            } else {
                newArguments.add(argument);
            }
        }
        if (!"avg".equals(aggItem.getName().toString())) {
            decomposedAggregations.add(new FunctionCall(aggItem.getLocation(), aggItem.getName(), aggItem.getWindow(), aggItem.getFilter(), aggItem.getOrderBy(), aggItem.isDistinct(), newArguments));
        } else {
            decomposedAggregations.add(aggItem);
            decomposedAggregations.add(new FunctionCall(aggItem.getLocation(), QualifiedName.of("sum"), aggItem.getWindow(), aggItem.getFilter(), aggItem.getOrderBy(), aggItem.isDistinct(), newArguments));
            decomposedAggregations.add(new FunctionCall(aggItem.getLocation(), QualifiedName.of("count"), aggItem.getWindow(), aggItem.getFilter(), aggItem.getOrderBy(), aggItem.isDistinct(), newArguments));
        }
    });
    return new CreateCube(getLocation(context), cubeName, sourceTableName, decomposedGroupingSet, decomposedAggregations, context.EXISTS() != null, properties, optionalExpression, sourceFilterPredicate.orElse(null));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QualifiedName(io.prestosql.sql.tree.QualifiedName) ArrayList(java.util.ArrayList) Identifier(io.prestosql.sql.tree.Identifier) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) TryExpression(io.prestosql.sql.tree.TryExpression) CreateCube(io.prestosql.sql.tree.CreateCube) FunctionCall(io.prestosql.sql.tree.FunctionCall) Property(io.prestosql.sql.tree.Property) FunctionProperty(io.prestosql.sql.tree.FunctionProperty)

Example 28 with FunctionCall

use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.

the class TestSetSessionTask method testSetSessionWithParameters.

@Test
public void testSetSessionWithParameters() {
    FunctionCall functionCall = new FunctionCallBuilder(metadata).setName(QualifiedName.of("concat")).addArgument(VARCHAR, new StringLiteral("ban")).addArgument(VARCHAR, new Parameter(0)).build();
    testSetSessionWithParameters("bar", functionCall, "banana", ImmutableList.of(new StringLiteral("ana")));
}
Also used : StringLiteral(io.prestosql.sql.tree.StringLiteral) Parameter(io.prestosql.sql.tree.Parameter) FunctionCall(io.prestosql.sql.tree.FunctionCall) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder) Test(org.testng.annotations.Test)

Example 29 with FunctionCall

use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.

the class TestEffectivePredicateExtractor method testValues.

@Test
public void testValues() {
    TypeProvider types = TypeProvider.copyOf(ImmutableMap.<Symbol, Type>builder().put(A, BIGINT).put(B, BIGINT).build());
    // one column
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)))), types, typeAnalyzer), new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))));
    // one column with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), or(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new IsNullPredicate(AE)));
    // all nulls
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), new IsNullPredicate(AE));
    // many rows
    List<List<RowExpression>> rows = IntStream.range(0, 500).mapToObj(TestEffectivePredicateExtractor::bigintLiteralRowExpression).map(ImmutableList::of).collect(toImmutableList());
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), rows), types, typeAnalyzer), new BetweenPredicate(AE, bigintLiteral(0), bigintLiteral(499)));
    // multiple columns
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), bigintLiteralRowExpression(100)), ImmutableList.of(bigintLiteralRowExpression(2), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new InPredicate(BE, new InListExpression(ImmutableList.of(bigintLiteral(100), bigintLiteral(200))))));
    // multiple columns with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString())), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(or(new ComparisonExpression(EQUAL, AE, bigintLiteral(1)), new IsNullPredicate(AE)), or(new ComparisonExpression(EQUAL, BE, bigintLiteral(200)), new IsNullPredicate(BE))));
    // non-deterministic
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()))))), types, typeAnalyzer), new ComparisonExpression(EQUAL, AE, bigintLiteral(1)));
    // non-constant
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(castToRowExpression(BE)))), types, typeAnalyzer), TRUE_LITERAL);
}
Also used : Cast(io.prestosql.sql.tree.Cast) ValuesNode(io.prestosql.spi.plan.ValuesNode) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) Symbol(io.prestosql.spi.plan.Symbol) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) NullLiteral(io.prestosql.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Example 30 with FunctionCall

use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.

the class TestExpressionDomainTranslator method testExpressionConstantFolding.

@Test
public void testExpressionConstantFolding() {
    FunctionCall fromHex = new FunctionCallBuilder(metadata).setName(QualifiedName.of("from_hex")).addArgument(VARCHAR, stringLiteral("123456")).build();
    Expression originalExpression = comparison(GREATER_THAN, toSymbolReference(C_VARBINARY), fromHex);
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456"));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false))));
    Expression expression = toPredicate(result.getTupleDomain());
    assertEquals(expression, comparison(GREATER_THAN, toSymbolReference(C_VARBINARY), varbinaryLiteral(value)));
}
Also used : InListExpression(io.prestosql.sql.tree.InListExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) FunctionCall(io.prestosql.sql.tree.FunctionCall) ExtractionResult(io.prestosql.sql.planner.ExpressionDomainTranslator.ExtractionResult) Test(org.testng.annotations.Test)

Aggregations

FunctionCall (io.prestosql.sql.tree.FunctionCall)33 StringLiteral (io.prestosql.sql.tree.StringLiteral)15 Test (org.testng.annotations.Test)15 Expression (io.prestosql.sql.tree.Expression)14 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)13 Identifier (io.prestosql.sql.tree.Identifier)13 LongLiteral (io.prestosql.sql.tree.LongLiteral)11 QualifiedName (io.prestosql.sql.tree.QualifiedName)10 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)9 IfExpression (io.prestosql.sql.tree.IfExpression)8 InListExpression (io.prestosql.sql.tree.InListExpression)8 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)8 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)8 QuerySpecification (io.prestosql.sql.tree.QuerySpecification)8 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)7 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)7 NotExpression (io.prestosql.sql.tree.NotExpression)7 OrderBy (io.prestosql.sql.tree.OrderBy)7 SortItem (io.prestosql.sql.tree.SortItem)7 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)7