Overwritten variables in prolog

Bug #867170 reported by Rodolfo Ochoa
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Zorba
Fix Released
High
Markos Zaharioudakis

Bug Description

Variables declared in Prolog are Overwritten inside the query.
example attached

Tags: context v2.0
Revision history for this message
Rodolfo Ochoa (rodolfo-ochoa) wrote :

The file context.cpp was added: None

Revision history for this message
Rodolfo Ochoa (rodolfo-ochoa) wrote :

Is working on new sources...

Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

Reopened because it was unintentionally closed.

Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

The attached example doesn't compile:

doc/cxx/examples/context.cpp: In function ‘bool context_example_11(zorba::Zorba*)’:
doc/cxx/examples/context.cpp:33:3: error: ‘PrologModuleURLResolver’ was not declared in this scope

Could you please double check if the bug still exists and provide the full example?

Changed in zorba:
milestone: none → 2.5
Revision history for this message
Rodolfo Ochoa (rodolfo-ochoa) wrote :

Attached example

Changed in zorba:
assignee: Rodolfo Ochoa (rodolfo-ochoa) → Markos Zaharioudakis (markos-za)
Revision history for this message
Rodolfo Ochoa (rodolfo-ochoa) wrote :
Download full text (3.2 KiB)

Captain James T. Kirk: Captain's log, stardate 9522.6:

Trying to solve this bug I created this Prolog and Query:

lProlog << "declare variable $A := <a>a</a>; declare variable $B := <b>b</b>; ";
lQuery << "declare variable $C := <c>c</c>; declare variable $D := <d>d</d>; $B ";

Then I printed the execution plan, giving me this:

<SequentialIterator id="0258F708">
  <SequentialIterator id="0258F778">
    <CtxVarDeclareIterator varid="2" varname="A" id="02783778">
      <ElementIterator id="02783700">
        <SingletonIterator value="xs:QName(,,a)" id="0279A900"/>
        <TextIterator id="0279A780">
          <SingletonIterator value="xs:string(a)" id="0279A720"/>
        </TextIterator>
      </ElementIterator>
    </CtxVarDeclareIterator>
    <CtxVarDeclareIterator varid="3" varname="B" id="02783FE8">
      <ElementIterator id="02783D18">
        <SingletonIterator value="xs:QName(,,b)" id="0279A6C0"/>
        <TextIterator id="0279A960">
          <SingletonIterator value="xs:string(b)" id="0279A660"/>
        </TextIterator>
      </ElementIterator>
    </CtxVarDeclareIterator>
    <FnConcatIterator id="0258F7E8"/>
  </SequentialIterator>
  <CtxVarDeclareIterator varid="2" varname="C" id="027840D8">
    <ElementIterator copyInputNodes="false" id="02784060">
      <SingletonIterator value="xs:QName(,,c)" id="0279A9C0"/>
      <TextIterator id="0279AA80">
        <SingletonIterator value="xs:string(c)" id="0279AA20"/>
      </TextIterator>
    </ElementIterator>
  </CtxVarDeclareIterator>
  <CtxVarDeclareIterator varid="3" varname="D" id="027841C8">
    <ElementIterator copyInputNodes="false" id="02784150">
      <SingletonIterator value="xs:QName(,,d)" id="0279AAE0"/>
      <TextIterator id="0279ABA0">
        <SingletonIterator value="xs:string(d)" id="0279AB40"/>
      </TextIterator>
    </ElementIterator>
  </CtxVarDeclareIterator>
  <CtxVarIterator varid="3" varname="B" varkind="global" id="00A9D3E8"/>
</SequentialIterator>

So, I can see that the Iterators have variables with duplicated varids, digging into the code, I found that to process the prolog an XQueryImpl object is created, so for every XQueryImpl created on xqueryimpl.cpp:586 the line:

  ulong nextDynamicVarId = 2;

initializes the "unique" varid to 2, so, after the prolog is processed when the real query starts it starts again from two... but why?
because on plan_visitor.cpp:605 the lines:

  if (varExpr->get_unique_id() == 0)
    varExpr->set_unique_id(theNextDynamicVarId++);

increment the varid only if it exists. So a quick fix could be to increment the varid out this verification, and everytime a new query is created the varid will be incremented correctly.
But this fix looks dirty, a better approach should be to initialize in xqueryimpl the nextDynamicVarId considering the number of variables that the prolog/staticContext has.
I tried to do that on staticContext, but it was very difficult, I tried putting this value on StaticContext, but after a while I realize that XQueryImpl uses static_context and this is different than StaticContext class.
So... my time went out and couldn't figure out how to pass this variable accross the classes...

Markos, hope this helps to...

Read more...

Revision history for this message
Markos Zaharioudakis (markos-za) wrote : Re: [Bug 867170] Re: Overwritten variables in prolog
Download full text (4.0 KiB)

Hi Rodolfo,

You really went quite far in your exploration....

I have a fix for this now, but since the trunk is frozen right now, i have to wait to commit it.

cheers,
Markos.

>________________________________
> From: Rodolfo Ochoa <email address hidden>
>To: <email address hidden>
>Sent: Saturday, May 5, 2012 7:46 AM
>Subject: [Bug 867170] Re: Overwritten variables in prolog
>
>Captain James T. Kirk: Captain's log, stardate 9522.6:
>
>Trying to solve this bug I created this Prolog and Query:
>
>lProlog << "declare variable $A := <a>a</a>; declare variable $B := <b>b</b>; ";
>lQuery << "declare variable $C := <c>c</c>; declare variable $D := <d>d</d>; $B ";
>
>Then I printed the execution plan, giving me this:
>
><SequentialIterator id="0258F708">
>  <SequentialIterator id="0258F778">
>    <CtxVarDeclareIterator varid="2" varname="A" id="02783778">
>      <ElementIterator id="02783700">
>        <SingletonIterator value="xs:QName(,,a)" id="0279A900"/>
>        <TextIterator id="0279A780">
>          <SingletonIterator value="xs:string(a)" id="0279A720"/>
>        </TextIterator>
>      </ElementIterator>
>    </CtxVarDeclareIterator>
>    <CtxVarDeclareIterator varid="3" varname="B" id="02783FE8">
>      <ElementIterator id="02783D18">
>        <SingletonIterator value="xs:QName(,,b)" id="0279A6C0"/>
>        <TextIterator id="0279A960">
>          <SingletonIterator value="xs:string(b)" id="0279A660"/>
>        </TextIterator>
>      </ElementIterator>
>    </CtxVarDeclareIterator>
>    <FnConcatIterator id="0258F7E8"/>
>  </SequentialIterator>
>  <CtxVarDeclareIterator varid="2" varname="C" id="027840D8">
>    <ElementIterator copyInputNodes="false" id="02784060">
>      <SingletonIterator value="xs:QName(,,c)" id="0279A9C0"/>
>      <TextIterator id="0279AA80">
>        <SingletonIterator value="xs:string(c)" id="0279AA20"/>
>      </TextIterator>
>    </ElementIterator>
>  </CtxVarDeclareIterator>
>  <CtxVarDeclareIterator varid="3" varname="D" id="027841C8">
>    <ElementIterator copyInputNodes="false" id="02784150">
>      <SingletonIterator value="xs:QName(,,d)" id="0279AAE0"/>
>      <TextIterator id="0279ABA0">
>        <SingletonIterator value="xs:string(d)" id="0279AB40"/>
>      </TextIterator>
>    </ElementIterator>
>  </CtxVarDeclareIterator>
>  <CtxVarIterator varid="3" varname="B" varkind="global" id="00A9D3E8"/>
></SequentialIterator>
>
>So, I can see that the Iterators have variables with duplicated varids,
>digging into the code, I found that to process the prolog an XQueryImpl
>object is created, so for every XQueryImpl created on xqueryimpl.cpp:586
>the line:
>
>  ulong nextDynamicVarId = 2;
>
>initializes the "unique" varid to 2, so, after the prolog is processed when the real query starts it starts again from two... but why?
>because on plan_visitor.cpp:605 the lines:
>
>  if (varExpr->get_unique_id() == 0)
>    varExpr->set_unique_id(theNextDynamicVarId++);
>
>increment the varid only if it exists. So a quick fix could be to increment the varid out this verification, and everytime a new query is created the varid will be incremented correctly.
>But this fix looks dirty, a better approach should be to initial...

Read more...

Changed in zorba:
status: Confirmed → Fix Committed
Changed in zorba:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.