Variable in the query loses the filter, when i swap the OPTIONAL blocks

Hello, I Have one query, that gives me 10000 results (maximum for my config)
#WRONG_QUERY

PREFIX crm2: <http://sp7.ru/ontology/>PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
PREFIX owl:<http://www.w3.org/2002/07/owl#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
PREFIX crm2:<http://sp7.ru/ontology/>
PREFIX sysont:<http://ns.ontowiki.net/SysOnt/>
PREFIX owconf:<http://localhost/OntoWiki/Config/>
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
SELECT *
FROM <http://sp7.ru/>
WHERE { 

    OPTIONAL {
        ?gridRelatedUri crm2:logo ?logo.
        ?logo crm2:thumbnail ?logoThumbnail.
        ?logoThumbnail rdfs:label ?logoThumbnailLabel.
    }
    OPTIONAL {
        ?gridRelatedUri crm2:legalAddress ?legalAddress.
        ?legalAddress crm2:city ?legalAddressCity.
        ?legalAddressCity rdfs:label ?legalAddressCityLabel.
    }
    ?gridRelatedUri rdfs:label ?labelFull.
    OPTIONAL {
        ?gridRelatedUri crm2:shortName ?shortName.
        ?shortName rdfs:label ?shortLabel.
    }
    BIND(COALESCE(?shortLabel, ?labelFull) as ?label)

FILTER (?gridRelatedUri = <http://sp7.ru/ontology/eile_1097746232682_rus>)

}

The fact is that returns any available value in ?legalAddressCity without filtration by ?gridRelatedUri
But, if swap the first 2 optionals, it works great:
#RIGHT_QUERY

PREFIX crm2: <http://sp7.ru/ontology/>PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
PREFIX owl:<http://www.w3.org/2002/07/owl#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
PREFIX crm2:<http://sp7.ru/ontology/>
PREFIX sysont:<http://ns.ontowiki.net/SysOnt/>
PREFIX owconf:<http://localhost/OntoWiki/Config/>
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
SELECT *
FROM <http://sp7.ru/>
WHERE { 
    OPTIONAL {
        ?gridRelatedUri crm2:legalAddress ?legalAddress.
        ?legalAddress crm2:city ?legalAddressCity.
        ?legalAddressCity rdfs:label ?legalAddressCityLabel.
    }
    OPTIONAL {
        ?gridRelatedUri crm2:logo ?logo.
        ?logo crm2:thumbnail ?logoThumbnail.
        ?logoThumbnail rdfs:label ?logoThumbnailLabel.
    }
    ?gridRelatedUri rdfs:label ?labelFull.
    OPTIONAL {
        ?gridRelatedUri crm2:shortName ?shortName.
        ?shortName rdfs:label ?shortLabel.
    }
    BIND(COALESCE(?shortLabel, ?labelFull) as ?label)

FILTER (?gridRelatedUri = <http://sp7.ru/ontology/eile_1097746232682_rus>)

}

Now it gives only 3 results. It’s the right results, because I’ve checked relations for crm2eile_1097746232682_rus.

Why is this happening? Deep variables can lost FILTER in some cases? What cases?

If i use custom value instead filter - it works fine:

PREFIX crm2: <http://sp7.ru/ontology/>PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
PREFIX owl:<http://www.w3.org/2002/07/owl#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
PREFIX crm2:<http://sp7.ru/ontology/>
PREFIX sysont:<http://ns.ontowiki.net/SysOnt/>
PREFIX owconf:<http://localhost/OntoWiki/Config/>
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
SELECT *
FROM <http://sp7.ru/>
WHERE { 

    OPTIONAL {
        <http://sp7.ru/ontology/eile_1097746232682_rus> crm2:logo ?logo.
        ?logo crm2:thumbnail ?logoThumbnail.
        ?logoThumbnail rdfs:label ?logoThumbnailLabel.
    }
    OPTIONAL {
        <http://sp7.ru/ontology/eile_1097746232682_rus> crm2:legalAddress ?legalAddress.
        ?legalAddress crm2:city ?legalAddressCity.
        ?legalAddressCity rdfs:label ?legalAddressCityLabel.
    }
    <http://sp7.ru/ontology/eile_1097746232682_rus> rdfs:label ?labelFull.
    OPTIONAL {
        <http://sp7.ru/ontology/eile_1097746232682_rus> crm2:shortName ?shortName.
        ?shortName rdfs:label ?shortLabel.
    }
    BIND(COALESCE(?shortLabel, ?labelFull) as ?label)

} 

SPARQL_TRANSLATE for #WRONG_QUERY:

SELECT __bft( 'http://sp7.ru/ontology/eile_1097746232682_rus' , 1) AS "gridRelatedUri",
  __id2in ( "s_30_15"."logo") AS "logo",
  __id2in ( "s_30_15"."logoThumbnail") AS "logoThumbnail",
  __ro2sq ( "s_30_15"."logoThumbnailLabel") AS "logoThumbnailLabel",
  __id2in ( "s_30_15"."legalAddress") AS "legalAddress",
  __id2in ( "s_30_15"."legalAddressCity") AS "legalAddressCity",
  __ro2sq ( "s_30_15"."legalAddressCityLabel") AS "legalAddressCityLabel",
  __ro2sq ( "s_30_15"."labelFull") AS "labelFull",
  __id2in ( "s_30_15"."shortName") AS "shortName",
  __ro2sq ( "s_30_15"."shortLabel") AS "shortLabel",
  __ro2sq ( "s_30_15"."label") AS "label"
FROM (
   SELECT "s_30_14_t9"."S" AS "gridRelatedUri",
      "s_30_13"."logo" AS "logo",
      "s_30_13"."logoThumbnail" AS "logoThumbnail",
      "s_30_13"."logoThumbnailLabel" AS "logoThumbnailLabel",
      "s_23_7"."legalAddress" AS "legalAddress",
      "s_23_7"."legalAddressCity" AS "legalAddressCity",
      "s_23_7"."legalAddressCityLabel" AS "legalAddressCityLabel",
      "s_30_14_t9"."O" AS "labelFull",
      "s_28_11"."shortName" AS "shortName",
      "s_28_11"."shortLabel" AS "shortLabel",
      coalesce ( "s_28_11"."shortLabel", "s_30_14_t9"."O") AS "label"
    FROM (
        SELECT "s_18_3"."logoThumbnailLabel" AS "logoThumbnailLabel",
          "s_18_3"."logoThumbnail" AS "logoThumbnail",
          "s_18_3"."logo" AS "logo",
          "s_18_3"."gridRelatedUri" AS "gridRelatedUri"
        FROM
          (SELECT 1 AS __stub) AS "lojstub-s_30_13" LEFT OUTER JOIN (
            SELECT "s_18_3_t0"."S" AS "gridRelatedUri",
              "s_18_3_t0"."O" AS "logo",
              "s_18_3_t1"."O" AS "logoThumbnail",
              "s_18_3_t2"."O" AS "logoThumbnailLabel"
            FROM DB.DBA.RDF_QUAD AS "s_18_3_t0"
              INNER JOIN DB.DBA.RDF_QUAD AS "s_18_3_t1"
              ON (
                "s_18_3_t0"."O" = "s_18_3_t1"."S")
              INNER JOIN DB.DBA.RDF_QUAD AS "s_18_3_t2"
              ON (
                "s_18_3_t1"."O" = "s_18_3_t2"."S")
            WHERE
              "s_18_3_t0"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
              AND 
              "s_18_3_t0"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/logo' , 1))
              AND 
              "s_18_3_t1"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
              AND 
              "s_18_3_t1"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/thumbnail' , 1))
              AND 
              "s_18_3_t2"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
              AND 
              "s_18_3_t2"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))) AS "s_18_3"
          ON (1)) AS "s_30_13"
      LEFT OUTER JOIN (
        SELECT "s_23_7_t3"."S" AS "gridRelatedUri",
          "s_23_7_t3"."O" AS "legalAddress",
          "s_23_7_t4"."O" AS "legalAddressCity",
          "s_23_7_t5"."O" AS "legalAddressCityLabel"
        FROM DB.DBA.RDF_QUAD AS "s_23_7_t3"
          INNER JOIN DB.DBA.RDF_QUAD AS "s_23_7_t4"
          ON (
            "s_23_7_t3"."O" = "s_23_7_t4"."S")
          INNER JOIN DB.DBA.RDF_QUAD AS "s_23_7_t5"
          ON (
            "s_23_7_t4"."O" = "s_23_7_t5"."S")
        WHERE
          "s_23_7_t3"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_23_7_t3"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/legalAddress' , 1))
          AND 
          "s_23_7_t4"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_23_7_t4"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/city' , 1))
          AND 
          "s_23_7_t5"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_23_7_t5"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))) AS "s_23_7"
      ON (
        ( "s_30_13"."gridRelatedUri" IS NULL  OR ( "s_30_13"."gridRelatedUri" = "s_23_7"."gridRelatedUri" ) ))
      INNER JOIN DB.DBA.RDF_QUAD AS "s_30_14_t9"
      ON (
        "s_30_14_t9"."S" = "s_30_13"."gridRelatedUri")
      LEFT OUTER JOIN (
        SELECT "s_28_11_t6"."S" AS "gridRelatedUri",
          "s_28_11_t6"."O" AS "shortName",
          "s_28_11_t7"."O" AS "shortLabel"
        FROM DB.DBA.RDF_QUAD AS "s_28_11_t6"
          INNER JOIN DB.DBA.RDF_QUAD AS "s_28_11_t7"
          ON (
            "s_28_11_t6"."O" = "s_28_11_t7"."S")
        WHERE
          "s_28_11_t6"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_28_11_t6"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/shortName' , 1))
          AND 
          "s_28_11_t7"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_28_11_t7"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))) AS "s_28_11"
      ON (
        "s_30_14_t9"."S" = "s_28_11"."gridRelatedUri"
        AND 
        ( "s_30_13"."gridRelatedUri" IS NULL  OR ( "s_30_13"."gridRelatedUri" = "s_28_11"."gridRelatedUri" ) ))
    WHERE
      "s_30_14_t9"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
      AND 
      "s_30_14_t9"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))
OPTION (QUIETCAST)
    ) AS "s_30_15"
WHERE
 ( "s_30_15"."gridRelatedUri" =  __i2idn ( __bft( 'http://sp7.ru/ontology/eile_1097746232682_rus' , 1)))
OPTION (QUIETCAST)
1 Rows. -- 3 msec.

SPARQL_TRANSLATE for #RIGHT_QUERY:

SELECT __bft( 'http://sp7.ru/ontology/eile_1097746232682_rus' , 1) AS "gridRelatedUri",
  __id2in ( "s_30_15"."legalAddress") AS "legalAddress",
  __id2in ( "s_30_15"."legalAddressCity") AS "legalAddressCity",
  __ro2sq ( "s_30_15"."legalAddressCityLabel") AS "legalAddressCityLabel",
  __id2in ( "s_30_15"."logo") AS "logo",
  __id2in ( "s_30_15"."logoThumbnail") AS "logoThumbnail",
  __ro2sq ( "s_30_15"."logoThumbnailLabel") AS "logoThumbnailLabel",
  __ro2sq ( "s_30_15"."labelFull") AS "labelFull",
  __id2in ( "s_30_15"."shortName") AS "shortName",
  __ro2sq ( "s_30_15"."shortLabel") AS "shortLabel",
  __ro2sq ( "s_30_15"."label") AS "label"
FROM (
   SELECT "s_30_14_t9"."S" AS "gridRelatedUri",
      "s_30_13"."legalAddress" AS "legalAddress",
      "s_30_13"."legalAddressCity" AS "legalAddressCity",
      "s_30_13"."legalAddressCityLabel" AS "legalAddressCityLabel",
      "s_23_7"."logo" AS "logo",
      "s_23_7"."logoThumbnail" AS "logoThumbnail",
      "s_23_7"."logoThumbnailLabel" AS "logoThumbnailLabel",
      "s_30_14_t9"."O" AS "labelFull",
      "s_28_11"."shortName" AS "shortName",
      "s_28_11"."shortLabel" AS "shortLabel",
      coalesce ( "s_28_11"."shortLabel", "s_30_14_t9"."O") AS "label"
    FROM (
        SELECT "s_17_3"."legalAddressCityLabel" AS "legalAddressCityLabel",
          "s_17_3"."legalAddressCity" AS "legalAddressCity",
          "s_17_3"."legalAddress" AS "legalAddress",
          "s_17_3"."gridRelatedUri" AS "gridRelatedUri"
        FROM
          (SELECT 1 AS __stub) AS "lojstub-s_30_13" LEFT OUTER JOIN (
            SELECT "s_17_3_t0"."S" AS "gridRelatedUri",
              "s_17_3_t0"."O" AS "legalAddress",
              "s_17_3_t1"."O" AS "legalAddressCity",
              "s_17_3_t2"."O" AS "legalAddressCityLabel"
            FROM DB.DBA.RDF_QUAD AS "s_17_3_t0"
              INNER JOIN DB.DBA.RDF_QUAD AS "s_17_3_t1"
              ON (
                "s_17_3_t0"."O" = "s_17_3_t1"."S")
              INNER JOIN DB.DBA.RDF_QUAD AS "s_17_3_t2"
              ON (
                "s_17_3_t1"."O" = "s_17_3_t2"."S")
            WHERE
              "s_17_3_t0"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
              AND 
              "s_17_3_t0"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/legalAddress' , 1))
              AND 
              "s_17_3_t1"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
              AND 
              "s_17_3_t1"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/city' , 1))
              AND 
              "s_17_3_t2"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
              AND 
              "s_17_3_t2"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))) AS "s_17_3"
          ON (1)) AS "s_30_13"
      LEFT OUTER JOIN (
        SELECT "s_23_7_t3"."S" AS "gridRelatedUri",
          "s_23_7_t3"."O" AS "logo",
          "s_23_7_t4"."O" AS "logoThumbnail",
          "s_23_7_t5"."O" AS "logoThumbnailLabel"
        FROM DB.DBA.RDF_QUAD AS "s_23_7_t3"
          INNER JOIN DB.DBA.RDF_QUAD AS "s_23_7_t4"
          ON (
            "s_23_7_t3"."O" = "s_23_7_t4"."S")
          INNER JOIN DB.DBA.RDF_QUAD AS "s_23_7_t5"
          ON (
            "s_23_7_t4"."O" = "s_23_7_t5"."S")
        WHERE
          "s_23_7_t3"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_23_7_t3"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/logo' , 1))
          AND 
          "s_23_7_t4"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_23_7_t4"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/thumbnail' , 1))
          AND 
          "s_23_7_t5"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_23_7_t5"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))) AS "s_23_7"
      ON (
        ( "s_30_13"."gridRelatedUri" IS NULL  OR ( "s_30_13"."gridRelatedUri" = "s_23_7"."gridRelatedUri" ) ))
      INNER JOIN DB.DBA.RDF_QUAD AS "s_30_14_t9"
      ON (
        "s_30_14_t9"."S" = "s_30_13"."gridRelatedUri")
      LEFT OUTER JOIN (
        SELECT "s_28_11_t6"."S" AS "gridRelatedUri",
          "s_28_11_t6"."O" AS "shortName",
          "s_28_11_t7"."O" AS "shortLabel"
        FROM DB.DBA.RDF_QUAD AS "s_28_11_t6"
          INNER JOIN DB.DBA.RDF_QUAD AS "s_28_11_t7"
          ON (
            "s_28_11_t6"."O" = "s_28_11_t7"."S")
        WHERE
          "s_28_11_t6"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_28_11_t6"."P" = __i2idn ( __bft( 'http://sp7.ru/ontology/shortName' , 1))
          AND 
          "s_28_11_t7"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
          AND 
          "s_28_11_t7"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))) AS "s_28_11"
      ON (
        "s_30_14_t9"."S" = "s_28_11"."gridRelatedUri"
        AND 
        ( "s_30_13"."gridRelatedUri" IS NULL  OR ( "s_30_13"."gridRelatedUri" = "s_28_11"."gridRelatedUri" ) ))
    WHERE
      "s_30_14_t9"."G" = __i2idn ( __bft( 'http://sp7.ru/' , 1))
      AND 
      "s_30_14_t9"."P" = __i2idn ( __bft( 'http://www.w3.org/2000/01/rdf-schema#label' , 1))
OPTION (QUIETCAST)
    ) AS "s_30_15"
WHERE
 ( "s_30_15"."gridRelatedUri" =  __i2idn ( __bft( 'http://sp7.ru/ontology/eile_1097746232682_rus' , 1)))
OPTION (QUIETCAST)
1 Rows. -- 2 msec.

Maybe this line crashes the query? Why?

      ON (
        ( "s_30_13"."gridRelatedUri" IS NULL  OR ( "s_30_13"."gridRelatedUri" = "s_23_7"."gridRelatedUri" ) ))

Explain for #WRONG_QUERY:

    { 
RDF_QUAD         2 rows(t26.S, t26.O)
 inlined  P =  ##label  ,  S =  #/eile_1097746232682_rus  G =  #/ 
 
Precode:
      0: labelFull := Call __ro2sq (t26.O)
      5: BReturn 0
outer {
Subquery 33 
{ 
RDF_QUAD         1 rows(t19.O, t19.S)
 inlined  P =  #/logo  ,  S = k_t26.S G =  #/ 
RDF_QUAD         1 rows(t20.O)
 inlined  P =  #/thumbnail  ,  S = k_t19.O G =  #/ 
RDF_QUAD         1 rows(t21.O)
 inlined  P =  ##label  ,  S = k_t20.O G =  #/ 
 
After code:
      0: gridRelatedUri :=  := artm t19.S
      4: logo :=  := artm t19.O
      8: logoThumbnail :=  := artm t20.O
      12: logoThumbnailLabel :=  := artm t21.O
      16: BReturn 0
Subquery Select(gridRelatedUri, logo, logoThumbnail, logoThumbnailLabel)
}
 end of outer}
set_ctr
 out: (gridRelatedUri, logoThumbnailLabel, logoThumbnail, logo, set_no)
 shadow: ( <none> ,  <none> ,  <none> ,  <none> ,  <none> )
 
Precode:
      0: logo := Call __id2in (logo)
      5: logoThumbnail := Call __id2in (logoThumbnail)
      10: logoThumbnailLabel := Call __ro2sq (logoThumbnailLabel)
      15: BReturn 0
Subquery 72 
{ 
 
Precode:
      0: __stub :=  := artm  1 
      4: BReturn 0
END Node
Subquery Select(__stub)
}
outer {
Subquery 83 
{ 
RDF_QUAD       0.7 rows(t28.S, t28.O)
 inlined  P =  #/shortName  ,  S = k_t26.S G =  #/ 
END Node
After test:
      0: if (gridRelatedUri  IS NULL   0 ) then 8 else 4 unkn 4
      4: if (gridRelatedUri = t28.S) then 8 else 9 unkn 9
      8: BReturn 1
      9: BReturn 0
RDF_QUAD         1 rows(t29.O)
 inlined  P =  ##label  ,  S = k_t28.O G =  #/ 
 
After code:
      0: gridRelatedUri :=  := artm t28.S
      4: shortName :=  := artm t28.O
      8: shortLabel :=  := artm t29.O
      12: BReturn 0
Subquery Select(gridRelatedUri, shortName, shortLabel)
}
 end of outer}
set_ctr
 out: (gridRelatedUri, shortLabel, shortName, set_no)
 shadow: ( <none> ,  <none> ,  <none> ,  <none> )
 
Precode:
      0: shortName := Call __id2in (shortName)
      5: shortLabel := Call __ro2sq (shortLabel)
      10: if (shortLabel  IS NULL  <DB_NULL>) then 19 else 14 unkn 19
      14: coalesce_ret :=  := artm shortLabel
      18: Jump 32 (level=0)
      19: if (t26.O  IS NULL  <DB_NULL>) then 28 else 23 unkn 28
      23: coalesce_ret :=  := artm t26.O
      27: Jump 32 (level=0)
      28: coalesce_ret :=  := artm <DB_NULL>
      32: label := Call __ro2sq (coalesce_ret)
      37: BReturn 0
outer {
Subquery 120 
{ 
RDF_QUAD_POGS   7.2e+06 rows(t23.S, t23.O)
 inlined  P =  #/legalAddress  G =  #/ 
END Node
After test:
      0: if (gridRelatedUri  IS NULL   0 ) then 8 else 4 unkn 4
      4: if (gridRelatedUri = t23.S) then 8 else 9 unkn 9
      8: BReturn 1
      9: BReturn 0
RDF_QUAD         1 rows(t24.O)
 inlined  P =  #/city  ,  S = k_t23.O G =  #/ 
RDF_QUAD         1 rows(t25.O)
 inlined  P =  ##label  ,  S = k_t24.O G =  #/ 
 
After code:
      0: gridRelatedUri :=  := artm t23.S
      4: legalAddress :=  := artm t23.O
      8: legalAddressCity :=  := artm t24.O
      12: legalAddressCityLabel :=  := artm t25.O
      16: BReturn 0
Subquery Select(gridRelatedUri, legalAddress, legalAddressCity, legalAddressCityLabel)
}
 end of outer}
set_ctr
 out: (legalAddressCityLabel, gridRelatedUri, set_no, legalAddress, legalAddressCity)
 shadow: ( <none> ,  <none> ,  <none> ,  <none> ,  <none> )
 
After code:
      0: legalAddress := Call __id2in (legalAddress)
      5: legalAddressCity := Call __id2in (legalAddressCity)
      10: legalAddressCityLabel := Call __ro2sq (legalAddressCityLabel)
      15: BReturn 0
Select (<tag 182 flag 1 c http://sp7.ru/ontology/eile_1097746232682_rus>, logo, logoThumbnail, logoThumbnailLabel, legalAddress, legalAddressCity, legalAddressCityLabel, labelFull, shortName, shortLabel, label)
}

118 Rows. -- 15 msec.

EXPLAIN FOR #RIGHT_QUERY:

{ 
Subquery 27 
{ 
 
Precode:
      0: $28 "__stub" :=  := artm  1 
      4: BReturn 0
END Node
Subquery Select($28 "__stub")
}
RDF_QUAD         2 rows(t26.S, t26.O)
 inlined  P =  ##label  ,  S =  #/eile_1097746232682_rus  G =  #/ 
outer {
Subquery 42 
{ 
RDF_QUAD         1 rows(t19.O, t19.S)
 inlined  P =  #/legalAddress  ,  S = k_t26.S G =  #/ 
RDF_QUAD         1 rows(t20.O)
 inlined  P =  #/city  ,  S = k_t19.O G =  #/ 
RDF_QUAD         1 rows(t21.O)
 inlined  P =  ##label  ,  S = k_t20.O G =  #/ 
 
After code:
      0: gridRelatedUri :=  := artm t19.S
      4: legalAddress :=  := artm t19.O
      8: legalAddressCity :=  := artm t20.O
      12: legalAddressCityLabel :=  := artm t21.O
      16: BReturn 0
Subquery Select(gridRelatedUri, legalAddress, legalAddressCity, legalAddressCityLabel)
}
 end of outer}
set_ctr
 out: (legalAddressCityLabel, legalAddress, legalAddressCity, set_no, gridRelatedUri)
 shadow: ( <none> ,  <none> ,  <none> ,  <none> ,  <none> )
outer {
Subquery 75 
{ 
RDF_QUAD       0.7 rows(t28.S, t28.O)
 inlined  P =  #/shortName  ,  S = k_t26.S G =  #/ 
END Node
After test:
      0: if (gridRelatedUri  IS NULL   0 ) then 8 else 4 unkn 4
      4: if (gridRelatedUri = t28.S) then 8 else 9 unkn 9
      8: BReturn 1
      9: BReturn 0
RDF_QUAD         1 rows(t29.O)
 inlined  P =  ##label  ,  S = k_t28.O G =  #/ 
 
After code:
      0: gridRelatedUri :=  := artm t28.S
      4: shortName :=  := artm t28.O
      8: shortLabel :=  := artm t29.O
      12: BReturn 0
Subquery Select(gridRelatedUri, shortName, shortLabel)
}
 end of outer}
set_ctr
 out: (shortLabel, shortName, gridRelatedUri, set_no)
 shadow: ( <none> ,  <none> ,  <none> ,  <none> )
 
Precode:
      0: labelFull := Call __ro2sq (t26.O)
      5: legalAddressCityLabel := Call __ro2sq (legalAddressCityLabel)
      10: legalAddressCity := Call __id2in (legalAddressCity)
      15: legalAddress := Call __id2in (legalAddress)
      20: shortName := Call __id2in (shortName)
      25: shortLabel := Call __ro2sq (shortLabel)
      30: if (shortLabel  IS NULL  <DB_NULL>) then 39 else 34 unkn 39
      34: coalesce_ret :=  := artm shortLabel
      38: Jump 52 (level=0)
      39: if (t26.O  IS NULL  <DB_NULL>) then 48 else 43 unkn 48
      43: coalesce_ret :=  := artm t26.O
      47: Jump 52 (level=0)
      48: coalesce_ret :=  := artm <DB_NULL>
      52: label := Call __ro2sq (coalesce_ret)
      57: BReturn 0
outer {
Subquery 120 
{ 
RDF_QUAD_POGS   2.8e+04 rows(t23.S, t23.O)
 inlined  P =  #/logo  G =  #/ 
END Node
After test:
      0: if (gridRelatedUri  IS NULL   0 ) then 8 else 4 unkn 4
      4: if (gridRelatedUri = t23.S) then 8 else 9 unkn 9
      8: BReturn 1
      9: BReturn 0
RDF_QUAD         1 rows(t24.O)
 inlined  P =  #/thumbnail  ,  S = k_t23.O G =  #/ 
RDF_QUAD         1 rows(t25.O)
 inlined  P =  ##label  ,  S = k_t24.O G =  #/ 
 
After code:
      0: gridRelatedUri :=  := artm t23.S
      4: logo :=  := artm t23.O
      8: logoThumbnail :=  := artm t24.O
      12: logoThumbnailLabel :=  := artm t25.O
      16: BReturn 0
Subquery Select(gridRelatedUri, logo, logoThumbnail, logoThumbnailLabel)
}
 end of outer}
set_ctr
 out: (gridRelatedUri, logoThumbnail, logo, set_no, logoThumbnailLabel)
 shadow: ( <none> ,  <none> ,  <none> ,  <none> ,  <none> )
 
After code:
      0: logo := Call __id2in (logo)
      5: logoThumbnail := Call __id2in (logoThumbnail)
      10: logoThumbnailLabel := Call __ro2sq (logoThumbnailLabel)
      15: BReturn 0
Select (<tag 182 flag 1 c http://sp7.ru/ontology/eile_1097746232682_rus>, legalAddress, legalAddressCity, legalAddressCityLabel, logo, logoThumbnail, logoThumbnailLabel, labelFull, shortName, shortLabel, label)
}

112 Rows. -- 7 msec.

We have the dynamic query project, where users can generate queries on the fly with our tools. Please help me understand the nature of this behavior. And how can i detect and modify those queries “on the fly”

This could possibly be due to a bad query plan being generated by the Virtuoso cost-based query optimizer.

You can turn off query optimisation and force an explicit join order for the query by including the DEFINE sql:select-option "order" pragma at the beginning of the SPARQL query, as detailed in the documentation at:

I think, that bad query plan should affect only on the excecution time, but not to results?
In my case the question is not about the time, It’s about wrong results.