コード例 #1
0
 def test_naming_select(self):
     select = {"value": "result.duration", "aggregate": "avg"}
     result = _normalize_select(select, None)
     #DEEP NAMES ARE ALLOWED, AND NEEDED TO BUILD STRUCTURE FROM A QUERY
     expected = [{
         "name": "result.duration",
         "value": "result.duration",
         "aggregate": "average"
     }]
     self.assertEqual(result, expected)
コード例 #2
0
def sql_query(query, schema=None):
    """
    VERY BASIC QUERY EXPRESSION TO SQL
    :param query: jx-expression
    :return: SQL
    """
    from jx_base import jx_expression
    from jx_bigquery.expressions._utils import BQLang

    query = wrap(query)

    acc = [SQL_SELECT]
    if not query.select:
        acc.append(SQL_STAR)
    else:
        select = _normalize_select(query.select, query["from"], schema)
        acc.append(
            JoinSQL(
                SQL_COMMA,
                [
                    sql_alias(BQLang[jx_expression(s.value)].to_bq(schema),
                              escape_name(s.name)) for s in select
                ],
            ))

    acc.append(SQL_FROM)
    acc.append(quote_column(ApiName(*split_field(query["from"]))))
    if query.where:
        acc.append(SQL_WHERE)
        acc.append(BQLang[jx_expression(query.where)].to_bq(schema))
    if query.sort:
        sort = _normalize_sort(query.sort)
        acc.append(SQL_ORDERBY)
        acc.append(
            JoinSQL(
                SQL_COMMA,
                [
                    ConcatSQL(
                        BQLang[jx_expression(s.value)].to_bq(schema),
                        SQL_DESC if s.sort == -1 else SQL_ASC,
                    ) for s in sort
                ],
            ))
    if query.limit:
        acc.append(SQL_LIMIT)
        acc.append(BQLang[jx_expression(query.limit)].to_bq(schema))

    return ConcatSQL(*acc)