jihoonson commented on a change in pull request #10812:
URL: https://github.com/apache/druid/pull/10812#discussion_r568906614



##########
File path: sql/src/main/java/org/apache/druid/sql/SqlLifecycle.java
##########
@@ -116,6 +131,11 @@ public SqlLifecycle(
     this.parameters = Collections.emptyList();
   }
 
+  /**
+   * Initialize the query lifecycle, setting the raw string SQL, initial query 
context, and assign a sql query id.
+   *
+   * If successful (it will be), it will transition the lifecycle to {@link 
State#INITIALIZED}.
+   */

Review comment:
       Thanks for adding javadocs :+1: 

##########
File path: 
sql/src/main/java/org/apache/druid/sql/calcite/planner/SqlResourceCollectorShuttle.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.druid.sql.calcite.planner;
+
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.util.SqlShuttle;
+import org.apache.calcite.sql.validate.IdentifierNamespace;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.druid.server.security.Resource;
+import org.apache.druid.server.security.ResourceType;
+import org.apache.druid.sql.calcite.schema.NamedViewSchema;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Walks an {@link SqlNode} to collect a set of {@link Resource} for {@link 
ResourceType#DATASOURCE} and
+ * {@link ResourceType#VIEW} to use for authorization during query planning.
+ *
+ * It works by looking for {@link SqlIdentifier} which corespond to a {@link 
IdentifierNamespace}, where
+ * {@link SqlValidatorNamespace} is calcite-speak for sources of data and 
{@link IdentifierNamespace} specifically are
+ * namespaces which are identified by a single variable, e.g. table names.
+ */
+public class SqlResourceCollectorShuttle extends SqlShuttle
+{
+  private final Set<Resource> resources;
+  private final SqlValidator validator;
+  private final String druidSchemaName;
+
+  public SqlResourceCollectorShuttle(SqlValidator validator, String 
druidSchemaName)
+  {
+    this.validator = validator;
+    this.resources = new HashSet<>();
+    this.druidSchemaName = druidSchemaName;
+  }
+
+  @Override
+  public SqlNode visit(SqlIdentifier id)
+  {
+    // raw tables and views and such will have a IdentifierNamespace
+    // since we are scoped to identifiers here, we should only pick up these
+    SqlValidatorNamespace namespace = validator.getNamespace(id);
+    if (namespace instanceof IdentifierNamespace) {

Review comment:
       Per 
[Calcite](https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/sql/validate/SqlValidator.java#L99-L103),
 we should use `isWrappedFor()` instead of `instanceof`.

##########
File path: sql/src/main/java/org/apache/druid/sql/SqlLifecycle.java
##########
@@ -136,90 +157,97 @@ public String initialize(String sql, Map<String, Object> 
queryContext)
     return newContext;
   }
 
+  @GuardedBy("lock")
   private String sqlQueryId()
   {
     return (String) this.queryContext.get(PlannerContext.CTX_SQL_QUERY_ID);
   }
 
+  /**
+   * Assign dynamic parameters to be used to substitute values during query 
exection. This can be performed at any
+   * part of the lifecycle.
+   */
   public void setParameters(List<TypedValue> parameters)
-  {
-    this.parameters = parameters;
-  }
-
-  public PrepareResult prepare(AuthenticationResult authenticationResult)
-      throws ValidationException, RelConversionException, SqlParseException
   {
     synchronized (lock) {
-      try (DruidPlanner planner = plannerFactory.createPlanner(queryContext, 
parameters, authenticationResult)) {
-        // set planner context for logs/metrics in case something explodes 
early
-        this.plannerContext = planner.getPlannerContext();
-        this.prepareResult = planner.prepare(sql);
-        return prepareResult;
+      this.parameters = parameters;
+      if (this.plannerContext != null) {
+        this.plannerContext.setParameters(parameters);
       }
     }
   }
 
-  private PlannerContext plan(AuthenticationResult authenticationResult)
-      throws RelConversionException
+  /**
+   * Validate SQL query and authorize against any datasources or views which 
the query.

Review comment:
       `which the query reads`?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to