Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analyze properties system table and documentation for ANALYZE statement #12409

Merged
merged 2 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions presto-docs/src/main/sphinx/connector/hive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,27 @@ Column Type Collectible Statistics
Automatic column level statistics collection on write is controlled by
the ``collect-column-statistics-on-write`` catalog session property.

.. _hive_analyze:

Collecting table and column statistics
--------------------------------------

The Hive connector supports collection of table and partition statistics
via the :doc:`/sql/analyze` statement. When analyzing a partitioned table,
the partitions to analyze can be specified via the optional ``partitions``
property, which is an array containing the values of the partition keys
in the order they are declared in the table schema::

ANALYZE hive.sales WITH (
partitions = ARRAY[
ARRAY['partition1_value1', 'partition1_value2'],
ARRAY['partition2_value1', 'partition2_value2']]);

This query will collect statistics for 2 partitions with keys:

* ``partition1_value1, partition1_value2``
* ``partition2_value1, partition2_value2``

Schema Evolution
----------------

Expand Down
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This chapter describes the SQL syntax used in Presto.

sql/alter-schema
sql/alter-table
sql/analyze
sql/call
sql/commit
sql/create-role
Expand Down
43 changes: 43 additions & 0 deletions presto-docs/src/main/sphinx/sql/analyze.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
=======
ANALYZE
=======

Synopsis
--------

.. code-block:: none

ANALYZE table_name [ WITH ( property_name = expression [, ...] ) ]

Description
-----------

Collects table and column statistics for a given table.

The optional ``WITH`` clause can be used to provide
connector-specific properties. To list all available properties, run the following query::

SELECT * FROM system.metadata.analyze_properties

Currently, this statement is only supported by the
:ref:`Hive connector <hive_analyze>`.

Examples
--------

Analyze table ``web`` to collect table and column statistics::

ANALYZE web;

Analyze table ``stores`` in catalog ``hive`` and schema ``default``::

ANALYZE hive.default.stores;

Analyze partitions ``'1992-01-01', '1992-01-02'`` from a Hive partitioned table ``sales``::

ANALYZE hive.default.sales WITH (partitions = ARRAY[ARRAY['1992-01-01'], ARRAY['1992-01-02']]);

Analyze partitions with complex partition key (``state`` and ``city`` columns) from a Hive partitioned table ``customers``::

ANALYZE hive.default.customers WITH (partitions = ARRAY[ARRAY['CA', 'San Francisco'], ARRAY['NY', 'NY']]);

Original file line number Diff line number Diff line change
Expand Up @@ -3083,6 +3083,13 @@ public void testCollectColumnStatisticsOnInsert()
assertUpdate(format("DROP TABLE %s", tableName));
}

@Test
public void testAnalyzePropertiesSystemTable()
{
assertQuery("SELECT * FROM system.metadata.analyze_properties WHERE catalog_name = 'hive'",
"SELECT 'hive', 'partitions', '', 'array(array(varchar))', 'Partitions to be analyzed'");
}

@Test
public void testAnalyzeEmptyTable()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.connector.system;

import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.transaction.TransactionManager;

import javax.inject.Inject;

public class AnalyzePropertiesSystemTable
extends AbstractPropertiesSystemTable
{
@Inject
public AnalyzePropertiesSystemTable(TransactionManager transactionManager, Metadata metadata)
{
super("analyze_properties", transactionManager, () -> metadata.getAnalyzePropertyManager().getAllProperties());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void configure(Binder binder)
globalTableBinder.addBinding().to(SchemaPropertiesSystemTable.class).in(Scopes.SINGLETON);
globalTableBinder.addBinding().to(TablePropertiesSystemTable.class).in(Scopes.SINGLETON);
globalTableBinder.addBinding().to(ColumnPropertiesSystemTable.class).in(Scopes.SINGLETON);
globalTableBinder.addBinding().to(AnalyzePropertiesSystemTable.class).in(Scopes.SINGLETON);
globalTableBinder.addBinding().to(TransactionsSystemTable.class).in(Scopes.SINGLETON);

globalTableBinder.addBinding().to(AttributeJdbcTable.class).in(Scopes.SINGLETON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.facebook.presto.block.BlockEncodingManager;
import com.facebook.presto.connector.ConnectorId;
import com.facebook.presto.connector.ConnectorManager;
import com.facebook.presto.connector.system.AnalyzePropertiesSystemTable;
import com.facebook.presto.connector.system.CatalogSystemTable;
import com.facebook.presto.connector.system.ColumnPropertiesSystemTable;
import com.facebook.presto.connector.system.GlobalSystemConnector;
Expand Down Expand Up @@ -345,6 +346,7 @@ private LocalQueryRunner(Session defaultSession, FeaturesConfig featuresConfig,
new SchemaPropertiesSystemTable(transactionManager, metadata),
new TablePropertiesSystemTable(transactionManager, metadata),
new ColumnPropertiesSystemTable(transactionManager, metadata),
new AnalyzePropertiesSystemTable(transactionManager, metadata),
new TransactionsSystemTable(typeRegistry, transactionManager)),
ImmutableSet.of());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ system| information_schema| views| table_catalog| varchar| YES| null| null|
system| information_schema| views| table_schema| varchar| YES| null| null|
system| information_schema| views| table_name| varchar| YES| null| null|
system| information_schema| views| view_definition| varchar| YES| null| null|
system| metadata| analyze_properties| catalog_name| varchar| YES| null| null|
system| metadata| analyze_properties| property_name| varchar| YES| null| null|
system| metadata| analyze_properties| default_value| varchar| YES| null| null|
system| metadata| analyze_properties| type| varchar| YES| null| null|
system| metadata| analyze_properties| description| varchar| YES| null| null|
system| metadata| catalogs| catalog_name| varchar| YES| null| null|
system| metadata| catalogs| connector_id| varchar| YES| null| null|
system| metadata| column_properties| catalog_name| varchar| YES| null| null|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- delimiter: |; ignoreOrder: true;
catalogs|
analyze_properties|
schema_properties|
table_properties|
column_properties|
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public void testTooLongQuery()
assertQueryFails(longQuery, "Query text length \\(1000037\\) exceeds the maximum length \\(1000000\\)");
}

@Test
public void testAnalyzePropertiesSystemTable()
{
assertQuery("SELECT COUNT(*) FROM system.metadata.analyze_properties WHERE catalog_name = 'tpch'", "SELECT 0");
}

@Test
public void testAnalyze()
{
Expand Down