Skip to content

Commit

Permalink
[ISSUE-193] Support date type for dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhiwei authored Oct 11, 2023
1 parent dbf759d commit 4288afa
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.antgroup.geaflow.common.type.primitive.BinaryStringType;
import com.antgroup.geaflow.common.type.primitive.BooleanType;
import com.antgroup.geaflow.common.type.primitive.ByteType;
import com.antgroup.geaflow.common.type.primitive.DateType;
import com.antgroup.geaflow.common.type.primitive.DecimalType;
import com.antgroup.geaflow.common.type.primitive.DoubleType;
import com.antgroup.geaflow.common.type.primitive.FloatType;
Expand All @@ -28,6 +29,7 @@
import com.antgroup.geaflow.common.type.primitive.TimestampType;
import com.google.common.collect.ImmutableMap;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Locale;

Expand All @@ -52,6 +54,7 @@ public class Types {
public static final String TYPE_NAME_OBJECT = "OBJECT";
public static final String TYPE_NAME_BINARY_STRING = "BINARY_STRING";
public static final String TYPE_NAME_TIMESTAMP = "TIMESTAMP";
public static final String TYPE_NAME_DATE = "DATE";

public static final IType<Boolean> BOOLEAN = BooleanType.INSTANCE;
public static final IType<Byte> BYTE = ByteType.INSTANCE;
Expand All @@ -64,6 +67,7 @@ public class Types {
public static final IType<BigDecimal> DECIMAL = DecimalType.INSTANCE;
public static final IType<BinaryString> BINARY_STRING = BinaryStringType.INSTANCE;
public static final IType<Timestamp> TIMESTAMP = TimestampType.INSTANCE;
public static final IType<Date> DATE = DateType.INSTANCE;

public static final ImmutableMap<Class, IType> TYPE_IMMUTABLE_MAP =
ImmutableMap.<Class, IType>builder()
Expand All @@ -78,6 +82,7 @@ public class Types {
.put(DECIMAL.getTypeClass(), DECIMAL)
.put(BINARY_STRING.getTypeClass(), BINARY_STRING)
.put(TIMESTAMP.getTypeClass(), TIMESTAMP)
.put(DATE.getTypeClass(), DATE)
.build();

public static <T> IType<T> getType(Class<T> type) {
Expand Down Expand Up @@ -109,6 +114,8 @@ public static IType<?> of(String typeName) {
return BINARY_STRING;
case TYPE_NAME_TIMESTAMP:
return TIMESTAMP;
case TYPE_NAME_DATE:
return DATE;
default:
throw new IllegalArgumentException("Not support typeName: " + typeName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.antgroup.geaflow.common.type.primitive;

import com.antgroup.geaflow.common.type.IType;
import com.antgroup.geaflow.common.type.Types;
import com.google.common.primitives.Longs;
import java.sql.Date;

public class DateType implements IType<Date> {

public static final DateType INSTANCE = new DateType();

@Override
public String getName() {
return Types.TYPE_NAME_DATE;
}

@Override
public Class<Date> getTypeClass() {
return Date.class;
}

@Override
public byte[] serialize(Date obj) {
return Longs.toByteArray(obj.getTime());
}

@Override
public Date deserialize(byte[] bytes) {
return new Date(Longs.fromByteArray(bytes));
}

@Override
public int compare(Date x, Date y) {
return Long.compare(x.getTime(), y.getTime());
}

@Override
public boolean isPrimitive() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.antgroup.geaflow.dsl.common.types.ArrayType;
import com.antgroup.geaflow.dsl.common.util.FunctionCallUtils;
import java.lang.reflect.Array;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Locale;

Expand Down Expand Up @@ -60,6 +61,9 @@ public static PropertyFieldReader<?> getPropertyFieldReader(IType<?> type) {
case Types.TYPE_NAME_TIMESTAMP:
return (baseObject, offset) -> new Timestamp(BinaryOperations.getLong(baseObject,
offset));
case Types.TYPE_NAME_DATE:
return (baseObject, offset) -> new Date(BinaryOperations.getLong(baseObject,
offset));
case Types.TYPE_NAME_OBJECT:
case Types.TYPE_NAME_VERTEX:
case Types.TYPE_NAME_EDGE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.antgroup.geaflow.common.type.Types;
import com.antgroup.geaflow.dsl.common.exception.GeaFlowDSLException;
import com.antgroup.geaflow.dsl.common.types.ArrayType;
import java.sql.Timestamp;
import java.util.Locale;

public class FieldWriterFactory {
Expand Down Expand Up @@ -81,7 +80,8 @@ public static PropertyFieldWriter<?> getPropertyFieldWriter(IType<?> type) {
}
};
case Types.TYPE_NAME_TIMESTAMP:
return (PropertyFieldWriter<Timestamp>) (writerBuffer, nullBitsOffset, index, value) -> {
case Types.TYPE_NAME_DATE:
return (PropertyFieldWriter<java.util.Date>) (writerBuffer, nullBitsOffset, index, value) -> {
if (value == null) {
writerBuffer.setNullAt(nullBitsOffset, index);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.antgroup.geaflow.common.type.IType;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;

public class TypeCastUtil {
Expand Down Expand Up @@ -146,8 +147,14 @@ public static Object cast(Object o, Class<?> targetType) {
return BinaryString.fromString(o.toString());
}
if (targetType == Timestamp.class) {
if (isInteger((String) o)) {
return new Timestamp(Long.parseLong((String) o));
}
return Timestamp.valueOf((String) o);
}
if (targetType == Date.class) {
return Date.valueOf((String) o);
}
}
if (o.getClass() == BinaryString.class) {
if (targetType == Long.class) {
Expand All @@ -166,8 +173,14 @@ public static Object cast(Object o, Class<?> targetType) {
return o.toString();
}
if (targetType == Timestamp.class) {
if (isInteger((BinaryString) o)) {
return new Timestamp(Long.parseLong(o.toString()));
}
return Timestamp.valueOf(o.toString());
}
if (targetType == Date.class) {
return Date.valueOf(o.toString());
}
}
if (o.getClass() == Double.class) {
if (targetType == BigDecimal.class) {
Expand Down Expand Up @@ -199,4 +212,28 @@ public static Object cast(Object o, Class<?> targetType) {
}
throw new IllegalArgumentException("Cannot cast " + o + " from " + o.getClass() + " to " + targetType);
}

public static boolean isInteger(String s) {
if (s == null) {
return false;
}
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
return false;
}
}
return true;
}

public static boolean isInteger(BinaryString s) {
if (s == null) {
return false;
}
for (int i = 0; i < s.getLength(); i++) {
if (!Character.isDigit(s.getByte(i))) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.antgroup.geaflow.dsl.runtime.query;

import org.testng.annotations.Test;

public class TypesTest {

@Test
public void testBooleanType_001() throws Exception {
QueryTester
.build()
.withQueryPath("/query/type_boolean_001.sql")
.execute()
.checkSinkResult();
}

@Test
public void testTimestampType_001() throws Exception {
QueryTester
.build()
.withQueryPath("/query/type_timestamp_001.sql")
.execute()
.checkSinkResult();
}

@Test
public void testDateType_001() throws Exception {
QueryTester
.build()
.withQueryPath("/query/type_date_001.sql")
.execute()
.checkSinkResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1489576693884, 10, 170.0, alex, true, 2023-10-10
1489576693883, 11, 175.0, Jane, true, 2023-10-01
1489576693822, 12, 170.0, jack, false, 2023-10-02
1489576693834, 13, 165.0, snow, false, 2023-10-03
1489576693834, 15, 170.0, one, false, 2023-10-02
1489576693834, 16, 160.0, sam, false, 2023-10-02
1489576693883, 17, 170.0, dog, false, 2023-10-03
1489576693844, 18, 180.0, mahone, false, 2023-10-04
1489576693854, 19, 170.0, tbeg, false, 2023-10-05
1489576693864, 31, 180.0, ok, false, 2023-10-05
1489576693874, 42, 190.0, good1, false, 2023-10-06
1489576693874, 40, 190.0, good2, false, 2023-10-07
1489576693874, 41, 190.0, good3, false, 2023-10-08
1489576693874, 43, 190.0, good4, false, 2023-10-08
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
false,12
true,2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
2023-10-10,1
2023-10-05,2
2023-10-03,2
2023-10-08,2
2023-10-01,1
2023-10-06,1
2023-10-04,1
2023-10-07,1
2023-10-02,3
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
2017-03-15 19:18:13.883,2
2017-03-15 19:18:13.864,1
2017-03-15 19:18:13.822,1
2017-03-15 19:18:13.854,1
2017-03-15 19:18:13.884,1
2017-03-15 19:18:13.874,4
2017-03-15 19:18:13.844,1
2017-03-15 19:18:13.834,3
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE IF NOT EXISTS users3 (
rt bigint,
f1 bigint,
f2 double,
f3 varchar,
f4 boolean,
f5 varchar
) WITH (
type='file',
geaflow.dsl.file.path = 'resource:///data/users3.txt'
);

CREATE TABLE console(
f4 boolean,
cnt bigint
) WITH (
type='file',
geaflow.dsl.file.path='${target}'
);

INSERT INTO console
SELECT
f4,
count(f1)
FROM users3
group by f4
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE IF NOT EXISTS users3 (
rt TIMESTAMP,
f1 bigint,
f2 double,
f3 varchar,
f4 boolean,
f5 date
) WITH (
type='file',
geaflow.dsl.file.path = 'resource:///data/users3.txt'
);

CREATE TABLE console(
f5 date,
cnt bigint
) WITH (
type='file',
geaflow.dsl.file.path='${target}'
);

INSERT INTO console
SELECT
f5,
count(f1)
FROM users3
group by f5
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE IF NOT EXISTS users3 (
rt TIMESTAMP,
f1 bigint,
f2 double,
f3 varchar,
f4 boolean,
f5 varchar
) WITH (
type='file',
geaflow.dsl.file.path = 'resource:///data/users3.txt'
);

CREATE TABLE console(
rt TIMESTAMP,
cnt bigint
) WITH (
type='file',
geaflow.dsl.file.path='${target}'
);

INSERT INTO console
SELECT
rt,
count(f1)
FROM users3
group by rt
;

0 comments on commit 4288afa

Please sign in to comment.