Skip to content

Commit

Permalink
introduce uuid type + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dshaaban01 committed Jan 1, 2025
1 parent 584a68e commit 21197c0
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def Substrait_Dialect : Dialect {
more natural in MLIR to represent several message types as a single op and
express message sub-types with interfaces instead.
}];
let useDefaultAttributePrinterParser = 1;
let useDefaultTypePrinterParser = 1;
}

#endif // SUBSTRAIT_DIALECT_SUBSTRAIT_IR_SUBSTRAITDIALECT
24 changes: 22 additions & 2 deletions include/substrait-mlir/Dialect/Substrait/IR/SubstraitTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,41 @@ class Substrait_Attr<string name, string typeMnemonic, list<Trait> traits = []>
let mnemonic = typeMnemonic;
}

def Substrait_UUIDType : Substrait_Type<"UUID", "uuid"> {
let summary = "Substrait uuid type";
let description = [{
This type represents a substrait uuid (universally-unique identifier) type.
This is a 16-byte binary value.
}];
}

def Substrait_UUIDAttr : Substrait_Attr<"UUID", "binary"> {
let summary = "Substrait uuid type";
let description = [{
This type represents a substrait uuid (universally-unique identifier) attribute type.
This is a 16-byte binary value.
}];
let parameters = (ins ArrayRefParameter<"uint8_t", "">:$value);
let assemblyFormat = [{ `<` $value `>` }];
}

/// Currently supported atomic types. These correspond directly to the types in
/// https://github.com/substrait-io/substrait/blob/main/proto/substrait/type.proto.
// TODO(ingomueller): Add the other low-hanging fruits here.
def Substrait_AtomicTypes {
list<Type> types = [
SI1, // Boolean
SI32 // I32
SI32, // I32
Substrait_UUIDType, // UUID
];
}

/// Attributes of currently supported atomic types.
def Substrait_AtomicAttributes {
list<Attr> attrs = [
SI1Attr, // Boolean
SI32Attr // I32
SI32Attr, // I32
Substrait_UUIDAttr, // UUID
];
}

Expand Down
16 changes: 16 additions & 0 deletions lib/Target/SubstraitPB/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ SubstraitExporter::exportType(Location loc, mlir::Type mlirType) {
return std::move(type);
}

// Handle uuid.
if (mlirType.isa<UUIDType>()) {
// TODO(ingomueller): support other nullability modes.
auto uuidType = std::make_unique<proto::Type::UUID>();
uuidType->set_nullability(
Type_Nullability::Type_Nullability_NULLABILITY_REQUIRED);

auto type = std::make_unique<proto::Type>();
type->set_allocated_uuid(uuidType.release());
return std::move(type);
}

if (auto tupleType = llvm::dyn_cast<TupleType>(mlirType)) {
auto structType = std::make_unique<proto::Type::Struct>();
for (mlir::Type fieldType : tupleType.getTypes()) {
Expand Down Expand Up @@ -428,6 +440,10 @@ SubstraitExporter::exportOperation(LiteralOp op) {
default:
op->emitOpError("has integer value with unsupported width");
}
} // `UUIDType`.
else if (auto binaryType = dyn_cast<UUIDType>(literalType)) {
ArrayRef<uint8_t> ref = value.cast<UUIDAttr>().getValue();
literal->set_binary(std::string(ref.begin(), ref.end()));
} else
op->emitOpError("has unsupported value");

Expand Down
9 changes: 9 additions & 0 deletions lib/Target/SubstraitPB/Import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ static mlir::FailureOr<mlir::Type> importType(MLIRContext *context,
return IntegerType::get(context, 1, IntegerType::Signed);
case proto::Type::kI32:
return IntegerType::get(context, 32, IntegerType::Signed);
case proto::Type::kUuid:
return UUIDType::get(context);
case proto::Type::kStruct: {
const proto::Type::Struct &structType = type.struct_();
llvm::SmallVector<mlir::Type> fieldTypes;
Expand Down Expand Up @@ -266,6 +268,13 @@ importLiteral(ImplicitLocOpBuilder builder,
IntegerType::get(context, 32, IntegerType::Signed), message.i32());
return builder.create<LiteralOp>(attr);
}
case Expression::Literal::LiteralTypeCase::kUuid: {
auto attr = UUIDAttr::get(
context, ArrayRef<uint8_t>(
reinterpret_cast<const uint8_t *>(message.uuid().data()),
message.uuid().size()));
return builder.create<LiteralOp>(attr);
}
default: {
const pb::FieldDescriptor *desc =
Expression::Literal::GetDescriptor()->FindFieldByNumber(literalType);
Expand Down
25 changes: 25 additions & 0 deletions test/Target/SubstraitPB/Export/types.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@
// RUN: --split-input-file --output-split-marker="# -----" \
// RUN: | FileCheck %s

// CHECK-LABEL: relations {
// CHECK-NEXT: rel {
// CHECK-NEXT: read {
// CHECK: base_schema {
// CHECK-NEXT: names: "a"
// CHECK-NEXT: struct {
// CHECK-NEXT: types {
// CHECK-NEXT: uuid {
// CHECK-NEXT: nullability: NULLABILITY_REQUIRED
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: nullability: NULLABILITY_REQUIRED
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: named_table {

substrait.plan version 0 : 42 : 1 {
relation {
%0 = named_table @t1 as ["a"] : tuple<!substrait.uuid>
yield %0 : tuple<!substrait.uuid>
}
}

// -----

// CHECK-LABEL: relations {
// CHECK-NEXT: rel {
// CHECK-NEXT: read {
Expand Down
36 changes: 36 additions & 0 deletions test/Target/SubstraitPB/Import/types.textpb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,42 @@
# RUN: --split-input-file="# ""-----" --output-split-marker="// -----" \
# RUN: | FileCheck %s

# CHECK: substrait.plan
# CHECK-NEXT: relation
# CHECK-NEXT: named_table
# CHECK-SAME: : tuple<!substrait.uuid>

relations {
rel {
read {
common {
direct {
}
}
base_schema {
names: "a"
struct {
types {
uuid {
nullability: NULLABILITY_REQUIRED
}
}
nullability: NULLABILITY_REQUIRED
}
}
named_table {
names: "t1"
}
}
}
}
version {
minor_number: 42
patch_number: 1
}

# -----

# CHECK: substrait.plan
# CHECK-NEXT: relation
# CHECK-NEXT: named_table
Expand Down

0 comments on commit 21197c0

Please sign in to comment.