LakshSingla commented on a change in pull request #12163:
URL: https://github.com/apache/druid/pull/12163#discussion_r800902023



##########
File path: 
sql/src/main/java/org/apache/druid/sql/calcite/parser/DruidSqlInsert.java
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.parser;
+
+import com.google.common.base.Preconditions;
+import org.apache.calcite.sql.SqlInsert;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.druid.java.util.common.granularity.Granularity;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+/**
+ * Extends the 'insert' call to hold custom parameters specific to Druid i.e. 
PARTITIONED BY and CLUSTERED BY
+ * This class extends the {@link SqlInsert} so that this SqlNode can be used in
+ * {@link org.apache.calcite.sql2rel.SqlToRelConverter} for getting converted 
into RelNode, and further processing
+ */
+public class DruidSqlInsert extends SqlInsert
+{
+  public static final String SQL_INSERT_SEGMENT_GRANULARITY = 
"sqlInsertSegmentGranularity";
+
+  // This allows reusing super.unparse
+  public static final SqlOperator OPERATOR = SqlInsert.OPERATOR;
+
+  private final Granularity partitionedBy;
+
+  @Nullable
+  private final SqlNodeList clusteredBy;
+
+  public DruidSqlInsert(
+      @Nonnull SqlInsert insertNode,
+      @Nonnull Granularity partitionedBy,
+      @Nullable SqlNodeList clusteredBy
+  )
+  {
+    super(
+        insertNode.getParserPosition(),
+        (SqlNodeList) insertNode.getOperandList().get(0), // No better getter 
to extract this
+        insertNode.getTargetTable(),
+        insertNode.getSource(),
+        insertNode.getTargetColumnList()
+    );
+    Preconditions.checkNotNull(partitionedBy); // Shouldn't hit due to how the 
parser is written
+    this.partitionedBy = partitionedBy;
+    this.clusteredBy = clusteredBy;
+  }
+
+  @Nullable
+  public SqlNodeList getClusteredBy()
+  {
+    return clusteredBy;
+  }
+
+  public Granularity getPartitionedBy()
+  {
+    return partitionedBy;
+  }
+
+  @Nonnull
+  @Override
+  public SqlOperator getOperator()
+  {
+    return OPERATOR;
+  }
+
+  @Override
+  public void unparse(SqlWriter writer, int leftPrec, int rightPrec)
+  {
+    super.unparse(writer, leftPrec, rightPrec);
+    writer.keyword("PARTITIONED BY");
+    writer.keyword(getPartitionedBy().toString()); // TODO: Make it cleaner by 
directly unparsing the SqlNode

Review comment:
       Updated the method, with the proper implementation. Here are a few 
unparse examples after the change:
   ```
   INSERT INTO `dst`
   (SELECT *
   FROM `foo`) PARTITIONED BY ALL TIME
   
   INSERT INTO `dst`
   (SELECT *
   FROM `view`.`aview`) PARTITIONED BY `TIME_FLOOR`(`__TIME`, 'PT2H')
   
   INSERT INTO `druid`.`dst`
   (SELECT `__time`, FLOOR(`m1`) AS `floor_m1`, `dim1`, CEIL(`m2`)
   FROM `foo`) PARTITIONED BY FLOOR(`__TIME` TO DAY) CLUSTERED BY 2 `dim1` DESC 
CEIL(`m2`)
   ```
   
   `unparse` method is important since this is being used in `SqlNode`'s 
`toString` method. So it gets used while debugging to view the original node as 
a SQL string. (Also, it gets used in other unparse methods, so not relevant to 
DruidSqlInsert currently since it's a top-level node, but it might cause say 
Explain on a DriudSqlInsert to fail, which is going to be implemented).




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

Reply via email to