title: SPARQLWrapper QueryBadFormed error for long SELECT query
body: <p>Is there a limit on the size of the query you can use with with a SELECT query? I have a long SELECT query (posted below) that keeps throwing a QueryBadFormed error. I have validated the query on sparql.org and I have ran the query on the triple store. I am using GraphDB 8.6 SE. The query runs fine.</p>
  
  <p>Code:</p>
  
  <pre class="lang-python prettyprint-override"><code>from SPARQLWrapper import SPARQLWrapper, SPARQLWrapper2, JSON,  CSV, TSV
  
  # set endpoint and query
  endpoint = r"http://localhost:7200/repositories/EDR"
  query = get_dental_procedures_query()  # return query below
  
  # get results from endpoint
  sparql = SPARQLWrapper(endpoint)
  setReturnFormat(JSON)  # I've also tried CSV and TSV
  sparql.setQuery(query)
  results = sparql.query().convert()
  </code></pre>
  
  <p>Error returned:</p>
  
  <blockquote>
    <p><code>SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed.</code></p>
  </blockquote>
  
  <p>Here is the query:</p>
  
  <pre><code>BASE    &lt;http://purl.regenstrief.org/NDPBRN/dental-practice/&gt;
  PREFIX  mesial_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Mesial_surface_enamel_of_tooth&gt;
  PREFIX  exception: &lt;http://purl.obolibrary.org/obo/OHD_0000404&gt;
  PREFIX  ada_num: &lt;http://purl.obolibrary.org/obo/OHD_0000065&gt;
  PREFIX  occurrence_date: &lt;http://purl.obolibrary.org/obo/OHD_0000015&gt;
  PREFIX  owl:  &lt;http://www.w3.org/2002/07/owl#&gt;
  PREFIX  resin_filling_proc: &lt;http://purl.obolibrary.org/obo/OHD_0000042&gt;
  PREFIX  birth_date: &lt;http://purl.obolibrary.org/obo/OHD_0000050&gt;
  PREFIX  restored_buccal: &lt;http://purl.obolibrary.org/obo/OHD_0000222&gt;
  PREFIX  caries_finding: &lt;http://purl.obolibrary.org/obo/OHD_0000024&gt;
  PREFIX  dental_finding: &lt;http://purl.obolibrary.org/obo/OHD_0000010&gt;
  PREFIX  molar: &lt;http://purl.obolibrary.org/obo/FMA_55638&gt;
  PREFIX  male_gender_role: &lt;http://purl.obolibrary.org/obo/OMRSE_00000007&gt;
  PREFIX  endodontically_restored_tooth: &lt;http://purl.obolibrary.org/obo/0000236&gt;
  PREFIX  root_canal_treatment: &lt;http://purl.obolibrary.org/obo/OHD_0000230&gt;
  PREFIX  has_part: &lt;http://purl.obolibrary.org/obo/BFO_0000051&gt;
  PREFIX  gender_role: &lt;http://purl.obolibrary.org/obo/OMRSE_00000007&gt;
  PREFIX  part_of: &lt;http://purl.obolibrary.org/obo/BFO_0000050&gt;
  PREFIX  inheres_in: &lt;http://purl.obolibrary.org/obo/BFO_0000052&gt;
  PREFIX  missing_tooth_finding: &lt;http://purl.obolibrary.org/obo/OHD_0000026&gt;
  PREFIX  pbrn_id: &lt;http://purl.obolibrary.org/obo/OHD_0000273&gt;
  PREFIX  distal_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Distal_surface_enamel_of_tooth&gt;
  PREFIX  has_output: &lt;http://purl.obolibrary.org/obo/OBI_0000299&gt;
  PREFIX  occlusal_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Occlusal_surface_enamel_of_tooth&gt;
  PREFIX  incisor: &lt;http://purl.obolibrary.org/obo/FMA_12823&gt;
  PREFIX  graph: &lt;http://purl.regenstrief.org/NDPBRN/dental-practice#&gt;
  PREFIX  patient_role: &lt;http://purl.obolibrary.org/obo/OHD_0000190&gt;
  PREFIX  anterior_tooth: &lt;http://purl.obolibrary.org/obo/OHD_0000307&gt;
  PREFIX  resin: &lt;http://purl.obolibrary.org/obo/OHD_0000036&gt;
  PREFIX  restored_lingual: &lt;http://purl.obolibrary.org/obo/OHD_0000226&gt;
  PREFIX  dental_proc: &lt;http://purl.obolibrary.org/obo/OHD_0000002&gt;
  PREFIX  restored_surface: &lt;http://purl.obolibrary.org/obo/OHD_0000208&gt;
  PREFIX  extracoronally_restored_tooth: &lt;http://purl.obolibrary.org/obo/0000238&gt;
  PREFIX  lingual_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Lingual_surface_enamel_of_tooth&gt;
  PREFIX  dentition: &lt;http://purl.obolibrary.org/obo/FMA_75152&gt;
  PREFIX  sesame: &lt;http://www.openrdf.org/schema/sesame#&gt;
  PREFIX  lesion: &lt;http://purl.obolibrary.org/obo/OHD_0000021&gt;
  PREFIX  labial_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Labial_surface_enamel_of_tooth&gt;
  PREFIX  has_input: &lt;http://purl.obolibrary.org/obo/OBI_0000293&gt;
  PREFIX  posterior_tooth: &lt;http://purl.obolibrary.org/obo/OHD_0000308&gt;
  PREFIX  extraction_proc: &lt;http://purl.obolibrary.org/obo/OHD_0000057&gt;
  PREFIX  xsd:  &lt;http://www.w3.org/2001/XMLSchema#&gt;
  PREFIX  rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
  PREFIX  restored_occlusal: &lt;http://purl.obolibrary.org/obo/OHD_0000228&gt;
  PREFIX  is_about: &lt;http://purl.obolibrary.org/obo/IAO_0000136&gt;
  PREFIX  restored_labial: &lt;http://purl.obolibrary.org/obo/OHD_0000225&gt;
  PREFIX  coronally_restored_tooth: &lt;http://purl.obolibrary.org/obo/0000237&gt;
  PREFIX  patient: &lt;http://purl.obolibrary.org/obo/OHD_0000012&gt;
  PREFIX  prop: &lt;http://purl.regenstrief.org/NDPBRN/property/&gt;
  PREFIX  restoration_proc: &lt;http://purl.obolibrary.org/obo/OHD_0000004&gt;
  PREFIX  last_visit_date: &lt;http://purl.obolibrary.org/obo/OHD_0000219&gt;
  PREFIX  tooth: &lt;http://purl.obolibrary.org/obo/FMA_12516&gt;
  PREFIX  intracoronally_restored_tooth: &lt;http://purl.obolibrary.org/obo/0000239&gt;
  PREFIX  bearer_of: &lt;http://purl.obolibrary.org/obo/BFO_0000053&gt;
  PREFIX  first_visit_date: &lt;http://purl.obolibrary.org/obo/OHD_0000218&gt;
  PREFIX  surgically_modified_tooth: &lt;http://purl.obolibrary.org/obo/0000231&gt;
  PREFIX  canine: &lt;http://purl.obolibrary.org/obo/FMA_55636&gt;
  PREFIX  facial_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Facial_surface_enamel_of_tooth&gt;
  PREFIX  restored_distal: &lt;http://purl.obolibrary.org/obo/OHD_0000223&gt;
  PREFIX  premolar: &lt;http://purl.obolibrary.org/obo/FMA_55637&gt;
  PREFIX  restored_tooth: &lt;http://purl.obolibrary.org/obo/OHD_0000189&gt;
  PREFIX  restored_facial: &lt;http://purl.obolibrary.org/obo/OHD_0000235&gt;
  PREFIX  material: &lt;http://purl.obolibrary.org/obo/OHD_0000000&gt;
  PREFIX  missing_tooth_num: &lt;http://purl.obolibrary.org/obo/OHD_0000234&gt;
  PREFIX  buccal_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Buccal_surface_enamel_of_tooth&gt;
  PREFIX  realizes: &lt;http://purl.obolibrary.org/obo/BFO_0000055&gt;
  PREFIX  female_gender_role: &lt;http://purl.obolibrary.org/obo/OMRSE_00000008&gt;
  PREFIX  restored_mesial: &lt;http://purl.obolibrary.org/obo/OHD_0000227&gt;
  PREFIX  restored_incisal: &lt;http://purl.obolibrary.org/obo/OHD_0000224&gt;
  PREFIX  visit: &lt;http://purl.obolibrary.org/obo/OHD_0000009&gt;
  PREFIX  obo:  &lt;http://purl.obolibrary.org/obo/&gt;
  PREFIX  incisal_surface: &lt;http://purl.obolibrary.org/obo/FMA_no_fmaid_Incisal_surface_enamel_of_tooth&gt;
  
  SELECT DISTINCT  ?practice ?patient_id ?gender ?dob ?first_visit ?last_visit ?tooth_id ?tooth_num ?first_PCR ?first_RCT ?event_name ?ada_code ?event_date ?extract_date ?missing_date (if(bound(?surface_m), 1, 0) AS ?m) (if(bound(?surface_o), 1, 0) AS ?o) (if(bound(?surface_d), 1, 0) AS ?d) (if(bound(?surface_b), 1, 0) AS ?b) (if(bound(?surface_l), 1, 0) AS ?l) (if(bound(?surface_f), 1, 0) AS ?f) (if(bound(?surface_incisal), 1, 0) AS ?i)
  WHERE
    { ?patient_i  a                   patient: ;
            birth_date:           ?dob ;
            pbrn_id:              ?pbrn_id
      OPTIONAL
        { ?gender_t  rdfs:subClassOf   male_gender_role: ;
                  rdfs:label         ?gender_name .
          ?gender_i  sesame:directType  ?gender_t ;
                  inheres_in:        ?patient_i
        }
    ?patient_i  first_visit_date:  ?first_visit ;
            last_visit_date:   ?last_visit .
    ?tooth_t  rdfs:subClassOf    tooth: ;
            ada_num:           ?ada_num .
    ?tooth_i  sesame:directType  ?tooth_t
    OPTIONAL
      { ?tooth_i  prop:first_PCR_date  ?first_PCR }
    OPTIONAL
      { ?tooth_i  prop:first_RCT_date  ?first_RCT }
    OPTIONAL
      { ?tooth_i  prop:extraction_date  ?extract_date }
    OPTIONAL
        { ?tooth_i  prop:missing_tooth_finding_date  ?missing_date }
    ?event_t  rdfs:subClassOf    dental_proc: ;
              rdfs:label         ?event_name .
    ?event_i  sesame:directType  ?event_t ;
              has_input:         ?patient_i ;
              has_output:        ?tooth_i ;
              occurrence_date:   ?event_date ;
              prop:ada_code      ?ada_code
    OPTIONAL
      { ?event_i  has_output:        ?surface_m .
        ?surface_m  sesame:directType  restored_mesial: ;
                part_of:           ?tooth_i
      }
    OPTIONAL
      { ?event_i  has_output:        ?surface_o .
        ?surface_o  sesame:directType  restored_occlusal: ;
                part_of:           ?tooth_i
      }
    OPTIONAL
      { ?event_i  has_output:        ?surface_d .
      ?surface_d  sesame:directType  restored_distal: ;
                part_of:           ?tooth_i
      }
    OPTIONAL
      { ?event_i  has_output:        ?surface_b .
        ?surface_b  sesame:directType  restored_buccal: ;
                part_of:           ?tooth_i
      }
  OPTIONAL
    { ?event_i  has_output:        ?surface_l .
      ?surface_l  sesame:directType  restored_lingual: ;
                part_of:           ?tooth_i
      }
    OPTIONAL
      { ?event_i  has_output:        ?surface_f .
        ?surface_f  sesame:directType  restored_facial: ;
                part_of:           ?tooth_i
      }
    OPTIONAL
      { ?event_i  has_output:        ?surface_incisal .
        ?surface_incisal
                sesame:directType  restored_incisal: ;
                part_of:           ?tooth_i
      }
    BIND(strafter(str(?tooth_i), "tooth/") AS ?tooth_id)
    BIND(strafter(str(?patient_i), "patient/") AS ?patient_id)
    BIND(strbefore(str(?gender_name), " ") AS ?gender)
    BIND(strafter(str(?ada_num), "Tooth ") AS ?tooth_num)
    BIND(strafter(str(?pbrn_id), "NDPBRN practice ") AS ?practice)
  }
  limit 5
  </code></pre>