htsql / src / htsql_mysql / core / tr /

# Copyright (c) 2006-2013, Prometheus Research, LLC

from htsql.core.domain import BooleanDomain, IntegerDomain
from import (WrapperTerm, FilterTerm, OrderTerm,
        PermanentTerm, JoinTerm, ScalarTerm)
from import (LiteralCode, FormulaCode, ScalarUnit,
from import coerce
from import IsEqualSig, AndSig, CompareSig
from import AddSig
from import CompileCovering
from .signature import (UserVariableSig, UserVariableAssignmentSig,
        NoOpConditionSig, IfSig)

class MySQLCompileCovering(CompileCovering):

    def clip(self, term, order, partition):
        prefix = u"!htsql:%s" % term.tag
        row_number = FormulaCode(UserVariableSig(u"%s:row_number" % prefix),
        keys = []
        for idx, code in enumerate(partition):
            key = FormulaCode(UserVariableSig(u"%s:partition:%s"
                                              % (prefix, idx+1)),
        #term = PermanentTerm(self.state.tag(), term,
        #           , term.baseline, term.routes.copy())
        zero_term = ScalarTerm(self.state.tag(),
                               self.state.root, self.state.root, {})
        zero_units = []
        code = FormulaCode(UserVariableAssignmentSig(),
                           rop=LiteralCode(None, row_number.domain,
        unit = ScalarUnit(code, self.state.root, code.flow)
        for key in keys:
            code = FormulaCode(UserVariableAssignmentSig(),
                               rop=LiteralCode(None, key.domain,
            unit = ScalarUnit(code, self.state.root, code.flow)
        tag = self.state.tag()
        routes = {}
        for unit in zero_units:
            routes[unit] = tag
        zero_term.routes = routes
        zero_term = PermanentTerm(tag, zero_term,
                        , zero_term.baseline, routes)
        filters = [FormulaCode(NoOpConditionSig(), coerce(BooleanDomain()),
         , op=unit) for unit in zero_units]
        filter = FormulaCode(AndSig(), coerce(BooleanDomain()),
                   , ops=filters)
        zero_term = FilterTerm(self.state.tag(), zero_term, filter,
                     , zero_term.baseline, {})
        term = JoinTerm(self.state.tag(), term, zero_term, [],
                        False, False,, term.baseline,
        order = [(code, +1) for code in partition]+order
        term = OrderTerm(self.state.tag(), term, order, None, None,
               , term.baseline, term.routes.copy())
        term = PermanentTerm(self.state.tag(), term,
                   , term.baseline, term.routes)
        next_units = []
        conditions = []
        for lop, rop in zip(keys, partition):
            condition = FormulaCode(IsEqualSig(+1), coerce(BooleanDomain()),
                          , lop=lop, rop=rop)
        if len(conditions) == 1:
            [condition] = conditions
            condition = FormulaCode(AndSig(), coerce(BooleanDomain()),
                          , ops=conditions)
        one_literal = LiteralCode(1, coerce(IntegerDomain()),
        on_true = FormulaCode(AddSig(), coerce(IntegerDomain()),
                              lop=row_number, rop=one_literal)
        on_false = one_literal
        value = FormulaCode(IfSig(), row_number.domain,,
                            condition=condition, on_true=on_true,
        code = FormulaCode(UserVariableAssignmentSig(),
                           lop=row_number, rop=value)
        row_number_unit = ScalarUnit(code,, code.flow)
        for lop, rop in zip(keys, partition):
            code = FormulaCode(UserVariableAssignmentSig(), lop.domain,
                          , lop=lop, rop=rop)
            unit = ScalarUnit(code,, code.flow)
        tag = self.state.tag()
        routes = term.routes.copy()
        for unit in next_units:
            routes[unit] = tag
        term = PermanentTerm(tag, term,, term.baseline, routes)
        left_bound = 1
        if is not None:
            left_bound =
        right_bound = left_bound+1
        if is not None:
            right_bound =
        left_bound_code = LiteralCode(left_bound, coerce(IntegerDomain()),
        right_bound_code = LiteralCode(right_bound, coerce(IntegerDomain()),
        left_filter = FormulaCode(CompareSig('>='), coerce(BooleanDomain()),
                                  lop=row_number_unit, rop=left_bound_code)
        right_filter = FormulaCode(CompareSig('<'), coerce(BooleanDomain()),
                                   lop=row_number_unit, rop=right_bound_code)
        filters = [left_filter, right_filter]
        filters += [FormulaCode(NoOpConditionSig(), coerce(BooleanDomain()),
          , op=unit) for unit in next_units]
        filter = FormulaCode(AndSig(), coerce(BooleanDomain()),
                   , ops=filters)
        term = FilterTerm(self.state.tag(), term, filter,
                , term.baseline, term.routes)
        return term
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.