Example #1
0
    def test_standalone(self):
        table1 = self.tables.people

        # no special alias handling even though clause is not in the
        # context of a FROM clause
        self.assert_compile(
            tablesample(table1, 1, name='alias'),
            'people AS alias TABLESAMPLE system(:system_1)'
        )

        self.assert_compile(
            table1.tablesample(1, name='alias'),
            'people AS alias TABLESAMPLE system(:system_1)'
        )

        self.assert_compile(
            tablesample(table1, func.bernoulli(1), name='alias',
                        seed=func.random()),
            'people AS alias TABLESAMPLE bernoulli(:bernoulli_1) '
            'REPEATABLE (random())'
        )
Example #2
0
async def get_history(conn, group_by=False, args=[], limit=10, offset=0):

    sampling = False
    if group_by == 'minute':
        sampling = 0
    if group_by == 'hour':
        sampling = 0.3
    if group_by == 'day':
        sampling = 0.04
    if group_by == 'week':
        sampling = 0.01
    if group_by == 'month':
        sampling = 0.001

    if sampling:
        table = sa.tablesample(history,
                               sa.func.system(sampling),
                               seed=sa.cast('1', REAL))
    else:
        table = history

    args = [arg(table) for arg in args]

    query = table.c.resp[0].astext.cast(JSONB)['asks'][0][0].astext.cast(REAL)
    query = query + \
        table.c.resp[0].astext.cast(JSONB)['bids'][0][0].astext.cast(REAL)
    query = query / 2

    if (group_by):
        query = sa.func.avg(query)

    date = table.c.pub_date
    collums = [query.label('price')]
    if group_by:
        date = sa.func.date_trunc(group_by, table.c.pub_date).label('pub_date')
        collums += [date, sa.func.count().label('count')]

    query = select(collums) \
        .where(sa.sql.and_(*args)) \
        .order_by(date.desc())

    if group_by:
        query = query \
            .group_by(date)
    else:
        query = query \
            .limit(limit) \
            .offset(offset)
    cursor = await conn.execute(query)
    items = await cursor.fetchall()
    return items
Example #3
0
def get_random_sentence():
    sample = aliased(JapaneseExample, tablesample(JapaneseExample, 1))
    return session.query(sample).limit(1).first().entry