Search in sources :

Example 31 with StringLiteral

use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.

the class TestSqlParser method testSubstringRegisteredFunction.

@Test
public void testSubstringRegisteredFunction() {
    final String givenString = "ABCDEF";
    assertStatement(format("SELECT substring('%s', 2)", givenString), new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement(format("SELECT substring('%s', 2, 3)", givenString), new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2"), new LongLiteral("3")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 32 with StringLiteral

use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.

the class TestSqlParser method testLimitAll.

@Test
public void testLimitAll() {
    Query valuesQuery = query(values(row(new LongLiteral("1"), new StringLiteral("1")), row(new LongLiteral("2"), new StringLiteral("2"))));
    assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) LIMIT ALL", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of("ALL")));
}
Also used : Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) AllColumns(com.facebook.presto.sql.tree.AllColumns) Test(org.testng.annotations.Test)

Example 33 with StringLiteral

use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.

the class TestSqlParser method testValues.

@Test
public void testValues() {
    Query valuesQuery = query(values(row(new StringLiteral("a"), new LongLiteral("1"), new DoubleLiteral("2.2")), row(new StringLiteral("b"), new LongLiteral("2"), new DoubleLiteral("3.3"))));
    assertStatement("VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0)", valuesQuery);
    assertStatement("SELECT * FROM (VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0))", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery)));
}
Also used : Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) AllColumns(com.facebook.presto.sql.tree.AllColumns) Test(org.testng.annotations.Test)

Example 34 with StringLiteral

use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.

the class RowExpressionVerifier method visitCast.

@Override
protected Boolean visitCast(Cast expected, RowExpression actual) {
    // TODO: clean up cast path
    if (actual instanceof ConstantExpression && expected.getExpression() instanceof Literal && expected.getType().equals(actual.getType().toString())) {
        Literal literal = (Literal) expected.getExpression();
        if (literal instanceof StringLiteral) {
            Object value = LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actual);
            String actualString = value instanceof Slice ? ((Slice) value).toStringUtf8() : String.valueOf(value);
            return ((StringLiteral) literal).getValue().equals(actualString);
        }
        return getValueFromLiteral(literal).equals(String.valueOf(LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actual)));
    }
    if (actual instanceof VariableReferenceExpression && expected.getExpression() instanceof SymbolReference && expected.getType().equals(actual.getType().toString())) {
        return visitSymbolReference((SymbolReference) expected.getExpression(), actual);
    }
    if (!(actual instanceof CallExpression) || !functionResolution.isCastFunction(((CallExpression) actual).getFunctionHandle())) {
        return false;
    }
    if (!expected.getType().equalsIgnoreCase(actual.getType().toString()) && !(expected.getType().toLowerCase(ENGLISH).equals(VARCHAR) && actual.getType().getTypeSignature().getBase().equals(VARCHAR))) {
        return false;
    }
    return process(expected.getExpression(), ((CallExpression) actual).getArguments().get(0));
}
Also used : StringLiteral(com.facebook.presto.sql.tree.StringLiteral) Slice(io.airlift.slice.Slice) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) Literal(com.facebook.presto.sql.tree.Literal) DecimalLiteral(com.facebook.presto.sql.tree.DecimalLiteral) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 35 with StringLiteral

use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.

the class TestHiveLogicalPlanner method testMetadataAggregationFoldingWithEmptyPartitions.

@Test
public void testMetadataAggregationFoldingWithEmptyPartitions() {
    QueryRunner queryRunner = getQueryRunner();
    Session optimizeMetadataQueries = Session.builder(this.getQueryRunner().getDefaultSession()).setSystemProperty(OPTIMIZE_METADATA_QUERIES, Boolean.toString(true)).build();
    Session optimizeMetadataQueriesIgnoreStats = Session.builder(this.getQueryRunner().getDefaultSession()).setSystemProperty(OPTIMIZE_METADATA_QUERIES_IGNORE_STATS, Boolean.toString(true)).build();
    Session shufflePartitionColumns = Session.builder(this.getQueryRunner().getDefaultSession()).setCatalogSessionProperty(HIVE_CATALOG, SHUFFLE_PARTITIONED_COLUMNS_FOR_TABLE_WRITE, Boolean.toString(true)).build();
    queryRunner.execute(shufflePartitionColumns, "CREATE TABLE test_metadata_aggregation_folding_with_empty_partitions WITH (partitioned_by = ARRAY['ds']) AS " + "SELECT orderkey, CAST(to_iso8601(date_add('DAY', orderkey % 2, date('2020-07-01'))) AS VARCHAR) AS ds FROM orders WHERE orderkey < 1000");
    ExtendedHiveMetastore metastore = replicateHiveMetastore((DistributedQueryRunner) queryRunner);
    MetastoreContext metastoreContext = new MetastoreContext(getSession().getUser(), getSession().getQueryId().getId(), Optional.empty(), Optional.empty(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
    Table table = metastore.getTable(metastoreContext, getSession().getSchema().get(), "test_metadata_aggregation_folding_with_empty_partitions").get();
    // Add one partition with no statistics.
    String partitionNameNoStats = "ds=2020-07-20";
    Partition partitionNoStats = createDummyPartition(table, partitionNameNoStats);
    metastore.addPartitions(metastoreContext, table.getDatabaseName(), table.getTableName(), ImmutableList.of(new PartitionWithStatistics(partitionNoStats, partitionNameNoStats, PartitionStatistics.empty())));
    // Add one partition with statistics indicating that it has no rows.
    String emptyPartitionName = "ds=2020-06-30";
    Partition emptyPartition = createDummyPartition(table, emptyPartitionName);
    metastore.addPartitions(metastoreContext, table.getDatabaseName(), table.getTableName(), ImmutableList.of(new PartitionWithStatistics(emptyPartition, emptyPartitionName, PartitionStatistics.builder().setBasicStatistics(new HiveBasicStatistics(1, 0, 0, 0)).build())));
    try {
        // Max ds doesn't have stats. Disable rewrite.
        assertPlan(optimizeMetadataQueries, "SELECT * FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds = (SELECT max(ds) from test_metadata_aggregation_folding_with_empty_partitions)", anyTree(anyTree(PlanMatchPattern.tableScan("test_metadata_aggregation_folding_with_empty_partitions")), anyTree(PlanMatchPattern.tableScan("test_metadata_aggregation_folding_with_empty_partitions"))));
        // Ignore metastore stats. Enable rewrite.
        assertPlan(optimizeMetadataQueriesIgnoreStats, "SELECT * FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds = (SELECT max(ds) from test_metadata_aggregation_folding_with_empty_partitions)", anyTree(join(INNER, ImmutableList.of(), tableScan("test_metadata_aggregation_folding_with_empty_partitions", getSingleValueColumnDomain("ds", "2020-07-20"), TRUE_CONSTANT, ImmutableSet.of("ds")), anyTree(any()))));
        // Max ds matching the filter has stats. Enable rewrite.
        assertPlan(optimizeMetadataQueries, "SELECT * FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds = (SELECT max(ds) from test_metadata_aggregation_folding_with_empty_partitions WHERE ds <= '2020-07-02')", anyTree(join(INNER, ImmutableList.of(), tableScan("test_metadata_aggregation_folding_with_empty_partitions", getSingleValueColumnDomain("ds", "2020-07-02"), TRUE_CONSTANT, ImmutableSet.of("ds")), anyTree(any()))));
        // Min ds partition stats indicates that it is an empty partition. Disable rewrite.
        assertPlan(optimizeMetadataQueries, "SELECT * FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds = (SELECT min(ds) from test_metadata_aggregation_folding_with_empty_partitions)", anyTree(anyTree(PlanMatchPattern.tableScan("test_metadata_aggregation_folding_with_empty_partitions")), anyTree(PlanMatchPattern.tableScan("test_metadata_aggregation_folding_with_empty_partitions"))));
        // Min ds partition matching the filter has non-empty stats. Enable rewrite.
        assertPlan(optimizeMetadataQueries, "SELECT * FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds = (SELECT min(ds) from test_metadata_aggregation_folding_with_empty_partitions WHERE ds >= '2020-07-01')", anyTree(join(INNER, ImmutableList.of(), tableScan("test_metadata_aggregation_folding_with_empty_partitions", getSingleValueColumnDomain("ds", "2020-07-01"), TRUE_CONSTANT, ImmutableSet.of("ds")), anyTree(any()))));
        // Test the non-reducible code path.
        // Disable rewrite as there are partitions with empty stats.
        assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_metadata_aggregation_folding_with_empty_partitions", anyTree(tableScanWithConstraint("test_metadata_aggregation_folding_with_empty_partitions", ImmutableMap.of("ds", multipleValues(VARCHAR, utf8Slices("2020-06-30", "2020-07-01", "2020-07-02", "2020-07-20"))))));
        // Enable rewrite as all matching partitions have stats.
        assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds BETWEEN '2020-07-01' AND '2020-07-03'", anyTree(values(ImmutableList.of("ds"), ImmutableList.of(ImmutableList.of(new StringLiteral("2020-07-01")), ImmutableList.of(new StringLiteral("2020-07-02"))))));
        // One of two resulting partitions doesn't have stats. Disable rewrite.
        assertPlan(optimizeMetadataQueries, "SELECT MIN(ds), MAX(ds) FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds BETWEEN '2020-06-30' AND '2020-07-03'", anyTree(tableScanWithConstraint("test_metadata_aggregation_folding_with_empty_partitions", ImmutableMap.of("ds", multipleValues(VARCHAR, utf8Slices("2020-06-30", "2020-07-01", "2020-07-02"))))));
        // Ignore metadata stats. Always enable rewrite.
        assertPlan(optimizeMetadataQueriesIgnoreStats, "SELECT MIN(ds), MAX(ds) FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds BETWEEN '2020-06-30' AND '2020-07-03'", anyTree(project(ImmutableMap.of("min", expression("'2020-06-30'"), "max", expression("'2020-07-02'")), anyTree(values()))));
        // Both resulting partitions have stats. Enable rewrite.
        assertPlan(optimizeMetadataQueries, "SELECT MIN(ds), MAX(ds) FROM test_metadata_aggregation_folding_with_empty_partitions WHERE ds BETWEEN '2020-07-01' AND '2020-07-03'", anyTree(project(ImmutableMap.of("min", expression("'2020-07-01'"), "max", expression("'2020-07-02'")), anyTree(values()))));
    } finally {
        queryRunner.execute("DROP TABLE IF EXISTS test_metadata_aggregation_folding_with_empty_partitions");
    }
}
Also used : Partition(com.facebook.presto.hive.metastore.Partition) Table(com.facebook.presto.hive.metastore.Table) PartitionWithStatistics(com.facebook.presto.hive.metastore.PartitionWithStatistics) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) ExtendedHiveMetastore(com.facebook.presto.hive.metastore.ExtendedHiveMetastore) QueryRunner(com.facebook.presto.testing.QueryRunner) DistributedQueryRunner(com.facebook.presto.tests.DistributedQueryRunner) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Aggregations

StringLiteral (com.facebook.presto.sql.tree.StringLiteral)35 Test (org.testng.annotations.Test)25 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)19 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)15 DoubleLiteral (com.facebook.presto.sql.tree.DoubleLiteral)9 Query (com.facebook.presto.sql.tree.Query)8 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)7 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)7 Expression (com.facebook.presto.sql.tree.Expression)7 Identifier (com.facebook.presto.sql.tree.Identifier)7 WithQuery (com.facebook.presto.sql.tree.WithQuery)7 AllColumns (com.facebook.presto.sql.tree.AllColumns)6 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)6 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)5 BooleanLiteral (com.facebook.presto.sql.tree.BooleanLiteral)5 GenericLiteral (com.facebook.presto.sql.tree.GenericLiteral)5 Property (com.facebook.presto.sql.tree.Property)5 Session (com.facebook.presto.Session)4 ArrayConstructor (com.facebook.presto.sql.tree.ArrayConstructor)4 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)4