Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add cast(ObjectId() as string) in mongodb connector #17225

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.LiteralParameters;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.ScalarOperator;
import com.facebook.presto.spi.function.SqlNullable;
Expand All @@ -25,13 +26,15 @@
import org.bson.types.ObjectId;

import static com.facebook.presto.common.function.OperatorType.BETWEEN;
import static com.facebook.presto.common.function.OperatorType.CAST;
import static com.facebook.presto.common.function.OperatorType.EQUAL;
import static com.facebook.presto.common.function.OperatorType.GREATER_THAN;
import static com.facebook.presto.common.function.OperatorType.GREATER_THAN_OR_EQUAL;
import static com.facebook.presto.common.function.OperatorType.HASH_CODE;
import static com.facebook.presto.common.function.OperatorType.LESS_THAN;
import static com.facebook.presto.common.function.OperatorType.LESS_THAN_OR_EQUAL;
import static com.facebook.presto.common.function.OperatorType.NOT_EQUAL;
import static io.airlift.slice.Slices.utf8Slice;

public class ObjectIdFunctions
{
Expand All @@ -53,6 +56,15 @@ public static Slice ObjectId(@SqlType(StandardTypes.VARCHAR) Slice value)
return Slices.wrappedBuffer(new ObjectId(CharMatcher.is(' ').removeFrom(value.toStringUtf8())).toByteArray());
}

@ScalarOperator(CAST)
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice castToVarchar(@SqlType("ObjectId") Slice value)
{
String hexString = new ObjectId(value.getBytes()).toString();
return utf8Slice(hexString);
}

@ScalarOperator(EQUAL)
@SqlType(StandardTypes.BOOLEAN)
@SqlNullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public void createTableWithEveryType()
", true _boolean" +
", DATE '1980-05-07' _date" +
", TIMESTAMP '1980-05-07 11:22:33.456' _timestamp" +
", ObjectId('ffffffffffffffffffffffff') _objectid";
", ObjectId('ffffffffffffffffffffffff') _objectid" +
", cast(ObjectId('ffffffffffffffffffffffff') as string) _objectid_string";

assertUpdate(query, 1);

Expand All @@ -90,6 +91,7 @@ public void createTableWithEveryType()
assertEquals(row.getField(4), true);
assertEquals(row.getField(5), LocalDate.of(1980, 5, 7));
assertEquals(row.getField(6), LocalDateTime.of(1980, 5, 7, 11, 22, 33, 456_000_000));
assertEquals(row.getField(8), "ffffffffffffffffffffffff");
assertUpdate("DROP TABLE test_types_table");

assertFalse(getQueryRunner().tableExists(getSession(), "test_types_table"));
Expand Down