Skip to content

Commit

Permalink
feat: insert stmt support for map data type
Browse files Browse the repository at this point in the history
WIP!
  • Loading branch information
aceforeverd committed Jan 26, 2024
1 parent 7ce7503 commit 69abf5b
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 18 deletions.
7 changes: 2 additions & 5 deletions hybridse/include/codec/fe_schema_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
#define HYBRIDSE_INCLUDE_CODEC_FE_SCHEMA_CODEC_H_

#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "vm/catalog.h"

namespace hybridse {
Expand Down Expand Up @@ -56,7 +53,7 @@ class SchemaCodec {
if (it->name().size() >= 128) {
return false;
}
uint8_t name_size = (uint8_t)(it->name().size());
uint8_t name_size = static_cast<uint8_t>(it->name().size());
memcpy(cbuffer, static_cast<const void*>(&name_size), 1);
cbuffer += 1;
memcpy(cbuffer, static_cast<const void*>(it->name().c_str()),
Expand All @@ -66,7 +63,7 @@ class SchemaCodec {
return true;
}

static bool Decode(const std::string& buf, vm::Schema* schema) {
static bool Decode(const std::string& buf, codec::Schema* schema) {
if (schema == NULL) return false;
if (buf.size() <= 0) return true;
const char* buffer = buf.c_str();
Expand Down
8 changes: 4 additions & 4 deletions hybridse/include/sdk/base_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ typedef ::google::protobuf::RepeatedPtrField< ::hybridse::type::TableDef>

class SchemaImpl : public Schema {
public:
explicit SchemaImpl(const vm::Schema& schema);
explicit SchemaImpl(const codec::Schema& schema);
SchemaImpl() {}

~SchemaImpl();

const vm::Schema& GetSchema() const { return schema_; }
inline void SetSchema(const vm::Schema& schema) { schema_ = schema; }
const codec::Schema& GetSchema() const { return schema_; }

Check warning on line 38 in hybridse/include/sdk/base_impl.h

View check run for this annotation

Codecov / codecov/patch

hybridse/include/sdk/base_impl.h#L38

Added line #L38 was not covered by tests
inline void SetSchema(const codec::Schema& schema) { schema_ = schema; }
int32_t GetColumnCnt() const;

const std::string& GetColumnName(uint32_t index) const;
Expand All @@ -46,7 +46,7 @@ class SchemaImpl : public Schema {
const bool IsConstant(uint32_t index) const;

private:
vm::Schema schema_;
codec::Schema schema_;
};

class TableImpl : public Table {
Expand Down
2 changes: 1 addition & 1 deletion hybridse/src/codegen/buf_ir_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ bool BufNativeIRBuilder::BuildGetStringField(uint32_t col_idx, uint32_t offset,

BufNativeEncoderIRBuilder::BufNativeEncoderIRBuilder(CodeGenContextBase* ctx,
const std::map<uint32_t, NativeValue>* outputs,
const vm::Schema* schema)
const codec::Schema* schema)
: ctx_(ctx),
outputs_(outputs),
schema_(schema),
Expand Down
9 changes: 2 additions & 7 deletions hybridse/src/codegen/buf_ir_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
#include "codegen/row_ir_builder.h"
#include "codegen/scope_var.h"
#include "codegen/variable_ir_builder.h"
#include "vm/catalog.h"

namespace hybridse {
namespace codegen {

class BufNativeEncoderIRBuilder : public RowEncodeIRBuilder {
public:
BufNativeEncoderIRBuilder(CodeGenContextBase* ctx, const std::map<uint32_t, NativeValue>* outputs,
const vm::Schema* schema);
const codec::Schema* schema);

~BufNativeEncoderIRBuilder() override;

Expand All @@ -55,10 +54,6 @@ class BufNativeEncoderIRBuilder : public RowEncodeIRBuilder {
::llvm::Value* str_addr_space, ::llvm::Value* str_body_offset, uint32_t str_field_idx,
::llvm::Value** output);

// encode SQL map data type into row
base::Status AppendMapVal(const type::ColumnSchema& sc, llvm::Value* i8_ptr, uint32_t field_idx,
const NativeValue& val, llvm::Value* str_addr_space, llvm::Value* str_body_offset,
uint32_t str_field_idx, llvm::Value** next_str_body_offset);
absl::StatusOr<llvm::Function*> GetOrBuildAppendMapFn(const type::ColumnSchema& sc) const;

base::Status AppendHeader(::llvm::Value* i8_ptr, ::llvm::Value* size,
Expand All @@ -74,7 +69,7 @@ class BufNativeEncoderIRBuilder : public RowEncodeIRBuilder {
private:
CodeGenContextBase* ctx_;
const std::map<uint32_t, NativeValue>* outputs_;
const vm::Schema* schema_;
const codec::Schema* schema_;
uint32_t str_field_start_offset_;
// n = offset_vec_[i] is
// schema_[i] is base type (except string): col encode offset in row
Expand Down
37 changes: 37 additions & 0 deletions hybridse/src/codegen/insert_row_builder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2024 OpenMLDB authors
*
* Licensed 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.
*/

#include "codegen/insert_row_builder.h"

#include "absl/status/status.h"

namespace hybridse {
namespace codegen {

InsertRowBuilder::InsertRowBuilder(CodeGenContextBase* ctx, const codec::Schema* schema) : ctx_(ctx), schema_(schema) {}

Check warning on line 24 in hybridse/src/codegen/insert_row_builder.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/codegen/insert_row_builder.cc#L24

Added line #L24 was not covered by tests

absl::StatusOr<std::shared_ptr<int8_t>> InsertRowBuilder::ComputeRow(absl::Span<const node::ExprNode*> values) {
return absl::OkStatus();

Check warning on line 27 in hybridse/src/codegen/insert_row_builder.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/codegen/insert_row_builder.cc#L26-L27

Added lines #L26 - L27 were not covered by tests
}

absl::StatusOr<llvm::Function*> InsertRowBuilder::BuildFn(absl::Span<const node::ExprNode*>) {
return absl::OkStatus();

Check warning on line 31 in hybridse/src/codegen/insert_row_builder.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/codegen/insert_row_builder.cc#L30-L31

Added lines #L30 - L31 were not covered by tests
}

// build the function that transform a single insert row values into encoded row
absl::StatusOr<llvm::Function*> InsertRowBuilder::BuildEncodeFn() { return absl::OkStatus(); }

Check warning on line 35 in hybridse/src/codegen/insert_row_builder.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/codegen/insert_row_builder.cc#L35

Added line #L35 was not covered by tests
} // namespace codegen
} // namespace hybridse
54 changes: 54 additions & 0 deletions hybridse/src/codegen/insert_row_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2024 OpenMLDB authors
*
* Licensed 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.
*/

#ifndef HYBRIDSE_SRC_CODEGEN_INSERT_ROW_BUILDER_H_
#define HYBRIDSE_SRC_CODEGEN_INSERT_ROW_BUILDER_H_

#include <memory>

#include "absl/status/statusor.h"
#include "codec/fe_row_codec.h"
#include "codegen/context.h"

namespace hybridse {
namespace codegen {

class InsertRowBuilder {
public:
InsertRowBuilder(CodeGenContextBase* ctx, const codec::Schema* schema);

// compute the encoded row result for insert statement's single values expression list
//
// currently, expressions in insert values do not expect external source, so unsupported expressions
// will simply fail on resolving.
absl::StatusOr<std::shared_ptr<int8_t>> ComputeRow(absl::Span<const node::ExprNode*> values);

private:
// build the function the will output the row from single insert values
//
// the function is just equivalent to C: `int8* fn()`.
// BuildFn returns different function with different name on every invocation
absl::StatusOr<llvm::Function*> BuildFn(absl::Span<const node::ExprNode*>);

// build the function that transform a single insert row values into encoded row
absl::StatusOr<llvm::Function*> BuildEncodeFn();

CodeGenContextBase* ctx_;
const codec::Schema* schema_;
};
} // namespace codegen
} // namespace hybridse
#endif // HYBRIDSE_SRC_CODEGEN_INSERT_ROW_BUILDER_H_
2 changes: 1 addition & 1 deletion hybridse/src/sdk/base_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace sdk {

static const std::string EMPTY_STR; // NOLINT

SchemaImpl::SchemaImpl(const vm::Schema& schema) : schema_(schema) {}
SchemaImpl::SchemaImpl(const codec::Schema& schema) : schema_(schema) {}

SchemaImpl::~SchemaImpl() {}

Expand Down
10 changes: 10 additions & 0 deletions src/codec/codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1152,5 +1152,15 @@ bool RowProject::Project(const int8_t* row_ptr, uint32_t size, int8_t** output_p
return true;
}

bool ColumnSupportLegacyCodec(const openmldb::common::ColumnDesc& col_desc) {
auto dt = col_desc.data_type();
if (col_desc.has_schema()) {
dt = col_desc.schema().type();
}

return (dt >= openmldb::type::kBool && dt <= openmldb::type::kTimestamp) || dt == openmldb::type::kVarchar ||
dt == openmldb::type::kString;
}

} // namespace codec
} // namespace openmldb
2 changes: 2 additions & 0 deletions src/codec/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ class RowView {
std::vector<uint32_t> offset_vec_;
};

bool ColumnSupportLegacyCodec(const openmldb::common::ColumnDesc&);

namespace v1 {

inline int8_t GetAddrSpace(uint32_t size) {
Expand Down
8 changes: 8 additions & 0 deletions src/sdk/sql_cluster_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ bool SQLClusterRouter::GetMultiRowInsertInfo(const std::string& db, const std::s
return false;
}
std::map<uint32_t, uint32_t> column_map;

bool insert_codegen = false;
for (size_t j = 0; j < insert_stmt->columns_.size(); ++j) {
const std::string& col_name = insert_stmt->columns_[j];
bool find_flag = false;
Expand All @@ -535,6 +537,12 @@ bool SQLClusterRouter::GetMultiRowInsertInfo(const std::string& db, const std::s
}
column_map.insert(std::make_pair(i, j));
find_flag = true;

auto& col_desc = (*table_info)->column_desc(i);
if (!codec::ColumnSupportLegacyCodec(col_desc)) {
insert_codegen = true;

Check warning on line 543 in src/sdk/sql_cluster_router.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/sql_cluster_router.cc#L543

Added line #L543 was not covered by tests
}

break;
}
}
Expand Down

0 comments on commit 69abf5b

Please sign in to comment.