Format a WITH aggregation value into buf */
| 4703 | |
| 4704 | /* Format a WITH aggregation value into buf */ |
| 4705 | static void with_agg_format(const char *func, with_agg_t *agg, int ci, char *buf, size_t buf_sz) { |
| 4706 | if (strcmp(func, "SUM") == 0) { |
| 4707 | snprintf(buf, buf_sz, "%.10g", agg->sums[ci]); |
| 4708 | } else if (strcmp(func, "AVG") == 0) { |
| 4709 | snprintf(buf, buf_sz, "%.10g", agg->counts[ci] > 0 ? agg->sums[ci] / agg->counts[ci] : 0.0); |
| 4710 | } else if (strcmp(func, "MIN") == 0) { |
| 4711 | snprintf(buf, buf_sz, "%.10g", agg->mins[ci]); |
| 4712 | } else if (strcmp(func, "MAX") == 0) { |
| 4713 | snprintf(buf, buf_sz, "%.10g", agg->maxs[ci]); |
| 4714 | } else { |
| 4715 | snprintf(buf, buf_sz, "%d", agg->counts[ci]); |
| 4716 | } |
| 4717 | } |
| 4718 | |
| 4719 | /* Add a scalar virtual variable binding for one WITH item. The value lives in |
| 4720 | * .name; the owned alias string (var_names points at it) lives in .project, |