|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "axiom/sql/presto/TableVisitor.h" |
| 18 | + |
| 19 | +#include <fmt/format.h> |
| 20 | + |
| 21 | +#include "velox/common/base/Exceptions.h" |
| 22 | + |
| 23 | +namespace axiom::sql::presto { |
| 24 | + |
| 25 | +TableVisitor::TableVisitor( |
| 26 | + const std::string& defaultConnectorId, |
| 27 | + const std::optional<std::string>& defaultSchema) |
| 28 | + : defaultConnectorId_(defaultConnectorId), defaultSchema_(defaultSchema) {} |
| 29 | + |
| 30 | +void TableVisitor::visitWithQuery(WithQuery* node) { |
| 31 | + // To cover the case where a CTE aliases an underlying |
| 32 | + // table, e.g. 'WITH t AS (SELECT * FROM t)', we need to |
| 33 | + // traverse the inner query before tracking the CTE alias. |
| 34 | + DefaultTraversalVisitor::visitWithQuery(node); |
| 35 | + ctes_.insert(node->name()->value()); |
| 36 | +} |
| 37 | + |
| 38 | +void TableVisitor::visitTable(Table* node) { |
| 39 | + const auto& parts = node->name()->parts(); |
| 40 | + if (parts.size() == 1 && ctes_.count(parts[0]) > 0) { |
| 41 | + return; |
| 42 | + } |
| 43 | + inputTables_.insert(constructTableName(*node->name())); |
| 44 | + DefaultTraversalVisitor::visitTable(node); |
| 45 | +} |
| 46 | + |
| 47 | +void TableVisitor::visitInsert(Insert* node) { |
| 48 | + setOutputTable(*node->target()); |
| 49 | + DefaultTraversalVisitor::visitInsert(node); |
| 50 | +} |
| 51 | + |
| 52 | +void TableVisitor::visitCreateTableAsSelect(CreateTableAsSelect* node) { |
| 53 | + setOutputTable(*node->name()); |
| 54 | + DefaultTraversalVisitor::visitCreateTableAsSelect(node); |
| 55 | +} |
| 56 | + |
| 57 | +void TableVisitor::visitUpdate(Update* node) { |
| 58 | + setOutputTable(*node->table()); |
| 59 | + DefaultTraversalVisitor::visitUpdate(node); |
| 60 | +} |
| 61 | + |
| 62 | +void TableVisitor::visitDelete(Delete* node) { |
| 63 | + setOutputTable(*node->table()); |
| 64 | + DefaultTraversalVisitor::visitDelete(node); |
| 65 | +} |
| 66 | + |
| 67 | +void TableVisitor::visitCreateTable(CreateTable* node) { |
| 68 | + setOutputTable(*node->name()); |
| 69 | + DefaultTraversalVisitor::visitCreateTable(node); |
| 70 | +} |
| 71 | + |
| 72 | +void TableVisitor::visitCreateView(CreateView* node) { |
| 73 | + setOutputTable(*node->name()); |
| 74 | + DefaultTraversalVisitor::visitCreateView(node); |
| 75 | +} |
| 76 | + |
| 77 | +void TableVisitor::visitCreateMaterializedView(CreateMaterializedView* node) { |
| 78 | + setOutputTable(*node->name()); |
| 79 | + DefaultTraversalVisitor::visitCreateMaterializedView(node); |
| 80 | +} |
| 81 | + |
| 82 | +void TableVisitor::visitDropTable(DropTable* node) { |
| 83 | + setOutputTable(*node->tableName()); |
| 84 | + DefaultTraversalVisitor::visitDropTable(node); |
| 85 | +} |
| 86 | + |
| 87 | +void TableVisitor::visitDropView(DropView* node) { |
| 88 | + setOutputTable(*node->viewName()); |
| 89 | + DefaultTraversalVisitor::visitDropView(node); |
| 90 | +} |
| 91 | + |
| 92 | +void TableVisitor::visitDropMaterializedView(DropMaterializedView* node) { |
| 93 | + setOutputTable(*node->viewName()); |
| 94 | + DefaultTraversalVisitor::visitDropMaterializedView(node); |
| 95 | +} |
| 96 | + |
| 97 | +std::string TableVisitor::constructTableName(const QualifiedName& name) const { |
| 98 | + const auto& parts = name.parts(); |
| 99 | + VELOX_CHECK(!parts.empty(), "Table name cannot be empty"); |
| 100 | + VELOX_CHECK_LE( |
| 101 | + parts.size(), |
| 102 | + 3, |
| 103 | + "Table name must have 1-3 components, '{}'", |
| 104 | + name.fullyQualifiedName()); |
| 105 | + switch (parts.size()) { |
| 106 | + case 1: |
| 107 | + if (defaultSchema_.has_value()) { |
| 108 | + return fmt::format( |
| 109 | + "{}.{}.{}", defaultConnectorId_, defaultSchema_.value(), parts[0]); |
| 110 | + } |
| 111 | + return fmt::format("{}.{}", defaultConnectorId_, parts[0]); |
| 112 | + case 2: |
| 113 | + return fmt::format("{}.{}.{}", defaultConnectorId_, parts[0], parts[1]); |
| 114 | + case 3: |
| 115 | + return fmt::format("{}.{}.{}", parts[0], parts[1], parts[2]); |
| 116 | + default: |
| 117 | + VELOX_UNREACHABLE(); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +void TableVisitor::setOutputTable(const QualifiedName& name) { |
| 122 | + VELOX_CHECK(!outputTable_.has_value()); |
| 123 | + outputTable_ = constructTableName(name); |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace axiom::sql::presto |
0 commit comments