<commit_before>from sqlalchemy import or_, extract
from sqlalchemy.orm import aliased

from radar.database import db_session
from radar.models import Patient, UnitPatient, Unit, UnitUser, DiseaseGroupPatient, DiseaseGroup, DiseaseGroupUser, User, \
    SDAPatient, SDAContainer, SDAPatientName, SDAPatientNumber


def sda_patient_sub_query(*args):
    patient_alias = aliased(Patient)
    sub_query = db_session.query(SDAPatient)\
        .join(SDAContainer)\
        .join(patient_alias)\
        .filter(Patient.id == patient_alias.id)\
        .filter(*args)\
        .exists()
    return sub_query

def sda_patient_name_sub_query(*args):
    patient_alias = aliased(Patient)
    sub_query = db_session.query(SDAPatientName)\
        .join(SDAPatient)\
        .join(SDAContainer)\
        .join(patient_alias)\
        .filter(Patient.id == patient_alias.id)\
        .filter(*args)\
        .exists()
    return sub_query

def sda_patient_number_sub_query(*args):
    patient_alias = aliased(Patient)
    sub_query = db_session.query(SDAPatientNumber)\
        .join(SDAPatient)\
        .join(SDAContainer)\
        .join(patient_alias)\
        .filter(Patient.id == patient_alias.id)\
        .filter(*args)\
        .exists()
    return sub_query

def first_name_filter(first_name):
    first_name_like = first_name + '%'

    patient_filter = sda_patient_sub_query(SDAPatient.name_given_name.like(first_name_like))
    alias_filter = sda_patient_name_sub_query(SDAPatientName.given_name.like(first_name_like))

    return or_(patient_filter, alias_filter)

def last_name_filter(last_name):
    last_name_like = last_name + '%'

    patient_filter = sda_patient_sub_query(SDAPatient.name_family_name.like(last_name_like))
    alias_filter = sda_patient_name_sub_query(SDAPatientName.family_name.like(last_name_like))

    return or_(patient_filter, alias_filter)

def date_of_birth_filter(date_of_birth):
    return sda_patient_sub_query(SDAPatient.birth_time == date_of_birth)

def patient_number_filter(number):
    # One of the patient's identifiers matches
    number_like = number + '%'
    number_filter = sda_patient_number_sub_query(SDAPatientNumber.number.like(number_like))

    # RaDaR ID matches
    radar_filter = Patient.id == number<commit_msg>Fix date of birth filter<commit_after>from datetime import timedelta
from sqlalchemy import or_, extract
from sqlalchemy.orm import aliased

from radar.database import db_session
from radar.models import Patient, UnitPatient, Unit, UnitUser, DiseaseGroupPatient, DiseaseGroup, DiseaseGroupUser, User, \
    SDAPatient, SDAContainer, SDAPatientName, SDAPatientNumber


def sda_patient_sub_query(*args):
    patient_alias = aliased(Patient)
    sub_query = db_session.query(SDAPatient)\
        .join(SDAContainer)\
        .join(patient_alias)\
        .filter(Patient.id == patient_alias.id)\
        .filter(*args)\
        .exists()
    return sub_query

def sda_patient_name_sub_query(*args):
    patient_alias = aliased(Patient)
    sub_query = db_session.query(SDAPatientName)\
        .join(SDAPatient)\
        .join(SDAContainer)\
        .join(patient_alias)\
        .filter(Patient.id == patient_alias.id)\
        .filter(*args)\
        .exists()
    return sub_query

def sda_patient_number_sub_query(*args):
    patient_alias = aliased(Patient)
    sub_query = db_session.query(SDAPatientNumber)\
        .join(SDAPatient)\
        .join(SDAContainer)\
        .join(patient_alias)\
        .filter(Patient.id == patient_alias.id)\
        .filter(*args)\
        .exists()
    return sub_query

def first_name_filter(first_name):
    first_name_like = first_name + '%'

    patient_filter = sda_patient_sub_query(SDAPatient.name_given_name.like(first_name_like))
    alias_filter = sda_patient_name_sub_query(SDAPatientName.given_name.like(first_name_like))

    return or_(patient_filter, alias_filter)

def last_name_filter(last_name):
    last_name_like = last_name + '%'

    patient_filter = sda_patient_sub_query(SDAPatient.name_family_name.like(last_name_like))
    alias_filter = sda_patient_name_sub_query(SDAPatientName.family_name.like(last_name_like))

    return or_(patient_filter, alias_filter)

def date_of_birth_filter(date_of_birth):
    day = date_of_birth.date()
    next_day = day + timedelta(days=1)
    return sda_patient_sub_query(SDAPatient.birth_time >= day, SDAPatient.birth_time < next_day)

def patient_number_filter(number):
    # One of the patient's identifiers matches
    number_like = number + '%'
    number_filter = sda_patient_number_sub_query(SDAPatientNumber.number.like(number_like))

    # RaDaR ID matches
    radar_filter = Patient.id == number
{'fraction_non_alphanumeric': 0.09460616438356165, 'fraction_numerical': 0.00021404109589041095, 'mean_word_length': 4.024731182795699, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6841238', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1528, 'n_words': 248}