1
1
'''Omnisci TextEncodingNone type that corresponds to Omnisci type TEXT ENCODED NONE.
2
2
'''
3
3
4
- __all__ = ['TextEncodingNonePointer' , 'TextEncodingNone' , 'OmnisciTextEncodingNoneType ' ]
4
+ __all__ = ['TextEncodingNonePointer' , 'TextEncodingNone' , 'HeavyDBTextEncodingNoneType ' ]
5
5
6
+ import operator
6
7
from rbc import typesystem
8
+ from rbc .targetinfo import TargetInfo
9
+ from rbc .errors import RequireLiteralValue
7
10
from .buffer import (
8
11
BufferPointer , Buffer , OmnisciBufferType ,
9
12
omnisci_buffer_constructor )
10
- from numba .core import types , extending
13
+ from numba .core import types , extending , cgutils
14
+ from llvmlite import ir
15
+ from typing import Union
11
16
12
17
13
- class OmnisciTextEncodingNoneType (OmnisciBufferType ):
18
+ class HeavyDBTextEncodingNoneType (OmnisciBufferType ):
14
19
"""Omnisci TextEncodingNone type for RBC typesystem.
15
20
"""
16
21
22
+ @property
23
+ def numba_pointer_type (self ):
24
+ return TextEncodingNonePointer
25
+
17
26
@classmethod
18
27
def preprocess_args (cls , args ):
19
28
element_type = typesystem .Type .fromstring ('char8' )
@@ -32,7 +41,8 @@ def match(self, other):
32
41
return 2
33
42
34
43
35
- TextEncodingNonePointer = BufferPointer
44
+ class TextEncodingNonePointer (BufferPointer ):
45
+ pass
36
46
37
47
38
48
class TextEncodingNone (Buffer ):
@@ -53,25 +63,91 @@ class TextEncodingNone(Buffer):
53
63
54
64
from rbc.heavydb import TextEncodingNone
55
65
56
- @omnisci ('TextEncodingNone(int32, int32)')
66
+ @heavydb ('TextEncodingNone(int32, int32)')
57
67
def make_abc(first, n):
58
68
r = TextEncodingNone(n)
59
69
for i in range(n):
60
70
r[i] = first + i
61
71
return r
72
+
73
+
74
+ .. code-block:: python
75
+
76
+ from rbc.heavydb import TextEncodingNone
77
+ @heavydb('TextEncodingNone()')
78
+ def make_text():
79
+ return TextEncodingNone('some text here')
80
+
62
81
'''
63
82
64
- def __init__ (self , size : int ):
83
+ def __init__ (self , size : Union [ int , str ] ):
65
84
pass
66
85
67
86
87
+ @extending .overload (operator .eq )
88
+ def text_encoding_none_eq (a , b ):
89
+ if isinstance (a , TextEncodingNonePointer ) and isinstance (b , TextEncodingNonePointer ):
90
+
91
+ def impl (a , b ):
92
+ if len (a ) != len (b ):
93
+ return False
94
+ for i in range (0 , len (a )):
95
+ if a [i ] != b [i ]:
96
+ return False
97
+ return True
98
+ return impl
99
+ elif isinstance (a , TextEncodingNonePointer ) and isinstance (b , types .StringLiteral ):
100
+ lv = b .literal_value
101
+ sz = len (lv )
102
+
103
+ def impl (a , b ):
104
+ if len (a ) != sz :
105
+ return False
106
+ t = TextEncodingNone (lv )
107
+ return a == t
108
+ return impl
109
+
110
+
111
+ @extending .overload (operator .ne )
112
+ def text_encoding_none_ne (a , b ):
113
+ if isinstance (a , TextEncodingNonePointer ):
114
+ if isinstance (b , (TextEncodingNonePointer , types .StringLiteral )):
115
+ def impl (a , b ):
116
+ return not (a == b )
117
+ return impl
118
+
119
+
68
120
@extending .lower_builtin (TextEncodingNone , types .Integer )
69
121
def omnisci_text_encoding_none_constructor (context , builder , sig , args ):
70
122
return omnisci_buffer_constructor (context , builder , sig , args )
71
123
72
124
125
+ @extending .lower_builtin (TextEncodingNone , types .StringLiteral )
126
+ def omnisci_text_encoding_none_constructor_literal (context , builder , sig , args ):
127
+ int64_t = ir .IntType (64 )
128
+ int8_t_ptr = ir .IntType (8 ).as_pointer ()
129
+
130
+ literal_value = sig .args [0 ].literal_value
131
+ sz = int64_t (len (literal_value ))
132
+
133
+ # arr = {ptr, size, is_null}*
134
+ arr = omnisci_buffer_constructor (context , builder , sig .return_type (types .int64 ), [sz ])
135
+ ptr = builder .extract_value (builder .load (arr ), [0 ])
136
+
137
+ msg_bytes = literal_value .encode ('utf-8' )
138
+ msg_const = cgutils .make_bytearray (msg_bytes )
139
+ msg_global_var = cgutils .global_constant (builder .module , f"Text({ literal_value } )" , msg_const )
140
+ msg_ptr = builder .bitcast (msg_global_var , int8_t_ptr )
141
+ sizeof_char = TargetInfo ().sizeof ('char' )
142
+ cgutils .raw_memcpy (builder , ptr , msg_ptr , sz , sizeof_char )
143
+ return arr
144
+
145
+
73
146
@extending .type_callable (TextEncodingNone )
74
147
def type_omnisci_text_encoding_none (context ):
75
- def typer (size ):
76
- return typesystem .Type .fromobject ('TextEncodingNone' ).tonumba ()
148
+ def typer (arg ):
149
+ if isinstance (arg , types .UnicodeType ):
150
+ raise RequireLiteralValue ()
151
+ if isinstance (arg , (types .Integer , types .StringLiteral )):
152
+ return typesystem .Type .fromobject ('TextEncodingNone' ).tonumba ()
77
153
return typer
0 commit comments