Weird behaviour with sequence and random:integer
Wednesday, 22 May 2024 · 3 messages
ML
Marco Lettere22 May 2024 15:34 #
Dear all,
I have this strange behavior when running in BaseX 10.4:
["a","b","c"](1 + random:integer(3))I got something empty result, sometimes (correctly) one out of the three, sometimes two results...
I report the results and the trace in [1] and [2].
What is the reason for this?
Thank you.
Marco.
[1] Two results
a
c
Evaluating:
1
3
3
Compiling:
- rewrite list to xs:string sequence: ("a", "b", "c")
- swap operands: (1 +
random:integer(3))
Optimized Query:
("a", "b", "c")[trace((random:integer(3) + 1))]
Query:
("a", "b", "c")[trace(1 + random:integer(3))]
Result:
- Hit(s): 2 Items
- Updated: 0 Items
- Printed: 3 b
- Read Locking: (none)
- Write Locking: (none)
Timing:
- Parsing: 0.21 ms
- Compiling: 0.4 ms
- Optimizing: 0.1 ms
- Evaluating: 0.57 ms
- Printing: 0.02 ms
- Total Time: 1.31 ms
[2] Empty result
()
Evaluating:
3
3
1
Compiling:
- rewrite list to xs:string sequence: ("a", "b", "c")
- swap operands: (1 + random:integer(3))
Optimized Query:
("a", "b", "c")[trace((random:integer(3) + 1))]
Query:
("a", "b", "c")[trace(1 + random:integer(3))]
Result:
- Hit(s): 0 Items
- Updated: 0 Items
- Printed: 0 b
- Read Locking: (none)
- Write Locking: (none)
Timing:
- Parsing: 0.16 ms
- Compiling: 0.41 ms
- Optimizing: 0.15 ms
- Evaluating: 0.56 ms
- Printing: 0.02 ms
- Total Time: 1.3 ms
Query Plan:
<QueryPlan compiled="true" updating="false">
<CachedFilter type="xs:string*">
<StrSeq type="xs:string+" size="3">
<Str type="xs:string" size="1">a</Str>
<Str type="xs:string" size="1">b</Str>
<Str type="xs:string" size="1">c</Str>
</StrSeq>
<FnTrace name="trace" type="xs:integer" size="1">
<Arith op="+" type="xs:integer" size="1">
<RandomInteger name="random:integer" type="xs:integer" size="1"> <Int type="xs:integer" size="1">3</Int>
</RandomInteger>
<Int type="xs:integer" size="1">1</Int>
</Arith>
</FnTrace>
</CachedFilter>
</QueryPlan>CG
Christian Grün22 May 2024 15:41 #
Hi Marco,
Quoted text · Marco Lettere · 1 line
> ("a", "b", "c")[trace(1 + random:integer(3))]
The filter expression is defined in such a way that the predicate is evaluated anew for every item of the sequence. If you want
random:integer(3) to be evaluated only once, you can either bind it to a variable…let $r := random:integer(3) + 1
return ("a", "b", "c")]$r]…or use functions like
fn:subsequence:
subsequence(("a", "b", "c"), random:integer(3) + 1, 1)
Hope this helps,
ChristianQuoted text · Marco Lettere · 95 lines
Marco Lettere <m.lettere at gmail.com> schrieb am Mi., 22. Mai 2024, 15:34: > Dear all, > > I have this strange behavior when running in BaseX 10.4: > > ["a","b","c"](1 + random:integer(3)) > > I got something empty result, sometimes (correctly) one out of the > three, sometimes two results... > > I report the results and the trace in [1] and [2]. > > What is the reason for this? > > Thank you. > > Marco. > > [1] Two results > a > c > > Evaluating: > 1 > 3 > 3 > Compiling: > - rewrite list to xs:string sequence: ("a", "b", "c") > - swap operands: (1 + random:integer(3)) > Optimized Query: > ("a", "b", "c")[trace((random:integer(3) + 1))] > Query: > ("a", "b", "c")[trace(1 + random:integer(3))] > Result: > - Hit(s): 2 Items > - Updated: 0 Items > - Printed: 3 b > - Read Locking: (none) > - Write Locking: (none) > Timing: > - Parsing: 0.21 ms > - Compiling: 0.4 ms > - Optimizing: 0.1 ms > - Evaluating: 0.57 ms > - Printing: 0.02 ms > - Total Time: 1.31 ms > > > [2] Empty result > () > > Evaluating: > 3 > 3 > 1 > Compiling: > - rewrite list to xs:string sequence: ("a", "b", "c") > - swap operands: (1 + random:integer(3)) > Optimized Query: > ("a", "b", "c")[trace((random:integer(3) + 1))] > Query: > ("a", "b", "c")[trace(1 + random:integer(3))] > Result: > - Hit(s): 0 Items > - Updated: 0 Items > - Printed: 0 b > - Read Locking: (none) > - Write Locking: (none) > Timing: > - Parsing: 0.16 ms > - Compiling: 0.41 ms > - Optimizing: 0.15 ms > - Evaluating: 0.56 ms > - Printing: 0.02 ms > - Total Time: 1.3 ms > Query Plan: > <QueryPlan compiled="true" updating="false"> > <CachedFilter type="xs:string*"> > <StrSeq type="xs:string+" size="3"> > <Str type="xs:string" size="1">a</Str> > <Str type="xs:string" size="1">b</Str> > <Str type="xs:string" size="1">c</Str> > </StrSeq> > <FnTrace name="trace" type="xs:integer" size="1"> > <Arith op="+" type="xs:integer" size="1"> > <RandomInteger name="random:integer" type="xs:integer" size="1"> > <Int type="xs:integer" size="1">3</Int> > </RandomInteger> > <Int type="xs:integer" size="1">1</Int> > </Arith> > </FnTrace> > </CachedFilter> > </QueryPlan> > >
ML
Marco Lettere22 May 2024 15:44 #
Everytime one learns something new.
Thanks for the explanation!
M.
Quoted text · Christian Grün · 118 lines
On 22/05/24 15:41, Christian Grün wrote: > Hi Marco, > > > ("a", "b", "c")[trace(1 + random:integer(3))] > > The filter expression is defined in such a way that the predicate is > evaluated anew for every item of the sequence. If you want > random:integer(3) to be evaluated only once, you can either bind it to > a variable… > > let $r := random:integer(3) + 1 > return ("a", "b", "c")]$r] > > …or use functions like fn:subsequence: > > subsequence(("a", "b", "c"), random:integer(3) + 1, 1) > > Hope this helps, > Christian > > > > Marco Lettere <m.lettere at gmail.com> schrieb am Mi., 22. Mai 2024, 15:34: > > Dear all, > > I have this strange behavior when running in BaseX 10.4: > > ["a","b","c"](1 + random:integer(3)) > > I got something empty result, sometimes (correctly) one out of the > three, sometimes two results... > > I report the results and the trace in [1] and [2]. > > What is the reason for this? > > Thank you. > > Marco. > > [1] Two results > a > c > > Evaluating: > 1 > 3 > 3 > Compiling: > - rewrite list to xs:string sequence: ("a", "b", "c") > - swap operands: (1 + random:integer(3)) > Optimized Query: > ("a", "b", "c")[trace((random:integer(3) + 1))] > Query: > ("a", "b", "c")[trace(1 + random:integer(3))] > Result: > - Hit(s): 2 Items > - Updated: 0 Items > - Printed: 3 b > - Read Locking: (none) > - Write Locking: (none) > Timing: > - Parsing: 0.21 ms > - Compiling: 0.4 ms > - Optimizing: 0.1 ms > - Evaluating: 0.57 ms > - Printing: 0.02 ms > - Total Time: 1.31 ms > > > [2] Empty result > () > > Evaluating: > 3 > 3 > 1 > Compiling: > - rewrite list to xs:string sequence: ("a", "b", "c") > - swap operands: (1 + random:integer(3)) > Optimized Query: > ("a", "b", "c")[trace((random:integer(3) + 1))] > Query: > ("a", "b", "c")[trace(1 + random:integer(3))] > Result: > - Hit(s): 0 Items > - Updated: 0 Items > - Printed: 0 b > - Read Locking: (none) > - Write Locking: (none) > Timing: > - Parsing: 0.16 ms > - Compiling: 0.41 ms > - Optimizing: 0.15 ms > - Evaluating: 0.56 ms > - Printing: 0.02 ms > - Total Time: 1.3 ms > Query Plan: > <QueryPlan compiled="true" updating="false"> > <CachedFilter type="xs:string*"> > <StrSeq type="xs:string+" size="3"> > <Str type="xs:string" size="1">a</Str> > <Str type="xs:string" size="1">b</Str> > <Str type="xs:string" size="1">c</Str> > </StrSeq> > <FnTrace name="trace" type="xs:integer" size="1"> > <Arith op="+" type="xs:integer" size="1"> > <RandomInteger name="random:integer" type="xs:integer" > size="1"> > <Int type="xs:integer" size="1">3</Int> > </RandomInteger> > <Int type="xs:integer" size="1">1</Int> > </Arith> > </FnTrace> > </CachedFilter> > </QueryPlan> >
Written for BaseX 10.7. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery