Skip to content

Commit f6df697

Browse files
ZetaSQL Teamahirschberg-corp-oss
authored andcommitted
Export of internal ZetaSQL changes.
-- Change by ZetaSQL Team <no-reply@google.com>: Add "alwayslink=1" to the target compliance:sql_test_base. -- Change by ZetaSQL Team <no-reply@google.com>: Docs for JSON_KEYS function. -- Change by ZetaSQL Team <no-reply@google.com>: Clarify that INT64 is converted to a string in JavaScript -- Change by ZetaSQL Team <no-reply@google.com>: No public description -- Change by ZetaSQL Team <no-reply@google.com>: No public description -- Change by ZetaSQL Team <no-reply@google.com>: CL to regenerate artifacts. -- Change by ZetaSQL Team <no-reply@google.com>: Add MAP_VALUES_SORTED_BY_KEY function -- Change by ZetaSQL Team <no-reply@google.com>: No public description -- Change by ZetaSQL Team <no-reply@google.com>: No public description -- Change by ZetaSQL Team <no-reply@google.com>: No public description -- Change by ZetaSQL Team <no-reply@google.com>: Add map_keys_sorted, map_keys_unsorted, map_values_sorted, and map_values_unsorted functions. -- Change by ZetaSQL Team <no-reply@google.com>: Update ZetaSQL docs GitOrigin-RevId: d6002ecc05e21d26aef5686de0d2188ff622e244 Change-Id: Ifd9836e733968d7cf6ac4f17d94d3748cd6e95fe
1 parent 46aa917 commit f6df697

38 files changed

Lines changed: 1347 additions & 153 deletions

docs/functions-and-operators.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18862,6 +18862,7 @@ behavior:
1886218862
<td><a id="accessors"></a>Accessors</td>
1886318863
<td>
1886418864

18865+
1886518866
<a href="#json_type"><code>JSON_TYPE</code></a><br>
1886618867

1886718868
</td>
@@ -25324,7 +25325,7 @@ PARSE_JSON(json_string_expr[, wide_number_mode=>{ 'exact' | 'round' }])
2532425325

2532525326
**Description**
2532625327

25327-
Converts a JSON-formatted `STRING` value to a `JSON` value.
25328+
Converts a JSON-formatted `STRING` value to a [`JSON` value](https://www.json.org/json-en.html).
2532825329

2532925330
Arguments:
2533025331

@@ -25395,6 +25396,28 @@ SELECT PARSE_JSON('{"id": 922337203685477580701}', wide_number_mode=>'round') AS
2539525396
*------------------------------*/
2539625397
```
2539725398

25399+
You can also use valid JSON-formatted strings that don't represent name/value pairs. For example:
25400+
25401+
```sql
25402+
SELECT PARSE_JSON('6') AS json_data;
25403+
25404+
/*------------------------------*
25405+
| json_data |
25406+
+------------------------------+
25407+
| 6 |
25408+
*------------------------------*/
25409+
```
25410+
25411+
```sql
25412+
SELECT PARSE_JSON('"red"') AS json_data;
25413+
25414+
/*------------------------------*
25415+
| json_data |
25416+
+------------------------------+
25417+
| "red" |
25418+
*------------------------------*/
25419+
```
25420+
2539825421
### `STRING`
2539925422
<a id="string_for_json"></a>
2540025423

docs/json_functions.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ behavior:
249249
<td><a id="accessors"></a>Accessors</td>
250250
<td>
251251
252+
252253
<a href="#json_type"><code>JSON_TYPE</code></a><br>
253254
254255
</td>
@@ -6711,7 +6712,7 @@ PARSE_JSON(json_string_expr[, wide_number_mode=>{ 'exact' | 'round' }])
67116712

67126713
**Description**
67136714

6714-
Converts a JSON-formatted `STRING` value to a `JSON` value.
6715+
Converts a JSON-formatted `STRING` value to a [`JSON` value](https://www.json.org/json-en.html).
67156716

67166717
Arguments:
67176718

@@ -6782,6 +6783,28 @@ SELECT PARSE_JSON('{"id": 922337203685477580701}', wide_number_mode=>'round') AS
67826783
*------------------------------*/
67836784
```
67846785

6786+
You can also use valid JSON-formatted strings that don't represent name/value pairs. For example:
6787+
6788+
```sql
6789+
SELECT PARSE_JSON('6') AS json_data;
6790+
6791+
/*------------------------------*
6792+
| json_data |
6793+
+------------------------------+
6794+
| 6 |
6795+
*------------------------------*/
6796+
```
6797+
6798+
```sql
6799+
SELECT PARSE_JSON('"red"') AS json_data;
6800+
6801+
/*------------------------------*
6802+
| json_data |
6803+
+------------------------------+
6804+
| "red" |
6805+
*------------------------------*/
6806+
```
6807+
67856808
### `STRING`
67866809
<a id="string_for_json"></a>
67876810

docs/resolved_ast.md

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ See that file for comments on specific nodes and fields.
164164
<a href="#ResolvedDifferentialPrivacyAggregateScan">ResolvedDifferentialPrivacyAggregateScan</a>
165165
<a href="#ResolvedAnalyticScan">ResolvedAnalyticScan</a>
166166
<a href="#ResolvedArrayScan">ResolvedArrayScan</a>
167+
<a href="#ResolvedBarrierScan">ResolvedBarrierScan</a>
167168
<a href="#ResolvedExecuteAsRoleScan">ResolvedExecuteAsRoleScan</a>
168169
<a href="#ResolvedFilterScan">ResolvedFilterScan</a>
169170
<a href="#ResolvedGroupRowsScan">ResolvedGroupRowsScan</a>
@@ -739,8 +740,28 @@ class ResolvedNonScalarFunctionCallBase : public <a href="#ResolvedFunctionCallB
739740

740741
<p><pre><code class="lang-c++">
741742
<font color="brown">// An aggregate function call. The signature always has mode AGGREGATE.
742-
// This node only ever shows up as the outer function call in a
743-
// ResolvedAggregateScan::aggregate_list.</font>
743+
//
744+
// FEATURE_V_1_4_MULTILEVEL_AGGREGATION enables multi-level aggregate
745+
// expressions (e.g. &#39;SUM(AVG(1 + X) GROUP BY key)&#39; ). The GROUP BY modifier
746+
// within an aggregate function body indicates the presence of a multi-level
747+
// aggregate expression.
748+
//
749+
// `group_by_aggregate_list` can only be present if `group_by_list` is
750+
// present. `group_by_list` and `group_by_aggregate_list` are mutually
751+
// exclusive with `having_modifier`.
752+
//
753+
// If `group_by_list` is empty, then standard column visibility rules apply
754+
// (i.e. columns supplied by input scan to the enclosing AggregateScan are
755+
// visible to argument expressions and aggregate function modifiers, as are
756+
// correlated columns).
757+
//
758+
// If `group_by_list` is non-empty, the initial aggregation is applied first,
759+
// computing the aggregate and grouping columns in `group_by_aggregate_list`
760+
// `group_by_list`. Only these computed columns (plus correlated columns)
761+
// are visible to argument expressions and aggregate function modifiers
762+
// (e.g. DISTINCT, IGNORE / RESPECT NULLS, LIMIT, ORDER BY). These
763+
// modifiers are applied on the output rows from the initial aggregation,
764+
// as input to the final aggregation.</font>
744765
class ResolvedAggregateFunctionCall : public <a href="#ResolvedNonScalarFunctionCallBase">ResolvedNonScalarFunctionCallBase</a> {
745766
static const ResolvedNodeKind TYPE = RESOLVED_AGGREGATE_FUNCTION_CALL;
746767

@@ -768,6 +789,21 @@ class ResolvedAggregateFunctionCall : public <a href="#ResolvedNonScalarFunction
768789
// fully-resolved function body in context of the actual concrete
769790
// types of the arguments provided to the function call.</font>
770791
const std::shared_ptr&lt;<a href="#ResolvedFunctionCallInfo">ResolvedFunctionCallInfo</a>&gt;&amp; function_call_info() const;
792+
793+
<font color="brown"> // Group the stream of input values by columns in this list, and
794+
// compute the aggregates defined in `group_by_aggregate_list`.
795+
// Used only for multi-level aggregation, when
796+
// FEATURE_V_1_4_MULTILEVEL_AGGREGATION is enabled.</font>
797+
const std::vector&lt;std::unique_ptr&lt;const <a href="#ResolvedComputedColumnBase">ResolvedComputedColumnBase</a>&gt;&gt;&amp; group_by_list() const;
798+
int group_by_list_size() const;
799+
const <a href="#ResolvedComputedColumnBase">ResolvedComputedColumnBase</a>* group_by_list(int i) const;
800+
801+
<font color="brown"> // Aggregate columns to compute over the grouping keys defined in
802+
// `group_by_list`. Used only for multi-level aggregation, when
803+
// FEATURE_V_1_4_MULTILEVEL_AGGREGATION is enabled.</font>
804+
const std::vector&lt;std::unique_ptr&lt;const <a href="#ResolvedComputedColumnBase">ResolvedComputedColumnBase</a>&gt;&gt;&amp; group_by_aggregate_list() const;
805+
int group_by_aggregate_list_size() const;
806+
const <a href="#ResolvedComputedColumnBase">ResolvedComputedColumnBase</a>* group_by_aggregate_list(int i) const;
771807
};
772808
</code></pre></p>
773809

@@ -3167,7 +3203,7 @@ class ResolvedCreateIndexStmt : public <a href="#ResolvedCreateStatement">Resolv
31673203
// [OPTIONS (name=value, ...)]
31683204
//
31693205
// CREATE [OR REPLACE] [TEMP|TEMPORARY|PUBLIC|PRIVATE] EXTERNAL SCHEMA
3170-
// [IF NOT EXISTS] &lt;name&gt; WITH CONNECTION &lt;connection&gt;
3206+
// [IF NOT EXISTS] &lt;name&gt; [WITH CONNECTION] &lt;connection&gt;
31713207
// OPTIONS (name=value, ...)
31723208
//
31733209
// &lt;option_list&gt; contains engine-specific options associated with the schema</font>
@@ -3209,7 +3245,7 @@ class ResolvedCreateSchemaStmt : public <a href="#ResolvedCreateSchemaStmtBase">
32093245
<p><pre><code class="lang-c++">
32103246
<font color="brown">// This statement:
32113247
// CREATE [OR REPLACE] [TEMP|TEMPORARY|PUBLIC|PRIVATE] EXTERNAL SCHEMA
3212-
// [IF NOT EXISTS] &lt;name&gt; WITH CONNECTION &lt;connection&gt;
3248+
// [IF NOT EXISTS] &lt;name&gt; [WITH CONNECTION] &lt;connection&gt;
32133249
// OPTIONS (name=value, ...)
32143250
//
32153251
// &lt;connection&gt; encapsulates engine-specific metadata used to connect
@@ -4457,9 +4493,9 @@ class ResolvedAnalyticFunctionGroup : public <a href="#ResolvedArgument">Resolve
44574493

44584494
const <a href="#ResolvedWindowOrdering">ResolvedWindowOrdering</a>* order_by() const;
44594495

4460-
const std::vector&lt;std::unique_ptr&lt;const <a href="#ResolvedComputedColumn">ResolvedComputedColumn</a>&gt;&gt;&amp; analytic_function_list() const;
4496+
const std::vector&lt;std::unique_ptr&lt;const <a href="#ResolvedComputedColumnBase">ResolvedComputedColumnBase</a>&gt;&gt;&amp; analytic_function_list() const;
44614497
int analytic_function_list_size() const;
4462-
const <a href="#ResolvedComputedColumn">ResolvedComputedColumn</a>* analytic_function_list(int i) const;
4498+
const <a href="#ResolvedComputedColumnBase">ResolvedComputedColumnBase</a>* analytic_function_list(int i) const;
44634499
};
44644500
</code></pre></p>
44654501

@@ -5230,7 +5266,7 @@ class ResolvedPrivilege : public <a href="#ResolvedArgument">ResolvedArgument</a
52305266
<font color="brown">// Common superclass of GRANT/REVOKE statements.
52315267
//
52325268
// &lt;privilege_list&gt; is the list of privileges to be granted/revoked. ALL
5233-
// PRIVILEGES should be granted/fromed if it is empty.
5269+
// PRIVILEGES should be granted/revoked if it is empty.
52345270
// &lt;object_type_list&gt; is an optional list of string identifiers, e.g., TABLE,
52355271
// VIEW, MATERIALIZED VIEW.
52365272
// &lt;name_path&gt; is a vector of segments of the object identifier&#39;s pathname.
@@ -7623,3 +7659,27 @@ class ResolvedIdentityColumnInfo : public <a href="#ResolvedArgument">ResolvedAr
76237659
};
76247660
</code></pre></p>
76257661

7662+
### ResolvedBarrierScan
7663+
<a id="ResolvedBarrierScan"></a>
7664+
7665+
<p><pre><code class="lang-c++">
7666+
<font color="brown">// ResolvedBarrierScan marks an optimization barrier during query planning.
7667+
// It wraps an `input_scan` and ensures `input_scan` is evaluated as if
7668+
// `input_scan` stands alone; plan transformations that may cause
7669+
// different observable side effects may not cross the optimization barrier.
7670+
//
7671+
// The output rows of a ResolvedBarrierScan are the same as those of the
7672+
// `input_scan`, propagating the `is_ordered` property of `input_scan`.
7673+
//
7674+
// The following optimizations are allowed:
7675+
// * Prune an unused column of a ResolvedBarrierScan.
7676+
// * Prune the whole ResolvedBarrierScan.
7677+
//
7678+
// This node does not have a corresponding syntax.</font>
7679+
class ResolvedBarrierScan : public <a href="#ResolvedScan">ResolvedScan</a> {
7680+
static const ResolvedNodeKind TYPE = RESOLVED_BARRIER_SCAN;
7681+
7682+
const <a href="#ResolvedScan">ResolvedScan</a>* input_scan() const;
7683+
};
7684+
</code></pre></p>
7685+

docs/user-defined-functions.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,20 +275,20 @@ see [Modules][modules].
275275

276276
### Call a JavaScript UDF
277277

278-
You can call a JavaScript UDF in the same way that you call a built-in
278+
You can call a JavaScript UDF the same way that you call a built-in
279279
function. For details, see [Function calls][function-calls].
280280

281281
### SQL type encodings in JavaScript
282282
<a id="javascript_udf_data_types"></a>
283283

284284
[ZetaSQL data types][data-types] represent
285-
[JavaScript data types][javascript-types] in the following manner:
285+
[JavaScript data types][javascript-types] as follows:
286286

287287
<table>
288288
<thead>
289289
<tr>
290-
<th>ZetaSQL<br/> Data Type</th>
291-
<th>JavaScript<br/> Data Type</th>
290+
<th>ZetaSQL<br/> data type</th>
291+
<th>JavaScript<br/> data type</th>
292292
<th>Notes</th>
293293
</tr>
294294
</thead>
@@ -381,12 +381,9 @@ function. For details, see [Function calls][function-calls].
381381
<tr>
382382
<td>INT64</td>
383383
<td>
384-
N/A
384+
String
385385
</td>
386386
<td>
387-
INT64 is unsupported as an input type for JavaScript UDFs. Instead,
388-
use DOUBLE to represent integer values as a
389-
number, or STRING to represent integer values as a string.
390387
</td>
391388
</tr>
392389

zetasql/analyzer/resolver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,12 @@ class Resolver {
18531853
const ASTWhereClause* where_clause, const NameScope* from_scan_scope,
18541854
std::unique_ptr<const ResolvedScan>* current_scan);
18551855

1856+
// Check the ExprResolutionInfo for an expression resolved in this query,
1857+
// checking for any features required or not allowed in this SelectForm.
1858+
absl::Status CheckExprResolutionInfoForQuery(
1859+
const ASTNode* ast_location, QueryResolutionInfo* query_resolution_info,
1860+
const ExprResolutionInfo& expr_resolution_info);
1861+
18561862
// Performs first pass analysis on the SELECT list expressions against the
18571863
// FROM clause. This pass includes star and dot-star expansion, but defers
18581864
// resolution of expressions that use GROUP ROWS or GROUP BY modifiers (see

zetasql/analyzer/resolver_query.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,6 +3372,13 @@ absl::Status Resolver::ResolveSelectDotStar(
33723372
query_resolution_info);
33733373
ZETASQL_RETURN_IF_ERROR(
33743374
ResolveExpr(ast_expr, &expr_resolution_info, &resolved_dotstar_expr));
3375+
3376+
// Check for any features required or not allowed in this expression.
3377+
// This applies the checks from ResolveSelectColumnFirstPass here too
3378+
// since ResolveSelectDotStar is an early exit in that function.
3379+
ZETASQL_RETURN_IF_ERROR(CheckExprResolutionInfoForQuery(
3380+
ast_expr, query_resolution_info, expr_resolution_info));
3381+
33753382
const Type* source_type = resolved_dotstar_expr->type();
33763383

33773384
std::unique_ptr<const ResolvedColumnRef> src_column_ref;
@@ -3600,6 +3607,13 @@ absl::Status Resolver::AddColumnFieldsToSelectList(
36003607
return absl::OkStatus();
36013608
}
36023609

3610+
absl::Status Resolver::CheckExprResolutionInfoForQuery(
3611+
const ASTNode* ast_location, QueryResolutionInfo* query_resolution_info,
3612+
const ExprResolutionInfo& expr_resolution_info) {
3613+
3614+
return absl::OkStatus();
3615+
}
3616+
36033617
absl::Status Resolver::ResolveSelectColumnFirstPass(
36043618
const ASTSelectColumn* ast_select_column, const NameScope* from_scan_scope,
36053619
const std::shared_ptr<const NameList>& from_clause_name_list,
@@ -3650,6 +3664,12 @@ absl::Status Resolver::ResolveSelectColumnFirstPass(
36503664
ZETASQL_RETURN_IF_ERROR(ResolveExpr(ast_select_expr, expr_resolution_info.get(),
36513665
&resolved_expr, inferred_type));
36523666

3667+
// Check for any features required or not allowed in this expression.
3668+
// Note that this also needs to happen inside the ResolveSelectDotStar
3669+
// call above that exists early.
3670+
ZETASQL_RETURN_IF_ERROR(CheckExprResolutionInfoForQuery(
3671+
ast_select_expr, query_resolution_info, *expr_resolution_info));
3672+
36533673
// We can set is_explicit=true unconditionally because this either came
36543674
// from an AS alias or from a path in the query, or it's an internal name
36553675
// for an anonymous column (that can't be looked up).

0 commit comments

Comments
 (0)