OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_runtime_udf.py
Go to the documentation of this file.
1 import pytest
2 import pandas as pd
3 import numpy as np
4 import pandas.testing as tm
5 
6 pytest.importorskip('rbc')
7 
8 
10  def new_mth(self, con):
11  try:
12  return mth(self, con)
13  except Exception as msg:
14  if type(
15  msg
16  ).__name__ == 'TDBException' and msg.error_msg.startswith(
17  'Runtime UDF and UDTF function registration is disabled'
18  ):
19  print('Ignoring `%s` failure' % (msg.error_msg))
20  return
21  raise
22 
23  new_mth.__name__ = mth.__name__
24  return new_mth
25 
26 
27 @pytest.mark.usefixtures("heavydb_server")
29  def load_test_udf_incr(self, con):
30  con.execute('drop table if exists test_udf_incr')
31  con.execute('create table test_udf_incr (i4 integer, f8 double)')
32  con.execute('insert into test_udf_incr values (1, 2.3);')
33  con.execute('insert into test_udf_incr values (2, 3.4);')
34 
35  @catch_udf_support_disabled
36  def test_udf_incr(self, con):
37  @con('int32(int32)', 'double(double)')
38  def incr(x):
39  return x + 1
40 
41  self.load_test_udf_incr(con)
42 
43  result = list(con.execute('select i4, incr(i4) from test_udf_incr'))
44  expected = [(1, 2), (2, 3)]
45  assert result == expected
46 
47  result = list(con.execute('select f8, incr(f8) from test_udf_incr'))
48  expected = [(2.3, 3.3), (3.4, 4.4)]
49  assert result == expected
50 
51  con.execute('drop table if exists test_udf_incr')
52 
53  @catch_udf_support_disabled
54  def test_udf_incr_read_sql(self, con):
55  @con('int32(int32)', 'double(double)')
56  def incr_read_sql(x):
57  return x + 1
58 
59  self.load_test_udf_incr(con)
60 
61  result = pd.read_sql(
62  '''select i4 as qty, incr_read_sql(i4) as price
63  from test_udf_incr''',
64  con,
65  )
66  expected = pd.DataFrame(
67  {
68  "qty": np.array([1, 2], dtype=np.int64),
69  "price": np.array([2, 3], dtype=np.int64),
70  }
71  )[['qty', 'price']]
72  tm.assert_frame_equal(result, expected)
73 
74  result = pd.read_sql(
75  '''select f8 as qty, incr_read_sql(f8) as price
76  from test_udf_incr''',
77  con,
78  )
79  expected = pd.DataFrame(
80  {
81  "qty": np.array([2.3, 3.4], dtype=np.float64),
82  "price": np.array([3.3, 4.4], dtype=np.float64),
83  }
84  )[['qty', 'price']]
85  tm.assert_frame_equal(result, expected)
86 
87  con.execute('drop table if exists test_udf_incr')