Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user definable functions aggregates and collations #155

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions bin/q
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ import hashlib
import uuid
import cStringIO
import math
import pkg_resources
import logging
import inspect

DEBUG = False


logger = logging.getLogger(__name__)


def get_stdout_encoding(encoding_override=None):
if encoding_override is not None and encoding_override != 'none':
return encoding_override
Expand Down Expand Up @@ -118,12 +125,61 @@ class Sqlite3DB(object):
str: 'TEXT', int: 'INT', long : 'INT' , float: 'FLOAT', None: 'TEXT'}
self.numeric_column_types = set([int, long, float])
self.add_user_functions()
self.add_entrypoints()

def add_user_functions(self):
self.conn.create_function("regexp", 2, regexp)
self.conn.create_function("sha1", 1, sha1)
self.conn.create_aggregate("percentile",2,StrictPercentile)

def _get_entrypoint_argument_count(self, entrypoint):
if isinstance(entrypoint, object):
return len(inspect.getargspec(entrypoint.step).args[1:])

return len(inspect.getargspec(entrypoint).args)

def add_entrypoints(self):
for entry_point in pkg_resources.iter_entry_points(group='q_functions'):
try:
loaded = entry_point.load()
self.conn.create_function(
entry_point.name,
self._get_entrypoint_argument_count(loaded),
loaded,
)
except ImportError:
logger.warning(
"Could not load entry point function: %s",
entry_point,
)

for entry_point in pkg_resources.iter_entry_points(group='q_aggregates'):
try:
loaded = entry_point.load()
self.conn.create_aggregate(
entry_point.name,
self._get_entrypoint_argument_count(loaded),
loaded,
)
except ImportError:
logger.warning(
"Could not load entry point aggregate: %s",
entry_point,
)

for entry_point in pkg_resources.iter_entry_points(group='q_collations'):
try:
loaded = entry_point.load()
self.conn.create_collation(
entry_point.name,
loaded,
)
except ImportError:
logger.warning(
"Could not load entry point collations: %s",
entry_point,
)

def is_numeric_type(self, column_type):
return column_type in self.numeric_column_types

Expand Down Expand Up @@ -1742,6 +1798,9 @@ def run_standalone():
print >> sys.stderr, "Max column length limit must be a positive integer (%s)" % max_column_length_limit
sys.exit(31)

if options.verbose:
logging.basicConfig(level=logging.DEBUG)

default_input_params = QInputParams(skip_header=options.skip_header,
delimiter=options.delimiter,
input_encoding=options.encoding,
Expand Down