LakshSingla commented on code in PR #15689: URL: https://github.com/apache/druid/pull/15689#discussion_r1456925619
########## sql/src/main/java/org/apache/druid/sql/calcite/parser/ExternalDestinationSqlIdentifier.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.collect.Iterables; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlWriter; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.druid.catalog.model.table.export.ExportDestination; + +import java.util.Map; + +/** + * Extends the {@link SqlIdentifier} to hold parameters for an external table destination. This contains information + * required for a task to write to a destination. + */ +public class ExternalDestinationSqlIdentifier extends SqlIdentifier +{ + private final ExportDestination exportDestination; + private final Map<String, String> propertiesForUnparse; + + public ExternalDestinationSqlIdentifier( + String name, + SqlParserPos pos, + ExportDestination exportDestination, + Map<String, String> propertiesForUnparse + ) + { + super(name, pos); + this.exportDestination = exportDestination; + this.propertiesForUnparse = propertiesForUnparse; + } + + public ExportDestination getExportDestination() + { + return exportDestination; + } + + @Override + public void unparse(SqlWriter writer, int leftPrec, int rightPrec) + { + SqlWriter.Frame externFrame = writer.startFunCall("EXTERN"); + SqlWriter.Frame frame = writer.startFunCall(Iterables.getOnlyElement(names)); Review Comment: We should use something other than Iterables.getOnlyElement to prevent a cryptic error message. As I understand, this case wouldn't be reached because of the SQL parser, however a `DruidException.defensive` seems more appropriate. ########## extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/indexing/destination/MSQSelectDestination.java: ########## @@ -30,6 +30,10 @@ public enum MSQSelectDestination * Writes all the results directly to the report. */ TASKREPORT("taskReport", false), + /** + * Writes the results as rows to a location. Review Comment: location is generic, can expand a bit further. ########## sql/src/main/codegen/includes/replace.ftl: ########## @@ -20,33 +20,52 @@ // Taken from syntax of SqlInsert statement from calcite parser, edited for replace syntax SqlNode DruidSqlReplaceEof() : { - SqlNode table; + final SqlIdentifier destination; SqlNode source; SqlNodeList columnList = null; final Span s; + SqlNode tableRef = null; SqlInsert sqlInsert; // Using fully qualified name for Pair class, since Calcite also has a same class name being used in the Parser.jj org.apache.druid.java.util.common.Pair<Granularity, String> partitionedBy = new org.apache.druid.java.util.common.Pair(null, null); SqlNodeList clusteredBy = null; final Pair<SqlNodeList, SqlNodeList> p; SqlNode replaceTimeQuery = null; + String exportFileFormat = null; } { <REPLACE> { s = span(); } <INTO> - table = CompoundIdentifier() - [ - p = ParenthesizedCompoundIdentifierList() { - if (p.left.size() > 0) { - columnList = p.left; - } + ( + LOOKAHEAD(2) + <EXTERN> <LPAREN> destination = ExternalDestination() <RPAREN> + | + destination = CompoundTableIdentifier() + ( tableRef = TableHints(destination) | { tableRef = destination; } ) + [ LOOKAHEAD(5) tableRef = ExtendTable(tableRef) ] + ) + ( + LOOKAHEAD(2) + p = ParenthesizedCompoundIdentifierList() { + if (p.right.size() > 0) { + tableRef = extend(tableRef, p.right); } + if (p.left.size() > 0) { + columnList = p.left; + } else { + columnList = null; + } + } + | { columnList = null; } Review Comment: Seems like there is some code duplication b/w INSERT and REPLACE. Can we unify some code, and only keep the high-level constructs in place? ########## sql/src/main/codegen/config.fmpp: ########## @@ -65,10 +70,16 @@ data: { "CLUSTERED" "OVERWRITE" "PARTITIONED" + "EXTERN" + "S3" + "CSV" Review Comment: Why make it as a keyword? If we can, let's keep it as non-reserved only. Also, it would be cool to have tests that test the parsing if any of these keywords are in the table identifier list, something like `SELECT csv, s3, col3, col4 FROM extern` (and a few permutations of these). Queries like these will most likely break if EXTERN is a keyword (because then the parser will prevent it from being an identifier). Also I am curious what happens if these queries are run now - IMO they should pass because the current EXTERN(..) function is like a sql function and not a syntax change. Some more examples can be : OVERWRITE INTO EXTERN(....) SELECT col1 FROM extern WHERE.... (Here first extern refer to the new syntax, while the second one refer to the table) If the parsing is correct, in these edge cases, we can probably make do with keeping it as non-reserved only. Else this change needs to be called out. ########## server/src/main/java/org/apache/druid/catalog/model/table/export/ExportDestination.java: ########## @@ -0,0 +1,26 @@ +/* + * 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.catalog.model.table.export; + +import org.apache.druid.catalog.model.table.IngestDestination; + +public interface ExportDestination extends IngestDestination Review Comment: Also, is this level of indirection required? Are we planning on adding some methods here? ########## sql/src/main/codegen/includes/insert.ftl: ########## @@ -21,68 +21,68 @@ * Parses an INSERT statement. This function is copied from SqlInsert in core/src/main/codegen/templates/Parser.jj, * with some changes to allow a custom error message if an OVERWRITE clause is present. */ -SqlNode DruidSqlInsert() : +// Using fully qualified name for Pair class, since Calcite also has a same class name being used in the Parser.jj +SqlNode DruidSqlInsertEof() : { - final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>(); - final SqlNodeList keywordList; - final SqlIdentifier tableName; - SqlNode tableRef; - SqlNode source; - final SqlNodeList columnList; - final Span s; - final Pair<SqlNodeList, SqlNodeList> p; + SqlNode insertNode; + final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>(); + final SqlNodeList keywordList; + final SqlIdentifier destination; + SqlNode tableRef = null; + SqlNode source; + final SqlNodeList columnList; + final Span s; + final Pair<SqlNodeList, SqlNodeList> p; + org.apache.druid.java.util.common.Pair<Granularity, String> partitionedBy = new org.apache.druid.java.util.common.Pair(null, null); + SqlNodeList clusteredBy = null; + String exportFileFormat = null; } { - ( - <INSERT> + ( + <INSERT> | - <UPSERT> { keywords.add(SqlInsertKeyword.UPSERT.symbol(getPos())); } - ) - { s = span(); } - SqlInsertKeywords(keywords) { - keywordList = new SqlNodeList(keywords, s.addAll(keywords).pos()); - } - <INTO> tableName = CompoundTableIdentifier() - ( tableRef = TableHints(tableName) | { tableRef = tableName; } ) + <UPSERT> { keywords.add(SqlInsertKeyword.UPSERT.symbol(getPos())); } + ) + { s = span(); } + SqlInsertKeywords(keywords) { + keywordList = new SqlNodeList(keywords, s.addAll(keywords).pos()); + } + <INTO> + ( + LOOKAHEAD(2) + <EXTERN> <LPAREN> destination = ExternalDestination() <RPAREN> + | + destination = CompoundTableIdentifier() + ( tableRef = TableHints(destination) | { tableRef = destination; } ) [ LOOKAHEAD(5) tableRef = ExtendTable(tableRef) ] - ( - LOOKAHEAD(2) - p = ParenthesizedCompoundIdentifierList() { - if (p.right.size() > 0) { - tableRef = extend(tableRef, p.right); - } - if (p.left.size() > 0) { - columnList = p.left; - } else { - columnList = null; - } - } - | { columnList = null; } - ) - ( + ) + ( + LOOKAHEAD(2) Review Comment: Why is this lookahead required? ########## sql/src/main/java/org/apache/druid/sql/calcite/parser/ExternalDestinationSqlIdentifier.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.collect.Iterables; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlWriter; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.druid.catalog.model.table.export.ExportDestination; + +import java.util.Map; + +/** + * Extends the {@link SqlIdentifier} to hold parameters for an external table destination. This contains information + * required for a task to write to a destination. + */ +public class ExternalDestinationSqlIdentifier extends SqlIdentifier +{ + private final ExportDestination exportDestination; + private final Map<String, String> propertiesForUnparse; + + public ExternalDestinationSqlIdentifier( + String name, + SqlParserPos pos, + ExportDestination exportDestination, + Map<String, String> propertiesForUnparse + ) + { + super(name, pos); + this.exportDestination = exportDestination; + this.propertiesForUnparse = propertiesForUnparse; + } + + public ExportDestination getExportDestination() + { + return exportDestination; + } + + @Override + public void unparse(SqlWriter writer, int leftPrec, int rightPrec) + { + SqlWriter.Frame externFrame = writer.startFunCall("EXTERN"); + SqlWriter.Frame frame = writer.startFunCall(Iterables.getOnlyElement(names)); + for (Map.Entry<String, String> property : propertiesForUnparse.entrySet()) { + writer.sep(","); + writer.keyword(property.getKey()); + writer.print("="); + writer.identifier(property.getValue(), false); + } + writer.endFunCall(frame); + writer.endFunCall(externFrame); + } + + @Override + public SqlNode clone(SqlParserPos pos) + { + return new ExternalDestinationSqlIdentifier(Iterables.getOnlyElement(names), pos, exportDestination, propertiesForUnparse); + } + + @Override + @Deprecated + public Object clone() + { + throw new UnsupportedOperationException("Function is deprecated, please use clone(SqlNode) instead."); Review Comment: nit: DruidException.defensive, to let the users know that its not an issue on their end, if they ever encounter it. ########## sql/src/main/java/org/apache/druid/sql/calcite/parser/ExternalDestinationSqlIdentifier.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.collect.Iterables; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlWriter; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.druid.catalog.model.table.export.ExportDestination; + +import java.util.Map; + +/** + * Extends the {@link SqlIdentifier} to hold parameters for an external table destination. This contains information Review Comment: What's an external table? -- 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]
