<s> import pytest <EOL> import pandas as pd <EOL> import ibis <EOL> from ibis import literal as L <EOL> from ibis . compat import unittest , StringIO , Decimal <EOL> from ibis . expr . datatypes import Category <EOL> from ibis . expr . tests . mocks import MockConnection <EOL> from ibis . impala . compiler import ImpalaExprTranslator , to_sql , ImpalaContext <EOL> from ibis . sql . tests . test_compiler import ExprTestCases <EOL> from ibis . impala . tests . common import ImpalaE2E <EOL> import ibis . expr . types as ir <EOL> import ibis . expr . api as api <EOL> def approx_equal ( a , b , eps ) : <EOL> assert abs ( a - b ) < eps <EOL> class ExprSQLTest ( object ) : <EOL> def _check_expr_cases ( self , cases , context = None , named = False ) : <EOL> for expr , expected in cases : <EOL> repr ( expr ) <EOL> result = self . _translate ( expr , named = named , context = context ) <EOL> assert result == expected <EOL> def _translate ( self , expr , named = False , context = None ) : <EOL> translator = ImpalaExprTranslator ( expr , context = context , named = named ) <EOL> return translator . get_result ( ) <EOL> class TestValueExprs ( unittest . TestCase , ExprSQLTest ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> self . int_cols = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] <EOL> self . bool_cols = [ '<STR_LIT:h>' ] <EOL> self . float_cols = [ '<STR_LIT:e>' , '<STR_LIT:f>' ] <EOL> def _check_literals ( self , cases ) : <EOL> for value , expected in cases : <EOL> lit_expr = L ( value ) <EOL> result = self . _translate ( lit_expr ) <EOL> assert result == expected <EOL> def test_string_literals ( self ) : <EOL> cases = [ <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> ] <EOL> for value , expected in cases : <EOL> lit_expr = L ( value ) <EOL> result = self . _translate ( lit_expr ) <EOL> assert result == expected <EOL> def test_decimal_builtins ( self ) : <EOL> t = self . con . table ( '<STR_LIT>' ) <EOL> col = t . l_extendedprice <EOL> cases = [ <EOL> ( col . precision ( ) , '<STR_LIT>' ) , <EOL> ( col . scale ( ) , '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_number_boolean_literals ( self ) : <EOL> cases = [ <EOL> ( <NUM_LIT:5> , '<STR_LIT:5>' ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( True , '<STR_LIT>' ) , <EOL> ( False , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_literals ( cases ) <EOL> def test_column_ref_table_aliases ( self ) : <EOL> context = ImpalaContext ( ) <EOL> table1 = ibis . table ( [ <EOL> ( '<STR_LIT>' , '<STR_LIT:string>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] ) <EOL> table2 = ibis . table ( [ <EOL> ( '<STR_LIT>' , '<STR_LIT:string>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] ) <EOL> context . set_ref ( table1 , '<STR_LIT>' ) <EOL> context . set_ref ( table2 , '<STR_LIT>' ) <EOL> expr = table1 [ '<STR_LIT>' ] - table2 [ '<STR_LIT>' ] <EOL> result = self . _translate ( expr , context = context ) <EOL> expected = '<STR_LIT>' <EOL> assert result == expected <EOL> def test_column_ref_quoting ( self ) : <EOL> schema = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> table = ibis . table ( schema ) <EOL> self . _translate ( table [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_identifier_quoting ( self ) : <EOL> schema = [ ( '<STR_LIT:date>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT:string>' ) ] <EOL> table = ibis . table ( schema ) <EOL> self . _translate ( table [ '<STR_LIT:date>' ] , '<STR_LIT>' ) <EOL> self . _translate ( table [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_named_expressions ( self ) : <EOL> a , b , g = self . table . get_columns ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:g>' ] ) <EOL> cases = [ <EOL> ( g . cast ( '<STR_LIT>' ) . name ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( g . name ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( ( ( a - b ) * a ) . name ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> ] <EOL> return self . _check_expr_cases ( cases , named = True ) <EOL> def test_binary_infix_operators ( self ) : <EOL> a , b , h = self . table . get_columns ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:h>' ] ) <EOL> bool_col = a > <NUM_LIT:0> <EOL> cases = [ <EOL> ( a + b , '<STR_LIT>' ) , <EOL> ( a - b , '<STR_LIT>' ) , <EOL> ( a * b , '<STR_LIT>' ) , <EOL> ( a / b , '<STR_LIT>' ) , <EOL> ( a ** b , '<STR_LIT>' ) , <EOL> ( a < b , '<STR_LIT>' ) , <EOL> ( a <= b , '<STR_LIT>' ) , <EOL> ( a > b , '<STR_LIT>' ) , <EOL> ( a >= b , '<STR_LIT>' ) , <EOL> ( a == b , '<STR_LIT>' ) , <EOL> ( a != b , '<STR_LIT>' ) , <EOL> ( h & bool_col , '<STR_LIT>' ) , <EOL> ( h | bool_col , '<STR_LIT>' ) , <EOL> ( h ^ bool_col , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_binary_infix_parenthesization ( self ) : <EOL> a , b , c = self . table . get_columns ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> cases = [ <EOL> ( ( a + b ) + c , '<STR_LIT>' ) , <EOL> ( a . log ( ) + c , '<STR_LIT>' ) , <EOL> ( b + ( - ( a + c ) ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_between ( self ) : <EOL> cases = [ <EOL> ( self . table . f . between ( <NUM_LIT:0> , <NUM_LIT:1> ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_isnull_notnull ( self ) : <EOL> cases = [ <EOL> ( self . table [ '<STR_LIT:g>' ] . isnull ( ) , '<STR_LIT>' ) , <EOL> ( self . table [ '<STR_LIT:a>' ] . notnull ( ) , '<STR_LIT>' ) , <EOL> ( ( self . table [ '<STR_LIT:a>' ] + self . table [ '<STR_LIT:b>' ] ) . isnull ( ) , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_casts ( self ) : <EOL> a , d , g = self . table . get_columns ( [ '<STR_LIT:a>' , '<STR_LIT:d>' , '<STR_LIT:g>' ] ) <EOL> cases = [ <EOL> ( a . cast ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( a . cast ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( a . cast ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( a . cast ( '<STR_LIT:float>' ) , '<STR_LIT>' ) , <EOL> ( a . cast ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( a . cast ( '<STR_LIT:string>' ) , '<STR_LIT>' ) , <EOL> ( d . cast ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( g . cast ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( g . cast ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_misc_conditionals ( self ) : <EOL> a = self . table . a <EOL> cases = [ <EOL> ( a . nullif ( <NUM_LIT:0> ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_decimal_casts ( self ) : <EOL> cases = [ <EOL> ( L ( '<STR_LIT>' ) . cast ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" ) , <EOL> ( self . table . f . cast ( '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_negate ( self ) : <EOL> cases = [ <EOL> ( - self . table [ '<STR_LIT:a>' ] , '<STR_LIT>' ) , <EOL> ( - self . table [ '<STR_LIT:f>' ] , '<STR_LIT>' ) , <EOL> ( - self . table [ '<STR_LIT:h>' ] , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_timestamp_extract_field ( self ) : <EOL> fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> cases = [ ( getattr ( self . table . i , field ) ( ) , <EOL> "<STR_LIT>" . format ( field ) ) <EOL> for field in fields ] <EOL> self . _check_expr_cases ( cases ) <EOL> expr = self . table [ self . table . i . year ( ) . name ( '<STR_LIT>' ) , <EOL> self . table . i . month ( ) . name ( '<STR_LIT>' ) , <EOL> self . table . i . day ( ) . name ( '<STR_LIT>' ) ] <EOL> result = to_sql ( expr ) <EOL> expected = """<STR_LIT>""" <EOL> assert result == expected <EOL> def test_timestamp_now ( self ) : <EOL> cases = [ <EOL> ( ibis . now ( ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_timestamp_deltas ( self ) : <EOL> units = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> t = self . table . i <EOL> f = '<STR_LIT>' <EOL> cases = [ ] <EOL> for unit in units : <EOL> K = <NUM_LIT:5> <EOL> offset = getattr ( ibis , unit ) ( K ) <EOL> template = '<STR_LIT>' <EOL> cases . append ( ( t + offset , template . format ( unit , f , K ) ) ) <EOL> cases . append ( ( t - offset , template . format ( unit , f , - K ) ) ) <EOL> self . _check_expr_cases ( cases ) <EOL> def test_timestamp_literals ( self ) : <EOL> from pandas import Timestamp <EOL> tv1 = '<STR_LIT>' <EOL> ex1 = ( "<STR_LIT>" ) <EOL> cases = [ <EOL> ( L ( Timestamp ( tv1 ) ) , ex1 ) , <EOL> ( L ( Timestamp ( tv1 ) . to_pydatetime ( ) ) , ex1 ) , <EOL> ( ibis . timestamp ( tv1 ) , ex1 ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_timestamp_from_integer ( self ) : <EOL> col = self . table . c <EOL> cases = [ <EOL> ( col . to_timestamp ( ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ( col . to_timestamp ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ( col . to_timestamp ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_correlated_predicate_subquery ( self ) : <EOL> t0 = self . table <EOL> t1 = t0 . view ( ) <EOL> expr = t0 . g == t1 . g <EOL> ctx = ImpalaContext ( ) <EOL> ctx . make_alias ( t0 ) <EOL> subctx = ctx . subcontext ( ) <EOL> subctx . make_alias ( t1 ) <EOL> subctx . make_alias ( t0 ) <EOL> result = self . _translate ( expr , context = subctx ) <EOL> expected = "<STR_LIT>" <EOL> assert result == expected <EOL> def test_any_all ( self ) : <EOL> t = self . table <EOL> bool_expr = t . f == <NUM_LIT:0> <EOL> cases = [ <EOL> ( bool_expr . any ( ) , '<STR_LIT>' ) , <EOL> ( - bool_expr . any ( ) , '<STR_LIT>' ) , <EOL> ( bool_expr . all ( ) , '<STR_LIT>' ) , <EOL> ( - bool_expr . all ( ) , '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> class TestUnaryBuiltins ( unittest . TestCase , ExprSQLTest ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> def test_numeric_unary_builtins ( self ) : <EOL> functions = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> cases = [ ] <EOL> for what in functions : <EOL> if isinstance ( what , tuple ) : <EOL> ibis_name , sql_name = what <EOL> else : <EOL> ibis_name = sql_name = what <EOL> for cname in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> expr = getattr ( self . table [ cname ] , ibis_name ) ( ) <EOL> cases . append ( ( expr , '<STR_LIT>' . format ( <EOL> sql_name , '<STR_LIT>' . format ( cname ) ) ) ) <EOL> self . _check_expr_cases ( cases ) <EOL> def test_log_other_bases ( self ) : <EOL> cases = [ <EOL> ( self . table . double_col . log ( <NUM_LIT:5> ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_round ( self ) : <EOL> cases = [ <EOL> ( self . table . double_col . round ( ) , '<STR_LIT>' ) , <EOL> ( self . table . double_col . round ( <NUM_LIT:0> ) , '<STR_LIT>' ) , <EOL> ( self . table . double_col . round ( <NUM_LIT:2> , ) , '<STR_LIT>' ) , <EOL> ( self . table . double_col . round ( self . table . tinyint_col ) , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_hash ( self ) : <EOL> expr = self . table . int_col . hash ( ) <EOL> assert isinstance ( expr , ir . Int64Array ) <EOL> assert isinstance ( self . table . int_col . sum ( ) . hash ( ) , <EOL> ir . Int64Scalar ) <EOL> cases = [ <EOL> ( self . table . int_col . hash ( ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_reduction_where ( self ) : <EOL> cond = self . table . bigint_col < <NUM_LIT> <EOL> c = self . table . double_col <EOL> tmp = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> cases = [ <EOL> ( c . sum ( where = cond ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ( c . count ( where = cond ) , tmp . format ( '<STR_LIT:count>' ) ) , <EOL> ( c . mean ( where = cond ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ( c . max ( where = cond ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ( c . min ( where = cond ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ( c . std ( where = cond ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ( c . std ( where = cond , how = '<STR_LIT>' ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ( c . var ( where = cond ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ( c . var ( where = cond , how = '<STR_LIT>' ) , tmp . format ( '<STR_LIT>' ) ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_reduction_invalid_where ( self ) : <EOL> condbad_literal = L ( '<STR_LIT:T>' ) <EOL> c = self . table . double_col <EOL> for reduction in [ c . sum , c . count , c . mean , c . max , c . min ] : <EOL> with self . assertRaises ( TypeError ) : <EOL> reduction ( where = condbad_literal ) <EOL> class TestCaseExprs ( unittest . TestCase , ExprSQLTest , ExprTestCases ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> def test_isnull_1_0 ( self ) : <EOL> expr = self . table . g . isnull ( ) . ifelse ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> result = self . _translate ( expr ) <EOL> expected = '<STR_LIT>' <EOL> assert result == expected <EOL> result = self . _translate ( expr . sum ( ) ) <EOL> expected = '<STR_LIT>' <EOL> assert result == expected <EOL> def test_simple_case ( self ) : <EOL> expr = self . _case_simple_case ( ) <EOL> result = self . _translate ( expr ) <EOL> expected = """<STR_LIT>""" <EOL> assert result == expected <EOL> def test_search_case ( self ) : <EOL> expr = self . _case_search_case ( ) <EOL> result = self . _translate ( expr ) <EOL> expected = """<STR_LIT>""" <EOL> assert result == expected <EOL> def test_where_use_if ( self ) : <EOL> expr = ibis . where ( self . table . f > <NUM_LIT:0> , self . table . e , self . table . a ) <EOL> assert isinstance ( expr , ir . FloatValue ) <EOL> result = self . _translate ( expr ) <EOL> expected = "<STR_LIT>" <EOL> assert result == expected <EOL> def test_nullif_ifnull ( self ) : <EOL> table = self . con . table ( '<STR_LIT>' ) <EOL> f = table . l_quantity <EOL> cases = [ <EOL> ( f . nullif ( f == <NUM_LIT:0> ) , <EOL> '<STR_LIT>' ) , <EOL> ( f . fillna ( <NUM_LIT:0> ) , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_decimal_fillna_cast_arg ( self ) : <EOL> table = self . con . table ( '<STR_LIT>' ) <EOL> f = table . l_extendedprice <EOL> cases = [ <EOL> ( f . fillna ( <NUM_LIT:0> ) , <EOL> '<STR_LIT>' ) , <EOL> ( f . fillna ( <NUM_LIT:0.0> ) , '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> class TestBucketHistogram ( unittest . TestCase , ExprSQLTest ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> def test_bucket_to_case ( self ) : <EOL> buckets = [ <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ] <EOL> expr1 = self . table . f . bucket ( buckets ) <EOL> expected1 = """<STR_LIT>""" <EOL> expr2 = self . table . f . bucket ( buckets , close_extreme = False ) <EOL> expected2 = """<STR_LIT>""" <EOL> expr3 = self . table . f . bucket ( buckets , closed = '<STR_LIT:right>' ) <EOL> expected3 = """<STR_LIT>""" <EOL> expr4 = self . table . f . bucket ( buckets , closed = '<STR_LIT:right>' , <EOL> close_extreme = False ) <EOL> expected4 = """<STR_LIT>""" <EOL> expr5 = self . table . f . bucket ( buckets , include_under = True ) <EOL> expected5 = """<STR_LIT>""" <EOL> expr6 = self . table . f . bucket ( buckets , <EOL> include_under = True , <EOL> include_over = True ) <EOL> expected6 = """<STR_LIT>""" <EOL> expr7 = self . table . f . bucket ( buckets , <EOL> close_extreme = False , <EOL> include_under = True , <EOL> include_over = True ) <EOL> expected7 = """<STR_LIT>""" <EOL> expr8 = self . table . f . bucket ( buckets , closed = '<STR_LIT:right>' , <EOL> close_extreme = False , <EOL> include_under = True ) <EOL> expected8 = """<STR_LIT>""" <EOL> expr9 = self . table . f . bucket ( [ <NUM_LIT:10> ] , closed = '<STR_LIT:right>' , <EOL> include_over = True , <EOL> include_under = True ) <EOL> expected9 = """<STR_LIT>""" <EOL> expr10 = self . table . f . bucket ( [ <NUM_LIT:10> ] , include_over = True , <EOL> include_under = True ) <EOL> expected10 = """<STR_LIT>""" <EOL> cases = [ <EOL> ( expr1 , expected1 ) , <EOL> ( expr2 , expected2 ) , <EOL> ( expr3 , expected3 ) , <EOL> ( expr4 , expected4 ) , <EOL> ( expr5 , expected5 ) , <EOL> ( expr6 , expected6 ) , <EOL> ( expr7 , expected7 ) , <EOL> ( expr8 , expected8 ) , <EOL> ( expr9 , expected9 ) , <EOL> ( expr10 , expected10 ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_cast_category_to_int_noop ( self ) : <EOL> expr = ( self . table . f . bucket ( [ <NUM_LIT:10> ] , include_over = True , <EOL> include_under = True ) <EOL> . cast ( '<STR_LIT>' ) ) <EOL> expected = """<STR_LIT>""" <EOL> expr2 = ( self . table . f . bucket ( [ <NUM_LIT:10> ] , include_over = True , <EOL> include_under = True ) <EOL> . cast ( '<STR_LIT>' ) ) <EOL> expected2 = """<STR_LIT>""" <EOL> self . _check_expr_cases ( [ ( expr , expected ) , <EOL> ( expr2 , expected2 ) ] ) <EOL> def test_bucket_assign_labels ( self ) : <EOL> buckets = [ <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ] <EOL> bucket = self . table . f . bucket ( buckets , include_under = True ) <EOL> size = self . table . group_by ( bucket . name ( '<STR_LIT>' ) ) . size ( ) <EOL> labelled = size . tier . label ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> nulls = '<STR_LIT:error>' ) . name ( '<STR_LIT>' ) <EOL> expr = size [ labelled , size [ '<STR_LIT:count>' ] ] <EOL> expected = """<STR_LIT>""" <EOL> result = to_sql ( expr ) <EOL> assert result == expected <EOL> self . assertRaises ( ValueError , size . tier . label , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . assertRaises ( ValueError , size . tier . label , <EOL> [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' ] ) <EOL> class TestInNotIn ( unittest . TestCase , ExprSQLTest ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> def test_field_in_literals ( self ) : <EOL> cases = [ <EOL> ( self . table . g . isin ( [ "<STR_LIT:foo>" , "<STR_LIT:bar>" , "<STR_LIT>" ] ) , <EOL> "<STR_LIT>" ) , <EOL> ( self . table . g . notin ( [ "<STR_LIT:foo>" , "<STR_LIT:bar>" , "<STR_LIT>" ] ) , <EOL> "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_literal_in_list ( self ) : <EOL> cases = [ <EOL> ( L ( <NUM_LIT:2> ) . isin ( [ self . table . a , self . table . b , self . table . c ] ) , <EOL> '<STR_LIT>' ) , <EOL> ( L ( <NUM_LIT:2> ) . notin ( [ self . table . a , self . table . b , self . table . c ] ) , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_isin_notin_in_select ( self ) : <EOL> filtered = self . table [ self . table . g . isin ( [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] ) ] <EOL> result = to_sql ( filtered ) <EOL> expected = """<STR_LIT>""" <EOL> assert result == expected <EOL> filtered = self . table [ self . table . g . notin ( [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] ) ] <EOL> result = to_sql ( filtered ) <EOL> expected = """<STR_LIT>""" <EOL> assert result == expected <EOL> class TestCoalesceGreaterLeast ( unittest . TestCase , ExprSQLTest ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> def test_coalesce ( self ) : <EOL> t = self . table <EOL> cases = [ <EOL> ( ibis . coalesce ( t . string_col , '<STR_LIT:foo>' ) , <EOL> "<STR_LIT>" ) , <EOL> ( ibis . coalesce ( t . int_col , t . bigint_col ) , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_greatest ( self ) : <EOL> t = self . table <EOL> cases = [ <EOL> ( ibis . greatest ( t . string_col , '<STR_LIT:foo>' ) , <EOL> "<STR_LIT>" ) , <EOL> ( ibis . greatest ( t . int_col , t . bigint_col ) , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_least ( self ) : <EOL> t = self . table <EOL> cases = [ <EOL> ( ibis . least ( t . string_col , '<STR_LIT:foo>' ) , <EOL> "<STR_LIT>" ) , <EOL> ( ibis . least ( t . int_col , t . bigint_col ) , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> class TestAnalyticFunctions ( unittest . TestCase , ExprSQLTest ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> def test_analytic_exprs ( self ) : <EOL> t = self . table <EOL> w = ibis . window ( order_by = t . float_col ) <EOL> cases = [ <EOL> ( ibis . row_number ( ) . over ( w ) , <EOL> '<STR_LIT>' ) , <EOL> ( t . string_col . lag ( ) , '<STR_LIT>' ) , <EOL> ( t . string_col . lag ( <NUM_LIT:2> ) , '<STR_LIT>' ) , <EOL> ( t . string_col . lag ( default = <NUM_LIT:0> ) , '<STR_LIT>' ) , <EOL> ( t . string_col . lead ( ) , '<STR_LIT>' ) , <EOL> ( t . string_col . lead ( <NUM_LIT:2> ) , '<STR_LIT>' ) , <EOL> ( t . string_col . lead ( default = <NUM_LIT:0> ) , '<STR_LIT>' ) , <EOL> ( t . double_col . first ( ) , '<STR_LIT>' ) , <EOL> ( t . double_col . last ( ) , '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> class TestStringBuiltins ( unittest . TestCase , ExprSQLTest ) : <EOL> def setUp ( self ) : <EOL> self . con = MockConnection ( ) <EOL> self . table = self . con . table ( '<STR_LIT>' ) <EOL> def test_unary_ops ( self ) : <EOL> s = self . table . string_col <EOL> cases = [ <EOL> ( s . lower ( ) , '<STR_LIT>' ) , <EOL> ( s . upper ( ) , '<STR_LIT>' ) , <EOL> ( s . reverse ( ) , '<STR_LIT>' ) , <EOL> ( s . strip ( ) , '<STR_LIT>' ) , <EOL> ( s . lstrip ( ) , '<STR_LIT>' ) , <EOL> ( s . rstrip ( ) , '<STR_LIT>' ) , <EOL> ( s . capitalize ( ) , '<STR_LIT>' ) , <EOL> ( s . length ( ) , '<STR_LIT>' ) , <EOL> ( s . ascii_str ( ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_substr ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . substr ( <NUM_LIT:2> ) , '<STR_LIT>' ) , <EOL> ( self . table . string_col . substr ( <NUM_LIT:0> , <NUM_LIT:3> ) , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_strright ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . right ( <NUM_LIT:4> ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_like ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . like ( '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_rlike ( self ) : <EOL> ex = "<STR_LIT>" <EOL> cases = [ <EOL> ( self . table . string_col . rlike ( '<STR_LIT>' ) , ex ) , <EOL> ( self . table . string_col . re_search ( '<STR_LIT>' ) , ex ) , <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_re_extract ( self ) : <EOL> sql = "<STR_LIT>" <EOL> cases = [ <EOL> ( self . table . string_col . re_extract ( '<STR_LIT>' , <NUM_LIT:0> ) , sql ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_re_replace ( self ) : <EOL> sql = "<STR_LIT>" <EOL> cases = [ <EOL> ( self . table . string_col . re_replace ( '<STR_LIT>' , '<STR_LIT>' ) , sql ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_parse_url ( self ) : <EOL> sql = "<STR_LIT>" <EOL> cases = [ <EOL> ( self . table . string_col . parse_url ( '<STR_LIT>' ) , sql ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_repeat ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . repeat ( <NUM_LIT:2> ) , '<STR_LIT>' ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_translate ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . translate ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , <EOL> "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_find ( self ) : <EOL> s = self . table . string_col <EOL> i1 = self . table . tinyint_col <EOL> cases = [ <EOL> ( s . find ( '<STR_LIT:a>' ) , "<STR_LIT>" ) , <EOL> ( s . find ( '<STR_LIT:a>' , <NUM_LIT:2> ) , "<STR_LIT>" ) , <EOL> ( s . find ( '<STR_LIT:a>' , start = i1 ) , <EOL> "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_lpad ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . lpad ( <NUM_LIT:1> , '<STR_LIT:a>' ) , "<STR_LIT>" ) , <EOL> ( self . table . string_col . lpad ( <NUM_LIT> ) , "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_rpad ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . rpad ( <NUM_LIT:1> , '<STR_LIT:a>' ) , "<STR_LIT>" ) , <EOL> ( self . table . string_col . rpad ( <NUM_LIT> ) , "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_find_in_set ( self ) : <EOL> cases = [ <EOL> ( self . table . string_col . find_in_set ( [ '<STR_LIT:a>' ] ) , <EOL> "<STR_LIT>" ) , <EOL> ( self . table . string_col . find_in_set ( [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) , <EOL> "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> def test_string_join ( self ) : <EOL> cases = [ <EOL> ( L ( '<STR_LIT:U+002C>' ) . join ( [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) , "<STR_LIT>" ) <EOL> ] <EOL> self . _check_expr_cases ( cases ) <EOL> class TestImpalaExprs ( ImpalaE2E , unittest . TestCase , ExprTestCases ) : <EOL> def test_embedded_identifier_quoting ( self ) : <EOL> t = self . con . table ( '<STR_LIT>' ) <EOL> expr = ( t [ [ ( t . double_col * <NUM_LIT:2> ) . name ( '<STR_LIT>' ) ] ] <EOL> [ '<STR_LIT>' ] . sum ( ) ) <EOL> expr . execute ( ) <EOL> def test_table_info ( self ) : <EOL> t = self . con . table ( '<STR_LIT>' ) <EOL> buf = StringIO ( ) <EOL> t . info ( buf = buf ) <EOL> assert buf . getvalue ( ) is not None <EOL> def test_execute_exprs_no_table_ref ( self ) : <EOL> cases = [ <EOL> ( L ( <NUM_LIT:1> ) + L ( <NUM_LIT:2> ) , <NUM_LIT:3> ) <EOL> ] <EOL> for expr , expected in cases : <EOL> result = self . con . execute ( expr ) <EOL> assert result == expected <EOL> exlist = ibis . api . expr_list ( [ L ( <NUM_LIT:1> ) . name ( '<STR_LIT:a>' ) , <EOL> ibis . now ( ) . name ( '<STR_LIT:b>' ) , <EOL> L ( <NUM_LIT:2> ) . log ( ) . name ( '<STR_LIT:c>' ) ] ) <EOL> self . con . execute ( exlist ) <EOL> def test_summary_execute ( self ) : <EOL> table = self . alltypes <EOL> table = table . set_column ( '<STR_LIT>' , <EOL> table . double_col * <NUM_LIT:2> ) <EOL> expr = table . double_col . summary ( ) <EOL> repr ( expr ) <EOL> result = expr . execute ( ) <EOL> assert isinstance ( result , pd . DataFrame ) <EOL> expr = ( table . group_by ( '<STR_LIT>' ) <EOL> . aggregate ( [ table . double_col . summary ( ) . prefix ( '<STR_LIT>' ) , <EOL> table . float_col . summary ( ) . prefix ( '<STR_LIT>' ) , <EOL> table . string_col . summary ( ) . suffix ( '<STR_LIT>' ) ] ) ) <EOL> result = expr . execute ( ) <EOL> assert isinstance ( result , pd . DataFrame ) <EOL> def test_distinct_array ( self ) : <EOL> table = self . alltypes <EOL> expr = table . string_col . distinct ( ) <EOL> result = self . con . execute ( expr ) <EOL> assert isinstance ( result , pd . Series ) <EOL> def test_decimal_metadata ( self ) : <EOL> table = self . con . table ( '<STR_LIT>' ) <EOL> expr = table . l_quantity <EOL> assert expr . _precision == <NUM_LIT:12> <EOL> assert expr . _scale == <NUM_LIT:2> <EOL> def test_builtins_1 ( self ) : <EOL> table = self . alltypes <EOL> i1 = table . tinyint_col <EOL> i4 = table . int_col <EOL> i8 = table . bigint_col <EOL> d = table . double_col <EOL> s = table . string_col <EOL> exprs = [ <EOL> api . now ( ) , <EOL> api . e , <EOL> i4 . hash ( ) , <EOL> d . hash ( ) , <EOL> s . hash ( ) , <EOL> i1 % <NUM_LIT:5> , <EOL> i4 % <NUM_LIT:10> , <EOL> <NUM_LIT:20> % i1 , <EOL> d % <NUM_LIT:5> , <EOL> i1 . zeroifnull ( ) , <EOL> i4 . zeroifnull ( ) , <EOL> i8 . zeroifnull ( ) , <EOL> i4 . to_timestamp ( '<STR_LIT:s>' ) , <EOL> i4 . to_timestamp ( '<STR_LIT>' ) , <EOL> i4 . to_timestamp ( '<STR_LIT>' ) , <EOL> i8 . to_timestamp ( ) , <EOL> d . abs ( ) , <EOL> d . cast ( '<STR_LIT>' ) , <EOL> d . cast ( '<STR_LIT>' ) , <EOL> d . ceil ( ) , <EOL> d . exp ( ) , <EOL> d . isnull ( ) , <EOL> d . fillna ( <NUM_LIT:0> ) , <EOL> d . floor ( ) , <EOL> d . log ( ) , <EOL> d . ln ( ) , <EOL> d . log2 ( ) , <EOL> d . log10 ( ) , <EOL> d . notnull ( ) , <EOL> d . zeroifnull ( ) , <EOL> d . nullifzero ( ) , <EOL> d . round ( ) , <EOL> d . round ( <NUM_LIT:2> ) , <EOL> d . round ( i1 ) , <EOL> i1 . sign ( ) , <EOL> i4 . sign ( ) , <EOL> d . sign ( ) , <EOL> i1 . convert_base ( <NUM_LIT:10> , <NUM_LIT:2> ) , <EOL> i4 . convert_base ( <NUM_LIT:10> , <NUM_LIT:2> ) , <EOL> i8 . convert_base ( <NUM_LIT:10> , <NUM_LIT:2> ) , <EOL> s . convert_base ( <NUM_LIT:10> , <NUM_LIT:2> ) , <EOL> d . sqrt ( ) , <EOL> d . zeroifnull ( ) , <EOL> <NUM_LIT:5> / i1 . nullif ( <NUM_LIT:0> ) , <EOL> <NUM_LIT:5> / i1 . nullif ( i4 ) , <EOL> <NUM_LIT:5> / i4 . nullif ( <NUM_LIT:0> ) , <EOL> <NUM_LIT:5> / d . nullif ( <NUM_LIT:0> ) , <EOL> api . literal ( <NUM_LIT:5> ) . isin ( [ i1 , i4 , d ] ) , <EOL> d . bucket ( [ <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT:100> ] ) , <EOL> d . bucket ( [ <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ] , include_over = True ) , <EOL> d . bucket ( [ <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ] , include_over = True , close_extreme = False ) , <EOL> d . bucket ( [ <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT:100> ] , include_under = True ) , <EOL> d . histogram ( <NUM_LIT:10> ) , <EOL> d . histogram ( <NUM_LIT:5> , base = <NUM_LIT:10> ) , <EOL> d . histogram ( base = <NUM_LIT:10> , binwidth = <NUM_LIT:5> ) , <EOL> api . coalesce ( table . int_col , <EOL> api . null ( ) , <EOL> table . smallint_col , <EOL> table . bigint_col , <NUM_LIT:5> ) , <EOL> api . greatest ( table . float_col , <EOL> table . double_col , <NUM_LIT:5> ) , <EOL> api . least ( table . string_col , '<STR_LIT:foo>' ) , <EOL> s . contains ( '<STR_LIT>' ) , <EOL> s . like ( '<STR_LIT>' ) , <EOL> s . re_search ( '<STR_LIT>' ) , <EOL> s . re_extract ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> s . re_replace ( '<STR_LIT>' , '<STR_LIT:a>' ) , <EOL> s . repeat ( <NUM_LIT:2> ) , <EOL> s . translate ( "<STR_LIT:a>" , "<STR_LIT:b>" ) , <EOL> s . find ( "<STR_LIT:a>" ) , <EOL> s . lpad ( <NUM_LIT:10> , '<STR_LIT:a>' ) , <EOL> s . rpad ( <NUM_LIT:10> , '<STR_LIT:a>' ) , <EOL> s . find_in_set ( [ "<STR_LIT:a>" ] ) , <EOL> s . lower ( ) , <EOL> s . upper ( ) , <EOL> s . reverse ( ) , <EOL> s . ascii_str ( ) , <EOL> s . length ( ) , <EOL> s . strip ( ) , <EOL> s . lstrip ( ) , <EOL> s . strip ( ) , <EOL> s . left ( i1 ) , <EOL> s . right ( i1 ) , <EOL> s . substr ( i1 , i1 + <NUM_LIT:2> ) , <EOL> s . repeat ( i1 ) <EOL> ] <EOL> proj_exprs = [ expr . name ( '<STR_LIT>' % i ) <EOL> for i , expr in enumerate ( exprs ) ] <EOL> projection = table [ proj_exprs ] <EOL> projection . limit ( <NUM_LIT:10> ) . execute ( ) <EOL> self . _check_impala_output_types_match ( projection ) <EOL> def _check_impala_output_types_match ( self , table ) : <EOL> query = to_sql ( table ) <EOL> t = self . con . sql ( query ) <EOL> def _clean_type ( x ) : <EOL> if isinstance ( x , Category ) : <EOL> x = x . to_integer_type ( ) <EOL> return x <EOL> left , right = t . schema ( ) , table . schema ( ) <EOL> for i , ( n , l , r ) in enumerate ( zip ( left . names , left . types , <EOL> right . types ) ) : <EOL> l = _clean_type ( l ) <EOL> r = _clean_type ( r ) <EOL> if l != r : <EOL> pytest . fail ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( n , l , r ) ) <EOL> def assert_cases_equality ( self , cases ) : <EOL> for expr , expected in cases : <EOL> result = self . con . execute ( expr ) <EOL> assert result == expected , to_sql ( expr ) <EOL> def test_int_builtins ( self ) : <EOL> i8 = L ( <NUM_LIT:50> ) <EOL> i32 = L ( <NUM_LIT> ) <EOL> mod_cases = [ <EOL> ( i8 % <NUM_LIT:5> , <NUM_LIT:0> ) , <EOL> ( i32 % <NUM_LIT:10> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> % i8 , <NUM_LIT:0> ) , <EOL> ] <EOL> nullif_cases = [ <EOL> ( <NUM_LIT:5> / i8 . nullif ( <NUM_LIT:0> ) , <NUM_LIT:0.1> ) , <EOL> ( <NUM_LIT:5> / i8 . nullif ( i32 ) , <NUM_LIT:0.1> ) , <EOL> ( <NUM_LIT:5> / i32 . nullif ( <NUM_LIT:0> ) , <NUM_LIT> ) , <EOL> ( i32 . zeroifnull ( ) , <NUM_LIT> ) , <EOL> ] <EOL> self . assert_cases_equality ( mod_cases + nullif_cases ) <EOL> def test_column_types ( self ) : <EOL> df = self . alltypes . execute ( ) <EOL> assert df . tinyint_col . dtype . name == '<STR_LIT>' <EOL> assert df . smallint_col . dtype . name == '<STR_LIT>' <EOL> assert df . int_col . dtype . name == '<STR_LIT>' <EOL> assert df . bigint_col . dtype . name == '<STR_LIT>' <EOL> assert df . float_col . dtype . name == '<STR_LIT>' <EOL> assert df . double_col . dtype . name == '<STR_LIT>' <EOL> assert pd . core . common . is_datetime64_dtype ( df . timestamp_col . dtype ) <EOL> def test_timestamp_builtins ( self ) : <EOL> i32 = L ( <NUM_LIT> ) <EOL> i64 = L ( <NUM_LIT:5> * <NUM_LIT:10> ** <NUM_LIT:8> ) <EOL> stamp = ibis . timestamp ( '<STR_LIT>' ) <EOL> timestamp_cases = [ <EOL> ( i32 . to_timestamp ( '<STR_LIT:s>' ) , pd . to_datetime ( <NUM_LIT> , unit = '<STR_LIT:s>' ) ) , <EOL> ( i32 . to_timestamp ( '<STR_LIT>' ) , pd . to_datetime ( <NUM_LIT> , unit = '<STR_LIT>' ) ) , <EOL> ( i64 . to_timestamp ( ) , pd . to_datetime ( <NUM_LIT:5> * <NUM_LIT:10> ** <NUM_LIT:8> , unit = '<STR_LIT:s>' ) ) , <EOL> ( stamp . truncate ( '<STR_LIT:y>' ) , pd . Timestamp ( '<STR_LIT>' ) ) , <EOL> ( stamp . truncate ( '<STR_LIT:m>' ) , pd . Timestamp ( '<STR_LIT>' ) ) , <EOL> ( stamp . truncate ( '<STR_LIT:d>' ) , pd . Timestamp ( '<STR_LIT>' ) ) , <EOL> ( stamp . truncate ( '<STR_LIT:h>' ) , pd . Timestamp ( '<STR_LIT>' ) ) , <EOL> ( stamp . truncate ( '<STR_LIT>' ) , pd . Timestamp ( '<STR_LIT>' ) ) <EOL> ] <EOL> self . assert_cases_equality ( timestamp_cases ) <EOL> def test_decimal_builtins ( self ) : <EOL> d = L ( <NUM_LIT> ) <EOL> general_cases = [ <EOL> ( L ( - <NUM_LIT:5> ) . abs ( ) , <NUM_LIT:5> ) , <EOL> ( d . cast ( '<STR_LIT>' ) , <NUM_LIT:5> ) , <EOL> ( d . ceil ( ) , <NUM_LIT:6> ) , <EOL> ( d . isnull ( ) , False ) , <EOL> ( d . floor ( ) , <NUM_LIT:5> ) , <EOL> ( d . notnull ( ) , True ) , <EOL> ( d . round ( ) , <NUM_LIT:5> ) , <EOL> ( d . round ( <NUM_LIT:2> ) , Decimal ( '<STR_LIT>' ) ) , <EOL> ( d . sign ( ) , <NUM_LIT:1> ) , <EOL> ] <EOL> self . assert_cases_equality ( general_cases ) <EOL> def test_decimal_builtins_2 ( self ) : <EOL> d = L ( '<STR_LIT>' ) <EOL> dc = d . cast ( '<STR_LIT>' ) <EOL> cases = [ <EOL> ( dc % <NUM_LIT:5> , Decimal ( '<STR_LIT>' ) ) , <EOL> ( dc . fillna ( <NUM_LIT:0> ) , Decimal ( '<STR_LIT>' ) ) , <EOL> ( dc . exp ( ) , <NUM_LIT> ) , <EOL> ( dc . log ( ) , <NUM_LIT> ) , <EOL> ( dc . log2 ( ) , <NUM_LIT> ) , <EOL> ( dc . log10 ( ) , <NUM_LIT> ) , <EOL> ( dc . sqrt ( ) , <NUM_LIT> ) , <EOL> ( dc . zeroifnull ( ) , Decimal ( '<STR_LIT>' ) ) , <EOL> ( - dc , Decimal ( '<STR_LIT>' ) ) <EOL> ] <EOL> for expr , expected in cases : <EOL> result = self . con . execute ( expr ) <EOL> if isinstance ( expected , Decimal ) : <EOL> tol = Decimal ( '<STR_LIT>' ) <EOL> else : <EOL> tol = <NUM_LIT> <EOL> approx_equal ( result , expected , tol ) <EOL> def test_string_functions ( self ) : <EOL> string = L ( '<STR_LIT>' ) <EOL> strip_string = L ( '<STR_LIT>' ) <EOL> cases = [ <EOL> ( string . length ( ) , <NUM_LIT:4> ) , <EOL> ( L ( '<STR_LIT>' ) . lower ( ) , '<STR_LIT>' ) , <EOL> ( string . upper ( ) , '<STR_LIT>' ) , <EOL> ( string . reverse ( ) , '<STR_LIT>' ) , <EOL> ( string . ascii_str ( ) , <NUM_LIT> ) , <EOL> ( strip_string . strip ( ) , '<STR_LIT:a>' ) , <EOL> ( strip_string . lstrip ( ) , '<STR_LIT>' ) , <EOL> ( strip_string . rstrip ( ) , '<STR_LIT>' ) , <EOL> ( string . capitalize ( ) , '<STR_LIT>' ) , <EOL> ( string . substr ( <NUM_LIT:0> , <NUM_LIT:2> ) , '<STR_LIT>' ) , <EOL> ( string . left ( <NUM_LIT:2> ) , '<STR_LIT>' ) , <EOL> ( string . right ( <NUM_LIT:2> ) , '<STR_LIT>' ) , <EOL> ( string . repeat ( <NUM_LIT:2> ) , '<STR_LIT>' ) , <EOL> ( L ( '<STR_LIT>' ) . translate ( '<STR_LIT>' , '<STR_LIT:abc>' ) , '<STR_LIT>' ) , <EOL> ( string . find ( '<STR_LIT:a>' ) , <NUM_LIT:0> ) , <EOL> ( L ( '<STR_LIT>' ) . find ( '<STR_LIT:b>' , <NUM_LIT:2> ) , <NUM_LIT:5> ) , <EOL> ( string . lpad ( <NUM_LIT:1> , '<STR_LIT:->' ) , '<STR_LIT:a>' ) , <EOL> ( string . lpad ( <NUM_LIT:5> ) , '<STR_LIT>' ) , <EOL> ( string . rpad ( <NUM_LIT:1> , '<STR_LIT:->' ) , '<STR_LIT:a>' ) , <EOL> ( string . rpad ( <NUM_LIT:5> ) , '<STR_LIT>' ) , <EOL> ( string . find_in_set ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT>' ] ) , <NUM_LIT:2> ) , <EOL> ( L ( '<STR_LIT:U+002CU+0020>' ) . join ( [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) , '<STR_LIT>' ) , <EOL> ( string . like ( '<STR_LIT>' ) , True ) , <EOL> ( string . re_search ( '<STR_LIT>' ) , True ) , <EOL> ( string . re_extract ( '<STR_LIT>' , <NUM_LIT:0> ) , '<STR_LIT:a>' ) , <EOL> ( string . re_replace ( '<STR_LIT>' , '<STR_LIT:2>' ) , '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_cases ( cases ) <EOL> def _check_cases ( self , cases ) : <EOL> for expr , expected in cases : <EOL> result = self . con . execute ( expr ) <EOL> assert result == expected <EOL> def test_parse_url ( self ) : <EOL> cases = [ <EOL> ( L ( "<STR_LIT>" ) . parse_url ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" ) , <EOL> ( L ( '<STR_LIT>' ) <EOL> . parse_url ( '<STR_LIT>' , '<STR_LIT:v>' ) , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> self . _check_cases ( cases ) <EOL> def test_div_floordiv ( self ) : <EOL> cases = [ <EOL> ( L ( <NUM_LIT:7> ) / <NUM_LIT:2> , <NUM_LIT> ) , <EOL> ( L ( <NUM_LIT:7> ) // <NUM_LIT:2> , <NUM_LIT:3> ) , <EOL> ( L ( <NUM_LIT:7> ) . floordiv ( <NUM_LIT:2> ) , <NUM_LIT:3> ) , <EOL> ( L ( <NUM_LIT:2> ) . rfloordiv ( <NUM_LIT:7> ) , <NUM_LIT:3> ) , <EOL> ] <EOL> for expr , expected in cases : <EOL> result = self . con . execute ( expr ) <EOL> assert result == expected <EOL> def test_filter_predicates ( self ) : <EOL> t = self . con . table ( '<STR_LIT>' ) <EOL> predicates = [ <EOL> lambda x : x . n_name . lower ( ) . like ( '<STR_LIT>' ) , <EOL> lambda x : x . n_name . lower ( ) . contains ( '<STR_LIT>' ) , <EOL> lambda x : x . n_name . lower ( ) . rlike ( '<STR_LIT>' ) <EOL> ] <EOL> expr = t <EOL> for pred in predicates : <EOL> expr = expr [ pred ( expr ) ] . projection ( [ expr ] ) <EOL> expr . execute ( ) <EOL> def test_histogram_value_counts ( self ) : <EOL> t = self . alltypes <EOL> expr = t . double_col . histogram ( <NUM_LIT:10> ) . value_counts ( ) <EOL> expr . execute ( ) <EOL> def test_casted_expr_impala_bug ( self ) : <EOL> expr = self . alltypes . string_col . cast ( '<STR_LIT>' ) . value_counts ( ) <EOL> expr . execute ( ) <EOL> def test_decimal_timestamp_builtins ( self ) : <EOL> table = self . con . table ( '<STR_LIT>' ) <EOL> dc = table . l_quantity <EOL> ts = table . l_receiptdate . cast ( '<STR_LIT>' ) <EOL> exprs = [ <EOL> dc % <NUM_LIT:10> , <EOL> dc + <NUM_LIT:5> , <EOL> dc + dc , <EOL> dc / <NUM_LIT:2> , <EOL> dc * <NUM_LIT:2> , <EOL> dc ** <NUM_LIT:2> , <EOL> dc . cast ( '<STR_LIT>' ) , <EOL> api . where ( table . l_discount > <NUM_LIT:0> , <EOL> dc * table . l_discount , api . NA ) , <EOL> dc . fillna ( <NUM_LIT:0> ) , <EOL> ts < ( ibis . now ( ) + ibis . month ( <NUM_LIT:3> ) ) , <EOL> ts < ( ibis . timestamp ( '<STR_LIT>' ) + ibis . month ( <NUM_LIT:3> ) ) , <EOL> dc . hash ( ) , <EOL> ts . hash ( ) , <EOL> ts . truncate ( '<STR_LIT:y>' ) , <EOL> ts . truncate ( '<STR_LIT:q>' ) , <EOL> ts . truncate ( '<STR_LIT>' ) , <EOL> ts . truncate ( '<STR_LIT:d>' ) , <EOL> ts . truncate ( '<STR_LIT:w>' ) , <EOL> ts . truncate ( '<STR_LIT:h>' ) , <EOL> ts . truncate ( '<STR_LIT>' ) , <EOL> ] <EOL> timestamp_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> for field in timestamp_fields : <EOL> if hasattr ( ts , field ) : <EOL> exprs . append ( getattr ( ts , field ) ( ) ) <EOL> offset = getattr ( ibis , field ) ( <NUM_LIT:2> ) <EOL> exprs . append ( ts + offset ) <EOL> exprs . append ( ts - offset ) <EOL> proj_exprs = [ expr . name ( '<STR_LIT>' % i ) <EOL> for i , expr in enumerate ( exprs ) ] <EOL> projection = table [ proj_exprs ] . limit ( <NUM_LIT:10> ) <EOL> projection . execute ( ) <EOL> def test_timestamp_scalar_in_filter ( self ) : <EOL> table = self . alltypes <EOL> expr = ( table . filter ( [ table . timestamp_col < <EOL> ( ibis . timestamp ( '<STR_LIT>' ) + ibis . month ( <NUM_LIT:3> ) ) , <EOL> table . timestamp_col < ( ibis . now ( ) + ibis . day ( <NUM_LIT:10> ) ) <EOL> ] ) <EOL> . count ( ) ) <EOL> expr . execute ( ) <EOL> def test_aggregations ( self ) : <EOL> table = self . alltypes . limit ( <NUM_LIT:100> ) <EOL> d = table . double_col <EOL> s = table . string_col <EOL> cond = table . string_col . isin ( [ '<STR_LIT:1>' , '<STR_LIT>' ] ) <EOL> exprs = [ <EOL> table . bool_col . count ( ) , <EOL> d . sum ( ) , <EOL> d . mean ( ) , <EOL> d . min ( ) , <EOL> d . max ( ) , <EOL> s . approx_nunique ( ) , <EOL> d . approx_median ( ) , <EOL> s . group_concat ( ) , <EOL> d . std ( ) , <EOL> d . std ( how = '<STR_LIT>' ) , <EOL> d . var ( ) , <EOL> d . var ( how = '<STR_LIT>' ) , <EOL> table . bool_col . any ( ) , <EOL> table . bool_col . notany ( ) , <EOL> - table . bool_col . any ( ) , <EOL> table . bool_col . all ( ) , <EOL> table . bool_col . notall ( ) , <EOL> - table . bool_col . all ( ) , <EOL> table . bool_col . count ( where = cond ) , <EOL> d . sum ( where = cond ) , <EOL> d . mean ( where = cond ) , <EOL> d . min ( where = cond ) , <EOL> d . max ( where = cond ) , <EOL> d . std ( where = cond ) , <EOL> d . var ( where = cond ) , <EOL> ] <EOL> agg_exprs = [ expr . name ( '<STR_LIT>' % i ) <EOL> for i , expr in enumerate ( exprs ) ] <EOL> agged_table = table . aggregate ( agg_exprs ) <EOL> agged_table . execute ( ) <EOL> def test_analytic_functions ( self ) : <EOL> t = self . alltypes . limit ( <NUM_LIT:1000> ) <EOL> g = t . group_by ( '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) <EOL> f = t . float_col <EOL> exprs = [ <EOL> f . lag ( ) , <EOL> f . lead ( ) , <EOL> f . rank ( ) , <EOL> f . dense_rank ( ) , <EOL> f . first ( ) , <EOL> f . last ( ) , <EOL> f . first ( ) . over ( ibis . window ( preceding = <NUM_LIT:10> ) ) , <EOL> f . first ( ) . over ( ibis . window ( following = <NUM_LIT:10> ) ) , <EOL> ibis . row_number ( ) , <EOL> f . cumsum ( ) , <EOL> f . cummean ( ) , <EOL> f . cummin ( ) , <EOL> f . cummax ( ) , <EOL> ( f == <NUM_LIT:0> ) . cumany ( ) , <EOL> ( f == <NUM_LIT:0> ) . cumall ( ) , <EOL> f . sum ( ) , <EOL> f . mean ( ) , <EOL> f . min ( ) , <EOL> f . max ( ) <EOL> ] <EOL> proj_exprs = [ expr . name ( '<STR_LIT>' % i ) <EOL> for i , expr in enumerate ( exprs ) ] <EOL> proj_table = g . mutate ( proj_exprs ) <EOL> proj_table . execute ( ) <EOL> def test_anti_join_self_reference_works ( self ) : <EOL> case = self . _case_self_reference_limit_exists ( ) <EOL> self . con . explain ( case ) <EOL> def test_tpch_self_join_failure ( self ) : <EOL> region = self . con . table ( '<STR_LIT>' ) <EOL> nation = self . con . table ( '<STR_LIT>' ) <EOL> customer = self . con . table ( '<STR_LIT>' ) <EOL> orders = self . con . table ( '<STR_LIT>' ) <EOL> fields_of_interest = [ <EOL> region . r_name . name ( '<STR_LIT>' ) , <EOL> nation . n_name . name ( '<STR_LIT>' ) , <EOL> orders . o_totalprice . name ( '<STR_LIT>' ) , <EOL> orders . o_orderdate . cast ( '<STR_LIT>' ) . name ( '<STR_LIT>' ) ] <EOL> joined_all = ( <EOL> region . join ( nation , region . r_regionkey == nation . n_regionkey ) <EOL> . join ( customer , customer . c_nationkey == nation . n_nationkey ) <EOL> . join ( orders , orders . o_custkey == customer . c_custkey ) <EOL> [ fields_of_interest ] ) <EOL> year = joined_all . odate . year ( ) . name ( '<STR_LIT>' ) <EOL> total = joined_all . amount . sum ( ) . cast ( '<STR_LIT>' ) . name ( '<STR_LIT>' ) <EOL> annual_amounts = ( joined_all <EOL> . group_by ( [ '<STR_LIT>' , year ] ) <EOL> . aggregate ( total ) ) <EOL> current = annual_amounts <EOL> prior = annual_amounts . view ( ) <EOL> yoy_change = ( current . total - prior . total ) . name ( '<STR_LIT>' ) <EOL> yoy = ( current . join ( prior , ( ( current . region == prior . region ) & <EOL> ( current . year == ( prior . year - <NUM_LIT:1> ) ) ) ) <EOL> [ current . region , current . year , yoy_change ] ) <EOL> self . con . explain ( yoy ) <EOL> def test_tpch_correlated_subquery_failure ( self ) : <EOL> region = self . con . table ( '<STR_LIT>' ) <EOL> nation = self . con . table ( '<STR_LIT>' ) <EOL> customer = self . con . table ( '<STR_LIT>' ) <EOL> orders = self . con . table ( '<STR_LIT>' ) <EOL> fields_of_interest = [ customer , <EOL> region . r_name . name ( '<STR_LIT>' ) , <EOL> orders . o_totalprice . name ( '<STR_LIT>' ) , <EOL> orders . o_orderdate <EOL> . cast ( '<STR_LIT>' ) . name ( '<STR_LIT>' ) ] <EOL> tpch = ( region . join ( nation , region . r_regionkey == nation . n_regionkey ) <EOL> . join ( customer , customer . c_nationkey == nation . n_nationkey ) <EOL> . join ( orders , orders . o_custkey == customer . c_custkey ) <EOL> [ fields_of_interest ] ) <EOL> t2 = tpch . view ( ) <EOL> conditional_avg = t2 [ ( t2 . region == tpch . region ) ] . amount . mean ( ) <EOL> amount_filter = tpch . amount > conditional_avg <EOL> expr = tpch [ amount_filter ] . limit ( <NUM_LIT:0> ) <EOL> self . con . explain ( expr ) <EOL> def test_non_equijoin ( self ) : <EOL> t = self . con . table ( '<STR_LIT>' ) . limit ( <NUM_LIT:100> ) <EOL> t2 = t . view ( ) <EOL> expr = t . join ( t2 , t . tinyint_col < t2 . timestamp_col . minute ( ) ) . count ( ) <EOL> expr . execute ( ) <EOL> def test_char_varchar_types ( self ) : <EOL> sql = """<STR_LIT>""" <EOL> t = self . con . sql ( sql ) <EOL> assert isinstance ( t . varchar_col , api . StringArray ) <EOL> assert isinstance ( t . char_col , api . StringArray ) </s>
