Пример #1
0
def test_simulate():
    with pytest.raises(parse.BQLParseError):
        # Need limit.
        parse_bql_string('create table s as simulate x from t')
    with pytest.raises(parse.BQLParseError):
        # Need limit.
        parse_bql_string('create table s as simulate x from t given y = 0')
    assert parse_bql_string('create table s as'
            ' simulate x from t limit 10') == \
        [ast.CreateTabSim(False, False, 's',
            ast.Simulate(['x'], 't', ast.ExpLit(ast.LitNull(None)), [],
                ast.ExpLit(ast.LitInt(10))))]
    assert parse_bql_string('create table if not exists s as'
            ' simulate x, y from t given z = 0 limit 10') == \
        [ast.CreateTabSim(False, True, 's',
            ast.Simulate(['x', 'y'], 't', ast.ExpLit(ast.LitNull(None)),
                [('z', ast.ExpLit(ast.LitInt(0)))],
                ast.ExpLit(ast.LitInt(10))))]
    assert parse_bql_string('create temp table s as'
            ' simulate x, y from t given z = 0 limit 10') == \
        [ast.CreateTabSim(True, False, 's',
            ast.Simulate(['x', 'y'], 't', ast.ExpLit(ast.LitNull(None)),
                [('z', ast.ExpLit(ast.LitInt(0)))],
                ast.ExpLit(ast.LitInt(10))))]
    assert parse_bql_string('create temp table if not exists s as'
            ' simulate x, y from t given z = 0, w = 1 limit 10') == \
        [ast.CreateTabSim(True, True, 's',
            ast.Simulate(['x', 'y'], 't', ast.ExpLit(ast.LitNull(None)),
                [
                    ('z', ast.ExpLit(ast.LitInt(0))),
                    ('w', ast.ExpLit(ast.LitInt(1))),
                ],
                ast.ExpLit(ast.LitInt(10))))]
Пример #2
0
def test_simulate():
    with pytest.raises(parse.BQLParseError):
        # Need limit.
        parse_bql_string('create table s as simulate x from t')
    with pytest.raises(parse.BQLParseError):
        # Need limit.
        parse_bql_string('create table s as simulate x from t given y = 0')
    assert parse_bql_string('create table s as'
            ' simulate x from t limit 10') == \
        [ast.CreateTabAs(False, False, 's',
            ast.Simulate(
                ['x'], 't', None,
                [],
                ast.ExpLit(ast.LitInt(10)),
                None)
        )]
    assert parse_bql_string('create table if not exists s as'
            ' simulate x, y from t given z = 0 limit 10 accuracy 2') == \
        [ast.CreateTabAs(False, True, 's',
            ast.Simulate(
                ['x', 'y'], 't', None,
                [('z', ast.ExpLit(ast.LitInt(0)))],
                ast.ExpLit(ast.LitInt(10)),
                2)
        )]
    assert parse_bql_string('create temp table s as'
            ' simulate x, y from t given z = 0 limit 10') == \
        [ast.CreateTabAs(True, False, 's',
            ast.Simulate(
                ['x', 'y'], 't', None,
                [('z', ast.ExpLit(ast.LitInt(0)))],
                ast.ExpLit(ast.LitInt(10)),
                None),
        )]
    assert parse_bql_string(
            'create temp table if not exists s as'
            ' simulate x, y from t given z = 0, w = 1'
            ' limit 10 accuracy 19') == \
        [ast.CreateTabAs(True, True, 's',
            ast.Simulate(
                ['x', 'y'], 't', None,
                [
                    ('z', ast.ExpLit(ast.LitInt(0))),
                    ('w', ast.ExpLit(ast.LitInt(1))),
                ],
                ast.ExpLit(ast.LitInt(10)),
                19)
        )]
    # Specifying a quantity other than a variable should raise.
    with pytest.raises(parse.BQLParseError):
        parse_bql_string(
            'simulate a, dependence probability of a with b from t limit 10;')
    with pytest.raises(parse.BQLParseError):
        parse_bql_string(
            'create table f as simulate a, dependence probability of a with b '
            'from t limit 10;')
Пример #3
0
def test_using_model():
    assert parse_bql_string('simulate x from t using model 42'
            ' limit 10') == \
        [ast.Simulate(['x'], 't', ast.ExpLit(ast.LitInt(42)), [],
            ast.ExpLit(ast.LitInt(10)))]
    with pytest.raises(parse.BQLParseError):
        assert parse_bql_string('simulate x from t'
                ' using model (87)') == \
            [ast.Simulate(['x'], 't', ast.ExpLit(ast.LitInt(87)), [],
                ast.ExpLit(ast.LitInt(10)))]
    assert parse_bql_string('estimate x from t using model (1+2)') == \
        [ast.Estimate(ast.SELQUANT_ALL,
            [ast.SelColExp(ast.ExpCol(None, 'x'), None)],
            't',
            ast.ExpOp(ast.OP_ADD, (
                ast.ExpLit(ast.LitInt(1)),
                ast.ExpLit(ast.LitInt(2)),
            )),
            None, None, None, None)]
    assert parse_bql_string('estimate * from columns of t'
            ' using model modelno') == \
        [ast.EstCols([ast.SelColAll(None)], 't', ast.ExpCol(None, 'modelno'),
            None, None, None)]
    assert parse_bql_string('estimate 42 from columns of t'
            ' using model modelno') == \
        [ast.EstCols([(ast.ExpLit(ast.LitInt(42)), None)], 't',
            ast.ExpCol(None, 'modelno'),
            None, None, None)]
    assert parse_bql_string('estimate 42 from pairwise columns of t'
            ' using model modelno') == \
        [ast.EstPairCols([(ast.ExpLit(ast.LitInt(42)), None)], 't', None,
            ast.ExpCol(None, 'modelno'),
            None, None, None)]
    assert parse_bql_string('estimate similarity from pairwise t'
            ' using model modelno') == \
        [ast.EstPairRow([ast.SelColExp(ast.ExpBQLSim(None, [ast.ColListAll()]),
                None)],
            't', ast.ExpCol(None, 'modelno'),
            None, None, None)]
    assert parse_bql_string('infer x from t using model modelno') == \
        [ast.InferAuto([ast.InfColOne('x', None)], ast.ExpLit(ast.LitInt(0)),
            't', ast.ExpCol(None, 'modelno'),
            None, None, None, None)]
    assert parse_bql_string('infer explicit x from t using model modelno') == \
        [ast.InferExplicit([ast.SelColExp(ast.ExpCol(None, 'x'), None)],
            't', ast.ExpCol(None, 'modelno'),
            None, None, None, None)]
Пример #4
0
 def p_simulate_nolimit(self, cols, population, generator, constraints):
     # XXX Report source location.
     self.errors.append('simulate missing limit')
     if any(not isinstance(c.col, ast.ExpCol) for c in cols):
         self.errors.append('simulate only accepts population variables.')
         return None
     return ast.Simulate([c.col.column for c in cols], population,
                         generator, constraints, 0, None)
Пример #5
0
 def p_simulate_s(self, cols, population, generator, constraints, lim, acc):
     for c in cols:
         if isinstance(c, ast.SelColSub):
             continue
         if isinstance(c, ast.SelColExp) and \
            isinstance(c.expression, ast.ExpCol):
             continue
         self.errors.append('simulate only accepts population variables.')
         return None
     return ast.Simulate(cols, population, generator, constraints,
                         lim.limit, acc)
Пример #6
0
 def p_simulate_nolimit(self, cols, population, generator, constraints):
     # XXX Report source location.
     self.errors.append('simulate missing limit')
     for c in cols:
         if isinstance(c, ast.SelColSub):
             continue
         if isinstance(c, ast.SelColExp) and \
            isinstance(c.expression, ast.ExpCol):
             continue
         self.errors.append('simulate only accepts population variables.')
         return None
     return ast.Simulate(cols, population, generator, constraints, 0, None)
Пример #7
0
 def p_simulate_s(self, cols, population, generator, constraints, lim, acc):
     if any(not isinstance(c.col, ast.ExpCol) for c in cols):
         self.errors.append('simulate only accepts population variables.')
         return None
     return ast.Simulate([c.col.column for c in cols], population,
                         generator, constraints, lim.limit, acc)
Пример #8
0
 def p_simulate_nolimit(self, cols, generator, modelno, constraints):
     # XXX Report source location.
     self.errors.append('simulate missing limit')
     return ast.Simulate(cols, generator, modelno, constraints, 0)
Пример #9
0
 def p_simulate_s(self, cols, generator, modelno, constraints, lim):
     return ast.Simulate(cols, generator, modelno, constraints, lim.limit)
Пример #10
0
 def p_simulate_s(self, cols, population, generator, constraints, lim, acc):
     return ast.Simulate(cols, population, generator, constraints,
                         lim.limit, acc)