forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for nested expression serialization
Signed-off-by: MaxKsyunz <maxk@bitquilltech.com>
- Loading branch information
MaxKsyunz
committed
Mar 21, 2023
1 parent
ec5fb40
commit ba9130a
Showing
5 changed files
with
150 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...c/main/java/org/opensearch/sql/expression/serialization/NoEncodeExpressionSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.serialization; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import org.opensearch.sql.expression.Expression; | ||
|
||
public class NoEncodeExpressionSerializer { | ||
|
||
/** | ||
* Serialize an expression into a byte array. | ||
*/ | ||
public byte[] serialize(Expression expr) { | ||
try { | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
ObjectOutputStream objectOutput = new ObjectOutputStream(output); | ||
objectOutput.writeObject(expr); | ||
objectOutput.flush(); | ||
return output.toByteArray(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Failed to serialize expression: " + expr, e); | ||
} | ||
} | ||
|
||
/** | ||
* Create an expression from a serialized byte array. | ||
*/ | ||
public Expression deserialize(byte[] code) { | ||
try { | ||
ByteArrayInputStream input = new ByteArrayInputStream(code); | ||
ObjectInputStream objectInput = new ObjectInputStream(input); | ||
return (Expression) objectInput.readObject(); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Failed to deserialize expression code: " + code, e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...st/java/org/opensearch/sql/expression/serialization/NoEncodeExpressionSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.serialization; | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
import static org.opensearch.sql.expression.DSL.literal; | ||
import static org.opensearch.sql.expression.DSL.ref; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.DSL; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.ExpressionNodeVisitor; | ||
import org.opensearch.sql.expression.env.Environment; | ||
|
||
class NoEncodeExpressionSerializerTest { | ||
|
||
private final NoEncodeExpressionSerializer serializer = new NoEncodeExpressionSerializer(); | ||
|
||
@Test | ||
void can_serialize_and_deserialize_literals() { | ||
Expression original = literal(10); | ||
Expression actual = serializer.deserialize(serializer.serialize(original)); | ||
assertEquals(original, actual); | ||
} | ||
|
||
@Test | ||
void can_serialize_and_deserialize_references() { | ||
Expression original = ref("name", STRING); | ||
Expression actual = serializer.deserialize(serializer.serialize(original)); | ||
assertEquals(original, actual); | ||
} | ||
|
||
@Test | ||
void can_serialize_and_deserialize_predicates() { | ||
Expression original = DSL.or(literal(true), DSL.less(literal(1), literal(2))); | ||
Expression actual = serializer.deserialize(serializer.serialize(original)); | ||
assertEquals(original, actual); | ||
} | ||
|
||
@Test | ||
void can_serialize_and_deserialize_functions() { | ||
Expression original = DSL.abs(literal(30.0)); | ||
Expression actual = serializer.deserialize(serializer.serialize(original)); | ||
assertEquals(original, actual); | ||
} | ||
|
||
@Test | ||
void cannot_serialize_illegal_expression() { | ||
Expression illegalExpr = new Expression() { | ||
private final Object object = new Object(); // non-serializable | ||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(ExpressionNodeVisitor<T, C> visitor, C context) { | ||
return null; | ||
} | ||
}; | ||
assertThrows(IllegalStateException.class, () -> serializer.serialize(illegalExpr)); | ||
} | ||
|
||
@Test | ||
void cannot_deserialize_illegal_expression_code() { | ||
var arr = "hello world".getBytes(); | ||
assertThrows(IllegalStateException.class, () -> serializer.deserialize(arr)); | ||
} | ||
} |