# Basics: Aggregation, filtering, limits¶

Now that you have ibis installed and connecting to your Impala, let’s get our feet wet.

[1]:

import ibis
import os
hdfs_port = os.environ.get('IBIS_WEBHDFS_PORT', 50070)
hdfs = ibis.hdfs_connect(host='impala', port=hdfs_port)
con = ibis.impala.connect(host='impala', database='ibis_testing',
hdfs_client=hdfs)


## Accessing an Impala table¶

The optional database paramter allows to fully qualify the table.

[2]:

table = con.table('functional_alltypes')
table = con.table('functional_alltypes', database='ibis_testing')


## Column selection and basic expressions¶

Selecting columns from a table works just like pandas

[3]:

col = table.double_col

# alternately
col2 = table['bigint_col']


Table columns are equipped with a variety of math operations and other methods to assist in writing your analytics. For example:

[4]:

expr = col.log2() - 1


Some operations transform arrays to arrays, while others aggregate, like sum and mean:

[5]:

expr2 = expr.sum()


The methods that are available on columns depend on the type of the column. For example, you won’t see the substr or upper methods for strings on numeric columns:

[6]:

substr_expr = table.string_col.upper().substr(0, 2)


Notice that printing the expressions to the console does not actually do anything, and shows a graphical representation of the expression you’ve built.

Note: don’t worry too much about the details of the expression tree that’s outputted, it’s very likely to change over time

[7]:

expr2

[7]:


We can also execute an expression by calling execute on the Impala connection object

[8]:

con.execute(col.sum())

[8]:

331785.00000000006


There’s a shortcut to make this a little more convenient in interactive use

## Interactive mode¶

Many Ibis expressions can be immediately executed against the database, and it may improve your productivity to have the executed for you whenever you try to print the expression in the console / IPython notebook.

To do this, we have an interactive mode available, which can be turned on/off like so:

[9]:

ibis.options.interactive = True


Now, any expressions you write will be executed right away

[10]:

table.limit(10)

[10]:


You can select a row range with slicing syntax:

[11]:

table.double_col.sum()

[11]:


Don’t worry about the syntax here, but expressions resulting in tabular output will come back as a pandas DataFrame by default:

[12]:

metrics = [table.double_col.sum().name('total')]
expr = table.group_by('string_col').aggregate(metrics)
expr

[12]:


## Verbose mode and logging¶

You may want to see or log all the Impala queries that Ibis executes under the hood. To do that, use the options.verbose option:

[13]:

ibis.options.verbose = True

metrics = [table.double_col.sum().name('total')]
expr = table.group_by('string_col').aggregate(metrics)
expr

SELECT string_col, sum(double_col) AS total
FROM ibis_testing.functional_alltypes
GROUP BY 1
LIMIT 10000

[13]:

[14]:

queries = []
def logger(x):
queries.append(x)

ibis.options.verbose_log = logger

expr.execute()
expr.execute()

queries

[14]:

['SELECT string_col, sum(double_col) AS total\nFROM ibis_testing.functional_alltypes\nGROUP BY 1\nLIMIT 10000',
'SELECT string_col, sum(double_col) AS total\nFROM ibis_testing.functional_alltypes\nGROUP BY 1\nLIMIT 10000']

[15]:

ibis.options.verbose_log = print
ibis.options.verbose = False


## Aggregation basics¶

One of the essential table API functions is aggregate. Aggregation involves the following

• One or more named aggregate expressions, or reductions

• Zero or more grouping expressions or column names

This ends up working very similarly to pandas’s groupby mechanism.

[16]:

metric = table.double_col.sum()


As you saw above, you can execute this immediately and obtain a value:

[17]:

metric

[17]:


The reduced column can be more complex; for example, you could count the number of null values in a column like so:

[18]:

table.double_col.isnull().sum()

[18]:


To aggregate a table, potentially with grouping keys, we have to give the reduction a name and call aggregate

[19]:

metric = metric.name('double_total')
expr = table.aggregate([metric])
result = con.execute(expr)
result

[19]:

double_total
0 331785.0

The result here is actually a pandas DataFrame with 1 row and just the one column. We can add another metric and add a grouping key

[20]:

metric2 = (table.bigint_col + 1).log10().max().name('some_metric')

expr = table.aggregate([metric, metric2], by=['string_col'])
expr

[20]:


We provide a convenience group_by, a la pandas, to make this a little more composable:

[21]:

expr = (table.group_by('string_col')
.aggregate([metric, metric2]))
expr

[21]:


You can also group by named column expressions

[22]:

keys = [table.timestamp_col.hour().name('hour'), 'string_col']

expr = table.group_by(keys).aggregate([metric])

# Top 10 by double_total, more on this later
expr.sort_by([('double_total', False)]).limit(10)

[22]:


In most cases, an aggregation by itself can be evaluated:

[23]:

table.double_col.mean()

[23]:


This can also be done in simple cases along with group_by:

[24]:

table.group_by('string_col').double_col.mean()

[24]:


Many reduction functions have a default expression name, unlike many other Ibis expressions (for now!), to make some common analyses easier:

[25]:

d = table.double_col

(table.group_by('string_col')
.aggregate([d.sum(), d.mean(), d.min(), d.max()]))

[25]:


Of course, for this particular case you can always use summary

[26]:

table.group_by('string_col').double_col.summary()

[26]:


## Filtering¶

Filtering tables works similarly to pandas

[27]:

table.aggregate([table.bigint_col.max().name('bigint_max'),
table.bigint_col.min().name('bigint_min'),
table.int_col.max().name('int_max'),
table.int_col.min().name('int_min')])

[27]:

[28]:

table.count()

[28]:

[29]:

table[table.bigint_col > 50].count()

[29]:


Filters can be composed using & (and), | (or), and other logical array operators

[30]:

cond1 = table.bigint_col > 50
cond2 = table.int_col.between(2, 7)
table[cond1 | cond2].count()

[30]:


There’s a filter function that allows you to pass a list of conditions (that are all required to hold):

[31]:

table.filter([cond1, cond2]).count()

[31]:


Note this is the same as &-ing the boolean conditions yourself:

[32]:

table[cond1 & cond2].count()

[32]:


## Limits¶

Like in SQL, the limit function selects the indicated numbers of rows from the beginning of a table’s result set

[33]:

table.limit(2)

[33]: