# Question about query that stuck the KDB process

**URL:** <https://forum.kx.com/t/question-about-query-that-stuck-the-kdb-process/12523>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [April 29, 2022, 8:51am UTC](https://forum.kx.com/t/question-about-query-that-stuck-the-kdb-process/12523 "2022-04-29T08:51:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carfield1](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@carfield1](https://forum.kx.com/u/carfield1)\
**Post date:** [April 29, 2022, 8:51am UTC](https://forum.kx.com/t/question-about-query-that-stuck-the-kdb-process/12523/1 "2022-04-29T08:51:00Z")

</div>

HI all

&nbsp;

Weve run below query

```
select minute, date, sums(size)/sum(size) from select sum(size) by 1 xbar time.minute, date from table where filtering

```

We understand we actually should use % instead of / for divide, but I believe it just return something not correct or throw an error. However, somehow this query hang the process and even click SIGINT interrupt signal it doesnt interrupt, will you have any idea about why?

&nbsp;

Regards,

Carfield

---

<div class="post-metadata">

**Author:** ![SJT1](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@SJT1](https://forum.kx.com/u/SJT1)\
**Post date:** [April 29, 2022, 4:08pm UTC](https://forum.kx.com/t/question-about-query-that-stuck-the-kdb-process/12523/2 "2022-04-29T16:08:00Z")

</div>

Looks like you have already spotted part of the problem. The `/` does not denote Divide `%` but the iteration operator Over. Depending on the values in the `size` column, you might have inadvertently specified a Converge iteration, and one that might not actually converge. Iterations defined this way run very tight and can be difficult to interrupt.

The result column you want is perhaps `sums[size]%sum[size]`, or, avoiding computing both `sum` and `sums`,  
`.[%]1 last\sums size`.

&nbsp;

q)show size:5?10 6 6 1 8 5 q)sums[size]%sum[size] 0.2307692 0.4615385 0.5 0.8076923 1 q).[%]1 last\sums size 0.2307692 0.4615385 0.5 0.8076923 1

&nbsp;

The use above of `1 last\` is an example of the Zen monks idiom. &nbsp;

---

<div class="post-metadata">

**Author:** ![rocuinneagain1](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rocuinneagain1](https://forum.kx.com/u/rocuinneagain1)\
**Post date:** [April 29, 2022, 5:05pm UTC](https://forum.kx.com/t/question-about-query-that-stuck-the-kdb-process/12523/3 "2022-04-29T17:05:00Z")

</div>

https://code.kx.com/q4m3/6\_Functions/#677-more-over-iteration

_The final overload of&nbsp;`/`&nbsp;is equivalent to a while loop in imperative programming. It provides a declarative way to specify a test to end the iteration. Thinking functionally, we provide a predicate function that is applied to the result at each step. The iteration continues as long as the predicate result is&nbsp;`1b`&nbsp;and stops otherwise. For example, we stop the computation of the Fibonacci sequence once it exceeds 1000 as follows._

q)fib:{x,sum -2#x} q)fib/[{1000\>last x}; 1 1] 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

&nbsp;

Your code accidentally boiled down to an infinite loop as the result never moves to 0b

1 1/[sums;1]

&nbsp;

/Each iteration it runs 1 1[1] / Which returns 1 /Then sums 1 /This returns 1 /This is a non zero value which gets treated as 1b so the loop starts again /Infinite loop

---

<div class="post-metadata">

**Author:** ![rocuinneagain1](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rocuinneagain1](https://forum.kx.com/u/rocuinneagain1)\
**Post date:** [April 29, 2022, 5:13pm UTC](https://forum.kx.com/t/question-about-query-that-stuck-the-kdb-process/12523/4 "2022-04-29T17:13:00Z")

</div>

Using parse helps to see what is happening.

&nbsp;

Parsing a simplified version of your query helped to show the order of execution in the parse tree:

q)-1 .Q.s1 last value last parse “select sums(size)/sum(size) from tab”; ((/;`size);+\;(sum;`size))

The q equivalent can then be shown to have the same parse tree

q)-1 .Q.s1 parse"size/[sums;sum size]"; ((/;`size);+\;(sum;`size))

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![carfield1](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@carfield1](https://forum.kx.com/u/carfield1)\
**Post date:** [April 30, 2022, 2:04am UTC](https://forum.kx.com/t/question-about-query-that-stuck-the-kdb-process/12523/5 "2022-04-30T02:04:00Z")

</div>

Thanks a lot, not sure if you have more idea about why this is difficult to interrupt?
