Exemplo n.º 1
0
def test_binary_operators():
    assert sqleval('x = x')
    assert sqleval('x = y') is False
    assert sqleval('x != x') is False
    assert sqleval('x != y')
    assert sqleval('x <= y') == ('LESSEQUAL', (ast.String('x'), ),
                                 ((ast.String('y'), )))
Exemplo n.º 2
0
 def TypeCast(self, node: ast.TypeCast):
     if node.typeName.names == [
             ast.String('pg_catalog'),
             ast.String('bool')
     ]:
         return node.arg == ast.A_Const('t')
     if node.typeName.names == [ast.String('date')]:
         return datetime.date.fromisoformat(node.arg(self))
     raise ast.SqlError(f'Unknown type cast: {node}')
Exemplo n.º 3
0
def test_boolean_operators():
    assert sqleval('x and y and z') == ('AND', (ast.String('x'), ),
                                        (ast.String('y'), ),
                                        (ast.String('z'), ))
    assert sqleval('x or y') == ('OR', (ast.String('x'), ),
                                 (ast.String('y'), ))
    assert sqleval('a or (not b) and c and d') == ('OR', (ast.String('a'), ), (
        'AND',
        ('NOT', (ast.String('b'), )),
        (ast.String('c'), ),
        (ast.String('d'), ),
    ))
Exemplo n.º 4
0
 def A_Indirection(self, node: ast.A_Indirection):
     '''
     This evaluator is only for fits header
     '''
     if node.arg.fields != (ast.String('fits_header'), ):
         raise ast.SqlError(f'Unknown Column: {node.arg.fields}')
     if len(node.indirection) == 1:
         keyword, = node.indirection
         if (isinstance(keyword, ast.A_Indices)
                 and isinstance(keyword.uidx.value, str)):
             value = M.obslog_fits_header.cards_dict[keyword.uidx.value]
             # return case(
             #     (sqlalchemy.func.jsonb_typeof(value) == 'boolean',
             #         case(
             #             (value.astext == 'true', 'T'),  # type: ignore
             #             else_='F'),
             #      ),
             #     else_=value.astext
             # )
             return value.astext
     raise ast.SqlError(
         f"""accessing FITS header should be something like this:\n"""
         """  `WHERE fits_header['OBSERVER'] LIKE '%Arthur%'`,\n"""
         """  `WHERE fits_header['NAXIS']::integer = 2`, or\n"""
         """  `WHERE fits_header['SIMPLE'] == 'true'`""")
Exemplo n.º 5
0
 def ColumnRef(self, node: ast.ColumnRef):
     columns: dict[tuple[Union[ast.String, ast.A_Star]], object] = {
         (ast.String('any_column'), ): AnyColumn,
         (ast.String('visit_id'), ): M.pfs_visit.pfs_visit_id,
         (ast.String('id'), ): M.pfs_visit.pfs_visit_id,
         (ast.String('sequence_type'), ): M.iic_sequence.sequence_type,
         (ast.String('issued_at'), ): M.pfs_visit.issued_at,
         (ast.String('is_sps_visit'), ): M.sps_visit.pfs_visit_id != None,
         (ast.String('is_mcs_visit'), ):
         M.mcs_exposure.mcs_frame_id != None,
         (ast.String('is_agc_visit'), ):
         M.agc_exposure.agc_exposure_id != None,
         (ast.String('fits_header'), ): M.obslog_fits_header.cards_dict,
     }
     if node.fields not in columns:
         raise ast.SqlError(f'Unknown column: {node.fields}')
     return columns[node.fields]
Exemplo n.º 6
0
def test_like():
    assert sqleval('x LIKE y') == ('LIKE', (ast.String('x'), ),
                                   (ast.String('y'), ))
    assert sqleval('x NOT LIKE y') == ('NOTLIKE', (ast.String('x'), ),
                                       (ast.String('y'), ))
    assert sqleval('NOT x LIKE y') == ('NOT', ('LIKE', (ast.String('x'), ),
                                               (ast.String('y'), )))
Exemplo n.º 7
0
 def TypeCast(self, node: ast.TypeCast):
     if node.typeName.names == [ast.String('date')]:
         return cast(node.arg(self), Date)
     if node.typeName.names == [
             ast.String(str='pg_catalog'),
             ast.String(str='float8')
     ]:
         return cast(node.arg(self), sqlalchemy.Float)
     if node.typeName.names == [
             ast.String(str='pg_catalog'),
             ast.String(str='int4')
     ]:
         return cast(node.arg(self), sqlalchemy.Integer)
     if node.typeName.names == [ast.String(str='safe_float')]:
         return sqlalchemy.func.try_cast_float(node.arg(self))
     if node.typeName.names == [ast.String(str='safe_int')]:
         return sqlalchemy.func.try_cast_int(node.arg(self))
     raise ast.SqlError(f'Unknown type cast: {node}')
Exemplo n.º 8
0
 def ColumnRef(self, node: ast.ColumnRef):
     m: dict[tuple[Union[ast.String, ast.A_Star]], int] = {
         (ast.String('$one'), ): 1,
     }
     return m.get(node.fields) or node.fields
Exemplo n.º 9
0
def test_a_star():
    assert sqleval('x.*') == (ast.String('x'), ast.A_Star())
Exemplo n.º 10
0
def test_target_list(ctx):
    s = parse('''SELECT t.f as "a"''')[0]
    assert isinstance(s, ast.SelectStmt)
    assert s.targetList[0].val(ctx) == (ast.String('t'), ast.String('f'))
    assert s.targetList[0].name == 'a'