Skip to content

Commit

Permalink
Merge pull request #34 from project-tsurugi/feat-restricted-features
Browse files Browse the repository at this point in the history
feat: compiler can restrict unsupported features.
  • Loading branch information
kuron99 authored Jul 9, 2024
2 parents b25ca10 + 1df773e commit 7ce44cc
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/yugawara/compiler_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace yugawara {
enum class compiler_code {
/// @brief unknown diagnostic.
unknown = 0,
/// @brief the runtime does not support such target.
unsupported_feature,
/// @brief input type is not supported in this operation.
unsupported_type,
/// @brief input type is not distinguished for the overloaded operations.
Expand All @@ -34,6 +36,7 @@ inline constexpr std::string_view to_string_view(compiler_code value) noexcept {
using kind = compiler_code;
switch (value) {
case kind::unknown: return "unknown"sv;
case kind::unsupported_feature: return "unsupported_feature"sv;
case kind::unsupported_type: return "unsupported_type"sv;
case kind::ambiguous_type: return "ambiguous_type"sv;
case kind::inconsistent_type: return "inconsistent_type"sv;
Expand Down
34 changes: 33 additions & 1 deletion include/yugawara/compiler_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

#include <takatori/util/maybe_shared_ptr.h>

#include <yugawara/runtime_feature.h>
#include <yugawara/analyzer/index_estimator.h>
#include <yugawara/storage/prototype_processor.h>

#include "runtime_feature.h"
#include "restricted_feature.h"

namespace yugawara {

/**
Expand All @@ -21,6 +23,24 @@ class compiler_options {
*/
static constexpr runtime_feature_set default_runtime_features { runtime_feature_all };

/**
* @brief the default list of restricted features.
* @see restricted_features()
*/
static constexpr restricted_feature_set default_restricted_features {};

/**
* @brief creates a new instance with default options.
* @param runtime_features the supported runtime features
* @param storage_processor the storage element prototype processor for accepting storage element definitions
* @param index_estimator the index estimator for index selection
* @see runtime_features()
* @see restricted_features()
*/
compiler_options( // NOLINT(*-explicit-constructor, *-explicit-conversions)
::takatori::util::maybe_shared_ptr<::yugawara::storage::prototype_processor> storage_processor,
::takatori::util::maybe_shared_ptr<::yugawara::analyzer::index_estimator const> index_estimator = {}) noexcept;

/**
* @brief creates a new instance with default options.
* @param runtime_features the supported runtime features
Expand All @@ -35,12 +55,23 @@ class compiler_options {
/**
* @brief returns the available feature set of the target environment.
* @return the available features
* @see restricted_features()
*/
[[nodiscard]] runtime_feature_set& runtime_features() noexcept;

/// @copydoc runtime_features()
[[nodiscard]] runtime_feature_set const& runtime_features() const noexcept;

/**
* @brief returns the restricted feature set of the target environment.
* @return the restricted features
* @see runtime_feature()
*/
[[nodiscard]] restricted_feature_set& restricted_features() noexcept;

/// @copydoc restricted_features()
[[nodiscard]] restricted_feature_set const& restricted_features() const noexcept;

/**
* @brief returns the storage element processor for handling storage element definitions.
* @return the storage element prototype processor
Expand Down Expand Up @@ -71,6 +102,7 @@ class compiler_options {

private:
runtime_feature_set runtime_features_ { default_runtime_features };
restricted_feature_set restricted_features_ { default_restricted_features };
::takatori::util::maybe_shared_ptr<::yugawara::storage::prototype_processor> storage_processor_ {};
::takatori::util::maybe_shared_ptr<analyzer::index_estimator const> index_estimator_ {};
};
Expand Down
157 changes: 157 additions & 0 deletions include/yugawara/restricted_feature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#pragma once

#include <ostream>
#include <string>
#include <string_view>

#include <cstdint>
#include <cstdlib>

#include <takatori/util/enum_set.h>

namespace yugawara {

/**
* @brief represents restricted features of runtime.
* @attention If the compilation result includes any of these features,
* the compilation will just fail and report compiler_code::restricted_feature.
* @see runtime_feature
*/
enum class restricted_feature {

// scalar expressions

// relation expressions

/// @brief restrict `buffer` operator.
relation_buffer,

/// @brief restrict `identify` operator.
relation_identify,

/// @brief restrict `join_find` operator.
relation_join_find,

/// @brief restrict `join_scan` operator.
relation_join_scan,

/// @brief restrict `write` operator with `insert` operations.
relation_write_insert,

/// @brief restrict `values` operator.
relation_values,

/// @brief restrict `difference` operator.
relation_difference,

/// @brief restrict `flatten` operator.
relation_flatten,

/// @brief restrict `intersection` operator.
relation_intersection,

// exchanges

/// @brief restrict `aggregate` exchange.
exchange_aggregate,

/// @brief restrict `broadcast` exchange.
exchange_broadcast,

/// @brief restrict `discard` exchange.
exchange_discard,

/// @brief restrict `forward` exchange.
exchange_forward,

/// @brief restrict `group` exchange.
exchange_group,

// statements

/// @brief restrict `write` statement with delete operation.
statement_write_delete,

/// @brief restrict `write` statement with update operation.
statement_write_update,
};

/**
* @brief a set of intermediate_plan_optimizer_feature.
*/
using restricted_feature_set = ::takatori::util::enum_set<
restricted_feature,
restricted_feature::relation_buffer,
restricted_feature::statement_write_update>;

/// @brief all scalar expressions of restricted_feature_set.
constexpr restricted_feature_set restricted_feature_scalar_expressions {};

/// @brief all relation expressions of restricted_feature_set.
constexpr restricted_feature_set restricted_feature_relation_expressions {
restricted_feature::relation_buffer,
restricted_feature::relation_identify,
restricted_feature::relation_join_find,
restricted_feature::relation_join_scan,
restricted_feature::relation_write_insert,
restricted_feature::relation_values,
restricted_feature::relation_difference,
restricted_feature::relation_flatten,
restricted_feature::relation_intersection,
};

/// @brief all exchange steps of restricted_feature_set.
constexpr restricted_feature_set restricted_feature_exchange_steps {
restricted_feature::exchange_aggregate,
restricted_feature::exchange_broadcast,
restricted_feature::exchange_discard,
restricted_feature::exchange_forward,
restricted_feature::exchange_group,
};

/// @brief all statements of restricted_feature_set.
constexpr restricted_feature_set restricted_feature_statements {
restricted_feature::statement_write_delete,
restricted_feature::statement_write_update,
};

/**
* @brief returns string representation of the value.
* @param value the target value
* @return the corresponded string representation
*/
inline constexpr std::string_view to_string_view(restricted_feature value) noexcept {
using namespace std::string_view_literals;
using kind = restricted_feature;
switch (value) {
case kind::relation_buffer: return "buffer operator"sv;
case kind::relation_identify: return "identify operator"sv;
case kind::relation_join_find: return "join_find operator"sv;
case kind::relation_join_scan: return "join_scan operator"sv;
case kind::relation_write_insert: return "write operator with insert operation"sv;
case kind::relation_values: return "values operator"sv;
case kind::relation_difference: return "difference operator"sv;
case kind::relation_flatten: return "flatten operator"sv;
case kind::relation_intersection: return "intersection operator"sv;
case kind::exchange_aggregate: return "aggregate exchange"sv;
case kind::exchange_broadcast: return "broadcast exchange"sv;
case kind::exchange_discard: return "discard exchange"sv;
case kind::exchange_forward: return "forward exchange"sv;
case kind::exchange_group: return "group exchange"sv;
case kind::statement_write_delete: return "write statement with delete operation"sv;
case kind::statement_write_update: return "write statement with update operation"sv;
}
std::abort();
}

/**
* @brief appends string representation of the given value.
* @param out the target output
* @param value the target value
* @return the output
*/
inline std::ostream& operator<<(std::ostream& out, restricted_feature value) {
return out << to_string_view(value);
}

} // namespace yugawara
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(yugawara
yugawara/compiler_options.cpp
yugawara/compiled_info.cpp
yugawara/compiler_result.cpp
yugawara/details/collect_restricted_features.cpp

# storage information
yugawara/storage/relation.cpp
Expand Down
9 changes: 8 additions & 1 deletion src/yugawara/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <yugawara/analyzer/step_plan_builder.h>

#include <yugawara/storage/basic_prototype_processor.h>
#include <yugawara/storage/resolve_prototype.h>

#include "storage/resolve_prototype.h"
#include "details/collect_restricted_features.h"

namespace yugawara {

Expand Down Expand Up @@ -110,6 +111,12 @@ class engine {
return result_type { diagnostics };
}
}
if (!options_.restricted_features().empty()) {
auto diagnostics = details::collect_restricted_features(options_.restricted_features(), *stmt);
if (!diagnostics.empty()) {
return result_type { std::move(diagnostics) };
}
}

expression_analyzer_.resolve(*stmt, true, type_repository_);
if (expression_analyzer_.has_diagnostics()) {
Expand Down
15 changes: 15 additions & 0 deletions src/yugawara/compiler_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace yugawara {

using ::takatori::util::maybe_shared_ptr;

compiler_options::compiler_options(
maybe_shared_ptr<storage::prototype_processor> storage_processor,
maybe_shared_ptr<analyzer::index_estimator const> index_estimator) noexcept :
storage_processor_ { std::move(storage_processor) },
index_estimator_ { std::move(index_estimator) }
{}

compiler_options::compiler_options(
runtime_feature_set runtime_features,
maybe_shared_ptr<storage::prototype_processor> storage_processor,
Expand All @@ -23,6 +30,14 @@ runtime_feature_set const& compiler_options::runtime_features() const noexcept {
return runtime_features_;
}

restricted_feature_set& compiler_options::restricted_features() noexcept {
return restricted_features_;
}

restricted_feature_set const& compiler_options::restricted_features() const noexcept {
return restricted_features_;
}

maybe_shared_ptr<storage::prototype_processor> compiler_options::storage_processor() const noexcept {
return storage_processor_;
}
Expand Down
Loading

0 comments on commit 7ce44cc

Please sign in to comment.