dtenedor commented on code in PR #60367:
URL: https://github.com/apache/spark/pull/60367#discussion_r4620497374
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -548,7 +596,13 @@ class Analyzer(
override def execute(plan: LogicalPlan): LogicalPlan = {
AnalysisContext.withNewAnalysisContext {
- executeSameContext(plan)
+ val analyzed = if (conf.optionalReferencesEnabled &&
AnalysisContext.definesView(plan)) {
Review Comment:
Fixed as suggested. `Analyzer.execute` now sets `isViewDefinition` for every
plan that defines a view, whatever the session conf, and the field doc says
why. The new `SQLFunctionSuite` test creates a temporary and a persistent
function with `lbl.missing?` in a labeled block while the conf is on, then
leaves the conf unset. A temporary view, a persistent view, `ALTER VIEW ...
AS`, and `CACHE TABLE ... AS SELECT` that call these functions in a labeled
block now fail at creation with UNRESOLVED_COLUMN. A query in the labeled block
still reads NULL, and a temporary view of a captured `lbl.y` still reads its
value.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/OptionalReferenceValidation.scala:
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.parser
+
+import org.antlr.v4.runtime.ParserRuleContext
+import org.antlr.v4.runtime.tree.ParseTree
+
+import org.apache.spark.sql.catalyst.parser.SqlBaseParser._
+import org.apache.spark.sql.catalyst.util.SparkParserUtils
+import org.apache.spark.sql.errors.QueryParsingErrors
+
+/** Checks where the `?` optional-reference operator can appear in a SQL
statement. */
+object OptionalReferenceValidation {
+
+ /**
+ * Throws an error for the optional reference `ctx`, i.e. the parse tree of
a reference with a
+ * `?` after one of its name parts, if `ctx` is part of an UPDATE, DELETE,
MERGE, or AUTO CDC
+ * statement, of the REPLACE condition of INSERT, of a partition
specification, or of a DEFAULT,
+ * GENERATED ALWAYS AS, CHECK, or OPTIONS clause of a table or function
definition.
Review Comment:
Done, with the suggested wording.
##########
sql/core/src/test/resources/sql-tests/inputs/optional-references.sql:
##########
@@ -0,0 +1,646 @@
+-- Tests for optional column, field, variable, and named parameter references:
a `?` after an
+-- identifier makes that identifier optional, so that a reference that cannot
be resolved
+-- evaluates to a static NULL of type VOID instead of raising a not-found
error.
+
+--SET spark.sql.optionalReferences.enabled=true
+
+CREATE TEMPORARY VIEW people AS SELECT * FROM VALUES
+ (1, named_struct('city', 'SF', 'zip', '94105')),
+ (2, named_struct('city', 'NYC', 'zip', '10001'))
+ AS t(id, addr);
+
+CREATE TEMPORARY VIEW orgs AS SELECT * FROM VALUES
+ (1, named_struct('addr', named_struct('city', 'SF', 'zip', '94105')))
+ AS t(id, info);
+
+CREATE TEMPORARY VIEW orders AS SELECT * FROM VALUES
+ (1, array(named_struct('sku', 'a', 'qty', 5))),
+ (2, array(named_struct('sku', 'b', 'qty', 9)))
+ AS t(id, items);
+
+-- A missing column resolves to NULL and an existing column resolves normally.
Either way, the
+-- output column of a reference that is a whole SELECT item keeps the name of
the reference, and an
+-- expression over a missing reference is named after NULL.
+SELECT id?, name? FROM people;
+SELECT typeof(id?), typeof(name?) FROM people LIMIT 1;
+SELECT * FROM (SELECT id?, name? FROM people);
+
+-- Whitespace and comments between the identifier and `?` are insignificant.
+SELECT id ?, name /* comment */ ? FROM people;
+
+-- Optional references in expressions, predicates, grouping, ordering, and
function arguments.
+SELECT id, bonus? + 1 AS b FROM people;
+SELECT coalesce(archived?, false) AS archived, count(*) AS cnt
+FROM people
+WHERE archived? IS NULL
+GROUP BY archived?;
+SELECT id FROM people WHERE coalesce(archived?, false) = false ORDER BY id;
+SELECT id FROM people ORDER BY missing?, id DESC;
+SELECT id, sum(id) OVER (PARTITION BY region? ORDER BY id) AS running FROM
people;
+SELECT id FROM people GROUP BY id HAVING max(missing?) IS NULL ORDER BY id;
+SELECT id FROM people GROUP BY id HAVING max(addr.state?) IS NULL ORDER BY id;
+SELECT id FROM people GROUP BY id ORDER BY max(addr.state?), id;
+SELECT id, CASE WHEN missing? IS NULL THEN 'none' ELSE 'some' END AS c FROM
people;
+SELECT a.id FROM people a JOIN people b ON a.id = b.id AND a.missing? IS NULL
ORDER BY a.id;
+SELECT a.id, b.missing?, b.addr.state? FROM people a JOIN people b USING (id)
ORDER BY a.id;
+SELECT id, missing?, count(*) FROM people GROUP BY ALL ORDER BY id;
+SELECT count(*) + missing? AS c FROM people GROUP BY ALL;
+SELECT missing?, count(*) FROM people;
+SELECT count(missing?), max(id?) FROM people;
+SELECT DISTINCT missing?, addr?.city FROM people ORDER BY city;
+SELECT id, row_number() OVER (ORDER BY id) AS rn FROM people QUALIFY rn = 1
AND missing? IS NULL;
+
+-- GROUP BY, HAVING, and ORDER BY can reference aliases of the SELECT list as
usual.
+SELECT coalesce(region?, 'none') AS region, count(*) AS cnt FROM people GROUP
BY region?;
+SELECT coalesce(region?, 'none') AS region, count(*) AS cnt FROM people
+GROUP BY 1 HAVING region? = 'none';
+SELECT coalesce(rank?, id) AS rank FROM people ORDER BY rank? DESC;
+
+-- Where a name can resolve in more than one way, an optional reference
resolves like the same
+-- reference without `?`: ORDER BY falls back from the SELECT list to the
columns of the FROM
+-- clause, and HAVING falls back from those columns to the SELECT list.
+SELECT id, named_struct('city', addr.city) AS addr FROM people ORDER BY
addr.zip?;
+SELECT id, named_struct('city', addr.city) AS addr FROM people ORDER BY
addr.state?, id DESC;
+SELECT id, named_struct('city', addr.city) AS addr FROM people ORDER BY
addr?.state;
+SELECT named_struct('city', 'x') AS addr FROM people GROUP BY id ORDER BY
addr.zip?;
+SELECT named_struct('state', 'CA') AS addr FROM people GROUP BY addr HAVING
addr.state? = 'CA';
+SELECT named_struct('state', 'CA') AS addr FROM people GROUP BY addr
+HAVING addr.country? IS NULL;
+-- `addr.city` exists below the SELECT list but is not grouped, so HAVING
falls back to the alias
+-- `addr` of the SELECT list, which has no `city`.
+SELECT named_struct('state', 'CA') AS addr FROM people GROUP BY id HAVING
addr.city? IS NULL;
+-- A column that is out of scope is not found.
+SELECT DISTINCT id FROM people ORDER BY addr?, id DESC;
+-- ORDER BY and WHERE do not reach below an aliased subquery or a pipe
operator, like without `?`.
+SELECT id FROM (SELECT id FROM people) s ORDER BY addr?, id;
+SELECT id FROM (SELECT id FROM people) s ORDER BY addr, id;
+FROM people |> SELECT id |> WHERE addr? IS NULL;
+FROM people |> SELECT id |> WHERE addr IS NULL;
+
+-- Common table expressions and set operations.
+WITH cte AS (SELECT id, missing? AS m FROM people) SELECT id, m, other? FROM
cte ORDER BY id;
+SELECT id?, missing? FROM people UNION ALL SELECT missing?, id? FROM people
+ORDER BY id NULLS FIRST, missing NULLS FIRST;
+
+-- An optional reference that resolves keeps its type and nullability.
+SELECT id? + 1 AS next_id, addr?.city AS city FROM people;
+
+-- Qualified and nested references: `?` guards only the identifier that it
follows.
+SELECT p.name? FROM people AS p;
+SELECT people.name?, people.addr.state? FROM people;
+SELECT addr?.city FROM people;
+SELECT addr?.state FROM people;
+SELECT addr.state? FROM people;
+SELECT ghost?.city FROM people;
+SELECT ghost.city? FROM people;
+SELECT ghost?.city? FROM people;
+SELECT t.name? FROM people;
+SELECT people?, p? FROM people AS p;
+-- The qualifier of a table can include its namespace and catalog, like
without `?`.
+CREATE TABLE qualified_t (id INT) USING parquet;
+INSERT INTO qualified_t VALUES (1);
+SELECT qualified_t.missing? AS a, default.qualified_t.missing? AS b,
+ spark_catalog.default.qualified_t.missing? AS c
+FROM qualified_t;
+SELECT other.qualified_t.missing? FROM qualified_t;
+DROP TABLE qualified_t;
+
+-- Three-part names: `?` binds to the immediately preceding identifier only.
+SELECT info.addr.zip? FROM orgs;
+SELECT info.addr.geo? FROM orgs;
+SELECT info.loc?.city FROM orgs;
+SELECT info.loc.city? FROM orgs;
+SELECT info?.addr.zip FROM orgs;
+SELECT a?.b?.c? FROM orgs;
+SELECT missing?.a.b, info.addr.geo?.x FROM orgs;
+-- The fields of a column of type VOID are NULL, like without `?`. Such a
field is found, so its
+-- name is a lateral column alias.
+SELECT n.a, n.a? FROM VALUES (NULL) AS t(n);
+SELECT n.a?, a + 1 FROM VALUES (NULL) AS t(n);
+SELECT struct(id?, missing?) AS s FROM orgs;
+
+-- Errors other than not-found are still raised, and so is the not-found error
of an ORDER BY
+-- name that matches more than one column of the FROM clause, which is not a
missing column.
Review Comment:
Done, with the suggested wording.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]