Beispiel #1
0
def _find_in_set(t, expr):
    op = expr.op()
    return (
        sa.func.find_in_set(
            t.translate(op.needle),
            sa.func.concat_ws(",", *map(t.translate, op.values)),
        )
        - 1
    )


operation_registry.update(
    {
        ops.Literal: _literal,
        ops.IfNull: fixed_arity(sa.func.ifnull, 2),
        # strings
        ops.StringFind: _gen_string_find(sa.func.locate),
        ops.FindInSet: _find_in_set,
        ops.Capitalize: _capitalize,
        ops.RegexSearch: fixed_arity(lambda x, y: x.op('REGEXP')(y), 2),
        # math
        ops.Log: _log,
        ops.Log2: unary(sa.func.log2),
        ops.Log10: unary(sa.func.log10),
        ops.Round: _round,
        # dates and times
        ops.DateAdd: fixed_arity(operator.add, 2),
        ops.DateSub: fixed_arity(operator.sub, 2),
        ops.DateDiff: fixed_arity(sa.func.datediff, 2),
        ops.TimestampAdd: fixed_arity(operator.add, 2),
Beispiel #2
0
operation_registry.update({
    ops.Cast:
    _cast,
    ops.Substring:
    _substr,
    ops.StrRight:
    _string_right,
    ops.StringFind:
    _string_find,
    ops.Least:
    varargs(sa.func.min),
    ops.Greatest:
    varargs(sa.func.max),
    ops.IfNull:
    fixed_arity(sa.func.ifnull, 2),
    ops.DateTruncate:
    _truncate(sa.func.date),
    ops.TimestampTruncate:
    _truncate(sa.func.datetime),
    ops.Strftime:
    _strftime,
    ops.ExtractYear:
    _strftime_int('%Y'),
    ops.ExtractMonth:
    _strftime_int('%m'),
    ops.ExtractDay:
    _strftime_int('%d'),
    ops.ExtractDayOfYear:
    _strftime_int('%j'),
    ops.ExtractQuarter:
Beispiel #3
0
 ops.Literal:
 _literal,
 # We override this here to support time zones
 ops.TableColumn:
 _table_column,
 # types
 ops.TypeOf:
 _typeof,
 # Floating
 ops.IsNan:
 _is_nan,
 ops.IsInf:
 _is_inf,
 # null handling
 ops.IfNull:
 fixed_arity(sa.func.coalesce, 2),
 # boolean reductions
 ops.Any:
 unary(sa.func.bool_or),
 ops.All:
 unary(sa.func.bool_and),
 ops.NotAny:
 unary(lambda x: sa.not_(sa.func.bool_or(x))),
 ops.NotAll:
 unary(lambda x: sa.not_(sa.func.bool_and(x))),
 # strings
 ops.GroupConcat:
 _string_agg,
 ops.Capitalize:
 unary(sa.func.initcap),
 ops.RegexSearch:
Beispiel #4
0
def _day_of_week_index(t, expr):
    (arg, ) = expr.op().args
    left = sa.func.dayofweek(t.translate(arg)) - 2
    right = 7
    return ((left % right) + right) % right


def _day_of_week_name(t, expr):
    (arg, ) = expr.op().args
    return sa.func.dayname(t.translate(arg))


operation_registry.update({
    ops.Literal: _literal,
    ops.IfNull: fixed_arity(sa.func.ifnull, 2),
    # strings
    ops.Substring: _substr,
    ops.StringFind: _string_find,
    ops.Capitalize: _capitalize,
    ops.RegexSearch: infix_op('REGEXP'),
    # math
    ops.Log: _log,
    ops.Log2: unary(sa.func.log2),
    ops.Log10: unary(sa.func.log10),
    ops.Round: _round,
    ops.RandomScalar: _random,
    # dates and times
    ops.Date: unary(sa.func.date),
    ops.DateAdd: infix_op('+'),
    ops.DateSub: infix_op('-'),
Beispiel #5
0
 # We override this here to support time zones
 ops.TableColumn:
 _table_column,
 # types
 ops.Cast:
 _cast,
 ops.TypeOf:
 _typeof,
 # Floating
 ops.IsNan:
 _is_nan,
 ops.IsInf:
 _is_inf,
 # null handling
 ops.IfNull:
 fixed_arity(sa.func.coalesce, 2),
 # boolean reductions
 ops.Any:
 unary(sa.func.bool_or),
 ops.All:
 unary(sa.func.bool_and),
 ops.NotAny:
 unary(lambda x: sa.not_(sa.func.bool_or(x))),
 ops.NotAll:
 unary(lambda x: sa.not_(sa.func.bool_and(x))),
 # strings
 ops.Substring:
 _substr,
 ops.StringFind:
 _string_find,
 ops.GroupConcat:
Beispiel #6
0
 ops.Substring:
 _substr,
 ops.StrRight:
 _string_right,
 ops.StringFind:
 _string_find,
 ops.StringJoin:
 _string_join,
 ops.StringConcat:
 _string_concat,
 ops.Least:
 varargs(sa.func.min),
 ops.Greatest:
 varargs(sa.func.max),
 ops.IfNull:
 fixed_arity(sa.func.ifnull, 2),
 ops.DateFromYMD:
 _date_from_ymd,
 ops.TimeFromHMS:
 _time_from_hms,
 ops.TimestampFromYMDHMS:
 _timestamp_from_ymdhms,
 ops.DateTruncate:
 _truncate(sa.func.date),
 ops.Date:
 unary(sa.func.date),
 ops.TimestampTruncate:
 _truncate(sa.func.datetime),
 ops.Strftime:
 _strftime,
 ops.ExtractYear:
Beispiel #7
0
    return sa.func.trim(sa.func.to_char(sa_arg, 'Day'))


operation_registry.update(
    {
        ops.Literal: _literal,
        # We override this here to support time zones
        ops.TableColumn: _table_column,
        # types
        ops.Cast: _cast,
        ops.TypeOf: _typeof,
        # Floating
        ops.IsNan: _is_nan,
        ops.IsInf: _is_inf,
        # null handling
        ops.IfNull: fixed_arity(sa.func.coalesce, 2),
        # boolean reductions
        ops.Any: unary(sa.func.bool_or),
        ops.All: unary(sa.func.bool_and),
        ops.NotAny: unary(lambda x: sa.not_(sa.func.bool_or(x))),
        ops.NotAll: unary(lambda x: sa.not_(sa.func.bool_and(x))),
        # strings
        ops.Substring: _substr,
        ops.StringFind: _string_find,
        ops.GroupConcat: _string_agg,
        ops.Capitalize: unary(sa.func.initcap),
        ops.RegexSearch: infix_op('~'),
        ops.RegexReplace: _regex_replace,
        ops.Translate: fixed_arity('translate', 3),
        ops.RegexExtract: _regex_extract_,
        ops.StringSplit: fixed_arity(sa.func.string_to_array, 2),
Beispiel #8
0

def _rpad(t, expr):
    arg, length, pad = map(t.translate, expr.op().args)
    return arg + _generic_pad(arg, length, pad)


operation_registry.update(
    {
        ops.Cast: _cast,
        ops.Substring: _substr,
        ops.StrRight: _string_right,
        ops.StringFind: _string_find,
        ops.Least: varargs(sa.func.min),
        ops.Greatest: varargs(sa.func.max),
        ops.IfNull: fixed_arity(sa.func.ifnull, 2),
        ops.DateTruncate: _truncate(sa.func.date),
        ops.TimestampTruncate: _truncate(sa.func.datetime),
        ops.Strftime: _strftime,
        ops.ExtractYear: _strftime_int('%Y'),
        ops.ExtractMonth: _strftime_int('%m'),
        ops.ExtractDay: _strftime_int('%d'),
        ops.ExtractDayOfYear: _strftime_int('%j'),
        ops.ExtractQuarter: _extract_quarter,
        ops.ExtractEpochSeconds: _extract_epoch_seconds,
        ops.ExtractHour: _strftime_int('%H'),
        ops.ExtractMinute: _strftime_int('%M'),
        ops.ExtractSecond: _strftime_int('%S'),
        ops.ExtractMillisecond: _millisecond,
        ops.TimestampNow: _now,
        ops.IdenticalTo: _identical_to,