| 47 | } |
| 48 | |
| 49 | export function columnHistogramQuery( |
| 50 | baseQuery: QueryExp, |
| 51 | colId: string, |
| 52 | colType: ColumnType, |
| 53 | colStats: NumericSummaryStats |
| 54 | ): NumericColumnHistogramQuery | null { |
| 55 | const minVal = colStats.min; |
| 56 | const maxVal = colStats.max; |
| 57 | |
| 58 | if (minVal == null || maxVal == null || minVal === maxVal) { |
| 59 | return null; |
| 60 | } |
| 61 | const binCount = binsForColumn(colStats); |
| 62 | |
| 63 | const [niceMinVal, niceMaxVal] = nice(minVal, maxVal, binCount); |
| 64 | |
| 65 | const binWidth = (niceMaxVal - niceMinVal) / binCount; |
| 66 | |
| 67 | // add a column with bin number: |
| 68 | const binQuery = baseQuery |
| 69 | .extend("column", constVal(colId)) |
| 70 | .extend( |
| 71 | "bin", |
| 72 | cast( |
| 73 | floor( |
| 74 | divide( |
| 75 | minus( |
| 76 | cast(col(colId), doubleType), |
| 77 | cast(constVal(niceMinVal), doubleType) |
| 78 | ), |
| 79 | cast(constVal(binWidth), doubleType) |
| 80 | ) |
| 81 | ), |
| 82 | intType |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | const histoQuery = binQuery |
| 87 | .extend("binCount", constVal(1)) |
| 88 | .project(["column", "bin", "binCount"]) |
| 89 | .groupBy(["column", "bin"], [["count", "binCount"]]); |
| 90 | |
| 91 | const ret = { |
| 92 | colId, |
| 93 | histoQuery, |
| 94 | minVal, |
| 95 | maxVal, |
| 96 | niceMinVal, |
| 97 | niceMaxVal, |
| 98 | binCount, |
| 99 | binWidth, |
| 100 | }; |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | // histogram data for rendering a single column histogram |
| 105 | export interface NumericColumnHistogramData { |