use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.
the class TestSqlParser method testRefreshMaterializedView.
@Test
public void testRefreshMaterializedView() {
assertStatement("REFRESH MATERIALIZED VIEW a WHERE p = 'x'", new RefreshMaterializedView(table(QualifiedName.of("a")), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new Identifier("p"), new StringLiteral("x"))));
assertStatement("REFRESH MATERIALIZED VIEW a.b WHERE p = 'x'", new RefreshMaterializedView(table(QualifiedName.of("a", "b")), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, new Identifier("p"), new StringLiteral("x"))));
}
use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.
the class TransformCorrelatedScalarSubquery method apply.
@Override
public Result apply(LateralJoinNode lateralJoinNode, Captures captures, Context context) {
PlanNode subquery = context.getLookup().resolve(lateralJoinNode.getSubquery());
if (!searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).matches()) {
return Result.empty();
}
PlanNode rewrittenSubquery = searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).removeFirst();
if (isAtMostScalar(rewrittenSubquery, context.getLookup())) {
return Result.ofPlanNode(new LateralJoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), rewrittenSubquery, lateralJoinNode.getCorrelation(), lateralJoinNode.getType(), lateralJoinNode.getOriginSubqueryError()));
}
VariableReferenceExpression unique = context.getVariableAllocator().newVariable("unique", BIGINT);
LateralJoinNode rewrittenLateralJoinNode = new LateralJoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), new AssignUniqueId(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), unique), rewrittenSubquery, lateralJoinNode.getCorrelation(), lateralJoinNode.getType(), lateralJoinNode.getOriginSubqueryError());
VariableReferenceExpression isDistinct = context.getVariableAllocator().newVariable("is_distinct", BooleanType.BOOLEAN);
MarkDistinctNode markDistinctNode = new MarkDistinctNode(rewrittenLateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), rewrittenLateralJoinNode, isDistinct, rewrittenLateralJoinNode.getInput().getOutputVariables(), Optional.empty());
FilterNode filterNode = new FilterNode(markDistinctNode.getSourceLocation(), context.getIdAllocator().getNextId(), markDistinctNode, castToRowExpression(new SimpleCaseExpression(createSymbolReference(isDistinct), ImmutableList.of(new WhenClause(TRUE_LITERAL, TRUE_LITERAL)), Optional.of(new Cast(new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(new LongLiteral(Integer.toString(SUBQUERY_MULTIPLE_ROWS.toErrorCode().getCode())), new StringLiteral("Scalar sub-query has returned multiple rows"))), BOOLEAN)))));
return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), filterNode, identityAssignmentsAsSymbolReferences(lateralJoinNode.getOutputVariables())));
}
use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.
the class TestHiveLogicalPlanner method testOptimizeMetadataQueries.
@Test
public void testOptimizeMetadataQueries() {
QueryRunner queryRunner = getQueryRunner();
Session optimizeMetadataQueries = Session.builder(this.getQueryRunner().getDefaultSession()).setSystemProperty(OPTIMIZE_METADATA_QUERIES, Boolean.toString(true)).setCatalogSessionProperty(HIVE_CATALOG, PUSHDOWN_FILTER_ENABLED, Boolean.toString(true)).build();
queryRunner.execute("CREATE TABLE test_optimize_metadata_queries WITH (partitioned_by = ARRAY['ds']) AS " + "SELECT orderkey, CAST(to_iso8601(date_add('DAY', orderkey % 7, date('2020-10-01'))) AS VARCHAR) AS ds FROM orders WHERE orderkey < 1000");
queryRunner.execute("CREATE TABLE test_optimize_metadata_queries_multiple_partition_columns WITH (partitioned_by = ARRAY['ds', 'value']) AS " + "SELECT orderkey, CAST(to_iso8601(date_add('DAY', orderkey % 7, date('2020-10-01'))) AS VARCHAR) AS ds, 1 AS value FROM orders WHERE orderkey < 1000");
try {
assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_optimize_metadata_queries", anyTree(values(ImmutableList.of("ds"), ImmutableList.of(ImmutableList.of(new StringLiteral("2020-10-01")), ImmutableList.of(new StringLiteral("2020-10-02")), ImmutableList.of(new StringLiteral("2020-10-03")), ImmutableList.of(new StringLiteral("2020-10-04")), ImmutableList.of(new StringLiteral("2020-10-05")), ImmutableList.of(new StringLiteral("2020-10-06")), ImmutableList.of(new StringLiteral("2020-10-07"))))));
assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_optimize_metadata_queries WHERE ds > '2020-10-04'", anyTree(values(ImmutableList.of("ds"), ImmutableList.of(ImmutableList.of(new StringLiteral("2020-10-05")), ImmutableList.of(new StringLiteral("2020-10-06")), ImmutableList.of(new StringLiteral("2020-10-07"))))));
assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_optimize_metadata_queries WHERE ds = '2020-10-04' AND orderkey > 200", anyTree(tableScan("test_optimize_metadata_queries", withColumnDomains(ImmutableMap.of("orderkey", Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 200L)), false))), TRUE_CONSTANT, ImmutableSet.of("orderkey"))));
assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_optimize_metadata_queries WHERE ds = '2020-10-04' AND orderkey > 200", anyTree(tableScan("test_optimize_metadata_queries", withColumnDomains(ImmutableMap.of("orderkey", Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 200L)), false))), TRUE_CONSTANT, ImmutableSet.of("orderkey"))));
assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_optimize_metadata_queries_multiple_partition_columns", anyTree(values(ImmutableList.of("ds"), ImmutableList.of(ImmutableList.of(new StringLiteral("2020-10-01")), ImmutableList.of(new StringLiteral("2020-10-02")), ImmutableList.of(new StringLiteral("2020-10-03")), ImmutableList.of(new StringLiteral("2020-10-04")), ImmutableList.of(new StringLiteral("2020-10-05")), ImmutableList.of(new StringLiteral("2020-10-06")), ImmutableList.of(new StringLiteral("2020-10-07"))))));
assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_optimize_metadata_queries_multiple_partition_columns WHERE ds > '2020-10-04'", anyTree(values(ImmutableList.of("ds"), ImmutableList.of(ImmutableList.of(new StringLiteral("2020-10-05")), ImmutableList.of(new StringLiteral("2020-10-06")), ImmutableList.of(new StringLiteral("2020-10-07"))))));
assertPlan(optimizeMetadataQueries, "SELECT DISTINCT ds FROM test_optimize_metadata_queries_multiple_partition_columns WHERE ds = '2020-10-04' AND orderkey > 200", anyTree(tableScan("test_optimize_metadata_queries_multiple_partition_columns", withColumnDomains(ImmutableMap.of("orderkey", Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 200L)), false))), TRUE_CONSTANT, ImmutableSet.of("orderkey"))));
assertPlan(optimizeMetadataQueries, "SELECT ds, MAX(value) FROM test_optimize_metadata_queries_multiple_partition_columns WHERE ds > '2020-10-04' GROUP BY ds", anyTree(values(ImmutableList.of("ds", "value"), ImmutableList.of(ImmutableList.of(new StringLiteral("2020-10-05"), new LongLiteral("1")), ImmutableList.of(new StringLiteral("2020-10-06"), new LongLiteral("1")), ImmutableList.of(new StringLiteral("2020-10-07"), new LongLiteral("1"))))));
assertPlan(optimizeMetadataQueries, "SELECT MAX(ds), MAX(value) FROM test_optimize_metadata_queries_multiple_partition_columns WHERE ds > '2020-10-04'", anyTree(project(ImmutableMap.of("max", expression("'2020-10-07'"), "max_2", expression("1")), any(values()))));
assertPlan(optimizeMetadataQueries, "SELECT MAX(value), MAX(ds) FROM test_optimize_metadata_queries_multiple_partition_columns WHERE ds > '2020-10-04'", anyTree(project(ImmutableMap.of("max", expression("1"), "max_2", expression("'2020-10-07'")), any(values()))));
} finally {
queryRunner.execute("DROP TABLE IF EXISTS test_optimize_metadata_queries");
queryRunner.execute("DROP TABLE IF EXISTS test_optimize_metadata_queries_multiple_partition_columns");
}
}
use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.
the class TestHiveLogicalPlanner method testMetadataAggregationFoldingWithEmptyPartitionsAndMetastoreThreshold.
@Test
public void testMetadataAggregationFoldingWithEmptyPartitionsAndMetastoreThreshold() {
QueryRunner queryRunner = getQueryRunner();
Session optimizeMetadataQueriesWithHighThreshold = Session.builder(this.getQueryRunner().getDefaultSession()).setSystemProperty(OPTIMIZE_METADATA_QUERIES, Boolean.toString(true)).setSystemProperty(OPTIMIZE_METADATA_QUERIES_CALL_THRESHOLD, Integer.toString(100)).build();
Session optimizeMetadataQueriesWithLowThreshold = Session.builder(this.getQueryRunner().getDefaultSession()).setSystemProperty(OPTIMIZE_METADATA_QUERIES, Boolean.toString(true)).setSystemProperty(OPTIMIZE_METADATA_QUERIES_CALL_THRESHOLD, Integer.toString(1)).build();
Session optimizeMetadataQueriesIgnoreStatsWithLowThreshold = Session.builder(this.getQueryRunner().getDefaultSession()).setSystemProperty(OPTIMIZE_METADATA_QUERIES_IGNORE_STATS, Boolean.toString(true)).setSystemProperty(OPTIMIZE_METADATA_QUERIES_CALL_THRESHOLD, Integer.toString(1)).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_threshold 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_with_threshold").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 {
// Test the non-reducible code path.
// Enable rewrite as all matching partitions have stats.
assertPlan(optimizeMetadataQueriesWithHighThreshold, "SELECT DISTINCT ds FROM test_metadata_aggregation_folding_with_empty_partitions_with_threshold 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"))))));
// All matching partitions have stats, Metastore threshold is reached, Disable rewrite
assertPlan(optimizeMetadataQueriesWithLowThreshold, "SELECT DISTINCT ds FROM test_metadata_aggregation_folding_with_empty_partitions_with_threshold WHERE ds BETWEEN '2020-07-01' AND '2020-07-03'", anyTree(tableScanWithConstraint("test_metadata_aggregation_folding_with_empty_partitions_with_threshold", ImmutableMap.of("ds", multipleValues(VARCHAR, utf8Slices("2020-07-01", "2020-07-02"))))));
// All matching partitions have stats, Metastore threshold is reached, but IgnoreStats will overwrite, Enable rewrite
assertPlan(optimizeMetadataQueriesIgnoreStatsWithLowThreshold, "SELECT DISTINCT ds FROM test_metadata_aggregation_folding_with_empty_partitions_with_threshold 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"))))));
} finally {
queryRunner.execute("DROP TABLE IF EXISTS test_metadata_aggregation_folding_with_empty_partitions_with_threshold");
}
}
use of com.facebook.presto.sql.tree.StringLiteral in project presto by prestodb.
the class ExpressionInterpreter method createFailureFunction.
private Expression createFailureFunction(RuntimeException exception, Type type) {
requireNonNull(exception, "Exception is null");
String failureInfo = JsonCodec.jsonCodec(FailureInfo.class).toJson(Failures.toFailure(exception).toFailureInfo());
if (exception instanceof PrestoException) {
long errorCode = ((PrestoException) exception).getErrorCode().getCode();
FunctionCall jsonParse = new FunctionCall(QualifiedName.of("json_parse"), ImmutableList.of(new StringLiteral(failureInfo)));
FunctionCall failureFunction = new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(literalEncoder.toExpression(errorCode, INTEGER), jsonParse));
return new Cast(failureFunction, type.getTypeSignature().toString());
}
FunctionCall jsonParse = new FunctionCall(QualifiedName.of("json_parse"), ImmutableList.of(new StringLiteral(failureInfo)));
FunctionCall failureFunction = new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(jsonParse));
return new Cast(failureFunction, type.getTypeSignature().toString());
}
Aggregations