From 50bd0592b6d28a532ab5bd9eb58d2e3ada8edcdb Mon Sep 17 00:00:00 2001 From: Jason Smart Date: Sat, 21 Dec 2024 21:16:05 -0800 Subject: [PATCH] Added initial support for BQ HLL functions Signed-off-by: Jason Smart --- .../dialect/standardsql/dialect_functions.ts | 36 +++++++++++++++++++ .../src/dialect/standardsql/standardsql.ts | 1 + 2 files changed, 37 insertions(+) diff --git a/packages/malloy/src/dialect/standardsql/dialect_functions.ts b/packages/malloy/src/dialect/standardsql/dialect_functions.ts index b2f08dfd4..33be2308d 100644 --- a/packages/malloy/src/dialect/standardsql/dialect_functions.ts +++ b/packages/malloy/src/dialect/standardsql/dialect_functions.ts @@ -10,8 +10,12 @@ import { DefinitionBlueprint, DefinitionBlueprintMap, OverloadedDefinitionBlueprint, + TypeDescBlueprint, } from '../functions/util'; +// Cute shortcut So you can write things like: {array: T} and {dimension: T} +const T: TypeDescBlueprint = {generic: 'T'}; + const date_from_unix_date: DefinitionBlueprint = { takes: {'unix_date': 'number'}, returns: 'date', @@ -69,6 +73,38 @@ export const STANDARDSQL_DIALECT_FUNCTIONS: DefinitionBlueprintMap = { date_from_unix_date, string_agg, string_agg_distinct, + hll_accumulate: { + default: { + takes: {'value': {dimension: T}}, + returns: {measure: {sql_native: 'bytes'}}, + generic: { + 'T': ['string', 'number'], + }, + isSymmetric: true, + impl: {function: 'HLL_COUNT.INIT'}, + }, + }, + hll_combine: { + takes: {'value': {sql_native: 'bytes'}}, + returns: {measure: {sql_native: 'bytes'}}, + impl: {function: 'HLL_COUNT.MERGE_PARTIAL'}, + isSymmetric: true, + }, + hll_estimate: { + takes: {'value': {sql_native: 'bytes'}}, + returns: {dimension: 'number'}, + impl: {function: 'HLL_COUNT.EXTRACT'}, + }, + hll_export: { + takes: {'value': {sql_native: 'bytes'}}, + returns: {dimension: {sql_native: 'bytes'}}, + impl: {sql: 'CAST(${value} AS BYTES)'}, + }, + hll_import: { + takes: {'value': {sql_native: 'bytes'}}, + returns: {dimension: {sql_native: 'bytes'}}, + impl: {sql: 'CAST(${value} AS BYTES)'}, + }, ...def('repeat', {'str': 'string', 'n': 'number'}, 'string'), ...def('reverse', {'str': 'string'}, 'string'), }; diff --git a/packages/malloy/src/dialect/standardsql/standardsql.ts b/packages/malloy/src/dialect/standardsql/standardsql.ts index 7ea2dcdff..309504cc0 100644 --- a/packages/malloy/src/dialect/standardsql/standardsql.ts +++ b/packages/malloy/src/dialect/standardsql/standardsql.ts @@ -126,6 +126,7 @@ export class StandardSQLDialect extends Dialect { cantPartitionWindowFunctionsOnExpressions = true; hasModOperator = false; nestedArrays = false; // Can't have an array of arrays for some reason + supportsHyperLogLog = true; quoteTablePath(tablePath: string): string { return `\`${tablePath}\``;