@holistics/cli-core 2.5.5 → 2.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/commands.js +717 -450
  2. package/package.json +1 -1
package/dist/commands.js CHANGED
@@ -24022,7 +24022,7 @@ var require_dist4 = __commonJS((exports2) => {
24022
24022
  exports2.default = deferred;
24023
24023
  });
24024
24024
  // package.json
24025
- var version = "2.5.5";
24025
+ var version = "2.5.7";
24026
24026
 
24027
24027
  // ../../node_modules/.pnpm/commander@15.0.0/node_modules/commander/lib/error.js
24028
24028
  class CommanderError extends Error {
@@ -105272,7 +105272,7 @@ var TypeClass = /* @__PURE__ */ function(e9) {
105272
105272
  return e9.SCALAR = "scalar", e9.FIELD = "field", e9.ROW = "row", e9.TABLE = "table", e9.RELATIONSHIP = "relationship", e9.ARRAY = "array", e9.GROUPING_SET = "grouping_set", e9.FUNCTION = "function", e9.UNION = "union", e9.TYPE_PARAMETER = "type_parameter", e9.TYPE_CLASS = "type_class", e9.USER_ATTRIBUTE = "user_attribute", e9.MODULE = "module", e9.ERROR_ANY = "error_any", e9.NULL = "null", e9;
105273
105273
  }({});
105274
105274
  var ErrorCode = /* @__PURE__ */ function(e9) {
105275
- return e9[e9.UNEXPECTED_ERROR = 0] = "UNEXPECTED_ERROR", e9[e9.SYNTAX_ERROR = 1] = "SYNTAX_ERROR", e9[e9.INVALID_EXPRESSION_0 = 100] = "INVALID_EXPRESSION_0", e9[e9.RELATIONSHIP_NOT_FOUND_0 = 101] = "RELATIONSHIP_NOT_FOUND_0", e9[e9.MODEL_0_NOT_FOUND = 102] = "MODEL_0_NOT_FOUND", e9[e9.FIELD_0_NOT_FOUND_IN_MODEL_1 = 103] = "FIELD_0_NOT_FOUND_IN_MODEL_1", e9[e9.FANOUT_ON_0_FROM_1 = 104] = "FANOUT_ON_0_FROM_1", e9[e9.MODEL_IN_AQL_FIELD_NOT_FOUND = 105] = "MODEL_IN_AQL_FIELD_NOT_FOUND", e9[e9.RELATIONSHIP_PATH_NOT_FOUND_FROM_0_TO_1 = 106] = "RELATIONSHIP_PATH_NOT_FOUND_FROM_0_TO_1", e9[e9.INVALID_ARITY_EXPECT_0_FOR_FUNCTION_1_GOT_2 = 200] = "INVALID_ARITY_EXPECT_0_FOR_FUNCTION_1_GOT_2", e9[e9.INVALID_ARGUMENT_TYPE = 201] = "INVALID_ARGUMENT_TYPE", e9[e9.UNKNOWN_IDENTIFIER_0 = 202] = "UNKNOWN_IDENTIFIER_0", e9[e9.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1 = 203] = "UNSUPPORTED_OPERATOR_0_FOR_TYPE_1", e9[e9.EXPECTED_0_FOUND_1 = 204] = "EXPECTED_0_FOUND_1", e9[e9.NAMED_EXPRESSION_EXPECT_A_FIELD_OR_SCALAR_VALUE_FOUND_0 = 205] = "NAMED_EXPRESSION_EXPECT_A_FIELD_OR_SCALAR_VALUE_FOUND_0", e9[e9.EXPECTED_SCALAR_LIKE_TYPE_FOUND_0 = 206] = "EXPECTED_SCALAR_LIKE_TYPE_FOUND_0", e9[e9.NO_MATCHING_SIGNATURE_FOR_FUNCTION_0 = 207] = "NO_MATCHING_SIGNATURE_FOR_FUNCTION_0", e9[e9.INVALID_MEASURE_DEFINITION_FOR_FIELD_0_IN_MODEL_1 = 208] = "INVALID_MEASURE_DEFINITION_FOR_FIELD_0_IN_MODEL_1", e9[e9.DIMENSIONALIZE_NOT_ALLOWED_DUE_TO_0 = 209] = "DIMENSIONALIZE_NOT_ALLOWED_DUE_TO_0", e9[e9.CANNOT_REFERENCE_AQL_FIELD_0_FROM_SQL = 210] = "CANNOT_REFERENCE_AQL_FIELD_0_FROM_SQL", e9[e9.RIGHT_SIDE_OF_PIPE_MUST_BE_A_FUNCTION = 211] = "RIGHT_SIDE_OF_PIPE_MUST_BE_A_FUNCTION", e9[e9.INVALID_METRIC_DEFINITION_0_IN_DATASET_1 = 212] = "INVALID_METRIC_DEFINITION_0_IN_DATASET_1", e9[e9.MEMBER_0_NOT_FOUND_IN_MODULE_1 = 213] = "MEMBER_0_NOT_FOUND_IN_MODULE_1", e9[e9.INVALID_BIZ_CAL_FUNCTION_0 = 214] = "INVALID_BIZ_CAL_FUNCTION_0", e9[e9.USER_ATTRIBUTE_0_NOT_FOUND = 215] = "USER_ATTRIBUTE_0_NOT_FOUND", e9[e9.INVALID_USAGE_OF_USER_ATTRIBUTE_0_MUTIPLE_VALUES = 216] = "INVALID_USAGE_OF_USER_ATTRIBUTE_0_MUTIPLE_VALUES", e9[e9.TABLE_LITERAL_ROWS_MUST_HAVE_SAME_NUMBER_OF_COLUMNS = 217] = "TABLE_LITERAL_ROWS_MUST_HAVE_SAME_NUMBER_OF_COLUMNS", e9[e9.INVALID_ROW_LEVEL_PERMISSION = 218] = "INVALID_ROW_LEVEL_PERMISSION", e9[e9.PERMISSION_RULE_VIOLATION_ERROR = 219] = "PERMISSION_RULE_VIOLATION_ERROR", e9[e9.UNSUPPORTED_BEHAVIOR_0 = 220] = "UNSUPPORTED_BEHAVIOR_0", e9[e9.POTENTIAL_DB_RUNTIME_ERROR_0 = 221] = "POTENTIAL_DB_RUNTIME_ERROR_0", e9[e9.AMBIGUOUS_RELATIONSHIP_0 = 222] = "AMBIGUOUS_RELATIONSHIP_0", e9[e9.QUERY_PARAM_UNSUPPORTED_OPERATOR = 223] = "QUERY_PARAM_UNSUPPORTED_OPERATOR", e9[e9.QUERY_PARAM_INVALID_PARAM_INPUT = 224] = "QUERY_PARAM_INVALID_PARAM_INPUT", e9[e9.ROW_MUST_CONTAIN_DIMENSIONS_ONLY_0 = 225] = "ROW_MUST_CONTAIN_DIMENSIONS_ONLY_0", e9[e9.ROW_COLUMNS_MUST_BE_SUBSET_OF_TABLE_COLUMNS = 226] = "ROW_COLUMNS_MUST_BE_SUBSET_OF_TABLE_COLUMNS", e9[e9.CANNOT_MATCH_ROW_WITH_LITERAL_TABLE_0 = 227] = "CANNOT_MATCH_ROW_WITH_LITERAL_TABLE_0", e9[e9.INVALID_FIELD_DECLARATION = 228] = "INVALID_FIELD_DECLARATION", e9[e9.CANNOT_REDECLARE_FIELD = 229] = "CANNOT_REDECLARE_FIELD", e9[e9.ADHOC_FIELD_UNSUPPORTED_FIELD_TYPE = 230] = "ADHOC_FIELD_UNSUPPORTED_FIELD_TYPE", e9[e9.CANNOT_RUN_WINDOW_FUNCTION_IN_THIS_CONTEXT_0 = 231] = "CANNOT_RUN_WINDOW_FUNCTION_IN_THIS_CONTEXT_0", e9[e9.INVALID_WINDOW_FRAME_0 = 232] = "INVALID_WINDOW_FRAME_0", e9[e9.CYLIC_FIELD_DEPENDENCY_DETECTED_0 = 233] = "CYLIC_FIELD_DEPENDENCY_DETECTED_0", e9[e9.INVALID_SHORTHAND_AGGREGATION_0 = 234] = "INVALID_SHORTHAND_AGGREGATION_0", e9[e9.UNSUPPORTED_DATE_UNIT_0 = 235] = "UNSUPPORTED_DATE_UNIT_0", e9[e9.INVALID_CONDITIONAL_EXPRESSION_IN_0 = 236] = "INVALID_CONDITIONAL_EXPRESSION_IN_0", e9[e9.UNKNOWN_FUNCTION_0 = 237] = "UNKNOWN_FUNCTION_0", e9[e9.CANNOT_MATCH_ROW_WITH_TABLE_0 = 238] = "CANNOT_MATCH_ROW_WITH_TABLE_0", e9[e9.DUPLICATE_FIELD_NAME_0 = 239] = "DUPLICATE_FIELD_NAME_0", e9[e9.FUNCTION_0_EXPECT_1_GOT_2 = 240] = "FUNCTION_0_EXPECT_1_GOT_2", e9[e9.NESTED_WINDOW_FUNCTION = 241] = "NESTED_WINDOW_FUNCTION", e9[e9.FIELD_0_NOT_FOUND_IN_SELECTED_FIELDS = 242] = "FIELD_0_NOT_FOUND_IN_SELECTED_FIELDS", e9[e9.REFERENCE_FIELD_0_NOT_GROUP_NOR_AGGREGATED = 243] = "REFERENCE_FIELD_0_NOT_GROUP_NOR_AGGREGATED", e9[e9.INVALID_TABLE_CONDITION_0 = 244] = "INVALID_TABLE_CONDITION_0", e9[e9.INVALID_DATETIME_LITERAL_0 = 245] = "INVALID_DATETIME_LITERAL_0", e9[e9.NO_PRIMARY_KEY_FOUND_FOR_MODEL_0_REASON_1 = 246] = "NO_PRIMARY_KEY_FOUND_FOR_MODEL_0_REASON_1", e9[e9.RECURSIVE_AQL_FUNCTION_0_CHAIN_1 = 247] = "RECURSIVE_AQL_FUNCTION_0_CHAIN_1", e9[e9.INVALID_RETURN_TYPE_FOR_FUNCTION_0_EXPECTED_1_GOT_2 = 248] = "INVALID_RETURN_TYPE_FOR_FUNCTION_0_EXPECTED_1_GOT_2", e9[e9.INVALID_REF_ARGUMENT_FOR_PARAM_0_EXPECTED_1_GOT_2 = 249] = "INVALID_REF_ARGUMENT_FOR_PARAM_0_EXPECTED_1_GOT_2", e9[e9.AQL_0_1_SHADOWS_BUILTIN = 250] = "AQL_0_1_SHADOWS_BUILTIN", e9[e9.RELATIONSHIP_FILTER_ON_0_READS_1 = 251] = "RELATIONSHIP_FILTER_ON_0_READS_1", e9[e9.RELATIONSHIP_MATCH_READS_0 = 252] = "RELATIONSHIP_MATCH_READS_0", e9[e9.RELATIONSHIP_MATCH_MISSES_0 = 253] = "RELATIONSHIP_MATCH_MISSES_0", e9[e9.AQL_NAME_0_DECLARED_AS_1_AND_2_IN_3 = 254] = "AQL_NAME_0_DECLARED_AS_1_AND_2_IN_3", e9[e9.SHOULD_FOLLOW_GROUP_WITH_SELECT_OR_FILTER = 300] = "SHOULD_FOLLOW_GROUP_WITH_SELECT_OR_FILTER", e9;
105275
+ return e9[e9.UNEXPECTED_ERROR = 0] = "UNEXPECTED_ERROR", e9[e9.SYNTAX_ERROR = 1] = "SYNTAX_ERROR", e9[e9.INVALID_EXPRESSION_0 = 100] = "INVALID_EXPRESSION_0", e9[e9.RELATIONSHIP_NOT_FOUND_0 = 101] = "RELATIONSHIP_NOT_FOUND_0", e9[e9.MODEL_0_NOT_FOUND = 102] = "MODEL_0_NOT_FOUND", e9[e9.FIELD_0_NOT_FOUND_IN_MODEL_1 = 103] = "FIELD_0_NOT_FOUND_IN_MODEL_1", e9[e9.FANOUT_ON_0_FROM_1 = 104] = "FANOUT_ON_0_FROM_1", e9[e9.MODEL_IN_AQL_FIELD_NOT_FOUND = 105] = "MODEL_IN_AQL_FIELD_NOT_FOUND", e9[e9.RELATIONSHIP_PATH_NOT_FOUND_FROM_0_TO_1 = 106] = "RELATIONSHIP_PATH_NOT_FOUND_FROM_0_TO_1", e9[e9.INVALID_ARITY_EXPECT_0_FOR_FUNCTION_1_GOT_2 = 200] = "INVALID_ARITY_EXPECT_0_FOR_FUNCTION_1_GOT_2", e9[e9.INVALID_ARGUMENT_TYPE = 201] = "INVALID_ARGUMENT_TYPE", e9[e9.UNKNOWN_IDENTIFIER_0 = 202] = "UNKNOWN_IDENTIFIER_0", e9[e9.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1 = 203] = "UNSUPPORTED_OPERATOR_0_FOR_TYPE_1", e9[e9.EXPECTED_0_FOUND_1 = 204] = "EXPECTED_0_FOUND_1", e9[e9.NAMED_EXPRESSION_EXPECT_A_FIELD_OR_SCALAR_VALUE_FOUND_0 = 205] = "NAMED_EXPRESSION_EXPECT_A_FIELD_OR_SCALAR_VALUE_FOUND_0", e9[e9.EXPECTED_SCALAR_LIKE_TYPE_FOUND_0 = 206] = "EXPECTED_SCALAR_LIKE_TYPE_FOUND_0", e9[e9.NO_MATCHING_SIGNATURE_FOR_FUNCTION_0 = 207] = "NO_MATCHING_SIGNATURE_FOR_FUNCTION_0", e9[e9.INVALID_MEASURE_DEFINITION_FOR_FIELD_0_IN_MODEL_1 = 208] = "INVALID_MEASURE_DEFINITION_FOR_FIELD_0_IN_MODEL_1", e9[e9.DIMENSIONALIZE_NOT_ALLOWED_DUE_TO_0 = 209] = "DIMENSIONALIZE_NOT_ALLOWED_DUE_TO_0", e9[e9.CANNOT_REFERENCE_AQL_FIELD_0_FROM_SQL = 210] = "CANNOT_REFERENCE_AQL_FIELD_0_FROM_SQL", e9[e9.RIGHT_SIDE_OF_PIPE_MUST_BE_A_FUNCTION = 211] = "RIGHT_SIDE_OF_PIPE_MUST_BE_A_FUNCTION", e9[e9.INVALID_METRIC_DEFINITION_0_IN_DATASET_1 = 212] = "INVALID_METRIC_DEFINITION_0_IN_DATASET_1", e9[e9.MEMBER_0_NOT_FOUND_IN_MODULE_1 = 213] = "MEMBER_0_NOT_FOUND_IN_MODULE_1", e9[e9.INVALID_BIZ_CAL_FUNCTION_0 = 214] = "INVALID_BIZ_CAL_FUNCTION_0", e9[e9.USER_ATTRIBUTE_0_NOT_FOUND = 215] = "USER_ATTRIBUTE_0_NOT_FOUND", e9[e9.INVALID_USAGE_OF_USER_ATTRIBUTE_0_MUTIPLE_VALUES = 216] = "INVALID_USAGE_OF_USER_ATTRIBUTE_0_MUTIPLE_VALUES", e9[e9.TABLE_LITERAL_ROWS_MUST_HAVE_SAME_NUMBER_OF_COLUMNS = 217] = "TABLE_LITERAL_ROWS_MUST_HAVE_SAME_NUMBER_OF_COLUMNS", e9[e9.INVALID_ROW_LEVEL_PERMISSION = 218] = "INVALID_ROW_LEVEL_PERMISSION", e9[e9.PERMISSION_RULE_VIOLATION_ERROR = 219] = "PERMISSION_RULE_VIOLATION_ERROR", e9[e9.UNSUPPORTED_BEHAVIOR_0 = 220] = "UNSUPPORTED_BEHAVIOR_0", e9[e9.POTENTIAL_DB_RUNTIME_ERROR_0 = 221] = "POTENTIAL_DB_RUNTIME_ERROR_0", e9[e9.AMBIGUOUS_RELATIONSHIP_0 = 222] = "AMBIGUOUS_RELATIONSHIP_0", e9[e9.QUERY_PARAM_UNSUPPORTED_OPERATOR = 223] = "QUERY_PARAM_UNSUPPORTED_OPERATOR", e9[e9.QUERY_PARAM_INVALID_PARAM_INPUT = 224] = "QUERY_PARAM_INVALID_PARAM_INPUT", e9[e9.ROW_MUST_CONTAIN_DIMENSIONS_ONLY_0 = 225] = "ROW_MUST_CONTAIN_DIMENSIONS_ONLY_0", e9[e9.ROW_COLUMNS_MUST_BE_SUBSET_OF_TABLE_COLUMNS = 226] = "ROW_COLUMNS_MUST_BE_SUBSET_OF_TABLE_COLUMNS", e9[e9.CANNOT_MATCH_ROW_WITH_LITERAL_TABLE_0 = 227] = "CANNOT_MATCH_ROW_WITH_LITERAL_TABLE_0", e9[e9.INVALID_FIELD_DECLARATION = 228] = "INVALID_FIELD_DECLARATION", e9[e9.CANNOT_REDECLARE_FIELD = 229] = "CANNOT_REDECLARE_FIELD", e9[e9.ADHOC_FIELD_UNSUPPORTED_FIELD_TYPE = 230] = "ADHOC_FIELD_UNSUPPORTED_FIELD_TYPE", e9[e9.CANNOT_RUN_WINDOW_FUNCTION_IN_THIS_CONTEXT_0 = 231] = "CANNOT_RUN_WINDOW_FUNCTION_IN_THIS_CONTEXT_0", e9[e9.INVALID_WINDOW_FRAME_0 = 232] = "INVALID_WINDOW_FRAME_0", e9[e9.CYLIC_FIELD_DEPENDENCY_DETECTED_0 = 233] = "CYLIC_FIELD_DEPENDENCY_DETECTED_0", e9[e9.INVALID_SHORTHAND_AGGREGATION_0 = 234] = "INVALID_SHORTHAND_AGGREGATION_0", e9[e9.UNSUPPORTED_DATE_UNIT_0 = 235] = "UNSUPPORTED_DATE_UNIT_0", e9[e9.INVALID_CONDITIONAL_EXPRESSION_IN_0 = 236] = "INVALID_CONDITIONAL_EXPRESSION_IN_0", e9[e9.UNKNOWN_FUNCTION_0 = 237] = "UNKNOWN_FUNCTION_0", e9[e9.CANNOT_MATCH_ROW_WITH_TABLE_0 = 238] = "CANNOT_MATCH_ROW_WITH_TABLE_0", e9[e9.DUPLICATE_FIELD_NAME_0 = 239] = "DUPLICATE_FIELD_NAME_0", e9[e9.FUNCTION_0_EXPECT_1_GOT_2 = 240] = "FUNCTION_0_EXPECT_1_GOT_2", e9[e9.NESTED_WINDOW_FUNCTION = 241] = "NESTED_WINDOW_FUNCTION", e9[e9.FIELD_0_NOT_FOUND_IN_SELECTED_FIELDS = 242] = "FIELD_0_NOT_FOUND_IN_SELECTED_FIELDS", e9[e9.REFERENCE_FIELD_0_NOT_GROUP_NOR_AGGREGATED = 243] = "REFERENCE_FIELD_0_NOT_GROUP_NOR_AGGREGATED", e9[e9.INVALID_TABLE_CONDITION_0 = 244] = "INVALID_TABLE_CONDITION_0", e9[e9.INVALID_DATETIME_LITERAL_0 = 245] = "INVALID_DATETIME_LITERAL_0", e9[e9.NO_PRIMARY_KEY_FOUND_FOR_MODEL_0_REASON_1 = 246] = "NO_PRIMARY_KEY_FOUND_FOR_MODEL_0_REASON_1", e9[e9.RECURSIVE_AQL_FUNCTION_0_CHAIN_1 = 247] = "RECURSIVE_AQL_FUNCTION_0_CHAIN_1", e9[e9.INVALID_RETURN_TYPE_FOR_FUNCTION_0_EXPECTED_1_GOT_2 = 248] = "INVALID_RETURN_TYPE_FOR_FUNCTION_0_EXPECTED_1_GOT_2", e9[e9.INVALID_REF_ARGUMENT_FOR_PARAM_0_EXPECTED_1_GOT_2 = 249] = "INVALID_REF_ARGUMENT_FOR_PARAM_0_EXPECTED_1_GOT_2", e9[e9.AQL_0_1_SHADOWS_BUILTIN = 250] = "AQL_0_1_SHADOWS_BUILTIN", e9[e9.RELATIONSHIP_FILTER_ON_0_READS_1 = 251] = "RELATIONSHIP_FILTER_ON_0_READS_1", e9[e9.RELATIONSHIP_MATCH_READS_0 = 252] = "RELATIONSHIP_MATCH_READS_0", e9[e9.RELATIONSHIP_MATCH_MISSES_0 = 253] = "RELATIONSHIP_MATCH_MISSES_0", e9[e9.AQL_NAME_0_DECLARED_AS_1_AND_2_IN_3 = 254] = "AQL_NAME_0_DECLARED_AS_1_AND_2_IN_3", e9[e9.OPERATOR_0_ON_PARAM_1 = 255] = "OPERATOR_0_ON_PARAM_1", e9[e9.SQL_PASSTHROUGH_FUNCTION_0_NOT_ALLOWED = 256] = "SQL_PASSTHROUGH_FUNCTION_0_NOT_ALLOWED", e9[e9.SHOULD_FOLLOW_GROUP_WITH_SELECT_OR_FILTER = 300] = "SHOULD_FOLLOW_GROUP_WITH_SELECT_OR_FILTER", e9;
105276
105276
  }({});
105277
105277
  var isFieldTrace = (e9) => e9.type !== "udf";
105278
105278
  var isUdfTrace = (e9) => e9.type === "udf";
@@ -108060,6 +108060,7 @@ var errorMessages = {
108060
108060
  [ErrorCode.INVALID_ARGUMENT_TYPE]: "Invalid argument type.",
108061
108061
  [ErrorCode.UNKNOWN_IDENTIFIER_0]: "Unknown identifier `{0}` is not defined.",
108062
108062
  [ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1]: "Unsupported operator `{0}` for type `{1}`.",
108063
+ [ErrorCode.OPERATOR_0_ON_PARAM_1]: "Param `{1}` holds a list of values, so operator `{0}` cannot use it directly. To read it, use `first({1})` for the first value, or `in {1}` to match any value.",
108063
108064
  [ErrorCode.EXPECTED_0_FOUND_1]: "Expected `{0}`, found `{1}`.",
108064
108065
  [ErrorCode.FUNCTION_0_EXPECT_1_GOT_2]: "The `{0}` function expects `{1}`, but got `{2}` instead. Please update the input or use another function.",
108065
108066
  [ErrorCode.NAMED_EXPRESSION_EXPECT_A_FIELD_OR_SCALAR_VALUE_FOUND_0]: "Named expression expects a field or scalar value, found `{0}`",
@@ -108073,6 +108074,7 @@ var errorMessages = {
108073
108074
  [ErrorCode.MEMBER_0_NOT_FOUND_IN_MODULE_1]: "Member `{0}` not found in module `{1}`.",
108074
108075
  [ErrorCode.INVALID_BIZ_CAL_FUNCTION_0]: "The function `{0}` is not part of the business calculation language. Please check the documentation for the list of supported functions. If you're trying to use a function from the AQL language, please convert this business calculation to an AQL field.",
108075
108076
  [ErrorCode.USER_ATTRIBUTE_0_NOT_FOUND]: "User attribute `{0}` not found.",
108077
+ [ErrorCode.SQL_PASSTHROUGH_FUNCTION_0_NOT_ALLOWED]: "SQL passthrough function `{0}` is not allowed in ad-hoc fields. Define the field in the dataset or model instead.",
108076
108078
  [ErrorCode.INVALID_USAGE_OF_USER_ATTRIBUTE_0_MUTIPLE_VALUES]: "Invalid usage of user attribute `{0}`. User attribute contains mutiple values and cannot be used in this context.",
108077
108079
  [ErrorCode.TABLE_LITERAL_ROWS_MUST_HAVE_SAME_NUMBER_OF_COLUMNS]: "Table literal rows must have the same number of columns.",
108078
108080
  [ErrorCode.INVALID_ROW_LEVEL_PERMISSION]: "Invalid row level permission.",
@@ -108122,7 +108124,7 @@ Complex conditions:
108122
108124
  [ErrorCode.RELATIONSHIP_MATCH_MISSES_0]: "A relationship `match` must read columns of both models, but it never reads `{0}`."
108123
108125
  };
108124
108126
  function createError(e9, t, n, r3, i, a, s3) {
108125
- let c3 = `${errorMessages[e9]} ${n || ""}`.replace("{0}", u(r3)).replace("{1}", u(i)).replace("{2}", u(a)).replace("{3}", u(s3));
108127
+ let c3 = `${errorMessages[e9]} ${n || ""}`.replace(/\{0\}/g, () => u(r3)).replace(/\{1\}/g, () => u(i)).replace(/\{2\}/g, () => u(a)).replace(/\{3\}/g, () => u(s3));
108126
108128
  (e9 === ErrorCode.EXPECTED_0_FOUND_1 && (u(r3) === "NamedField" || u(r3).match(/Union\(.*NamedField.*\)/)) || e9 === ErrorCode.FUNCTION_0_EXPECT_1_GOT_2 && (u(i) === "NamedField" || u(i).match(/Union\(.*NamedField.*\)/))) && (c3 = "Cannot infer a name for this field. Please provide an explicit name for this field.");
108127
108129
  let l = SyntaxError(String(c3));
108128
108130
  return l.args = [
@@ -108176,6 +108178,9 @@ function cylicFieldDependencyError(e9, t) {
108176
108178
  let n = e9.map((e10) => e10.join(" -> ")).join("; ");
108177
108179
  return createError(ErrorCode.CYLIC_FIELD_DEPENDENCY_DETECTED_0, t, n);
108178
108180
  }
108181
+ function shorthandAggregationOverConstMessage(e9, t) {
108182
+ return `\`${t}\` is a const, so ${e9}(${t}) cannot infer the table to aggregate over. Aggregate the expression itself, e.g. ${e9}(<expression>), or bind a table to the const.`;
108183
+ }
108179
108184
  function shorthandAggregationWithMultipleModelsMessage(e9, t) {
108180
108185
  return `When there are multiple models involved, the table must be explicitly stated to avoid ambiguity. Valid form of the expression: ${e9}(<table>, ${t.loc?.source ? `${t.loc?.source}` : "<field expression>"}) . Please add the table to the query and try again.
108181
108186
  Correct usages:
@@ -119697,7 +119702,9 @@ function checkRowLevelExpression(e10, t, n) {
119697
119702
  function u(t3, n3) {
119698
119703
  e10.checkExpression(t3.right, n3);
119699
119704
  let r4 = e10.getTypeFromNode(t3.right);
119700
- if (isScalarLikeType(r4)) {
119705
+ if (isTableType(r4) && r4.param)
119706
+ e10.addError(t3.right, ErrorCode.OPERATOR_0_ON_PARAM_1, t3.operator.loc.source, r4.param);
119707
+ else if (isScalarLikeType(r4)) {
119701
119708
  let n4 = getScalarType(r4), i3 = t3.operator.kind, a3;
119702
119709
  if (n4 === numberType$1 && i3 === Kind.MINUS ? a3 = numberType$1 : isAssignable$1(e10, n4, truefalseType$1) && (i3 === Kind.NOT_KEYWORD || i3 === Kind.EXCLAMATION) && (a3 = truefalseType$1), a3) {
119703
119710
  if (isFieldType(r4)) {
@@ -119715,7 +119722,21 @@ function checkRowLevelExpression(e10, t, n) {
119715
119722
  }
119716
119723
  function f(t3, n3) {
119717
119724
  e10.checkExpression(t3.left, n3), e10.checkExpression(t3.right, n3);
119718
- let r4 = e10.getTypeFromNode(t3.left), i3 = e10.getTypeFromNode(t3.right), a3 = isFieldType(r4) && r4.hasWindowFunction || isFieldType(i3) && i3.hasWindowFunction;
119725
+ let r4 = e10.getTypeFromNode(t3.left), i3 = e10.getTypeFromNode(t3.right), a3 = isFieldType(r4) && r4.hasWindowFunction || isFieldType(i3) && i3.hasWindowFunction, s4 = t3.operator.kind === Kind.IN_KEYWORD ? [{
119726
+ expr: t3.left,
119727
+ type: r4
119728
+ }] : [{
119729
+ expr: t3.left,
119730
+ type: r4
119731
+ }, {
119732
+ expr: t3.right,
119733
+ type: i3
119734
+ }];
119735
+ for (let { expr: n4, type: r5 } of s4)
119736
+ if (isTableType(r5) && r5.param) {
119737
+ e10.addError(n4, ErrorCode.OPERATOR_0_ON_PARAM_1, t3.operator.loc.source, r5.param);
119738
+ return;
119739
+ }
119719
119740
  if (isTableType(r4))
119720
119741
  e10.addError(t3.left, ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1, t3.operator.loc.source, "Table");
119721
119742
  else if (isRowType(r4))
@@ -119728,14 +119749,14 @@ function checkRowLevelExpression(e10, t, n) {
119728
119749
  e10.addError(t3, ErrorCode.EXPECTED_SCALAR_LIKE_TYPE_FOUND_0, r4);
119729
119750
  return;
119730
119751
  }
119731
- let s4;
119752
+ let s5;
119732
119753
  if (isTableType(i3)) {
119733
119754
  if (isTableLiteral(t3.right) && i3.rowType.columns.length > 1) {
119734
119755
  e10.addError(t3.right, ErrorCode.EXPECTED_0_FOUND_1, "Table Literal with one column", i3);
119735
119756
  return;
119736
119757
  }
119737
119758
  if (isTableLiteral(t3.right) || i3.isLiteral)
119738
- s4 = i3.rowType.columns[0]?.type || getScalarType(r4);
119759
+ s5 = i3.rowType.columns[0]?.type || getScalarType(r4);
119739
119760
  else if (isFieldType(r4) && r4.subClass === "dimension" && t3.operator.kind === Kind.IN_KEYWORD) {
119740
119761
  let n5 = r4, a4 = i3.rowType.columns.some((e11) => e11.origin && n5.origin && refEq(e11.origin, n5.origin)), c5 = i3.rowType.columns.length === 1 && isAssignable$1(e10, n5.type, i3.rowType.columns[0].type);
119741
119762
  if (i3.name) {
@@ -119743,7 +119764,7 @@ function checkRowLevelExpression(e10, t, n) {
119743
119764
  return;
119744
119765
  }
119745
119766
  if (a4 || c5)
119746
- s4 = getScalarType(r4);
119767
+ s5 = getScalarType(r4);
119747
119768
  else {
119748
119769
  e10.addError(t3.right, ErrorCode.EXPECTED_0_FOUND_1, `Table that includes the \`${r4.fieldName}\` dimension`, i3);
119749
119770
  return;
@@ -119753,9 +119774,9 @@ function checkRowLevelExpression(e10, t, n) {
119753
119774
  return;
119754
119775
  }
119755
119776
  } else
119756
- s4 = isNullType(i3) ? n4 : getScalarType(i3);
119777
+ s5 = isNullType(i3) ? n4 : getScalarType(i3);
119757
119778
  let c4 = t3.operator.kind;
119758
- if (sqlTypeEquivalent(n4.sqlType, s4.sqlType)) {
119779
+ if (sqlTypeEquivalent(n4.sqlType, s5.sqlType)) {
119759
119780
  if (isTableType(i3) && c4 !== Kind.IN_KEYWORD || !isTableType(i3) && !isUserAttributeType(i3) && c4 === Kind.IN_KEYWORD) {
119760
119781
  e10.addError(t3.operator, ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1, c4, i3);
119761
119782
  return;
@@ -119800,26 +119821,26 @@ function checkRowLevelExpression(e10, t, n) {
119800
119821
  default:
119801
119822
  e10.addError(t3, ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1, c4, n4);
119802
119823
  }
119803
- } else if (isAssignable$1(e10, n4, datetimeType$1) && isAssignable$1(e10, s4, durationType$1)) {
119824
+ } else if (isAssignable$1(e10, n4, datetimeType$1) && isAssignable$1(e10, s5, durationType$1)) {
119804
119825
  if (c4 === Kind.PLUS || c4 === Kind.MINUS) {
119805
119826
  let r5 = createFieldType$1("", n4, undefined);
119806
119827
  r5.hasWindowFunction = a3, e10.setNodeType(t3, r5);
119807
119828
  } else
119808
119829
  e10.addError(t3, ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1, c4, n4);
119809
119830
  } else if (isAssignable$1(e10, n4, durationType$1)) {
119810
- if (c4 === Kind.PLUS && isAssignable$1(e10, s4, datetimeType$1)) {
119811
- let n5 = createFieldType$1("", s4, undefined);
119831
+ if (c4 === Kind.PLUS && isAssignable$1(e10, s5, datetimeType$1)) {
119832
+ let n5 = createFieldType$1("", s5, undefined);
119812
119833
  n5.hasWindowFunction = a3, e10.setNodeType(t3, n5);
119813
119834
  } else
119814
119835
  e10.addError(t3, ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1, c4, n4);
119815
- } else if (isAssignable$1(e10, n4, numberType$1) && isAssignable$1(e10, s4, rangeType$1)) {
119836
+ } else if (isAssignable$1(e10, n4, numberType$1) && isAssignable$1(e10, s5, rangeType$1)) {
119816
119837
  if (c4 === Kind.IN_KEYWORD) {
119817
119838
  let n5 = createFieldType$1("", truefalseType$1, undefined);
119818
119839
  n5.hasWindowFunction = a3, e10.setNodeType(t3, n5);
119819
119840
  } else
119820
- e10.addError(t3, ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1, c4, s4);
119841
+ e10.addError(t3, ErrorCode.UNSUPPORTED_OPERATOR_0_FOR_TYPE_1, c4, s5);
119821
119842
  } else
119822
- c4 === Kind.OR_KEYWORD || c4 === Kind.AND_KEYWORD ? (isAssignable$1(e10, n4, truefalseType$1) || e10.addError(t3, ErrorCode.EXPECTED_0_FOUND_1, truefalseType$1, n4), isAssignable$1(e10, s4, truefalseType$1) || e10.addError(t3, ErrorCode.EXPECTED_0_FOUND_1, truefalseType$1, s4)) : e10.addError(t3.right, ErrorCode.EXPECTED_0_FOUND_1, n4, s4);
119843
+ c4 === Kind.OR_KEYWORD || c4 === Kind.AND_KEYWORD ? (isAssignable$1(e10, n4, truefalseType$1) || e10.addError(t3, ErrorCode.EXPECTED_0_FOUND_1, truefalseType$1, n4), isAssignable$1(e10, s5, truefalseType$1) || e10.addError(t3, ErrorCode.EXPECTED_0_FOUND_1, truefalseType$1, s5)) : e10.addError(t3.right, ErrorCode.EXPECTED_0_FOUND_1, n4, s5);
119823
119844
  } else
119824
119845
  isErrType(r4) || isErrType(i3) || e10.addError(t3, ErrorCode.EXPECTED_SCALAR_LIKE_TYPE_FOUND_0, r4);
119825
119846
  }
@@ -120991,10 +121012,13 @@ function _createTypeChecker(e10, t, n, r3, i = CheckFlags.Normal) {
120991
121012
  fieldName: u3
120992
121013
  };
120993
121014
  } else
120994
- f3 = c4.subClass === "param" ? createTableLiteralType$1(createRowType$1([{
120995
- ...c4,
120996
- subClass: undefined
120997
- }])) : {
121015
+ f3 = c4.subClass === "param" ? {
121016
+ ...createTableLiteralType$1(createRowType$1([{
121017
+ ...c4,
121018
+ subClass: undefined
121019
+ }])),
121020
+ param: `${i3}.${s4}`
121021
+ } : {
120998
121022
  ...c4,
120999
121023
  fieldName: u3
121000
121024
  };
@@ -121003,10 +121027,13 @@ function _createTypeChecker(e10, t, n, r3, i = CheckFlags.Normal) {
121003
121027
  n4 && isFieldType(n4) ? n4.origin && n4.subClass !== "param" ? (checkModelField(k3, n4.origin, t3), f3 = {
121004
121028
  ...n4,
121005
121029
  fieldName: u3
121006
- }) : f3 = n4.subClass === "param" ? createTableLiteralType$1(createRowType$1([{
121007
- ...n4,
121008
- subClass: undefined
121009
- }])) : {
121030
+ }) : f3 = n4.subClass === "param" ? {
121031
+ ...createTableLiteralType$1(createRowType$1([{
121032
+ ...n4,
121033
+ subClass: undefined
121034
+ }])),
121035
+ param: `${i3}.${s4}`
121036
+ } : {
121010
121037
  ...n4,
121011
121038
  fieldName: u3
121012
121039
  } : k3.addError(e11.right, ErrorCode.FIELD_0_NOT_FOUND_IN_MODEL_1, s4, i3);
@@ -121024,6 +121051,75 @@ function _createTypeChecker(e10, t, n, r3, i = CheckFlags.Normal) {
121024
121051
  }
121025
121052
  }
121026
121053
  var createTypeChecker = Rr3.wrapFuncWithinActiveSpan(_createTypeChecker);
121054
+ var NUMBER_AGGS = [
121055
+ "sum",
121056
+ "avg",
121057
+ "average",
121058
+ "median",
121059
+ "stdev",
121060
+ "stdevp",
121061
+ "var",
121062
+ "varp"
121063
+ ];
121064
+ var MINMAX_AGGS = ["min", "max"];
121065
+ var COUNT_AGGS = [
121066
+ "count",
121067
+ "count_distinct",
121068
+ "countd",
121069
+ "approx_count_distinct",
121070
+ "approx_countd"
121071
+ ];
121072
+ var FAN_INVARIANT_AGGS = /* @__PURE__ */ new Set([
121073
+ ...MINMAX_AGGS,
121074
+ "count_distinct",
121075
+ "countd",
121076
+ "approx_count_distinct",
121077
+ "approx_countd"
121078
+ ]);
121079
+ function isFanInvariantAgg(e10) {
121080
+ return FAN_INVARIANT_AGGS.has(e10);
121081
+ }
121082
+ var SHORTHAND_TABLE_AGGS = /* @__PURE__ */ new Set([
121083
+ ...NUMBER_AGGS,
121084
+ ...MINMAX_AGGS,
121085
+ ...COUNT_AGGS,
121086
+ "count_if"
121087
+ ]);
121088
+ var AGG_PASSTHROUGH_FUNCS = /* @__PURE__ */ new Set([
121089
+ "agg_text",
121090
+ "agg_number",
121091
+ "agg_truefalse",
121092
+ "agg_datetime",
121093
+ "agg_date"
121094
+ ]);
121095
+ function isAggPassthrough(e10) {
121096
+ return AGG_PASSTHROUGH_FUNCS.has(e10);
121097
+ }
121098
+ var SQL_PASSTHROUGH_FUNCS = /* @__PURE__ */ new Set([
121099
+ "sql_text",
121100
+ "sql_number",
121101
+ "sql_truefalse",
121102
+ "sql_datetime",
121103
+ "sql_date"
121104
+ ]);
121105
+ function invalidPassthroughFuncName(e10) {
121106
+ if (!/^[a-zA-Z0-9_.]+$/.test(e10))
121107
+ return `Invalid function name "${e10}". Function names must contain only alphanumeric characters, underscores, and dots`;
121108
+ }
121109
+ function isSqlPassthroughFunction(e10) {
121110
+ return SQL_PASSTHROUGH_FUNCS.has(e10) || AGG_PASSTHROUGH_FUNCS.has(e10);
121111
+ }
121112
+ function checkAdhocSqlPassthrough(e10) {
121113
+ let t = [];
121114
+ return e10.forEach((e11) => {
121115
+ visitAst((e12) => {
121116
+ if (!isFunctionCall(e12) || e12.name.length !== 1)
121117
+ return true;
121118
+ let [n] = e12.name;
121119
+ return isSqlPassthroughFunction(n.value) && t.push(createError(ErrorCode.SQL_PASSTHROUGH_FUNCTION_0_NOT_ALLOWED, n.loc, undefined, n.value)), true;
121120
+ }, e11);
121121
+ }), t;
121122
+ }
121027
121123
  function createFunctionType(e10, t, n, r3 = false, i = false) {
121028
121124
  return {
121029
121125
  typeClass: TypeClass.FUNCTION,
@@ -121883,61 +121979,6 @@ registerTypeRule([
121883
121979
  origin: undefined
121884
121980
  } : datetimeType;
121885
121981
  });
121886
- var NUMBER_AGGS = [
121887
- "sum",
121888
- "avg",
121889
- "average",
121890
- "median",
121891
- "stdev",
121892
- "stdevp",
121893
- "var",
121894
- "varp"
121895
- ];
121896
- var MINMAX_AGGS = ["min", "max"];
121897
- var COUNT_AGGS = [
121898
- "count",
121899
- "count_distinct",
121900
- "countd",
121901
- "approx_count_distinct",
121902
- "approx_countd"
121903
- ];
121904
- var FAN_INVARIANT_AGGS = /* @__PURE__ */ new Set([
121905
- ...MINMAX_AGGS,
121906
- "count_distinct",
121907
- "countd",
121908
- "approx_count_distinct",
121909
- "approx_countd"
121910
- ]);
121911
- function isFanInvariantAgg(e10) {
121912
- return FAN_INVARIANT_AGGS.has(e10);
121913
- }
121914
- var SHORTHAND_TABLE_AGGS = /* @__PURE__ */ new Set([
121915
- ...NUMBER_AGGS,
121916
- ...MINMAX_AGGS,
121917
- ...COUNT_AGGS,
121918
- "count_if"
121919
- ]);
121920
- var AGG_PASSTHROUGH_FUNCS = /* @__PURE__ */ new Set([
121921
- "agg_text",
121922
- "agg_number",
121923
- "agg_truefalse",
121924
- "agg_datetime",
121925
- "agg_date"
121926
- ]);
121927
- function isAggPassthrough(e10) {
121928
- return AGG_PASSTHROUGH_FUNCS.has(e10);
121929
- }
121930
- var SQL_PASSTHROUGH_FUNCS = /* @__PURE__ */ new Set([
121931
- "sql_text",
121932
- "sql_number",
121933
- "sql_truefalse",
121934
- "sql_datetime",
121935
- "sql_date"
121936
- ]);
121937
- function invalidPassthroughFuncName(e10) {
121938
- if (!/^[a-zA-Z0-9_.]+$/.test(e10))
121939
- return `Invalid function name "${e10}". Function names must contain only alphanumeric characters, underscores, and dots`;
121940
- }
121941
121982
  var CAST_TARGETS = {
121942
121983
  date: SqlType.DATE,
121943
121984
  datetime: SqlType.DATETIME,
@@ -122032,6 +122073,11 @@ function unsupportedOperator(e10, t, n, r3) {
122032
122073
  function checkOperatorCall(e10, t, n, r3, i = r3, a = []) {
122033
122074
  if (n.some(isErrType))
122034
122075
  return errType;
122076
+ for (let [i3, s4] of n.entries()) {
122077
+ let n3 = t === Kind.IN_KEYWORD && i3 === 1;
122078
+ if (isTableType(s4) && s4.param && !n3)
122079
+ return e10.dataset.diagnostics.push(createError(ErrorCode.OPERATOR_0_ON_PARAM_1, a[i3] ?? r3, undefined, t, s4.param)), errType;
122080
+ }
122035
122081
  if (n.length === 2) {
122036
122082
  if (isRowType(n[0]))
122037
122083
  return checkTableCondition(e10, t, n[0], n[1], r3);
@@ -123048,6 +123094,16 @@ function resolveBlock(e10, t, n) {
123048
123094
  n3.kind === Kind.FIELD_DECLARATION ? registerConstDecl(e10, n3, r3, i) : n3.kind === Kind.FUNCTION_DECLARATION ? registerFunctionDecl(e10, n3, r3, i) : r3.result = e10.resolveExpr(n3, i);
123049
123095
  return r3.ty = r3.result.ty, r3;
123050
123096
  }
123097
+ function makeBlock(e10, t, n, r3) {
123098
+ return {
123099
+ kind: "block",
123100
+ id: e10.newNodeId(t),
123101
+ ty: r3 ?? n.ty,
123102
+ result: n,
123103
+ declarations: /* @__PURE__ */ new Map,
123104
+ synthetic: true
123105
+ };
123106
+ }
123051
123107
  function registerConstDecl(e10, t, n, r3) {
123052
123108
  let { name: i } = flattenName(t.name), a = e10.iterationDepth, s3 = e10.resolveExpr(t.expr, r3);
123053
123109
  n.declarations.set(i, {
@@ -123665,12 +123721,12 @@ function involvesAggregation(e11) {
123665
123721
  return e11 ? e11.kind === "computed_ref" || isFieldType(e11.ty) && (e11.ty.subClass === "measure" || e11.ty.subClass === "aggregate") ? true : e11.kind === Kind.FUNCTION_CALL ? e11.args.some(involvesAggregation) : e11.kind === Kind.NAMED_EXPRESSION ? involvesAggregation(e11.expr) : e11.kind === "block" ? involvesAggregation(e11.result) : e11.kind === "lod" || e11.kind === "where" || e11.kind === Kind.INTERVAL ? involvesAggregation(e11.value) : false : false;
123666
123722
  }
123667
123723
  function expandMetric(e11, t, n, r3) {
123668
- let i = `metric:${t.name.name}`;
123669
- return recordDep(e11, i), e11.expanding.has(i) ? (reportCycle(e11, i, r3), makeRef(e11, t.name.name, [], t.ty, r3)) : (resolveMetricDecl(e11, t, n), withGuard(e11, i, () => makeRef(e11, t.name.name, [], t.ty, r3), () => {
123724
+ let i = `metric:${t.name.name}`, a = t.source ?? t.name.name;
123725
+ return recordDep(e11, i), e11.expanding.has(i) ? (reportCycle(e11, i, r3), makeRef(e11, a, [], t.ty, r3)) : (resolveMetricDecl(e11, t, n), withGuard(e11, i, () => makeRef(e11, a, [], t.ty, r3), () => {
123670
123726
  if (!t.resolved)
123671
- return makeRef(e11, t.name.name, [], t.ty, r3);
123727
+ return makeRef(e11, a, [], t.ty, r3);
123672
123728
  let n3 = t.resolved;
123673
- return nameInlined(e11, t.name.name, n3, r3, "measure", createModelFieldReference("", t.name.name));
123729
+ return nameInlined(e11, a, n3, r3, "measure", createModelFieldReference("", a));
123674
123730
  }));
123675
123731
  }
123676
123732
  var CUSTOM_AGG_FN = "custom_agg";
@@ -124623,7 +124679,7 @@ var ExtendFold = class extends HirFold {
124623
124679
  let t = this.planning.size === 0 ? e11 : {
124624
124680
  ...e11,
124625
124681
  children: e11.children.filter((e12) => e12.edge.join !== "semi" || !(e12.input.predicates ?? []).some((e13) => referencesPlanning(this, e13.predicate)))
124626
- }, n = materialiseExpandGrain(this, t);
124682
+ }, n = materialiseExpandGrain(this, t, [], "scan");
124627
124683
  return {
124628
124684
  ...t,
124629
124685
  ...foldExpandedPredicates(this, n)
@@ -124667,25 +124723,51 @@ function declOrigin(e11, t) {
124667
124723
  let n = e11.declarationsMap.get(t);
124668
124724
  return n?.kind === "dimension" ? n.origin : undefined;
124669
124725
  }
124670
- function materialiseExpandGrain(e11, t, n = []) {
124671
- let r3 = partGrainKeys(e11, t, n), i = (e12, t3 = []) => [
124726
+ function materialiseExpandGrain(e11, t, n = [], r3 = undefined) {
124727
+ let i = partGrainKeys(e11, t, n), a = (e12, t3 = []) => [
124672
124728
  ...e12,
124673
124729
  ...n,
124674
- ...r3,
124730
+ ...i,
124675
124731
  ...t3 ?? []
124676
- ], a = slotKeys(t), s3 = (t3, n3) => ({
124732
+ ], s3 = slotKeys(t), c3 = /* @__PURE__ */ new Set;
124733
+ walkTree(t, (e12) => c3.add(e12.model));
124734
+ let l = (n3, i3) => {
124735
+ let l3 = n3.extend ?? extendSlot(e11, n3.model, a(n3.fields ?? [], i3), s3.get(n3.model) ?? []), f = r3 ? joinPartKeyModels(e11, n3, l3, c3, r3 === "scan" && n3 === t) : n3;
124736
+ return u(f, l3, i3);
124737
+ }, u = (t3, n3, r4) => ({
124677
124738
  ...t3,
124678
- ...import_lodash2.omitBy({ extend: t3.extend ?? extendSlot(e11, t3.model, i(t3.fields ?? [], n3), a.get(t3.model) ?? []) }, import_lodash2.isUndefined),
124739
+ ...import_lodash2.omitBy({ extend: n3 }, import_lodash2.isUndefined),
124679
124740
  ...t3.fields ? { fields: foldComputedKeys(e11, t3.fields) } : {},
124680
124741
  children: t3.children.map((t4) => ownScope(t4) && !t4.expanded ? {
124681
124742
  ...t4,
124682
- input: materialiseExpandGrain(e11, t4.input, levelSubjects(t4.input))
124743
+ input: materialiseExpandGrain(e11, t4.input, levelSubjects(t4.input), "branch")
124683
124744
  } : {
124684
124745
  ...t4,
124685
- input: s3(t4.input, t4.expanded ? (t4.input.predicates ?? []).flatMap((e12) => e12.targets) : n3)
124746
+ input: l(t4.input, t4.expanded ? (t4.input.predicates ?? []).flatMap((e12) => e12.targets) : r4)
124686
124747
  })
124687
124748
  });
124688
- return s3(t);
124749
+ return l(t);
124750
+ }
124751
+ function keyModelPaths(e11, t, n, r3) {
124752
+ let i = import_lodash2.uniq(n.map((e12) => e12.modelName)).filter((e12) => !r3.has(e12));
124753
+ if (i.length === 0)
124754
+ return [];
124755
+ let a = resolveJoinTree(i, baseGraph(e11), t, true);
124756
+ return a ? a.paths.filter((e12) => e12[0].length > 0).map((n3) => pathToEdges(e11, t, n3[0])).filter((e12) => !r3.has(e12[e12.length - 1].destModel)) : [];
124757
+ }
124758
+ function joinPartKeyModels(e11, t, n, r3, i) {
124759
+ if (n === undefined || !i && !bareNode(extendDef(e11.resolver.dataset, n).base))
124760
+ return t;
124761
+ let a = partKeysOf(e11, t.fields ?? []).map((e12) => e12.origin), s3 = keyModelPaths(e11.resolver, t.model, a, r3);
124762
+ if (s3.length === 0)
124763
+ return t;
124764
+ let c3 = cloneTree(t);
124765
+ for (let e12 of s3) {
124766
+ for (let t3 of e12)
124767
+ r3.add(t3.destModel);
124768
+ placeChain(c3, e12);
124769
+ }
124770
+ return c3;
124689
124771
  }
124690
124772
  function levelSubjects(e11) {
124691
124773
  return (e11.predicates ?? []).flatMap((e12) => e12.targets);
@@ -124697,26 +124779,28 @@ function foldComputedKeys(e11, t) {
124697
124779
  } : t3);
124698
124780
  }
124699
124781
  function partGrainKeys(e11, t, n) {
124700
- let r3 = [], i = /* @__PURE__ */ new Set, a = [...n];
124701
- walkTree(t, (e12) => a.push(...e12.fields ?? []));
124702
- let s3 = /* @__PURE__ */ new Set, c3 = (t3) => {
124782
+ let r3 = [...n];
124783
+ return walkTree(t, (e12) => r3.push(...e12.fields ?? [])), partKeysOf(e11, r3);
124784
+ }
124785
+ function partKeysOf(e11, t) {
124786
+ let n = [], r3 = /* @__PURE__ */ new Set, i = /* @__PURE__ */ new Set, a = (t3) => {
124703
124787
  t3 && walkAll(t3, (t4) => {
124704
124788
  if (t4.kind === "computed_ref") {
124705
124789
  for (let e12 of t4.reduce.by) {
124706
124790
  let t5 = modelFieldRefAsKey(e12.origin);
124707
- i.has(t5) || (i.add(t5), r3.push(e12));
124791
+ r3.has(t5) || (r3.add(t5), n.push(e12));
124708
124792
  }
124709
124793
  return false;
124710
124794
  }
124711
124795
  if (t4.kind !== Kind.IDENTIFIER)
124712
124796
  return false;
124713
- let n3 = lookupDim(e11.resolver, t4);
124714
- return !n3 || s3.has(n3) || n3.body?.syntax !== "aql" || (s3.add(n3), c3(unwrapDimBody(n3))), false;
124797
+ let s3 = lookupDim(e11.resolver, t4);
124798
+ return !s3 || i.has(s3) || s3.body?.syntax !== "aql" || (i.add(s3), a(unwrapDimBody(s3))), false;
124715
124799
  });
124716
124800
  };
124717
- for (let t3 of a)
124718
- c3(dimBodyOf(e11, t3));
124719
- return r3;
124801
+ for (let n3 of t)
124802
+ a(dimBodyOf(e11, n3));
124803
+ return n;
124720
124804
  }
124721
124805
  function dimBodyOf(e11, t) {
124722
124806
  let n = lookupDim(e11.resolver, unwrapNamed(t.expr));
@@ -124823,13 +124907,20 @@ function defineSlot(e11, t, n, r3) {
124823
124907
  };
124824
124908
  }
124825
124909
  let f = n.filter((e12) => !l.has(e12)), p3 = {
124826
- base: f.some((e12) => e12.dependentModels.length > 0) ? dependentBase(e11, t, f, u) : u,
124910
+ base: f.some((e12) => e12.dependentModels.length > 0) ? dependentBase(e11, t, f.map(withPartKeyModels), u) : u,
124827
124911
  computed: f.map((t3) => foldPartScans(e11, t3)),
124828
124912
  projected: f.filter((e12) => !e12.unrollOnly).map((e12) => e12.computed.origin),
124829
124913
  parts: []
124830
124914
  };
124831
124915
  return a && (p3.sealed = true), defineExtend(i.dataset, p3);
124832
124916
  }
124917
+ function withPartKeyModels(e11) {
124918
+ let t = e11.partKeyModels.filter((t3) => !e11.dependentModels.includes(t3));
124919
+ return t.length === 0 ? e11 : {
124920
+ ...e11,
124921
+ dependentModels: [...e11.dependentModels, ...t]
124922
+ };
124923
+ }
124833
124924
  function planKeys(e11, t, n, r3) {
124834
124925
  let { resolver: i } = e11, a = [], s3 = (e12) => r3.some((t3) => t3.field.name.name === e12) || a.some((t3) => t3.field.name.name === e12);
124835
124926
  for (let r4 of n) {
@@ -124937,7 +125028,8 @@ function planDim(e11, t, n) {
124937
125028
  let u = t.modelPaths ? [...t.modelPaths.keys()] : dependentModels(r3, i, n), f = u.filter((e12) => c3.has(e12)), p3 = hasComputedRef$1(a) ? f.length === 0 ? [] : u.filter((e12) => c3.has(e12) || l.has(e12)) : u, m3 = {
124938
125029
  field: t,
124939
125030
  computed: computedColumn(r3, t),
124940
- dependentModels: p3
125031
+ dependentModels: p3,
125032
+ partKeyModels: u.filter((e12) => l.has(e12))
124941
125033
  };
124942
125034
  if (s3.length === 0)
124943
125035
  return [m3];
@@ -125690,7 +125782,10 @@ function resolveModuleRef(e11, t, n, r3, i, a, s3 = i) {
125690
125782
  function resolveFieldRef(e11, t, n, r3, i, a, s3) {
125691
125783
  if ((n.kind === "dimension" || n.kind === "measure") && resolveModelField(e11, t, n), n.kind === "measure")
125692
125784
  return expandMeasure(e11, t, n, a);
125693
- let c3 = createFieldType(genUniqueFieldName({ name: n.name.name }, t.name), n.ty.type, n.ty.origin, t.name, n.kind), l = n.kind === "param" ? createTableLiteralType(createRowType([createFieldType(n.name.name, n.ty.type, n.ty.origin, t.name, "param")])) : c3;
125785
+ let c3 = createFieldType(genUniqueFieldName({ name: n.name.name }, t.name), n.ty.type, n.ty.origin, t.name, n.kind), l = n.kind === "param" ? {
125786
+ ...createTableLiteralType(createRowType([createFieldType(n.name.name, n.ty.type, n.ty.origin, t.name, "param")])),
125787
+ param: `${t.name}.${n.name.name}`
125788
+ } : c3;
125694
125789
  if (n.kind === "param" && n.ty.origin) {
125695
125790
  let t3 = getParamBinding(e11, n.ty.origin);
125696
125791
  if (t3)
@@ -126309,12 +126404,20 @@ function resolveShorthandAggregate(e11, t, n, r3, i, a, s3) {
126309
126404
  let c3 = n[0], l = e11.resolveExpr(c3, r3);
126310
126405
  if (isTableType(l.ty))
126311
126406
  return buildReduce(e11, t, n, r3, i, a, s3);
126407
+ if (isUserConstReference(c3, r3))
126408
+ return e11.dataset.diagnostics.push(createError(ErrorCode.INVALID_SHORTHAND_AGGREGATION_0, i, undefined, shorthandAggregationOverConstMessage(t, c3.value))), finishAggregate(e11, t, [l], errType, i, a);
126312
126409
  if (isFieldType(l.ty)) {
126313
126410
  let u = AggregationArgFold.inspect(l), f = !u.producesRelation && !u.hasQueryLevelExpression ? clearOrigin(e11, [...u.models]) : undefined;
126314
126411
  return f ? buildReduce(e11, t, [isDotExpression(c3) ? c3.left : createIdentifier(f, borrow(c3.loc), true), c3], r3, i, a, s3) : buildReduce(e11, t, n, r3, i, a, s3);
126315
126412
  }
126316
126413
  return e11.dataset.diagnostics.push(createError(ErrorCode.INVALID_SHORTHAND_AGGREGATION_0, i, undefined, shorthandAggregationWithMultipleModelsMessage(t, c3))), buildReduce(e11, t, n, r3, i, a, s3);
126317
126414
  }
126415
+ function isUserConstReference(e11, t) {
126416
+ if (!isIdentifier(e11))
126417
+ return false;
126418
+ let n = blockConstExpr(e11.value, t);
126419
+ return n?.kind === "const" && !n.synthetic;
126420
+ }
126318
126421
  function buildReduce(e11, t, n, r3, i, a, s3) {
126319
126422
  let c3 = resolveAggregateArgs(e11, n, r3), l = c3.length === 1;
126320
126423
  if (l && isTableType(c3[0].ty) && c3[0].ty.rowType.columns.length !== 1)
@@ -127549,6 +127652,18 @@ function applyFilter(e11, t, n, r3, i) {
127549
127652
  let w3 = m3.find((t3) => t3.edge.keys.some((e12) => p3.keys.some((t4) => originEq$1(e12.from, t4.from))) && treeFitsCondition(e11, a, s3, t3, r3)), E3 = w3 ? conjoinScalarExistenceFilter(e11, a, s3, w3, r3) : buildScalarExistenceFilter(e11, a, s3, [r3], [p3.destModel]);
127550
127653
  return E3 ? (i.add(r3), withMembershipHops(S3(w3, E3))) : n;
127551
127654
  }
127655
+ function narrowToReachableRows(e11, t, n, r3) {
127656
+ let i = (r4) => r4 === n || r4 === "H.calendar" || joinPathTo(e11, t, n, r4) !== undefined;
127657
+ return r3.flatMap((t3) => {
127658
+ if (t3.predicate.kind !== "in_table_condition" || t3.role === "rlp")
127659
+ return [t3];
127660
+ let n3 = t3.predicate.row.filter((e12) => exprModels(e12.expr).every(i));
127661
+ return n3.length === t3.predicate.row.length ? [t3] : n3.length === 0 ? [] : [makeCondition(e11, {
127662
+ ...t3.predicate,
127663
+ row: n3
127664
+ }, t3.role)];
127665
+ });
127666
+ }
127552
127667
  function joinPathTo(e11, t, n, r3) {
127553
127668
  let i = t.findPath(n, r3, true);
127554
127669
  if (i && i[0].length !== 0)
@@ -128427,7 +128542,7 @@ var Window = class {
128427
128542
  return u.foldFunctionCall(e12);
128428
128543
  if (f.kind === "unsupported")
128429
128544
  return u.resolver.dataset.diagnostics.push(createError(ErrorCode.INVALID_EXPRESSION_0, l, undefined, `Window aggregation does not support \`${f.type}\` yet.`)), u.foldFunctionCall(e12);
128430
- let p3 = dedupKeys([...u.aggGrain(), ...modelFieldExprs(u.resolver, [...s3, ...a])]), m3 = f.kind === "simple" ? [{
128545
+ let p3 = [...s3, ...a].filter((e13) => !this.hasAggregate(e13)), m3 = dedupKeys([...u.aggGrain(), ...modelFieldExprs(u.resolver, p3)]), S3 = f.kind === "simple" ? [{
128431
128546
  windowFn: t,
128432
128547
  rollupFn: f.func,
128433
128548
  arg: n
@@ -128435,8 +128550,8 @@ var Window = class {
128435
128550
  windowFn: e13.aggFn,
128436
128551
  rollupFn: e13.rollupFn,
128437
128552
  arg: e13.arg
128438
- })), S3 = this.buildWindowLodParts(e12, m3, p3, r3, i, s3, a, c3, l);
128439
- return f.kind === "simple" ? S3[0] : f.combine(S3, e12.ty, l);
128553
+ })), w3 = this.buildWindowLodParts(e12, S3, m3, r3, i, s3, a, c3, l);
128554
+ return f.kind === "simple" ? w3[0] : f.combine(w3, e12.ty, l);
128440
128555
  }
128441
128556
  aggregateValue(e12, t, n, r3) {
128442
128557
  let i = createFieldType(n, e12 === "count" ? numberType : scalarOf(t.ty), undefined, exprModels(t)[0], "measure");
@@ -128445,24 +128560,47 @@ var Window = class {
128445
128560
  buildWindowLodParts(e12, t, n, r3, i, a, s3, c3, l) {
128446
128561
  let { g: u } = this, f = e12.ty, p3 = windowLodPartitionKeys(u, s3, a), m3 = u.currentIteration(), S3 = m3.groups;
128447
128562
  m3.groups = n;
128448
- let w3 = [], E3 = /* @__PURE__ */ new Map, k3 = u.inScope("window-lod", e12, () => t.map((e13) => {
128449
- let t3 = !this.hasAggregate(e13.arg), n3 = t3 ? `${e13.windowFn}(${argSig(e13.arg)})` : argSig(e13.arg), r4 = E3.get(n3);
128450
- if (r4)
128451
- return r4;
128452
- let i3 = `__wv_${this.windowLodCounter++}`, a3 = cloneResolved(u.resolver, e13.arg), s4 = u.foldExpr(t3 ? this.aggregateValue(e13.windowFn, a3, i3, l) : a3), c4 = createFieldType(i3, scalarOf(s4.ty));
128563
+ let w3 = [], E3 = /* @__PURE__ */ new Map, k3 = (e13, t3) => {
128564
+ let n3 = E3.get(e13);
128565
+ if (n3)
128566
+ return n3;
128567
+ let r4 = `__wv_${this.windowLodCounter++}`, i3 = u.foldExpr(t3(r4)), a3 = createFieldType(r4, scalarOf(i3.ty));
128453
128568
  w3.push({
128454
- name: i3,
128455
- ty: c4,
128456
- expr: s4
128569
+ name: r4,
128570
+ ty: a3,
128571
+ expr: i3
128457
128572
  });
128458
- let f3 = {
128459
- name: i3,
128460
- ty: c4
128573
+ let s4 = {
128574
+ name: r4,
128575
+ ty: a3
128461
128576
  };
128462
- return E3.set(n3, f3), f3;
128577
+ return E3.set(e13, s4), s4;
128578
+ }, { partValues: N3, orderCols: I3 } = u.inScope("window-lod", e12, () => ({
128579
+ partValues: t.map((e13) => {
128580
+ let t3 = !this.hasAggregate(e13.arg), n3 = t3 ? `${e13.windowFn}(${argSig(e13.arg)})` : argSig(e13.arg);
128581
+ return k3(n3, (n4) => {
128582
+ let r4 = cloneResolved(u.resolver, e13.arg);
128583
+ return t3 ? this.aggregateValue(e13.windowFn, r4, n4, l) : r4;
128584
+ });
128585
+ }),
128586
+ orderCols: a.map((e13) => this.hasAggregate(e13) ? k3(argSig(e13), () => cloneResolved(u.resolver, e13)) : undefined)
128463
128587
  }));
128464
128588
  m3.groups = S3;
128465
- let N3 = {
128589
+ let L3 = i.map((e13, t3) => {
128590
+ let n3 = I3[t3];
128591
+ if (!n3)
128592
+ return cloneResolved(u.resolver, e13);
128593
+ let r4 = makeIdent(u.resolver, n3.name, [], n3.ty, l), { expr: i3 } = e13, a3 = i3.kind === Kind.FUNCTION_CALL && unwrapOrderKey(i3) !== i3 ? {
128594
+ ...i3,
128595
+ id: u.resolver.newNodeId(l),
128596
+ args: [r4, ...i3.args.slice(1).map((e14) => cloneResolved(u.resolver, e14))]
128597
+ } : r4;
128598
+ return {
128599
+ ...e13,
128600
+ id: u.resolver.newNodeId(l),
128601
+ expr: a3
128602
+ };
128603
+ }), U3 = {
128466
128604
  kind: "merge",
128467
128605
  id: u.resolver.newNodeId(l),
128468
128606
  ty: createTableType(createRowType([...n.map((e13) => e13.ty), ...w3.map((e13) => e13.ty)]), undefined),
@@ -128475,37 +128613,37 @@ var Window = class {
128475
128613
  name: makeIdent(u.resolver, e13.name, [], e13.ty, l),
128476
128614
  expr: e13.expr
128477
128615
  }))]
128478
- }, I3 = t.map((e13, t3) => {
128479
- let n3 = `__w_${this.windowLodCounter++}`, a3 = createFieldType(n3, scalarOf(f)), s4 = k3[t3];
128616
+ }, pe3 = t.map((e13, t3) => {
128617
+ let n3 = `__w_${this.windowLodCounter++}`, i3 = createFieldType(n3, scalarOf(f)), a3 = N3[t3];
128480
128618
  return {
128481
128619
  name: n3,
128482
- ty: a3,
128620
+ ty: i3,
128483
128621
  value: {
128484
128622
  kind: Kind.FUNCTION_CALL,
128485
128623
  id: u.resolver.newNodeId(l),
128486
- ty: a3,
128487
- name: makeIdent(u.resolver, `window_${e13.windowFn}`, [], a3, l),
128624
+ ty: i3,
128625
+ name: makeIdent(u.resolver, `window_${e13.windowFn}`, [], i3, l),
128488
128626
  args: [
128489
- makeIdent(u.resolver, s4.name, [], s4.ty, l),
128627
+ makeIdent(u.resolver, a3.name, [], a3.ty, l),
128490
128628
  ...r3 ? [cloneResolved(u.resolver, r3)] : [],
128491
- ...i.map((e14) => cloneResolved(u.resolver, e14)),
128629
+ ...L3,
128492
128630
  ...p3.map((e14) => namedPartitionArg(u, e14, l)),
128493
128631
  ...c3 ? [cloneResolved(u.resolver, c3)] : []
128494
128632
  ]
128495
128633
  }
128496
128634
  };
128497
- }), L3 = {
128635
+ }), me3 = {
128498
128636
  kind: "window",
128499
128637
  id: u.resolver.newNodeId(l),
128500
- ty: N3.ty,
128501
- input: N3,
128502
- columns: I3.map((e13) => ({
128638
+ ty: U3.ty,
128639
+ input: U3,
128640
+ columns: pe3.map((e13) => ({
128503
128641
  name: e13.name,
128504
128642
  value: e13.value
128505
128643
  }))
128506
128644
  };
128507
128645
  return t.map((e13, t3) => {
128508
- let { name: n3, ty: r4 } = I3[t3], i3 = u.grain().map((e14) => cloneResolved(u.resolver, e14)), a3 = createFieldType(n3, scalarOf(f), undefined, undefined, "aggregate"), s4 = {
128646
+ let { name: n3, ty: r4 } = pe3[t3], i3 = u.grain().map((e14) => cloneResolved(u.resolver, e14)), a3 = createFieldType(n3, scalarOf(f), undefined, undefined, "aggregate"), s4 = {
128509
128647
  kind: Kind.FUNCTION_CALL,
128510
128648
  id: u.resolver.newNodeId(l),
128511
128649
  ty: a3,
@@ -128515,7 +128653,7 @@ var Window = class {
128515
128653
  kind: "reduce",
128516
128654
  id: u.resolver.newNodeId(l),
128517
128655
  ty: createTableType(createRowType([...i3.map((e14) => e14.ty), a3]), undefined),
128518
- input: L3,
128656
+ input: me3,
128519
128657
  by: i3,
128520
128658
  aggregates: [s4]
128521
128659
  }, p4 = isFieldType(f) ? createFieldType(n3, f.type, undefined, undefined, "aggregate") : a3;
@@ -129248,20 +129386,56 @@ function nonCyclicRlp(e13, t) {
129248
129386
  return t.filter((t3) => t3.role !== "rlp" || !filterRefKeys(t3.predicate).some((t4) => e13.foldingDimKeys.has(t4) || n.some((n3) => dependsOn(e13.resolver.dataset, t4, n3))));
129249
129387
  }
129250
129388
  function foldWhere(e13, t) {
129251
- let n = e13.contextFilters, r3 = mergeGrainsOf(t.value), i = e13.currentIteration(), a = t.conditions.map((t3) => {
129252
- let n3 = i.inheritedGroups, a3 = inheritsViz(t3) && r3.length === 0;
129253
- inheritsViz(t3) && r3.length > 0 && (i.inheritedGroups = r3);
129254
- let s4 = () => e13.withoutIteration(t3, () => makeCondition(e13.resolver, unwrapBlock$1(e13.foldExpr(t3)), "where"));
129255
- a3 && (i.inheritedGroups = e13.period.rawKeys(e13.grain()));
129256
- let c3 = a3 ? e13.period.withoutShift(t3, s4) : s4();
129257
- return i.inheritedGroups = n3, c3;
129389
+ let n = t.conditions.filter(isShorthand);
129390
+ if (n.length > 0 && hasUnion(t.value))
129391
+ return foldWhere(e13, {
129392
+ ...t,
129393
+ value: distributeOverBranches(e13, t.value, n),
129394
+ conditions: t.conditions.filter((e14) => !isShorthand(e14))
129395
+ });
129396
+ let r3 = e13.contextFilters, i = mergeGrainsOf(t.value), a = i.length > 0 ? [] : relationKeysOf(t.value), s3 = a.length > 0 ? dedupKeys([...e13.grain(), ...a]) : i, c3 = e13.currentIteration(), l = t.conditions.map((t3) => {
129397
+ let n3 = c3.inheritedGroups, r4 = inheritsViz(t3) && s3.length === 0;
129398
+ inheritsViz(t3) && s3.length > 0 && (c3.inheritedGroups = s3);
129399
+ let i3 = () => e13.withoutIteration(t3, () => makeCondition(e13.resolver, unwrapBlock$1(e13.foldExpr(t3)), "where"));
129400
+ r4 && (c3.inheritedGroups = e13.period.rawKeys(e13.grain()));
129401
+ let a3 = r4 ? e13.period.withoutShift(t3, i3) : i3();
129402
+ return c3.inheritedGroups = n3, a3;
129258
129403
  });
129259
- e13.contextFilters = [...n, ...a];
129260
- let s3 = e13.inScope("where-value", t, () => e13.foldExpr(t.value));
129261
- return e13.contextFilters = n, s3;
129404
+ e13.contextFilters = [...r3, ...l];
129405
+ let u = e13.inScope("where-value", t, () => e13.foldExpr(t.value));
129406
+ return e13.contextFilters = r3, u;
129262
129407
  }
129263
129408
  function mergeGrainsOf(e13) {
129264
- return e13.kind === "merge" ? e13.grains : e13.kind === "pivot_context" ? mergeGrainsOf(e13.value) : e13.kind === "union" && e13.branches.length > 0 ? mergeGrainsOf(e13.branches[0]) : [];
129409
+ return e13.kind === "merge" ? e13.grains : e13.kind === "pivot_context" ? mergeGrainsOf(e13.value) : e13.kind === "union" ? dedupKeys(e13.branches.flatMap(mergeGrainsOf)) : [];
129410
+ }
129411
+ function relationKeysOf(e13) {
129412
+ return e13.kind === "unique" || e13.kind === "group" ? e13.keys : [];
129413
+ }
129414
+ function isShorthand(e13) {
129415
+ return e13.kind === "block" && inheritsViz(e13);
129416
+ }
129417
+ function hasUnion(e13) {
129418
+ return e13.kind === "union" || e13.kind === "pivot_context" && hasUnion(e13.value);
129419
+ }
129420
+ function distributeOverBranches(e13, t, n) {
129421
+ if (t.kind === "pivot_context")
129422
+ return {
129423
+ ...t,
129424
+ value: distributeOverBranches(e13, t.value, n)
129425
+ };
129426
+ if (t.kind !== "union")
129427
+ return t;
129428
+ let r3 = e13.resolver.dataset.spans.get(t.id) ?? locStub();
129429
+ return {
129430
+ ...t,
129431
+ branches: t.branches.map((t3) => ({
129432
+ kind: "where",
129433
+ id: e13.resolver.newNodeId(r3),
129434
+ ty: t3.ty,
129435
+ value: t3,
129436
+ conditions: n.map((t4) => cloneResolved(e13.resolver, t4))
129437
+ }))
129438
+ };
129265
129439
  }
129266
129440
  function unwrapBlock$1(e13) {
129267
129441
  return e13.kind === "block" ? unwrapBlock$1(e13.result) : e13;
@@ -129279,7 +129453,7 @@ function grainTableCondition(e13, t) {
129279
129453
  table: foldTableAtGrain(e13, t.table, e13.grain())
129280
129454
  };
129281
129455
  }
129282
- let n = (t.inheritGrains ? e13.grain() : []).map((t3) => cloneResolved(e13.resolver, t3)), r3 = t.row.map((t3) => cloneResolved(e13.resolver, t3)), i = dedupKeys([...n, ...r3]), a = foldTableAtGrain(e13, t.table, i), s3 = a.kind === "filter" ? {
129456
+ let n = (t.inheritGrains ? e13.grain() : []).map((t3) => cloneResolved(e13.resolver, t3)), r3 = t.row.map((t3) => cloneResolved(e13.resolver, t3)), i = dedupKeys([...n, ...r3]), a = foldTableAtGrain(e13, withOwnGrain(t.table, i), []), s3 = a.kind === "filter" ? {
129283
129457
  ...a,
129284
129458
  ty: a.input.ty
129285
129459
  } : a;
@@ -129289,6 +129463,25 @@ function grainTableCondition(e13, t) {
129289
129463
  table: s3
129290
129464
  };
129291
129465
  }
129466
+ function withOwnGrain(e13, t) {
129467
+ if (e13.kind === "where")
129468
+ return {
129469
+ ...e13,
129470
+ value: withOwnGrain(e13.value, t)
129471
+ };
129472
+ if (e13.kind !== "filter" || e13.input.kind !== "unique")
129473
+ return e13;
129474
+ let n = createTableType(createRowType(t.map((e14) => e14.ty)), undefined);
129475
+ return {
129476
+ ...e13,
129477
+ ty: n,
129478
+ input: {
129479
+ ...e13.input,
129480
+ ty: n,
129481
+ keys: t
129482
+ }
129483
+ };
129484
+ }
129292
129485
  function foldTableAtGrain(e13, t, n) {
129293
129486
  return e13.inIteration({
129294
129487
  groups: undefined,
@@ -129551,7 +129744,7 @@ var GrainFold = class e13 extends HirFold {
129551
129744
  }
129552
129745
  }
129553
129746
  stateFingerprint() {
129554
- let { pkGrain: e14, inheritedGroups: t, groups: n, dim: r3, noSlice: i, source: a } = this.currentIteration();
129747
+ let { pkGrain: e14, inheritedGroups: t, groups: n, dim: r3, droppable: i, source: a } = this.currentIteration();
129555
129748
  return [
129556
129749
  `pk=${keyFingerprint(e14)}`,
129557
129750
  `inherited=${keyFingerprint(t)}`,
@@ -129699,29 +129892,40 @@ var GrainFold = class e13 extends HirFold {
129699
129892
  }
129700
129893
  }
129701
129894
  foldTransform(e14) {
129702
- let t = this.currentIteration(), n = groupingOf(e14.input), r3 = (t3, r4, i3) => this.inIteration({
129703
- groups: n,
129704
- source: {
129705
- relation: t3,
129706
- depth: r4
129707
- }
129708
- }, () => this.inScope("agg-output", e14, i3)), i;
129895
+ let t = this.currentIteration(), n = groupingOf(e14.input), r3 = (r4, i3, a) => {
129896
+ let s3 = new Set(unslicedOf(r4).keys.map((e15) => modelFieldRefAsKey(e15.origin)));
129897
+ return this.inIteration({
129898
+ groups: n,
129899
+ source: {
129900
+ relation: r4,
129901
+ depth: i3
129902
+ },
129903
+ inheritedGroups: t.inheritedGroups.filter((e15) => !s3.has(modelFieldRefAsKey(e15.origin)))
129904
+ }, () => this.inScope("agg-output", e14, a));
129905
+ }, i;
129709
129906
  switch (e14.kind) {
129710
129907
  case "group": {
129711
- let r4 = dedupKeys([...this.grain(), ...n ?? []]), a = this.foldKeys(this.period.applyToKeys(e14.keys)), s3 = dedupKeys([...a, ...r4]), c3 = this.inIteration({
129908
+ let r4 = dedupKeys([...this.grain(), ...n ?? []]), a = this.foldKeys(this.period.applyToKeys(e14.keys)), s3 = dedupKeys([...a, ...r4]), c3 = dedupKeys([...s3, ...t.inheritedGroups]), l = this.inIteration({
129712
129909
  groups: n,
129713
- inheritedGroups: dedupKeys([...s3, ...t.inheritedGroups])
129714
- }, () => this.inScope("group-input", e14, () => this.foldExpr(e14.input))), l = scanRootOf(c3);
129715
- l && this.diagnoseGroupFanout(this.graphContext.currentGraph(), l, a);
129716
- let u = this.resolver.dataset.spans.get(e14.id) ?? locStub();
129910
+ inheritedGroups: c3,
129911
+ droppable: c3.filter((e15) => !a.includes(e15))
129912
+ }, () => this.inScope("group-input", e14, () => this.foldExpr(e14.input))), u = scanRootOf(l);
129913
+ u && this.diagnoseGroupFanout(this.graphContext.currentGraph(), u, a);
129914
+ let f = unslicedOf(l), p3 = s3.filter((e15) => !f.keys.includes(e15)), m3 = f.scanRoot ? this.noSliceComments(f.scanRoot, f.keys) : [], S3 = this.resolver.dataset.spans.get(e14.id) ?? locStub();
129717
129915
  i = {
129718
129916
  kind: "reduce",
129719
- id: this.resolver.newNodeId(u),
129720
- ty: createTableType(createRowType(s3.map((e15) => e15.ty)), e14.ty.rootModel),
129721
- input: c3,
129722
- by: s3,
129917
+ id: this.resolver.newNodeId(S3),
129918
+ ty: createTableType(createRowType(p3.map((e15) => e15.ty)), e14.ty.rootModel),
129919
+ input: l,
129920
+ by: p3,
129723
129921
  aggregates: []
129724
- };
129922
+ }, f.keys.length > 0 && (i = {
129923
+ ...i,
129924
+ unsliced: f.keys
129925
+ }), m3.length > 0 && (i = {
129926
+ ...i,
129927
+ annotations: m3
129928
+ });
129725
129929
  break;
129726
129930
  }
129727
129931
  case "select": {
@@ -129781,26 +129985,28 @@ var GrainFold = class e13 extends HirFold {
129781
129985
  break;
129782
129986
  }
129783
129987
  case "reduce": {
129784
- let t3 = this.timeSpine.graphWithCalendar(this.graphContext.currentGraph()), r4 = this.inScope("reduce-keys", e14, () => this.foldKeys(this.period.applyToKeys(e14.by, e14))), a = dedupKeys([...r4, ...this.grain()]), s3 = [], c3 = this.inIteration({
129988
+ let t3 = this.timeSpine.graphWithCalendar(this.graphContext.currentGraph()), r4 = this.inScope("reduce-keys", e14, () => this.foldKeys(this.period.applyToKeys(e14.by, e14))), a = dedupKeys([...r4, ...this.grain()]), s3 = this.inIteration({
129785
129989
  groups: n,
129786
129990
  inheritedGroups: a,
129787
- noSlice: s3
129788
- }, () => this.inScope("reduce-input", e14, () => this.foldExpr(e14.input))), l = s3.filter((e15) => r4.includes(e15.key)), u = l.length > 0 ? r4.filter((e15) => !l.some((t4) => t4.key === e15)) : r4, f = bareLeafKeys(this.resolver, u, materialisedOrigins(c3)), p3 = this.inIteration({
129991
+ droppable: a
129992
+ }, () => this.inScope("reduce-input", e14, () => this.foldExpr(e14.input))), c3 = unslicedOf(s3), l = c3.keys.filter((e15) => r4.includes(e15)), u = l.length > 0 ? r4.filter((e15) => !l.includes(e15)) : r4, f = bareLeafKeys(this.resolver, u, materialisedOrigins(s3)), p3 = this.inIteration({
129789
129993
  groups: n,
129790
129994
  inheritedGroups: a,
129791
129995
  source: {
129792
- relation: c3,
129793
- depth: iterationDepthOf(c3)
129996
+ relation: s3,
129997
+ depth: iterationDepthOf(s3)
129794
129998
  }
129795
- }, () => this.inScope("agg-output", e14, () => this.foldExprs(e14.aggregates))), m3 = this.collectAccessedFields(p3, iterationDepthOf(c3));
129796
- c3 = addJoins(this.resolver, t3, c3, m3, e14.allowFanout ? "fanInvariant" : "rowLevel"), i = {
129999
+ }, () => this.inScope("agg-output", e14, () => this.foldExprs(e14.aggregates))), m3 = this.collectAccessedFields(p3, iterationDepthOf(s3));
130000
+ s3 = addJoins(this.resolver, t3, s3, m3, e14.allowFanout ? "fanInvariant" : "rowLevel"), i = {
129797
130001
  ...e14,
129798
130002
  by: f,
129799
- input: c3,
130003
+ input: s3,
129800
130004
  aggregates: p3
129801
- }, l.length > 0 && (i = {
130005
+ };
130006
+ let S3 = c3.scanRoot ? this.noSliceComments(c3.scanRoot, l) : [];
130007
+ S3.length > 0 && (i = {
129802
130008
  ...i,
129803
- annotations: [...e14.annotations ?? [], ...l.map((e15) => e15.comment)]
130009
+ annotations: [...e14.annotations ?? [], ...S3]
129804
130010
  });
129805
130011
  break;
129806
130012
  }
@@ -129979,26 +130185,18 @@ var GrainFold = class e13 extends HirFold {
129979
130185
  return;
129980
130186
  let n = this.graphContext.currentGraph(), r3 = t.model, i = this.timeSpine.graphWithCalendar(n), a = this.period.withExactRange(e14, () => effectiveFiltersForModel(this, r3));
129981
130187
  a = [...a, ...this.period.dayRoundtripFilters(this.period.rawKeys(this.currentIteration().inheritedGroups).map((e15) => e15.expr))], a = this.applicableRlp(r3, a), a = this.applicableFilters(i, r3, a);
129982
- let s3 = normalizeConditions(this.resolver, a, i, r3), c3 = /* @__PURE__ */ new Set, { noSlice: l } = this.currentIteration(), u = this.currentIteration().inheritedGroups;
129983
- if (l) {
129984
- let e15 = (e16) => e16 === "H.calendar" || n.canReach(r3, e16, {
129985
- allowFanout: true,
129986
- respectFilterDirection: true
129987
- }), t3 = u.filter((t4) => !exprModels(t4.expr).every(e15));
129988
- if (t3.length > 0) {
129989
- u = u.filter((e17) => !t3.includes(e17));
129990
- let e16 = this.noSliceComments(r3, t3);
129991
- l.push(...t3.map((t4, n3) => ({
129992
- key: t4,
129993
- comment: e16[n3]
129994
- })));
129995
- }
129996
- }
129997
- t = addJoins(this.resolver, i, t, u, "grainKey", s3, c3), t.kind === "expand_model" && (t = applyFilters(this.resolver, i, t, s3, c3));
129998
- let f = s3.filter((e15) => !c3.has(e15)), p3 = this.resolver.dataset.spans.get(e14.id) ?? locStub(), m3 = f.flatMap((e15) => e15.predicate.kind === "in_table_condition" ? e15.predicate.row.map((e16) => e16.expr) : []);
129999
- m3.length > 0 && (t = addJoins(this.resolver, n, t, leafFields(this.resolver, m3))), f.length > 0 && (t = this.filterInput(t, p3, f));
130000
- let S3 = this.period.strictBounds(bareLeafKeys(this.resolver, u, materialisedOrigins(t)).map((e15) => e15.expr), p3);
130001
- return S3.length > 0 ? this.filterInput(t, p3, S3) : t;
130188
+ let s3 = narrowToReachableRows(this.resolver, i, r3, normalizeConditions(this.resolver, a, i, r3)), c3 = /* @__PURE__ */ new Set, { droppable: l = [] } = this.currentIteration(), u = (e15) => e15 === "H.calendar" || n.canReach(r3, e15, {
130189
+ allowFanout: true,
130190
+ respectFilterDirection: true
130191
+ }), f = l.filter((e15) => !exprModels(e15.expr).every(u)), p3 = this.currentIteration().inheritedGroups.filter((e15) => !f.includes(e15));
130192
+ t = addJoins(this.resolver, i, t, p3, "grainKey", s3, c3), t.kind === "expand_model" && (t = applyFilters(this.resolver, i, t, s3, c3));
130193
+ let m3 = s3.filter((e15) => !c3.has(e15)), S3 = this.resolver.dataset.spans.get(e14.id) ?? locStub(), w3 = m3.flatMap((e15) => e15.predicate.kind === "in_table_condition" ? e15.predicate.row.map((e16) => e16.expr) : []);
130194
+ w3.length > 0 && (t = addJoins(this.resolver, n, t, leafFields(this.resolver, w3))), f.length > 0 && t.kind === "expand_model" && (t = {
130195
+ ...t,
130196
+ unsliced: f
130197
+ }), m3.length > 0 && (t = this.filterInput(t, S3, m3));
130198
+ let E3 = this.period.strictBounds(bareLeafKeys(this.resolver, p3, materialisedOrigins(t)).map((e15) => e15.expr), S3);
130199
+ return E3.length > 0 ? this.filterInput(t, S3, E3) : t;
130002
130200
  }
130003
130201
  diagnoseGroupFanout(e14, t, n) {
130004
130202
  let r3 = /* @__PURE__ */ new Set;
@@ -130195,6 +130393,15 @@ function wireGrains(e14, t) {
130195
130393
  let n = new GrainFold(e14);
130196
130394
  return n.foldRelationshipKeyDims(), n.foldBlock(t);
130197
130395
  }
130396
+ function unslicedOf(e14) {
130397
+ let t = e14;
130398
+ for (;t.kind === "filter" || t.kind === "select" || t.kind === "sort"; )
130399
+ t = t.input;
130400
+ return t.kind === "expand_model" ? {
130401
+ keys: t.unsliced ?? [],
130402
+ scanRoot: t.model
130403
+ } : t.kind === "reduce" ? { keys: t.unsliced ?? [] } : { keys: [] };
130404
+ }
130198
130405
  function scanRootOf(e14) {
130199
130406
  let t = e14;
130200
130407
  for (;; ) {
@@ -130208,18 +130415,6 @@ function scanRootOf(e14) {
130208
130415
  return;
130209
130416
  }
130210
130417
  }
130211
- function partGrainPaths(e14, t, n, r3) {
130212
- let i = [];
130213
- for (let e15 of n)
130214
- for (let t3 of e15.by) {
130215
- let e16 = isFieldType(t3.ty) ? t3.ty.origin : undefined;
130216
- e16 && !r3.has(e16.modelName) && !i.includes(e16.modelName) && i.push(e16.modelName);
130217
- }
130218
- if (i.length === 0)
130219
- return [];
130220
- let a = resolveJoinTree(i, baseGraph(e14), t, true);
130221
- return a ? a.paths.filter((e15) => e15[0].length > 0).map((n3) => pathToEdges(e14, t, n3[0])).filter((e15) => !r3.has(e15[e15.length - 1].destModel)) : [];
130222
- }
130223
130418
  function appendPartGrains(e14, t, n) {
130224
130419
  let r3 = (t3) => extendDef(e14.dataset, t3), i = /* @__PURE__ */ new Set;
130225
130420
  walkTree(t, (e15) => i.add(e15.model)), n && t.extend !== undefined && walkTree(r3(t.extend).base, (e15) => i.add(e15.model));
@@ -130230,12 +130425,14 @@ function appendPartGrains(e14, t, n) {
130230
130425
  if (t3.extend === undefined)
130231
130426
  continue;
130232
130427
  let { base: s4, parts: l } = r3(t3.extend);
130233
- if (l.length !== 0 && (bareNode(s4) || n && t3 === a))
130234
- for (let n3 of partGrainPaths(e14, t3.model, l, i)) {
130235
- for (let e15 of n3)
130236
- i.add(e15.destModel);
130237
- placeChain(t3, n3), c3 = true;
130238
- }
130428
+ if (l.length === 0 || !bareNode(s4) && !(n && t3 === a))
130429
+ continue;
130430
+ let u = l.flatMap((e15) => e15.by.flatMap((e16) => isFieldType(e16.ty) && e16.ty.origin ? [e16.ty.origin] : []));
130431
+ for (let n3 of keyModelPaths(e14, t3.model, u, i)) {
130432
+ for (let e15 of n3)
130433
+ i.add(e15.destModel);
130434
+ placeChain(t3, n3), c3 = true;
130435
+ }
130239
130436
  }
130240
130437
  return c3 ? a : t;
130241
130438
  }
@@ -130732,7 +130929,15 @@ var WindowFold = class extends HirFold {
130732
130929
  }
130733
130930
  return t;
130734
130931
  }
130932
+ levels = /* @__PURE__ */ new Map;
130735
130933
  windowInput(e15, t) {
130934
+ let n = [];
130935
+ for (let e16 of t)
130936
+ (n[this.levels.get(e16) ?? 0] ??= []).push(e16);
130937
+ let [r3, ...i] = n;
130938
+ return i.reduce((e16, t3) => this.windowLevel(e16, t3), this.windowLevel(e15, r3));
130939
+ }
130940
+ windowLevel(e15, t) {
130736
130941
  return {
130737
130942
  kind: "window",
130738
130943
  id: this.resolver.newNodeId(),
@@ -130775,17 +130980,17 @@ var WindowFold = class extends HirFold {
130775
130980
  if (!this.hasWindowBelow(e15))
130776
130981
  return e15;
130777
130982
  if (e15.kind === Kind.FUNCTION_CALL) {
130778
- let n = {
130983
+ let n = t.length, r3 = {
130779
130984
  ...e15,
130780
130985
  args: e15.args.map((e16) => this.hoistWindows(e16, t))
130781
130986
  };
130782
130987
  if (!isWindowFn(e15.name.name))
130783
- return n;
130784
- let r3 = `__w${this.counter++}`;
130785
- return t.push({
130786
- name: r3,
130787
- value: n
130788
- }), this.columnRef(r3, n.ty);
130988
+ return r3;
130989
+ let i = `__w${this.counter++}`, a = {
130990
+ name: i,
130991
+ value: r3
130992
+ }, s3 = t.slice(n).map((e16) => this.levels.get(e16) ?? 0);
130993
+ return this.levels.set(a, s3.length > 0 ? Math.max(...s3) + 1 : 0), t.push(a), this.columnRef(i, r3.ty);
130789
130994
  }
130790
130995
  return e15.kind === Kind.NAMED_EXPRESSION ? {
130791
130996
  ...e15,
@@ -131088,6 +131293,8 @@ function resolvePrefix(e15, t, n) {
131088
131293
  let s3;
131089
131294
  if (isErrType(r3.ty))
131090
131295
  s3 = errType;
131296
+ else if (isTableType(r3.ty) && r3.ty.param)
131297
+ e15.dataset.diagnostics.push(createError(ErrorCode.OPERATOR_0_ON_PARAM_1, t.right.loc, undefined, i, r3.ty.param)), s3 = errType;
131091
131298
  else if (!isScalarLikeType(r3.ty))
131092
131299
  e15.dataset.diagnostics.push(createError(ErrorCode.EXPECTED_SCALAR_LIKE_TYPE_FOUND_0, t.loc, undefined, r3.ty)), s3 = errType;
131093
131300
  else {
@@ -131510,6 +131717,27 @@ function resolveSorts(e15, t, n, r3) {
131510
131717
  return !isErrType(a.ty) && !isAssignable(SHIM, a.ty, fieldTypeClass) && e15.dataset.diagnostics.push(createError(ErrorCode.EXPECTED_0_FOUND_1, t3.loc, undefined, fieldTypeClass, a.ty)), [a, i];
131511
131718
  });
131512
131719
  }
131720
+ function measureAliasScope(e15, t, n) {
131721
+ let r3 = locStub(), i = makeBlock(e15, r3, aqlNull(e15, r3));
131722
+ for (let r4 of n) {
131723
+ if (!isNamedExpression(r4))
131724
+ continue;
131725
+ let n3 = r4.name.value, a = `${n3}'`, s3 = createFieldType(n3, unknownType, undefined, undefined, "metric");
131726
+ i.declarations.set(n3, {
131727
+ id: e15.newNodeId(r4.loc),
131728
+ kind: "metric",
131729
+ name: makeIdent(e15, a, [], s3),
131730
+ ty: s3,
131731
+ body: {
131732
+ syntax: "aql",
131733
+ definition: r4.expr
131734
+ },
131735
+ scope: t,
131736
+ source: n3
131737
+ });
131738
+ }
131739
+ return makeScope(t, i);
131740
+ }
131513
131741
  function namesResultField(e15, t) {
131514
131742
  if (isFunctionCall(e15) && functionCallName(e15) === Kind.NOT_KEYWORD && e15.args.length === 1)
131515
131743
  return namesResultField(e15.args[0], t);
@@ -131522,7 +131750,7 @@ function classifyFilters(e15, t, n, r3, i) {
131522
131750
  let a = [], s3 = [];
131523
131751
  return t.filters.forEach((t3) => {
131524
131752
  let c3 = namesResultField(t3, i) ? n : r3, l = e15.resolveExpr(t3, c3);
131525
- validateExploreFilter(e15, t3, l), !isErrType(l.ty) && (isResultFieldFilter(l) ? a.push(l) : isTableType(l.ty) ? s3.push(desugarTableCondition(e15, l, n)) : referencesAggregateAlias(l) ? e15.dataset.diagnostics.push(createError(ErrorCode.INVALID_CONDITIONAL_EXPRESSION_IN_0, t3.loc, undefined, "filters")) : involvesTable(l) || s3.push(desugarMeasureCondition(e15, desugarSimpleModelCondition(e15, desugarShorthandCondition(e15, l)))));
131753
+ validateExploreFilter(e15, t3, l), !isErrType(l.ty) && (isResultFieldFilter(l, i) ? a.push(l) : isTableType(l.ty) ? s3.push(desugarTableCondition(e15, l, n)) : referencesAggregateAlias(l) ? e15.dataset.diagnostics.push(createError(ErrorCode.INVALID_CONDITIONAL_EXPRESSION_IN_0, t3.loc, undefined, "filters")) : involvesTable(l) || s3.push(desugarMeasureCondition(e15, desugarSimpleModelCondition(e15, desugarShorthandCondition(e15, l)))));
131526
131754
  }), {
131527
131755
  resultFilterNodes: a,
131528
131756
  contextFilterNodes: s3
@@ -131579,35 +131807,35 @@ function resolveDynamicExploreInner(e15, t, n) {
131579
131807
  let t3 = originOf(e16.ty);
131580
131808
  t3 && li3.add(modelFieldRefAsKey(t3));
131581
131809
  }
131582
- let di3 = [...oi3];
131810
+ let di3 = [...oi3], fi3 = measureAliasScope(e15, me3, t.measures);
131583
131811
  for (let n3 of t.having) {
131584
- let t3 = e15.resolveExpr(n3, U3);
131812
+ let t3 = e15.resolveExpr(n3, fi3);
131585
131813
  validateHavingGrouped(e15, n3, t3, li3) && oi3.push(desugarMeasureCondition(e15, t3, di3, true));
131586
131814
  }
131587
131815
  r3.length === 0 && t.measures.length === 0 && e15.dataset.diagnostics.push(createError(ErrorCode.INVALID_EXPRESSION_0, t.loc, undefined, "Explore must have at least one dimension or measure."));
131588
- let fi3 = grainDimsOf(r3), pi3 = S3.map((e16) => e16.ty), mi3 = new Set(pi3.map((e16) => e16.fieldName)), hi3 = (e16) => {
131816
+ let pi3 = grainDimsOf(r3), mi3 = S3.map((e16) => e16.ty), hi3 = new Set(mi3.map((e16) => e16.fieldName)), gi3 = (e16) => {
131589
131817
  let t3 = unwrapNot(e16);
131590
- return t3.kind === Kind.FUNCTION_CALL && t3.args.length > 0 && t3.args[0].kind === Kind.IDENTIFIER && mi3.has(t3.args[0].name);
131591
- }, gi3 = yr4.filter(hi3), _i3 = yr4.filter((e16) => !hi3(e16)), vi3 = {
131818
+ return t3.kind === Kind.FUNCTION_CALL && t3.args.length > 0 && t3.args[0].kind === Kind.IDENTIFIER && hi3.has(t3.args[0].name);
131819
+ }, _i3 = yr4.filter(gi3), vi3 = yr4.filter((e16) => !gi3(e16)), yi3 = {
131592
131820
  resolver: e15,
131593
131821
  loc: t.loc,
131594
131822
  dimColumns: i,
131595
131823
  markedDims: k3,
131596
131824
  typedMeasures: w3,
131597
131825
  resultTableTy: L3,
131598
- grainDims: fi3
131599
- }, yi3 = {};
131826
+ grainDims: pi3
131827
+ }, bi3 = {};
131600
131828
  for (let [e16, t3] of Ze4) {
131601
131829
  let { ty: n3 } = e16;
131602
- isFieldType(n3) && n3.origin && (yi3[modelFieldRefAsKey(n3.origin)] = t3);
131830
+ isFieldType(n3) && n3.origin && (bi3[modelFieldRefAsKey(n3.origin)] = t3);
131603
131831
  }
131604
- let bi3 = (e16) => isFieldType(e16.ty) && e16.ty.origin ? modelFieldRefAsKey(e16.ty.origin) : undefined, xi3 = f.length + p3.length > 0, Si2 = (n3, r4) => {
131605
- if (!xi3)
131832
+ let xi3 = (e16) => isFieldType(e16.ty) && e16.ty.origin ? modelFieldRefAsKey(e16.ty.origin) : undefined, Si2 = f.length + p3.length > 0, Ci3 = (n3, r4) => {
131833
+ if (!Si2)
131606
131834
  return n3;
131607
131835
  let i3 = (e16) => {
131608
131836
  if (!r4)
131609
131837
  return true;
131610
- let t3 = bi3(e16);
131838
+ let t3 = xi3(e16);
131611
131839
  return t3 !== undefined && r4.has(t3);
131612
131840
  };
131613
131841
  return {
@@ -131616,35 +131844,35 @@ function resolveDynamicExploreInner(e15, t, n) {
131616
131844
  ty: n3.ty,
131617
131845
  rows: f.filter(i3),
131618
131846
  cols: p3.filter(i3),
131619
- sortDirs: yi3,
131847
+ sortDirs: bi3,
131620
131848
  value: n3
131621
131849
  };
131622
- }, Ci3 = f.length, wi3 = p3.length, Ti3 = [...new Set(t.measures.map((e16) => (isNamedExpression(e16) ? e16.expr : e16).loc.source))], Ei3 = (e16, t3) => {
131850
+ }, wi3 = f.length, Ti3 = p3.length, Ei3 = [...new Set(t.measures.map((e16) => (isNamedExpression(e16) ? e16.expr : e16).loc.source))], Di3 = (e16, t3) => {
131623
131851
  let n3 = new Set(t3.map((e17) => e17.origin ? modelFieldRefAsKey(e17.origin) : "").filter(Boolean)), r4 = (e17) => {
131624
- let t4 = bi3(e17);
131852
+ let t4 = xi3(e17);
131625
131853
  return t4 !== undefined && n3.has(t4);
131626
- }, i3 = exploreTypeLabel(f.filter(r4).length, p3.filter(r4).length, Ci3, wi3), a3 = t3.filter((e17) => e17.origin).map((e17) => `${e17.origin.modelName}.${e17.origin.fieldName}`), s4 = {
131854
+ }, i3 = exploreTypeLabel(f.filter(r4).length, p3.filter(r4).length, wi3, Ti3), a3 = t3.filter((e17) => e17.origin).map((e17) => `${e17.origin.modelName}.${e17.origin.fieldName}`), s4 = {
131627
131855
  ...a3.length > 0 ? { dimensions: a3 } : {},
131628
- ...Ti3.length > 0 ? { metrics: Ti3 } : {}
131856
+ ...Ei3.length > 0 ? { metrics: Ei3 } : {}
131629
131857
  };
131630
131858
  return {
131631
131859
  ...e16,
131632
131860
  annotation: [
131633
131861
  ...e16.annotation ?? [],
131634
131862
  { comments: [i3] },
131635
- ...a3.length > 0 || Ti3.length > 0 ? [{ meta: s4 }] : []
131863
+ ...a3.length > 0 || Ei3.length > 0 ? [{ meta: s4 }] : []
131636
131864
  ]
131637
131865
  };
131638
- }, Di3 = r3.filter((e16) => isConstantArrayType(e16.ty)), Oi3;
131639
- if (Di3.length > 0) {
131640
- let n3 = dedupeGrains(c3), r4 = expandRollup(Di3.map(groupingDimSets)), i3 = n3.map(grainName), a3 = /* @__PURE__ */ new Set, s4 = r4.filter((e16) => {
131866
+ }, Oi3 = r3.filter((e16) => isConstantArrayType(e16.ty)), ki4;
131867
+ if (Oi3.length > 0) {
131868
+ let n3 = dedupeGrains(c3), r4 = expandRollup(Oi3.map(groupingDimSets)), i3 = n3.map(grainName), a3 = /* @__PURE__ */ new Set, s4 = r4.filter((e16) => {
131641
131869
  let t3 = [.../* @__PURE__ */ new Set([...i3, ...e16.map(grainName)])].sort().join(" ");
131642
131870
  return !a3.has(t3) && (a3.add(t3), true);
131643
131871
  }).map((r5) => {
131644
- let i4 = dedupeGrains([...n3, ...r5]), a4 = grainFields(i4), s5 = buildBranch(vi3, i4), c4 = new Set(a4.map((e16) => e16.fieldName)), l3 = k3.length > 0 && k3.every((e16) => c4.has(e16.fieldName)) && _i3.length > 0 ? makeTransform(e15, "filter", [s5, ..._i3], s5.ty, t.loc) : s5, u3 = new Set(a4.map((e16) => e16.origin ? modelFieldRefAsKey(e16.origin) : "").filter(Boolean));
131645
- return Ei3(Si2(l3, u3), a4);
131872
+ let i4 = dedupeGrains([...n3, ...r5]), a4 = grainFields(i4), s5 = buildBranch(yi3, i4), c4 = new Set(a4.map((e16) => e16.fieldName)), l3 = k3.length > 0 && k3.every((e16) => c4.has(e16.fieldName)) && vi3.length > 0 ? makeTransform(e15, "filter", [s5, ...vi3], s5.ty, t.loc) : s5, u3 = new Set(a4.map((e16) => e16.origin ? modelFieldRefAsKey(e16.origin) : "").filter(Boolean));
131873
+ return Di3(Ci3(l3, u3), a4);
131646
131874
  });
131647
- Oi3 = s4.length === 1 ? s4[0] : {
131875
+ ki4 = s4.length === 1 ? s4[0] : {
131648
131876
  kind: "union",
131649
131877
  id: e15.newNodeId(t.loc),
131650
131878
  ty: L3,
@@ -131653,17 +131881,17 @@ function resolveDynamicExploreInner(e15, t, n) {
131653
131881
  branches: s4
131654
131882
  };
131655
131883
  } else
131656
- Oi3 = Ei3(buildBranch(vi3, fi3), grainFields(fi3));
131657
- oi3.length > 0 && (Oi3 = {
131884
+ ki4 = Di3(buildBranch(yi3, pi3), grainFields(pi3));
131885
+ oi3.length > 0 && (ki4 = {
131658
131886
  kind: "where",
131659
131887
  id: e15.newNodeId(t.loc),
131660
- ty: Oi3.ty,
131661
- value: Oi3,
131888
+ ty: ki4.ty,
131889
+ value: ki4,
131662
131890
  conditions: oi3
131663
131891
  });
131664
- let ki4 = t.options.some((e16) => e16.value === "include_empty_children_rows") || undefined, Ai3 = w3.length > 0, ji4 = [...oi3, ...yr4];
131665
- ki4 && !m3 && Ai3 && Di3.length === 0 && i.length > 0 && (Oi3 = buildEmptyChildrenChain(vi3, i, ji4, Oi3, [...i, ...I3]));
131666
- let Mi3 = S3.flatMap((e16) => {
131892
+ let Ai3 = t.options.some((e16) => e16.value === "include_empty_children_rows") || undefined, ji4 = w3.length > 0, Mi3 = [...oi3, ...yr4];
131893
+ Ai3 && !m3 && ji4 && Oi3.length === 0 && i.length > 0 && (ki4 = buildEmptyChildrenChain(yi3, i, Mi3, ki4, [...i, ...I3]));
131894
+ let Ni3 = S3.flatMap((e16) => {
131667
131895
  if (!e16.rollup)
131668
131896
  return [];
131669
131897
  let t3 = bizCalDimRefs(e16.expr).map((e17) => e17.ty.fieldName).find((e17) => s3.has(e17));
@@ -131671,91 +131899,97 @@ function resolveDynamicExploreInner(e15, t, n) {
131671
131899
  ty: createFieldType(rollupMarkerFieldName(e16.ty.fieldName), numberType),
131672
131900
  sourceName: rollupMarkerFieldName(t3)
131673
131901
  }] : [];
131674
- }), Ni3 = [
131902
+ }), Pi3 = [
131675
131903
  ...a,
131676
131904
  ...I3,
131677
- ...Mi3.map((e16) => e16.ty)
131905
+ ...Ni3.map((e16) => e16.ty)
131678
131906
  ].map((e16) => e16.fieldName);
131679
131907
  if (S3.length > 0) {
131680
131908
  let n3 = new Set(a.map((e16) => e16.fieldName)), r4 = m3 ? [...a, ...i.filter((e16) => !n3.has(e16.fieldName))] : a, s4 = createTableType(createRowType([
131681
131909
  ...r4,
131682
131910
  ...I3,
131683
- ...Mi3.map((e16) => e16.ty)
131911
+ ...Ni3.map((e16) => e16.ty)
131684
131912
  ]), undefined);
131685
- Oi3 = buildBizCalWrapper(e15, Oi3, r4, I3, S3, Mi3, s4, t.loc);
131913
+ ki4 = buildBizCalWrapper(e15, ki4, r4, I3, S3, Ni3, s4, t.loc);
131686
131914
  }
131687
- let Pi3 = Di3.length === 0 ? yr4 : gi3;
131688
- Pi3.length > 0 && (Oi3 = makeTransform(e15, "filter", [Oi3, ...Pi3], Oi3.ty, t.loc));
131689
- let Fi3 = !!e15.dataset.sqlGenerationFlags.shouldApplySortings, Ii2 = Fi3 && S3.length === 0 && ![...l].some((e16) => u.has(e16)), Li3 = Ze4.flatMap(([e16, t3]) => isFieldType(e16.ty) ? [[e16.ty.fieldName, t3]] : []);
131690
- if (!m3 && Fi3 && Ze4.length > 0 && (Oi3 = {
131915
+ let Fi3 = Oi3.length === 0 ? yr4 : _i3;
131916
+ Fi3.length > 0 && (ki4 = makeTransform(e15, "filter", [ki4, ...Fi3], ki4.ty, t.loc));
131917
+ let Ii2 = !!e15.dataset.sqlGenerationFlags.shouldApplySortings, Li3 = Ii2 && S3.length === 0 && ![...l].some((e16) => u.has(e16)), Ri3 = Ze4.flatMap(([e16, t3]) => isFieldType(e16.ty) ? [[e16.ty.fieldName, t3]] : []);
131918
+ if (!m3 && Ii2 && Ze4.length > 0 && (ki4 = {
131691
131919
  kind: "sort",
131692
131920
  id: e15.newNodeId(t.loc),
131693
- ty: Oi3.ty,
131694
- input: Oi3,
131695
- iterationDepth: iterationDepthOf(Oi3),
131921
+ ty: ki4.ty,
131922
+ input: ki4,
131923
+ iterationDepth: iterationDepthOf(ki4),
131696
131924
  by: Ze4
131697
131925
  }), m3) {
131698
- let n3 = fi3.filter((e16) => l.has(grainName(e16))), r4 = ki4 && n3.length > 0 && Ai3 ? buildEmptyChildrenPrefixes(vi3, n3, ji4).map((e16) => e16.node) : undefined;
131699
- Oi3 = {
131926
+ let n3 = pi3.filter((e16) => l.has(grainName(e16))), r4 = Ai3 && n3.length > 0 && ji4 ? buildEmptyChildrenPrefixes(yi3, n3, Mi3).map((e16) => e16.node) : undefined;
131927
+ ki4 = {
131700
131928
  kind: "pivot",
131701
131929
  id: e15.newNodeId(t.loc),
131702
- ty: Oi3.ty,
131703
- input: Oi3,
131704
- iterationDepth: iterationDepthOf(Oi3),
131930
+ ty: ki4.ty,
131931
+ input: ki4,
131932
+ iterationDepth: iterationDepthOf(ki4),
131705
131933
  rows: [...l],
131706
131934
  columns: [...u],
131707
131935
  rollupRows: [...l].filter((e16) => s3.has(e16)),
131708
131936
  rollupColumns: [...u].filter((e16) => s3.has(e16)),
131709
- includeEmptyChildrenRows: ki4,
131937
+ includeEmptyChildrenRows: Ai3,
131710
131938
  emptyChildrenPrefixes: r4,
131711
- outputColumns: S3.length > 0 ? Ni3 : undefined,
131712
- sorts: Ii2 ? Li3 : undefined
131939
+ outputColumns: S3.length > 0 ? Pi3 : undefined,
131940
+ sorts: Li3 ? Ri3 : undefined
131713
131941
  };
131714
131942
  }
131715
- return Di3.length === 0 && (Oi3 = Si2(Oi3)), {
131943
+ return Oi3.length === 0 && (ki4 = Ci3(ki4)), {
131716
131944
  kind: "explore_context",
131717
131945
  id: e15.newNodeId(t.loc),
131718
- ty: Oi3.ty,
131719
- value: Oi3,
131946
+ ty: ki4.ty,
131947
+ value: ki4,
131720
131948
  filters: ci3,
131721
131949
  allDimensions: [...f, ...p3]
131722
131950
  };
131723
131951
  }
131724
- function isBareAliasConstantFilter(e15) {
131725
- return e15.length === 2 && e15[0].kind === Kind.IDENTIFIER && e15[0].path.length === 0 && !involvesField(e15[1]);
131952
+ function isBareAliasResultFilter(e15, t) {
131953
+ return e15.length === 2 && e15[0].kind === Kind.IDENTIFIER && e15[0].path.length === 0 && readsOnlyResultColumns(e15[1], t);
131954
+ }
131955
+ function rhsAliases(e15) {
131956
+ return e15.kind === Kind.IDENTIFIER ? isFieldType(e15.ty) ? [e15.ty] : [] : e15.kind === Kind.FUNCTION_CALL ? e15.args.flatMap(rhsAliases) : e15.kind === Kind.NAMED_EXPRESSION ? rhsAliases(e15.expr) : [];
131957
+ }
131958
+ function readsOnlyResultColumns(e15, t) {
131959
+ return e15.kind === "computed_ref" ? false : e15.kind === Kind.IDENTIFIER ? !isFieldType(e15.ty) || e15.path.length === 0 && t.has(e15.name) : e15.kind === Kind.FUNCTION_CALL ? e15.args.every((e16) => readsOnlyResultColumns(e16, t)) : e15.kind !== Kind.NAMED_EXPRESSION || readsOnlyResultColumns(e15.expr, t);
131726
131960
  }
131727
131961
  function unwrapNot(e15) {
131728
131962
  return e15.kind === Kind.FUNCTION_CALL && e15.name.name === Kind.NOT_KEYWORD && e15.args.length === 1 ? unwrapNot(e15.args[0]) : e15;
131729
131963
  }
131730
- function isResultFieldFilter(e15) {
131964
+ function isResultFieldFilter(e15, t) {
131731
131965
  if (isErrType(e15.ty))
131732
131966
  return false;
131733
- let t = unwrapNot(e15);
131734
- if (t.kind !== Kind.FUNCTION_CALL)
131967
+ let n = unwrapNot(e15);
131968
+ if (n.kind !== Kind.FUNCTION_CALL)
131735
131969
  return false;
131736
- if (isBareAliasConstantFilter(t.args))
131970
+ if (isBareAliasResultFilter(n.args, t))
131737
131971
  return true;
131738
- let n = bareAliasLeaves(t);
131739
- return !!n && n.some(isAggregateAlias);
131972
+ let r3 = bareAliasLeaves(n, t);
131973
+ return !!r3 && r3.some(isAggregateAlias);
131740
131974
  }
131741
- function bareAliasLeaves(e15) {
131742
- let t = unwrapNot(e15);
131743
- if (t.kind !== Kind.FUNCTION_CALL)
131975
+ function bareAliasLeaves(e15, t) {
131976
+ let n = unwrapNot(e15);
131977
+ if (n.kind !== Kind.FUNCTION_CALL)
131744
131978
  return;
131745
- if (isBareAliasConstantFilter(t.args)) {
131746
- let e16 = t.args[0];
131747
- return isFieldType(e16.ty) ? [e16.ty] : undefined;
131979
+ if (isBareAliasResultFilter(n.args, t)) {
131980
+ let e16 = n.args[0];
131981
+ return isFieldType(e16.ty) ? [e16.ty, ...rhsAliases(n.args[1])] : undefined;
131748
131982
  }
131749
- if (t.name.name !== Kind.AND_KEYWORD && t.name.name !== Kind.OR_KEYWORD)
131983
+ if (n.name.name !== Kind.AND_KEYWORD && n.name.name !== Kind.OR_KEYWORD)
131750
131984
  return;
131751
- let n = [];
131752
- for (let e16 of t.args) {
131753
- let t3 = bareAliasLeaves(e16);
131754
- if (!t3)
131985
+ let r3 = [];
131986
+ for (let e16 of n.args) {
131987
+ let n3 = bareAliasLeaves(e16, t);
131988
+ if (!n3)
131755
131989
  return;
131756
- n.push(...t3);
131990
+ r3.push(...n3);
131757
131991
  }
131758
- return n;
131992
+ return r3;
131759
131993
  }
131760
131994
  function isAggregateAlias(e15) {
131761
131995
  return e15.subClass === "aggregate" || e15.subClass === "measure" || e15.subClass === "metric";
@@ -131766,9 +132000,6 @@ function referencesAggregateAlias(e15) {
131766
132000
  function involvesTable(e15) {
131767
132001
  return isTableType(e15.ty) && !e15.ty.isLiteral ? true : e15.kind === Kind.FUNCTION_CALL ? e15.args.some(involvesTable) : e15.kind === Kind.NAMED_EXPRESSION ? involvesTable(e15.expr) : e15.kind === "block" && involvesTable(e15.result);
131768
132002
  }
131769
- function involvesField(e15) {
131770
- return e15 ? e15.kind === "computed_ref" ? true : e15.kind === Kind.IDENTIFIER ? isFieldType(e15.ty) : e15.kind === Kind.FUNCTION_CALL ? e15.args.some(involvesField) : e15.kind === Kind.NAMED_EXPRESSION && involvesField(e15.expr) : false;
131771
- }
131772
132003
  function validateExploreFilter(e15, t, n) {
131773
132004
  let { ty: r3 } = n;
131774
132005
  isErrType(r3) || !isAssignable(SHIM, r3, truefalseType) && !isAssignable(SHIM, r3, tableTypeClass) && e15.dataset.diagnostics.push(createError(ErrorCode.EXPECTED_0_FOUND_1, t.loc, undefined, "Condition", r3));
@@ -132405,19 +132636,24 @@ function prepareCheck(e16) {
132405
132636
  };
132406
132637
  }
132407
132638
  function checkAql(e16) {
132408
- let { dataset: t, adhocMetrics: n, adhocDimensions: r3, aql: i, aqlType: a, fieldName: s3, modelUname: c3, userAttributes: l = [], checkFlags: u = CheckFlags.SkipCheckingRelationshipKey, allowAmbiguousPaths: f = false, useV2Compiler: p3 = false } = e16, { datasetStore: m3, fieldDeclarations: S3 } = prepareCheck(e16), w3;
132639
+ let { dataset: t, adhocMetrics: n, adhocDimensions: r3, aql: i, aqlType: a, fieldName: s3, modelUname: c3, userAttributes: l = [], checkFlags: u = CheckFlags.SkipCheckingRelationshipKey, allowAmbiguousPaths: f = false, useV2Compiler: p3 = false, allowAdhocSqlPassthroughFunctions: m3 = true } = e16, { datasetStore: S3, fieldDeclarations: w3 } = prepareCheck(e16), E3;
132409
132640
  try {
132410
- w3 = parse(i);
132641
+ E3 = parse(i);
132411
132642
  } catch (e17) {
132412
132643
  return [e17];
132413
132644
  }
132414
- let E3 = a === "dimension" && c3 ? getFqn(c3) : undefined;
132645
+ if (!m3) {
132646
+ let e17 = checkAdhocSqlPassthrough([E3]);
132647
+ if (e17.length > 0)
132648
+ return e17;
132649
+ }
132650
+ let k3 = a === "dimension" && c3 ? getFqn(c3) : undefined;
132415
132651
  if (p3 && a !== "condition") {
132416
132652
  let e17 = resolve({
132417
- store: m3,
132418
- program: w3,
132419
- modelContext: E3,
132420
- adhocFields: S3,
132653
+ store: S3,
132654
+ program: E3,
132655
+ modelContext: k3,
132656
+ adhocFields: w3,
132421
132657
  userAttributes: l,
132422
132658
  checkFlags: u,
132423
132659
  recordSpanTypes: true,
@@ -132426,31 +132662,39 @@ function checkAql(e16) {
132426
132662
  });
132427
132663
  if (e17.diagnostics.length > 0)
132428
132664
  return e17.diagnostics;
132429
- let t3 = import_lodash2.last(w3.exprs);
132665
+ let t3 = import_lodash2.last(E3.exprs);
132430
132666
  return t3 === undefined ? SqlType.UNKNOWN : sqlTypeOfCheckedExpression(e17.spanTypes?.at(t3.loc));
132431
132667
  }
132432
- let k3 = createTypeChecker(m3, l, S3, [], u), N3;
132433
- if (a === "condition" && (N3 = k3.getDiagnosticsForCondition(w3)), N3 = k3.getDiagnostics(w3, E3), N3.length > 0)
132434
- return N3;
132435
- let I3 = import_lodash2.last(w3.exprs)?.id;
132436
- return I3 === undefined ? SqlType.UNKNOWN : sqlTypeOfCheckedExpression(k3.getTypeFromNodeId(I3));
132668
+ let N3 = createTypeChecker(S3, l, w3, [], u), I3;
132669
+ if (a === "condition" && (I3 = N3.getDiagnosticsForCondition(E3)), I3 = N3.getDiagnostics(E3, k3), I3.length > 0)
132670
+ return I3;
132671
+ let L3 = import_lodash2.last(E3.exprs)?.id;
132672
+ return L3 === undefined ? SqlType.UNKNOWN : sqlTypeOfCheckedExpression(N3.getTypeFromNodeId(L3));
132437
132673
  }
132438
132674
  function checkAqlCondition(e16) {
132439
- let { userAttributes: t = [], checkFlags: n = CheckFlags.SkipCheckingRelationshipKey, useV2Compiler: r3 = false } = e16, i;
132675
+ let { userAttributes: t = [], checkFlags: n = CheckFlags.SkipCheckingRelationshipKey, useV2Compiler: r3 = false, allowAdhocSqlPassthroughFunctions: i = true } = e16, a;
132440
132676
  try {
132441
- i = parse(e16.aql);
132677
+ a = parse(e16.aql);
132442
132678
  } catch (e17) {
132443
132679
  return {
132444
132680
  diagnostics: [e17],
132445
132681
  conditionType: "normal"
132446
132682
  };
132447
132683
  }
132448
- let { datasetStore: a, fieldDeclarations: s3 } = prepareCheck(e16);
132684
+ if (!i) {
132685
+ let e17 = checkAdhocSqlPassthrough([a]);
132686
+ if (e17.length > 0)
132687
+ return {
132688
+ diagnostics: e17,
132689
+ conditionType: "normal"
132690
+ };
132691
+ }
132692
+ let { datasetStore: s3, fieldDeclarations: c3 } = prepareCheck(e16);
132449
132693
  if (r3) {
132450
132694
  let e17 = resolve({
132451
- store: a,
132452
- program: i,
132453
- adhocFields: s3,
132695
+ store: s3,
132696
+ program: a,
132697
+ adhocFields: c3,
132454
132698
  userAttributes: t,
132455
132699
  checkFlags: n,
132456
132700
  typeCheckOnly: true,
@@ -132461,13 +132705,13 @@ function checkAqlCondition(e16) {
132461
132705
  conditionType: e17.conditionKind ?? "normal"
132462
132706
  };
132463
132707
  }
132464
- let c3 = createTypeChecker(a, t, s3, [], n), l = c3.getDiagnosticsForCondition(i);
132465
- return l.length > 0 ? {
132466
- diagnostics: l,
132708
+ let l = createTypeChecker(s3, t, c3, [], n), u = l.getDiagnosticsForCondition(a);
132709
+ return u.length > 0 ? {
132710
+ diagnostics: u,
132467
132711
  conditionType: "normal"
132468
132712
  } : {
132469
- diagnostics: l,
132470
- conditionType: c3.measure_conditions.getMeasureCondition(i.exprs[0]) ? "measure" : "normal"
132713
+ diagnostics: u,
132714
+ conditionType: l.measure_conditions.getMeasureCondition(a.exprs[0]) ? "measure" : "normal"
132471
132715
  };
132472
132716
  }
132473
132717
  function createFieldRelationshipChecker(e16) {
@@ -135304,7 +135548,12 @@ function matchToPlan(e16, t) {
135304
135548
  type: SqlType.TRUEFALSE
135305
135549
  };
135306
135550
  }
135307
- return isUserAttributeType(n.ty) ? datetimeUaToPlan(e16[0], n, t) : unsupported$1("match against a non-literal range");
135551
+ return isUserAttributeType(n.ty) ? datetimeUaToPlan(e16[0], n, t) : isNullValue$1(n) ? {
135552
+ kind: "call_variadic",
135553
+ func: "is",
135554
+ exprs: [scalarToPlan(e16[0], t), scalarToPlan(n, t)],
135555
+ type: SqlType.TRUEFALSE
135556
+ } : unsupported$1("match against a non-literal range");
135308
135557
  }
135309
135558
  function negateMagnitude(e16) {
135310
135559
  return e16.kind === "literal" && e16.literal === "number" ? {
@@ -136418,7 +136667,7 @@ function attachLodParts(e17, t, n, r3, i, a) {
136418
136667
  type: SqlType.UNKNOWN
136419
136668
  }), k3 = relationType(n.relation), N3 = i3.by.map((e18, r5) => {
136420
136669
  let { expr: a4, origin: s4 } = i3.by[r5];
136421
- return grainKeyEq(t, s4, a4.kind === Kind.IDENTIFIER && s4.fieldName.includes("->") && l(a4) == null && n.tryIndexOf(a4) == null ? subtimeGrainScalar(n, k3, s4) : lodKeyScalar(derefExtend(t, i3.input, a4), n, u), E3(w3 + r5));
136670
+ return grainKeyEq(t, s4, a4.kind === Kind.IDENTIFIER && s4.fieldName.includes("->") && l(a4) == null && n.tryIndexOf(a4) == null ? subtimeGrainScalar(n, k3, s4) : lodKeyScalar(joinBackKey(t, i3.input, a4, n), n, u), E3(w3 + r5));
136422
136671
  });
136423
136672
  f = cons({
136424
136673
  kind: "join",
@@ -136586,6 +136835,7 @@ function lowerModelSourceRelation(e17, t, n) {
136586
136835
  return buildJoinScope(t, r3, i, s4, {
136587
136836
  rootRel: a,
136588
136837
  fanouts: c4,
136838
+ substitute: slotSubstitutes(t, n, e17),
136589
136839
  tree: n
136590
136840
  });
136591
136841
  })() : buildJoinScope(t, r3, i, [], { rootRel: a }), c3 = n.kind === "expand_model" ? expandTreeComputed(t, n) : [], l = n.kind === "expand_model" ? expandTreeComputedAll(t, n) : [], u = (e18) => {
@@ -136969,13 +137219,19 @@ function buildJoinScopeUncached(e17, t, n, r3, i) {
136969
137219
  };
136970
137220
  }
136971
137221
  function extendSlotsOf(e17) {
137222
+ let t = [];
137223
+ for (let n of scansOf(e17))
137224
+ forEachTreeNode(n, (e18) => {
137225
+ e18.extend !== undefined && t.push(e18);
137226
+ });
137227
+ return t;
137228
+ }
137229
+ function scansOf(e17) {
136972
137230
  let t = [], n = /* @__PURE__ */ new Set, r3 = (e18) => {
136973
137231
  if (!e18 || typeof e18 != "object" || n.has(e18))
136974
137232
  return;
136975
137233
  if (n.add(e18), e18.kind === "expand_model") {
136976
- forEachTreeNode(e18, (e19) => {
136977
- e19.extend !== undefined && t.push(e19);
136978
- });
137234
+ t.push(e18);
136979
137235
  return;
136980
137236
  }
136981
137237
  let i = e18;
@@ -136989,6 +137245,22 @@ function extendSlotsOf(e17) {
136989
137245
  };
136990
137246
  return r3(e17), t;
136991
137247
  }
137248
+ function joinBackKey(e17, t, n, r3) {
137249
+ let i = n.kind === Kind.IDENTIFIER ? refOrigin(n) : undefined;
137250
+ if (!i)
137251
+ return n;
137252
+ for (let a of scansOf(t)) {
137253
+ let t3;
137254
+ if (forEachTreeNode(a, (a3) => {
137255
+ if (t3 !== undefined || a3.extend === undefined)
137256
+ return;
137257
+ let s3 = extendDef(e17, a3.extend), c3 = s3.computed.find((e18) => e18.origin && originKey(e18.origin) === originKey(i));
137258
+ c3 && (t3 = !isCrossModelExtend(e17, a3) || readsPart(s3.parts, c3.expr) || r3.tryIndexOf(n) == null ? c3.expr : n);
137259
+ }), t3)
137260
+ return t3;
137261
+ }
137262
+ return n;
137263
+ }
136992
137264
  function derefExtend(e17, t, n) {
136993
137265
  if (n.kind !== Kind.IDENTIFIER)
136994
137266
  return n;
@@ -137239,6 +137511,12 @@ function baseTreeParts(e17, t) {
137239
137511
  t3.extend !== undefined && !isCrossModelExtend(e17, t3) && r3.push(...extendDef(e17, t3.extend).parts), t3 !== n && r3.push(...t3.parts ?? []);
137240
137512
  }), r3;
137241
137513
  }
137514
+ function expandTreeLodParts(e17, t) {
137515
+ let n = (t3) => t3.extend === undefined ? [] : extendDef(e17, t3.extend).parts, r3 = [];
137516
+ return walkTree(t, (i) => {
137517
+ i !== t && !isCrossModelExtend(e17, i) && r3.push(...n(i));
137518
+ }), [...t.extend === undefined ? [] : [...n(t), ...baseTreeParts(e17, extendDef(e17, t.extend))], ...r3];
137519
+ }
137242
137520
  function extendRootRelation(e17, t, n) {
137243
137521
  if (sealedSlot(e17, t))
137244
137522
  return lowerSealedSlot(e17, t, n);
@@ -137248,51 +137526,54 @@ function extendRootRelation(e17, t, n) {
137248
137526
  function lowerExtendRelation(e17, t, n) {
137249
137527
  return memoized("extendRelation", `${extendIdOf(t)}|${identityToken(n)}`, () => lowerExtendRelationUncached(e17, t, n));
137250
137528
  }
137529
+ function slotSubstitutes(e17, t, n) {
137530
+ let r3 = [];
137531
+ walkTree(t, (e18) => {
137532
+ e18 !== t && r3.push(e18);
137533
+ });
137534
+ let i = crossModelSubstitute(e17, r3.filter((t3) => isCrossModelExtend(e17, t3)), scopeModelsOf(t), n);
137535
+ for (let t3 of r3)
137536
+ sealedSlot(e17, t3) && i.set(t3.model, lowerSealedSlot(e17, t3, n));
137537
+ return i;
137538
+ }
137251
137539
  function lowerExtendRelationUncached(e17, t, n) {
137252
137540
  let r3 = extendDef(e17, extendIdOf(t)), i = r3.base;
137253
137541
  bareNode(i) && unsupported$1(`extend on '${t.model}' has no join tree`);
137254
- let a = [];
137255
- walkTree(i, (e18) => {
137256
- e18 !== i && a.push(e18);
137257
- });
137258
- let s3 = crossModelSubstitute(e17, a.filter((t3) => isCrossModelExtend(e17, t3)), scopeModelsOf(i), n);
137259
- for (let t3 of a)
137260
- sealedSlot(e17, t3) && s3.set(t3.model, lowerSealedSlot(e17, t3, n));
137261
- let c3 = i.model, l = /* @__PURE__ */ new Set([c3]), u = (t3, n3) => ({
137542
+ let a = slotSubstitutes(e17, i, n), s3 = i.model, c3 = /* @__PURE__ */ new Set([s3]), l = (t3, n3) => ({
137262
137543
  ...t3,
137263
137544
  children: t3.children.map((t4) => {
137264
137545
  let r4 = t4.input.model, i3 = [...n3, t4.edge];
137265
- if (!l.has(r4))
137266
- return l.add(r4), {
137546
+ if (!c3.has(r4))
137547
+ return c3.add(r4), {
137267
137548
  ...t4,
137268
- input: u(t4.input, i3)
137549
+ input: l(t4.input, i3)
137269
137550
  };
137270
137551
  let a3 = mintModelAlias(e17, r4, pathSig(i3));
137271
137552
  return {
137272
137553
  edge: retargetEdge(t4.edge, a3),
137273
- input: u(t4.input, i3)
137554
+ input: l(t4.input, i3)
137274
137555
  };
137275
137556
  })
137276
- }), f = buildJoinScope(e17, c3, undefined, expandEdges(u(i, [])), {
137557
+ }), u = buildJoinScope(e17, s3, undefined, expandEdges(l(i, [])), {
137277
137558
  rootRel: extendRootRelation(e17, i, n),
137278
- substitute: s3,
137559
+ substitute: a,
137279
137560
  tree: i
137280
- }), p3 = [...r3.parts, ...baseTreeParts(e17, r3)], m3 = f.relation, S3 = () => {
137561
+ }), f = [...r3.parts, ...baseTreeParts(e17, r3)], p3 = u.relation, m3 = () => {
137281
137562
  return;
137282
137563
  };
137283
- n && p3.length > 0 && ({ base: m3, partOf: S3 } = attachLodParts(n, e17, f, p3, f.relation, f.arity));
137284
- let w3 = inlineExtendComputed([...expandTreeComputedAll(e17, i), ...r3.computed]), E3 = projectedComputed(e17, t).filter((e18) => n != null && p3.length > 0 || !readsPart(p3, w3(e18.expr))), k3 = (t3) => {
137564
+ n && f.length > 0 && ({ base: p3, partOf: m3 } = attachLodParts(n, e17, u, f, u.relation, u.arity));
137565
+ let S3 = inlineExtendComputed([...expandTreeComputedAll(e17, i), ...r3.computed]), w3 = projectedComputed(e17, t).filter((e18) => n != null && f.length > 0 || !readsPart(f, S3(e18.expr))), E3 = (t3) => {
137285
137566
  let n3 = aliasedOrigin(e17, t3);
137286
- return n3 ? f.indexOfOrigin(n3) : S3(t3) ?? f.indexOf(t3);
137287
- }, N3 = E3.map((e18) => scalarToPlan(w3(e18.expr), k3)), I3 = E3.map((e18) => e18.origin);
137567
+ return n3 ? u.indexOfOrigin(n3) : m3(t3) ?? u.indexOf(t3);
137568
+ }, k3 = w3.map((e18) => scalarToPlan(S3(e18.expr), E3)), N3 = w3.map((e18) => e18.origin);
137288
137569
  return {
137289
- relation: N3.length > 0 ? cons({
137570
+ relation: k3.length > 0 ? cons({
137290
137571
  kind: "map",
137291
- input: m3,
137292
- scalars: N3,
137293
- origins: I3
137294
- }) : m3,
137295
- columns: [...relationType(m3), ...E3.map((e18) => ({
137572
+ input: p3,
137573
+ scalars: k3,
137574
+ origins: N3
137575
+ }) : p3,
137576
+ columns: [...relationType(p3), ...w3.map((e18) => ({
137296
137577
  type: sqlTypeOf(e18.ty),
137297
137578
  origin: e18.origin
137298
137579
  }))]
@@ -138041,45 +138322,34 @@ function lowerReduceUncached(e17, t, n) {
138041
138322
  });
138042
138323
  }
138043
138324
  return i.kind === Kind.IDENTIFIER ? buildJoinScope(t, i.name, refRowColumns(i), []) : unsupported$1(`reduce source kind '${i.kind}'`);
138044
- })(), w3 = (e18) => e18.extend === undefined ? [] : extendDef(t, e18.extend).parts, E3 = (e18) => e18.extend === undefined ? [] : [...w3(e18), ...baseTreeParts(t, extendDef(t, e18.extend))], k3 = (e18) => isCrossModelExtend(t, e18) ? [] : w3(e18), N3 = (() => {
138045
- if (i.kind !== "expand_model")
138046
- return [...s3];
138047
- let e18 = [];
138048
- return walkTree(i, (t3) => {
138049
- t3 !== i && e18.push(...k3(t3));
138050
- }), [
138051
- ...E3(i),
138052
- ...e18,
138053
- ...s3
138054
- ];
138055
- })(), I3 = inlineExtendComputed(scopeExtendComputedAll(t, i)), L3 = S3.arity, U3 = attachLodParts(e17, t, S3, N3, S3.relation, L3), pe3 = U3.base;
138056
- L3 = U3.arity;
138057
- let { partOf: me3 } = U3, Ze4 = i.kind === "expand_model" ? membershipBranches(i) : [], yr4 = Ze4.length > 0;
138058
- if (a.length > 0 && !yr4) {
138059
- let e18 = (e19) => me3(e19) ?? S3.indexOf(e19), n3 = (e19) => me3(e19) != null || S3.tryIndexOf(e19) != null;
138060
- pe3 = filterKeepingArity(pe3, a.map((r4) => scalarToPlan(inlineDeclDims(t, I3(r4), n3), e18)));
138061
- }
138062
- let oi3 = scopeExtendComputed(t, i), ci3 = (e18) => {
138325
+ })(), w3 = i.kind === "expand_model" ? [...expandTreeLodParts(t, i), ...s3] : [...s3], E3 = inlineExtendComputed(scopeExtendComputedAll(t, i)), k3 = S3.arity, N3 = attachLodParts(e17, t, S3, w3, S3.relation, k3), I3 = N3.base;
138326
+ k3 = N3.arity;
138327
+ let { partOf: L3 } = N3, U3 = i.kind === "expand_model" ? membershipBranches(i) : [], pe3 = U3.length > 0;
138328
+ if (a.length > 0 && !pe3) {
138329
+ let e18 = (e19) => L3(e19) ?? S3.indexOf(e19), n3 = (e19) => L3(e19) != null || S3.tryIndexOf(e19) != null;
138330
+ I3 = filterKeepingArity(I3, a.map((r4) => scalarToPlan(inlineDeclDims(t, E3(r4), n3), e18)));
138331
+ }
138332
+ let me3 = scopeExtendComputed(t, i), Ze4 = (e18) => {
138063
138333
  let n3 = aliasedOrigin(t, e18);
138064
138334
  return n3 ? S3.scope.tryResolveOrigin(n3) : undefined;
138065
- }, li3 = columnResolver((e18) => ci3(e18) ?? me3(e18) ?? S3.indexOf(e18), (e18) => ci3(e18) ?? me3(e18) ?? S3.tryResolve(e18)), di3 = oi3.map((e18) => scalarToPlan(I3(e18.expr), li3)), fi3 = oi3.map((e18) => e18.origin), pi3 = /* @__PURE__ */ new Map;
138066
- fi3.forEach((e18, t3) => pi3.set(originKey(e18), L3 + t3));
138067
- let mi3 = (e18) => {
138335
+ }, yr4 = columnResolver((e18) => Ze4(e18) ?? L3(e18) ?? S3.indexOf(e18), (e18) => Ze4(e18) ?? L3(e18) ?? S3.tryResolve(e18)), oi3 = me3.map((e18) => scalarToPlan(E3(e18.expr), yr4)), ci3 = me3.map((e18) => e18.origin), li3 = /* @__PURE__ */ new Map;
138336
+ ci3.forEach((e18, t3) => li3.set(originKey(e18), k3 + t3));
138337
+ let di3 = (e18) => {
138068
138338
  let t3 = e18;
138069
138339
  for (;t3.kind === Kind.NAMED_EXPRESSION; )
138070
138340
  t3 = t3.expr;
138071
138341
  let n3 = t3.kind === Kind.IDENTIFIER ? refOrigin(t3) : undefined;
138072
- return n3 ? pi3.get(originKey(n3)) : undefined;
138073
- }, hi3 = (e18) => me3(e18) ?? mi3(e18) ?? S3.indexOf(e18), gi3 = (e18) => me3(e18) ?? mi3(e18) ?? S3.tryIndexOf(e18), _i3 = L3 + di3.length, vi3 = relationType(S3.relation), yi3 = [], bi3 = [], xi3 = (e18) => {
138342
+ return n3 ? li3.get(originKey(n3)) : undefined;
138343
+ }, fi3 = (e18) => L3(e18) ?? di3(e18) ?? S3.indexOf(e18), pi3 = (e18) => L3(e18) ?? di3(e18) ?? S3.tryIndexOf(e18), mi3 = k3 + oi3.length, hi3 = relationType(S3.relation), gi3 = [], _i3 = [], vi3 = (e18) => {
138074
138344
  if (!e18 || !isSubtimeField(e18))
138075
138345
  return;
138076
138346
  let [t3] = e18.fieldName.split("->");
138077
138347
  if (S3.scope.tryResolveOrigin(createModelFieldReference(e18.modelName, t3)) == null)
138078
138348
  return S3.scope.tryResolveOrigin(e18);
138079
- }, Si2 = (e18) => e18.kind === Kind.IDENTIFIER ? me3(e18) != null : e18.kind === Kind.FUNCTION_CALL ? e18.args.some(Si2) : e18.kind === Kind.NAMED_EXPRESSION && Si2(e18.expr), Ci3 = (e18) => {
138080
- if (!Si2(e18)) {
138349
+ }, yi3 = (e18) => e18.kind === Kind.IDENTIFIER ? L3(e18) != null : e18.kind === Kind.FUNCTION_CALL ? e18.args.some(yi3) : e18.kind === Kind.NAMED_EXPRESSION && yi3(e18.expr), bi3 = (e18) => {
138350
+ if (!yi3(e18)) {
138081
138351
  let t3 = refOrigin(e18);
138082
- if (t3 && isSubtimeField(t3) && xi3(t3) != null)
138352
+ if (t3 && isSubtimeField(t3) && vi3(t3) != null)
138083
138353
  return {
138084
138354
  kind: Kind.IDENTIFIER,
138085
138355
  id: e18.id,
@@ -138090,40 +138360,40 @@ function lowerReduceUncached(e17, t, n) {
138090
138360
  }
138091
138361
  return e18.kind === Kind.FUNCTION_CALL ? {
138092
138362
  ...e18,
138093
- args: e18.args.map(Ci3)
138363
+ args: e18.args.map(bi3)
138094
138364
  } : e18.kind === Kind.NAMED_EXPRESSION ? {
138095
138365
  ...e18,
138096
- expr: Ci3(e18.expr)
138366
+ expr: bi3(e18.expr)
138097
138367
  } : e18;
138098
- }, wi3 = (e18) => {
138368
+ }, xi3 = (e18) => {
138099
138369
  let n3 = scopeExtendComputedAll(t, i).find((t3) => originKey(t3.origin) === originKey(e18));
138100
- return n3 ? scalarToPlan(I3(n3.expr), li3) : undefined;
138101
- }, Ti3 = r3.map((e18) => {
138102
- let t3 = gi3(e18);
138370
+ return n3 ? scalarToPlan(E3(n3.expr), yr4) : undefined;
138371
+ }, Si2 = r3.map((e18) => {
138372
+ let t3 = pi3(e18);
138103
138373
  if (t3 != null)
138104
138374
  return t3;
138105
- let n3 = refOrigin(e18), r4 = Si2(e18), i3 = r4 ? undefined : xi3(n3);
138375
+ let n3 = refOrigin(e18), r4 = yi3(e18), i3 = r4 ? undefined : vi3(n3);
138106
138376
  if (i3 == null && !r4) {
138107
138377
  let t4 = e18;
138108
138378
  for (;t4.kind === Kind.NAMED_EXPRESSION; )
138109
138379
  t4 = t4.expr;
138110
- t4 !== e18 && (i3 = xi3(refOrigin(t4)));
138380
+ t4 !== e18 && (i3 = vi3(refOrigin(t4)));
138111
138381
  }
138112
- return i3 ?? (!r4 && e18.kind === Kind.IDENTIFIER && n3 && isSubtimeField(n3) ? yi3.push(subtimeGrainScalar(S3, vi3, n3, wi3)) : r4 ? yi3.push(scalarToPlan(Ci3(I3(e18)), hi3)) : yi3.push(scalarToPlan(rewriteNestedSubtimes(I3(e18), (e19) => gi3(e19) != null), hi3)), bi3.push(n3), _i3 + yi3.length - 1);
138113
- }), Ei3 = [...di3, ...yi3], Di3 = [...fi3, ...bi3], Oi3 = attachSemiJoins(e17, t, Ei3.length > 0 ? cons({
138382
+ return i3 ?? (!r4 && e18.kind === Kind.IDENTIFIER && n3 && isSubtimeField(n3) ? gi3.push(subtimeGrainScalar(S3, hi3, n3, xi3)) : r4 ? gi3.push(scalarToPlan(bi3(E3(e18)), fi3)) : gi3.push(scalarToPlan(rewriteNestedSubtimes(E3(e18), (e19) => pi3(e19) != null), fi3)), _i3.push(n3), mi3 + gi3.length - 1);
138383
+ }), Ci3 = [...oi3, ...gi3], wi3 = [...ci3, ..._i3], Ti3 = attachSemiJoins(e17, t, Ci3.length > 0 ? cons({
138114
138384
  kind: "map",
138115
- input: pe3,
138116
- scalars: Ei3,
138117
- origins: Di3
138118
- }) : pe3, hi3, m3, (e18) => rewriteNestedSubtimes(I3(e18), (e19) => gi3(e19) != null)), ki4 = Ze4.reduce((n3, r4) => joinExistenceFilter(e17, t, n3, S3, r4), Oi3);
138385
+ input: I3,
138386
+ scalars: Ci3,
138387
+ origins: wi3
138388
+ }) : I3, fi3, m3, (e18) => rewriteNestedSubtimes(E3(e18), (e19) => pi3(e19) != null)), Ei3 = U3.reduce((n3, r4) => joinExistenceFilter(e17, t, n3, S3, r4), Ti3);
138119
138389
  return {
138120
138390
  reduce: cons({
138121
138391
  kind: "reduce",
138122
- input: a.length > 0 && yr4 ? filterKeepingArity(ki4, a.map((e18) => scalarToPlan(inlineDeclDims(t, I3(e18), (e19) => gi3(e19) != null), hi3))) : ki4,
138123
- groupKey: Ti3,
138392
+ input: a.length > 0 && pe3 ? filterKeepingArity(Ei3, a.map((e18) => scalarToPlan(inlineDeclDims(t, E3(e18), (e19) => pi3(e19) != null), fi3))) : Ei3,
138393
+ groupKey: Si2,
138124
138394
  aggregates: c3.map((e18) => ({
138125
138395
  func: e18.name.name,
138126
- args: e18.args.length > 0 ? e18.args.map((e19) => scalarToPlan(e19, hi3)) : [{
138396
+ args: e18.args.length > 0 ? e18.args.map((e19) => scalarToPlan(e19, fi3)) : [{
138127
138397
  kind: "literal",
138128
138398
  literal: "number",
138129
138399
  value: "1",
@@ -138142,7 +138412,7 @@ function lowerReduceUncached(e17, t, n) {
138142
138412
  annotations: n.annotations,
138143
138413
  ...n.distinct ? { preferDistinct: true } : {}
138144
138414
  }),
138145
- grainLen: Ti3.length
138415
+ grainLen: Si2.length
138146
138416
  };
138147
138417
  }
138148
138418
  function lowerWindow(e17, t, n) {
@@ -138428,7 +138698,8 @@ function lowerTransitionCoalesce(e17, t, n) {
138428
138698
  measureAt: (t4) => e18.get(t4),
138429
138699
  measures: k3.map((e19) => ({
138430
138700
  name: e19.name,
138431
- pos: e19.abs
138701
+ pos: e19.abs,
138702
+ alias: e19.alias
138432
138703
  }))
138433
138704
  };
138434
138705
  }
@@ -138458,7 +138729,8 @@ function lowerTransitionCoalesce(e17, t, n) {
138458
138729
  measureAt: (e19) => t3.get(e19),
138459
138730
  measures: k3.map((e19) => ({
138460
138731
  name: e19.name,
138461
- pos: e19.abs
138732
+ pos: e19.abs,
138733
+ alias: e19.alias
138462
138734
  })),
138463
138735
  nullMeasureFilter: true
138464
138736
  };
@@ -138531,7 +138803,8 @@ function lowerTransitionCoalesce(e17, t, n) {
138531
138803
  measureAt: (e18) => Ze4.get(e18),
138532
138804
  measures: k3.map((e18, t3) => ({
138533
138805
  name: e18.name,
138534
- pos: f + t3
138806
+ pos: f + t3,
138807
+ alias: e18.alias
138535
138808
  })),
138536
138809
  nullMeasureFilter: true
138537
138810
  };
@@ -138743,54 +139016,52 @@ function lowerSelect(e17, t, n) {
138743
139016
  }
138744
139017
  if (r3.kind !== Kind.IDENTIFIER && r3.kind !== "expand_model")
138745
139018
  return lowerSelectOver(n, lowerTop(e17, t, r3, originLeafRefs(n.projection)));
138746
- let a = r3.kind === "expand_model" && r3.extend !== undefined && !isCrossModelExtend(t, r3) ? r3 : undefined, { projection: s3 } = n, c3 = i, l, u;
138747
- r3.kind === "expand_model" ? (l = r3.model, u = undefined) : (l = refRootModel(r3) ?? r3.name, u = refRowColumns(r3));
138748
- let f = r3.kind === "expand_model" && isCrossModelExtend(t, r3) ? lowerExtendRelation(t, r3) : undefined, p3 = (() => {
139019
+ let { projection: a } = n, s3 = i, c3, l;
139020
+ r3.kind === "expand_model" ? (c3 = r3.model, l = undefined) : (c3 = refRootModel(r3) ?? r3.name, l = refRowColumns(r3));
139021
+ let u = r3.kind === "expand_model" && isCrossModelExtend(t, r3) ? lowerExtendRelation(t, r3) : undefined, f = (() => {
138749
139022
  if (r3.kind !== "expand_model")
138750
- return buildJoinScope(t, l, u, [], { rootRel: f });
139023
+ return buildJoinScope(t, c3, l, [], { rootRel: u });
138751
139024
  let { edges: n3, fanouts: i3 } = partitionFanoutExpansions(t, r3, e17), a3 = [];
138752
139025
  walkTree(r3, (e18) => {
138753
139026
  e18 !== r3 && a3.push(e18);
138754
139027
  });
138755
139028
  let s4 = crossModelSubstitute(t, a3.filter((e18) => isCrossModelExtend(t, e18)), scopeModelsOf(r3), e17);
138756
- return buildJoinScope(t, l, u, n3, {
138757
- rootRel: f,
139029
+ return buildJoinScope(t, c3, l, n3, {
139030
+ rootRel: u,
138758
139031
  fanouts: i3,
138759
139032
  substitute: s4,
138760
139033
  tree: r3
138761
139034
  });
138762
- })(), m3 = scopeExtendComputed(t, r3), S3 = inlineExtendComputed(scopeExtendComputedAll(t, r3)), { scalars: w3, tableConds: E3 } = splitTableConditions(c3), k3 = w3.length > 0 ? filterKeepingArity(p3.relation, w3.map((e18) => scalarToPlan(S3(e18), p3.indexOf))) : p3.relation;
138763
- k3 = attachSemiJoins(e17, t, k3, p3.indexOf, E3, S3), r3.kind === "expand_model" && (k3 = membershipBranches(r3).reduce((n3, r4) => joinExistenceFilter(e17, t, n3, p3, r4), k3));
138764
- let N3 = arity(k3), I3 = attachLodParts(e17, t, p3, a?.extend === undefined ? [] : extendDef(t, a.extend).parts, k3, N3);
138765
- ({ base: k3 } = I3), N3 = I3.arity;
138766
- let { partOf: L3 } = I3, U3 = (e18) => {
139035
+ })(), p3 = scopeExtendComputed(t, r3), m3 = inlineExtendComputed(scopeExtendComputedAll(t, r3)), S3 = attachLodParts(e17, t, f, r3.kind === "expand_model" ? expandTreeLodParts(t, r3) : [], f.relation, arity(f.relation)), { partOf: w3 } = S3, { scalars: E3, tableConds: k3 } = splitTableConditions(s3), N3 = (e18) => w3(e18) ?? f.indexOf(e18), I3 = E3.length > 0 ? filterKeepingArity(S3.base, E3.map((e18) => scalarToPlan(m3(e18), N3))) : S3.base;
139036
+ I3 = attachSemiJoins(e17, t, I3, N3, k3, m3), r3.kind === "expand_model" && (I3 = membershipBranches(r3).reduce((n3, r4) => joinExistenceFilter(e17, t, n3, f, r4), I3));
139037
+ let L3 = arity(I3), U3 = (e18) => {
138767
139038
  let n3 = aliasedOrigin(t, e18);
138768
139039
  if (n3) {
138769
- let e19 = p3.scope.tryResolveOrigin(n3);
139040
+ let e19 = f.scope.tryResolveOrigin(n3);
138770
139041
  if (e19 != null)
138771
139042
  return e19;
138772
139043
  }
138773
- return L3(e18) ?? p3.indexOf(e18);
138774
- }, pe3 = m3.map((e18) => scalarToPlan(S3(e18.expr), U3)), me3 = m3.map((e18) => e18.origin), Ze4 = /* @__PURE__ */ new Map;
138775
- me3.forEach((e18, t3) => Ze4.set(originKey(e18), N3 + t3));
139044
+ return w3(e18) ?? f.indexOf(e18);
139045
+ }, pe3 = p3.map((e18) => scalarToPlan(m3(e18.expr), U3)), me3 = p3.map((e18) => e18.origin), Ze4 = /* @__PURE__ */ new Map;
139046
+ me3.forEach((e18, t3) => Ze4.set(originKey(e18), L3 + t3));
138776
139047
  let yr4 = (e18) => {
138777
- let t3 = L3(e18);
139048
+ let t3 = w3(e18);
138778
139049
  if (t3 != null)
138779
139050
  return t3;
138780
139051
  let n3 = e18.kind === Kind.IDENTIFIER ? refOrigin(e18) : undefined;
138781
- return (n3 ? Ze4.get(originKey(n3)) : undefined) ?? p3.indexOf(e18);
139052
+ return (n3 ? Ze4.get(originKey(n3)) : undefined) ?? f.indexOf(e18);
138782
139053
  }, oi3 = [...pe3], ci3 = [...me3], li3 = [], di3 = [], fi3 = (e18, t3, n3) => {
138783
139054
  di3.push(e18), li3.push({
138784
139055
  name: t3,
138785
139056
  origin: n3
138786
139057
  });
138787
139058
  }, pi3 = (e18, t3, n3) => {
138788
- oi3.push(e18), ci3.push(n3), di3.push(N3 + oi3.length - 1), li3.push({
139059
+ oi3.push(e18), ci3.push(n3), di3.push(L3 + oi3.length - 1), li3.push({
138789
139060
  name: t3,
138790
139061
  origin: n3
138791
139062
  });
138792
139063
  };
138793
- return s3.forEach((e18, t3) => {
139064
+ return a.forEach((e18, t3) => {
138794
139065
  if (e18.kind === Kind.NAMED_EXPRESSION) {
138795
139066
  let { name: t4 } = e18.name, n3 = refOrigin(e18) ?? refOrigin(e18.expr);
138796
139067
  e18.expr.kind === Kind.IDENTIFIER && isColumnRef(e18.expr) ? fi3(yr4(e18.expr), t4, n3) : pi3(scalarToPlan(e18.expr, yr4), t4, n3);
@@ -138801,10 +139072,10 @@ function lowerSelect(e17, t, n) {
138801
139072
  kind: "project",
138802
139073
  input: oi3.length > 0 ? cons({
138803
139074
  kind: "map",
138804
- input: k3,
139075
+ input: I3,
138805
139076
  scalars: oi3,
138806
139077
  origins: ci3.some((e18) => e18) ? ci3 : undefined
138807
- }) : k3,
139078
+ }) : I3,
138808
139079
  outputs: di3
138809
139080
  }),
138810
139081
  outputs: li3
@@ -143306,9 +143577,11 @@ function collectJoins(e17, t = [], n = /* @__PURE__ */ new Set) {
143306
143577
  purpose: JoinPurpose.EXPANSION
143307
143578
  });
143308
143579
  } else {
143309
- let n3 = onLeftModel(e17.on) ?? fromRootAlias(e17.left);
143580
+ let n3 = /* @__PURE__ */ new Map;
143581
+ collectFieldRefs(e17.on, n3);
143582
+ let i = [...n3.keys()].filter((e18) => e18 !== r3);
143310
143583
  t.push({
143311
- sourceTable: [n3],
143584
+ sourceTable: i.length > 0 ? i : [fromRootAlias(e17.left)],
143312
143585
  destTable: r3,
143313
143586
  joinType: JOIN_TYPE[e17.joinType],
143314
143587
  joinOn: e17.on,
@@ -143335,17 +143608,6 @@ function toPlaceholderJoinOn(e17, t, n, r3) {
143335
143608
  operands: e17.operands.map((e18) => toPlaceholderJoinOn(e18, t, n, r3))
143336
143609
  };
143337
143610
  }
143338
- function onLeftModel(e17) {
143339
- if (typeof e17 != "string") {
143340
- if (e17.operator === OperationType.AND)
143341
- return onLeftModel(e17.operands[0]);
143342
- if (e17.operator === OperationType.EQ) {
143343
- let t = e17.operands[0];
143344
- if (typeof t != "string" && t.operator === OperationType.FIELD && typeof t.operands[0] == "string")
143345
- return t.operands[0];
143346
- }
143347
- }
143348
- }
143349
143611
  function collectJoinOnRefs(e17, t) {
143350
143612
  e17.kind === "join" ? (e17.on && collectFieldRefs(e17.on, t), collectJoinOnRefs(e17.left, t), collectJoinOnRefs(e17.right, t)) : e17.kind === "subquery" && e17.select;
143351
143613
  }
@@ -144863,18 +145125,23 @@ function getDiagnosticsV2(e17, { store: t, userAttributes: n = [], checkFlags: r
144863
145125
  typeCheckOnly: true
144864
145126
  }).diagnostics;
144865
145127
  }
144866
- function _compile(e17, { referenceTime: t = /* @__PURE__ */ new Date, timezoneRegion: n, databaseTimezone: r3 = "Etc/UTC", fallbackValueTimezoneRegion: i = "Etc/UTC", valueDatetimeFormat: a = "%FT%T.%L%:z", weekStartDay: s3, sqlGenerationFlags: c3, adhocFields: l = [], userAttributes: u, rowLevelPermissions: f, strictUserPermissionCheck: p3 = false, shouldDecomposeFanoutAggregate: m3 = false, calendarRange: S3 = [new Date(1900, 0, 1), new Date(3001, 0, 1)], useV2Compiler: w3 = false, store: E3, globals: k3 = createAmqlGlobals(), checkFlags: N3 = CheckFlags.Normal }) {
144867
- let I3 = parse(e17, "<main>"), L3 = parseAdhocFields(l);
144868
- if (c3?.shouldPrioritizeLengthForMixedPaths && (E3.modelGraph.shouldPrioritizeLengthForMixedPaths = true), w3) {
145128
+ function _compile(e17, { referenceTime: t = /* @__PURE__ */ new Date, timezoneRegion: n, databaseTimezone: r3 = "Etc/UTC", fallbackValueTimezoneRegion: i = "Etc/UTC", valueDatetimeFormat: a = "%FT%T.%L%:z", weekStartDay: s3, sqlGenerationFlags: c3, adhocFields: l = [], userAttributes: u, rowLevelPermissions: f, strictUserPermissionCheck: p3 = false, shouldDecomposeFanoutAggregate: m3 = false, calendarRange: S3 = [new Date(1900, 0, 1), new Date(3001, 0, 1)], useV2Compiler: w3 = false, allowAdhocSqlPassthroughFunctions: E3 = true, store: k3, globals: N3 = createAmqlGlobals(), checkFlags: I3 = CheckFlags.Normal }) {
145129
+ let L3 = parse(e17, "<main>"), U3 = parseAdhocFields(l);
145130
+ if (!E3) {
145131
+ let e18 = checkAdhocSqlPassthrough([L3, ...U3]);
145132
+ if (e18.length > 0)
145133
+ throw createAqlTypecheckError(e18);
145134
+ }
145135
+ if (c3?.shouldPrioritizeLengthForMixedPaths && (k3.modelGraph.shouldPrioritizeLengthForMixedPaths = true), w3) {
144869
145136
  let e18 = resolve({
144870
- program: I3,
144871
- store: E3,
144872
- adhocFields: L3,
145137
+ program: L3,
145138
+ store: k3,
145139
+ adhocFields: U3,
144873
145140
  recordSpanTypes: true,
144874
145141
  userAttributes: u,
144875
145142
  rowLevelPermissions: f,
144876
145143
  strictUserPermissionCheck: p3,
144877
- checkFlags: N3,
145144
+ checkFlags: I3,
144878
145145
  sqlGenerationFlags: c3,
144879
145146
  datetime: {
144880
145147
  timezoneRegion: n,
@@ -144895,14 +145162,14 @@ function _compile(e17, { referenceTime: t = /* @__PURE__ */ new Date, timezoneRe
144895
145162
  sqlGenerationFlags: c3
144896
145163
  });
144897
145164
  return {
144898
- expr: I3,
145165
+ expr: L3,
144899
145166
  explore: m4,
144900
145167
  context: w4,
144901
145168
  json: exploreToJSON(w4, m4),
144902
145169
  spanTypes: e18.spanTypes
144903
145170
  };
144904
145171
  }
144905
- E3.setModel(buildCalendarModel({
145172
+ k3.setModel(buildCalendarModel({
144906
145173
  calendarRange: S3,
144907
145174
  referenceTime: t,
144908
145175
  timezoneRegion: n,
@@ -144910,11 +145177,11 @@ function _compile(e17, { referenceTime: t = /* @__PURE__ */ new Date, timezoneRe
144910
145177
  fallbackValueTimezoneRegion: i,
144911
145178
  valueDatetimeFormat: a,
144912
145179
  weekStartDay: s3
144913
- }, E3.dataSource));
144914
- let U3 = createTypeChecker(E3, u, L3, f, N3), pe3 = U3.getDiagnostics(I3);
144915
- if (pe3.length > 0)
144916
- throw createAqlTypecheckError(pe3);
144917
- return compileAst(I3, {
145180
+ }, k3.dataSource));
145181
+ let pe3 = createTypeChecker(k3, u, U3, f, I3), me3 = pe3.getDiagnostics(L3);
145182
+ if (me3.length > 0)
145183
+ throw createAqlTypecheckError(me3);
145184
+ return compileAst(L3, {
144918
145185
  referenceTime: t,
144919
145186
  timezoneRegion: n,
144920
145187
  databaseTimezone: r3,
@@ -144922,15 +145189,15 @@ function _compile(e17, { referenceTime: t = /* @__PURE__ */ new Date, timezoneRe
144922
145189
  valueDatetimeFormat: a,
144923
145190
  weekStartDay: s3,
144924
145191
  sqlGenerationFlags: c3,
144925
- adhocFieldDeclarations: L3,
145192
+ adhocFieldDeclarations: U3,
144926
145193
  userAttributes: u,
144927
145194
  rowLevelPermissions: f,
144928
145195
  strictUserPermissionCheck: p3,
144929
145196
  shouldDecomposeFanoutAggregate: m3,
144930
145197
  calendarRange: S3,
144931
- store: E3,
144932
- globals: k3,
144933
- checker: U3
145198
+ store: k3,
145199
+ globals: N3,
145200
+ checker: pe3
144934
145201
  });
144935
145202
  }
144936
145203
  var compile = Rr3.wrapFuncWithinActiveSpan(_compile);
@@ -146582,7 +146849,7 @@ function _transform(e17, t) {
146582
146849
  });
146583
146850
  }
146584
146851
  function wi3(e18, t3, r4, i3) {
146585
- let a3 = createFunctionCallExpression(createIdentifier("select"), [t3, r4], e18.loc), s4 = i3.find((e19) => e19.name.value === "sep")?.expr ?? createTextLiteral(","), c4 = i3.find((e19) => e19.name.value === "order")?.expr ?? createTextLiteral("ASC"), l3 = i3.find((e19) => e19.name.value === "distinct")?.expr ?? createTruefalseLiteral(false), u3 = getSqlFromRowLevelObject(n, getRowLevelObjectFromExpression(n, getTextLiteralFromExpression(n, s4), false)), f3 = import_lodash2.toUpper(c4.value), p4 = l3.value ? "DISTINCT" : "", m4 = computeAggregationKey(n, "string_agg", a3, `sep: ${stableStringify(u3)}, order: ${f3}, distinctOp: ${p4}`);
146852
+ let a3 = createFunctionCallExpression(createIdentifier("select"), [t3, createNamedExpression(createIdentifier("_f1"), r4)], e18.loc), s4 = i3.find((e19) => e19.name.value === "sep")?.expr ?? createTextLiteral(","), c4 = i3.find((e19) => e19.name.value === "order")?.expr ?? createTextLiteral("ASC"), l3 = i3.find((e19) => e19.name.value === "distinct")?.expr ?? createTruefalseLiteral(false), u3 = getSqlFromRowLevelObject(n, getRowLevelObjectFromExpression(n, getTextLiteralFromExpression(n, s4), false)), f3 = import_lodash2.toUpper(c4.value), p4 = l3.value ? "DISTINCT" : "", m4 = computeAggregationKey(n, "string_agg", a3, `sep: ${stableStringify(u3)}, order: ${f3}, distinctOp: ${p4}`);
146586
146853
  return aggregateOnTable(n, e18, a3, m4, (e19, t4) => {
146587
146854
  let n3 = t4 ? createOperation(OperationType.FIELD, ["SOURCE", e19.fields[e19.fields.length - 1].name]) : e19.fields[e19.fields.length - 1].sql;
146588
146855
  return {