Search in sources :

Example 26 with QualifiedName

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

the class TestSqlParser method testInsertInto.

@Test
public void testInsertInto() {
    QualifiedName table = QualifiedName.of("a");
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    assertStatement("INSERT INTO a SELECT * FROM t", new Insert(table, Optional.empty(), query));
    assertStatement("INSERT INTO a (c1, c2) SELECT * FROM t", new Insert(table, Optional.of(ImmutableList.of(identifier("c1"), identifier("c2"))), query));
}
Also used : QueryUtil.simpleQuery(io.prestosql.sql.QueryUtil.simpleQuery) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) QualifiedName(io.prestosql.sql.tree.QualifiedName) AllColumns(io.prestosql.sql.tree.AllColumns) Insert(io.prestosql.sql.tree.Insert) Test(org.testng.annotations.Test)

Example 27 with QualifiedName

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

the class AstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(SqlBaseParser.FunctionCallContext context) {
    Optional<Expression> filter = visitIfPresent(context.filter(), Expression.class);
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(visit(context.sortItem(), SortItem.class)));
    }
    QualifiedName name = getQualifiedName(context.qualifiedName());
    boolean distinct = isDistinct(context.setQuantifier());
    boolean ignoreNulls = context.nullTreatment() != null && context.nullTreatment().IGNORE() != null;
    if (name.toString().equalsIgnoreCase("if")) {
        check(context.expression().size() == 2 || context.expression().size() == 3, "Invalid number of arguments for 'if' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'if' function", context);
        check(!distinct, "DISTINCT not valid for 'if' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'if' function", context);
        Expression elseExpression = null;
        if (context.expression().size() == 3) {
            elseExpression = (Expression) visit(context.expression(2));
        }
        return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
    }
    if (name.toString().equalsIgnoreCase("nullif")) {
        check(context.expression().size() == 2, "Invalid number of arguments for 'nullif' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'nullif' function", context);
        check(!distinct, "DISTINCT not valid for 'nullif' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'nullif' function", context);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    if (name.toString().equalsIgnoreCase("coalesce")) {
        check(context.expression().size() >= 2, "The 'coalesce' function must have at least two arguments", context);
        check(!window.isPresent(), "OVER clause not valid for 'coalesce' function", context);
        check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'coalesce' function", context);
        return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
    }
    if (name.toString().equalsIgnoreCase("try")) {
        check(context.expression().size() == 1, "The 'try' function must have exactly one argument", context);
        check(!window.isPresent(), "OVER clause not valid for 'try' function", context);
        check(!distinct, "DISTINCT not valid for 'try' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'try' function", context);
        return new TryExpression(getLocation(context), (Expression) visit(getOnlyElement(context.expression())));
    }
    if (name.toString().equalsIgnoreCase("format")) {
        check(context.expression().size() >= 2, "The 'format' function must have at least two arguments", context);
        check(!window.isPresent(), "OVER clause not valid for 'format' function", context);
        check(!distinct, "DISTINCT not valid for 'format' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'format' function", context);
        return new Format(getLocation(context), visit(context.expression(), Expression.class));
    }
    if (name.toString().equalsIgnoreCase("$internal$bind")) {
        check(context.expression().size() >= 1, "The '$internal$bind' function must have at least one arguments", context);
        check(!window.isPresent(), "OVER clause not valid for '$internal$bind' function", context);
        check(!distinct, "DISTINCT not valid for '$internal$bind' function", context);
        check(!filter.isPresent(), "FILTER not valid for '$internal$bind' function", context);
        int numValues = context.expression().size() - 1;
        List<Expression> arguments = context.expression().stream().map(this::visit).map(Expression.class::cast).collect(toImmutableList());
        return new BindExpression(getLocation(context), arguments.subList(0, numValues), arguments.get(numValues));
    }
    return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, ignoreNulls, visit(context.expression(), Expression.class));
}
Also used : Window(io.prestosql.sql.tree.Window) OrderBy(io.prestosql.sql.tree.OrderBy) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) IfExpression(io.prestosql.sql.tree.IfExpression) QualifiedName(io.prestosql.sql.tree.QualifiedName) TryExpression(io.prestosql.sql.tree.TryExpression) SortItem(io.prestosql.sql.tree.SortItem) ExplainFormat(io.prestosql.sql.tree.ExplainFormat) Format(io.prestosql.sql.tree.Format) 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) BindExpression(io.prestosql.sql.tree.BindExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Example 28 with QualifiedName

use of io.prestosql.sql.tree.QualifiedName 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 29 with QualifiedName

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

the class TestDropCacheTask method testDropCacheNotExistsTrue.

@Test
public void testDropCacheNotExistsTrue() {
    QualifiedName tableName = QualifiedName.of(CATALOG_NAME, schema, table);
    DropCache statement = new DropCache(tableName, true);
    getFutureValue(new DropCacheTask().execute(statement, createTestTransactionManager(), metadata, new AllowAllAccessControl(), stateMachine, Collections.emptyList(), new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
    assertFalse(SplitCacheMap.getInstance().cacheExists(tableName));
    QualifiedName table2Name = QualifiedName.of(CATALOG_NAME, schema, table2);
    DropCache statement2 = new DropCache(table2Name, true);
    assertTrue(SplitCacheMap.getInstance().cacheExists(table2Name));
    getFutureValue(new DropCacheTask().execute(statement2, createTestTransactionManager(), metadata, new AllowAllAccessControl(), stateMachine, Collections.emptyList(), new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
    assertFalse(SplitCacheMap.getInstance().cacheExists(table2Name));
}
Also used : DropCache(io.prestosql.sql.tree.DropCache) AllowAllAccessControl(io.prestosql.security.AllowAllAccessControl) QualifiedName(io.prestosql.sql.tree.QualifiedName) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) HetuMetaStoreManager(io.prestosql.metastore.HetuMetaStoreManager) FileSystemClientManager(io.prestosql.filesystem.FileSystemClientManager) Test(org.testng.annotations.Test)

Example 30 with QualifiedName

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

the class TestSetSessionTask method testSetSessionWithParameters.

private void testSetSessionWithParameters(String property, Expression expression, String expectedValue, List<Expression> parameters) {
    QualifiedName qualifiedPropName = QualifiedName.of(CATALOG_NAME, property);
    QueryStateMachine stateMachine = QueryStateMachine.begin(format("set %s = 'old_value'", qualifiedPropName), Optional.empty(), TEST_SESSION, URI.create("fake://uri"), new ResourceGroupId("test"), new NoOpResourceGroupManager(), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP);
    getFutureValue(new SetSessionTask().execute(new SetSession(qualifiedPropName, expression), transactionManager, metadata, accessControl, stateMachine, parameters, new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
    Map<String, String> sessionProperties = stateMachine.getSetSessionProperties();
    assertEquals(sessionProperties, ImmutableMap.of(qualifiedPropName.toString(), expectedValue));
}
Also used : ResourceGroupId(io.prestosql.spi.resourcegroups.ResourceGroupId) SetSession(io.prestosql.sql.tree.SetSession) QualifiedName(io.prestosql.sql.tree.QualifiedName) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) HetuMetaStoreManager(io.prestosql.metastore.HetuMetaStoreManager) NoOpResourceGroupManager(io.prestosql.execution.resourcegroups.NoOpResourceGroupManager) FileSystemClientManager(io.prestosql.filesystem.FileSystemClientManager)

Aggregations

QualifiedName (io.prestosql.sql.tree.QualifiedName)30 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)13 Expression (io.prestosql.sql.tree.Expression)11 Test (org.testng.annotations.Test)10 Identifier (io.prestosql.sql.tree.Identifier)9 FunctionCall (io.prestosql.sql.tree.FunctionCall)8 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)8 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)7 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)7 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)7 LongLiteral (io.prestosql.sql.tree.LongLiteral)7 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)7 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)7 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)6 IfExpression (io.prestosql.sql.tree.IfExpression)6 InListExpression (io.prestosql.sql.tree.InListExpression)6 NotExpression (io.prestosql.sql.tree.NotExpression)6 NullIfExpression (io.prestosql.sql.tree.NullIfExpression)6 SearchedCaseExpression (io.prestosql.sql.tree.SearchedCaseExpression)6 SimpleCaseExpression (io.prestosql.sql.tree.SimpleCaseExpression)6