-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: add parsers for events and unbounded expressions #4308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
df0e467
SNS Implementation for Event Notification
1edc3ec
Expression Parser (toJson) (#3)
8bc441e
Include ExpressionParser in EventParser (#4)
0c6a0bc
Add SQSListener for automatic integration test (#6)
29aa16e
Fix integration test and cleanup codebase
c57789f
Catalog Listener (#7) (#9)
f1cbe93
fix rebase
ed16ef5
refactor listener and catalog initialization logics
da57f74
Added Event Types in Json (#10)
7aec26f
EventParser and Expression fromJson Serialization (#11)
b969fb3
add some fixes to parser
55c1505
Updated FixedLiteral and Testing
kunal0829 e61ce99
Added fromJson Event Serialization
kunal0829 0aa2f8c
Fixed BaseLiteral
kunal0829 b545ade
Fixed Space
kunal0829 39dee63
Removed Listener Implementations
kunal0829 d990df1
Updated Parser
kunal0829 2bde5a8
Updated Bound Predicates Json Serialization
kunal0829 636c9b8
Updated Unbound Predicates Json Serialization
kunal0829 73ec648
Merge branch 'listener-and-event-parser' of https://github.com/kunal0…
kunal0829 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
312 changes: 312 additions & 0 deletions
312
core/src/main/java/org/apache/iceberg/expressions/ExpressionParser.java
This file contains hidden or 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,312 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| package org.apache.iceberg.expressions; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.io.IOException; | ||
| import java.io.StringWriter; | ||
| import java.io.UncheckedIOException; | ||
| import java.math.BigDecimal; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import org.apache.iceberg.exceptions.RuntimeIOException; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.types.Conversions; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.iceberg.util.JsonUtil; | ||
|
|
||
| public class ExpressionParser { | ||
| private static final String TYPE = "type"; | ||
| private static final String VALUE = "value"; | ||
| private static final String OPERATION = "operation"; | ||
| private static final String LITERALS = "literals"; | ||
| private static final String TERM = "term"; | ||
| private static final String LEFT_OPERAND = "left-operand"; | ||
| private static final String RIGHT_OPERAND = "right-operand"; | ||
| private static final String OPERAND = "operand"; | ||
| private static final String UNBOUND_PREDICATE = "unbound-predicate"; | ||
| private static final String BOUND_LITERAL_PREDICATE = "bound-literal-predicate"; | ||
| private static final String BOUND_SET_PREDICATE = "bound-set-predicate"; | ||
| private static final String BOUND_UNARY_PREDICATE = "bound-unary-predicate"; | ||
| private static final String NAMED_REFERENCE = "named-reference"; | ||
| private static final String BOUND_REFERENCE = "bound-reference"; | ||
|
|
||
| private static final Set<Expression.Operation> ONE_INPUTS = ImmutableSet.of( | ||
| Expression.Operation.IS_NULL, | ||
| Expression.Operation.NOT_NULL, | ||
| Expression.Operation.IS_NAN, | ||
| Expression.Operation.NOT_NAN); | ||
|
|
||
| private ExpressionParser() { | ||
| } | ||
|
|
||
| public static String toJson(Expression expression, boolean pretty) { | ||
| try { | ||
| StringWriter writer = new StringWriter(); | ||
| JsonGenerator generator = JsonUtil.factory().createGenerator(writer); | ||
| if (pretty) { | ||
| generator.useDefaultPrettyPrinter(); | ||
| } | ||
| toJson(expression, generator); | ||
| generator.flush(); | ||
| return writer.toString(); | ||
|
|
||
| } catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to write json", e); | ||
| } | ||
| } | ||
|
|
||
| public static void toJson(Expression expression, JsonGenerator generator) throws IOException { | ||
| if (expression instanceof And) { | ||
| toJson((And) expression, generator); | ||
| } else if (expression instanceof Or) { | ||
| toJson((Or) expression, generator); | ||
| } else if (expression instanceof Not) { | ||
| toJson((Not) expression, generator); | ||
| } else if (expression instanceof True) { | ||
| toJson((True) expression, generator); | ||
| } else if (expression instanceof False) { | ||
| toJson((False) expression, generator); | ||
| } else if (expression instanceof Predicate) { | ||
| toJson((Predicate<?, ?>) expression, generator); | ||
| } else { | ||
| throw new IllegalArgumentException("Invalid Operation Type"); | ||
| } | ||
| } | ||
|
|
||
| private static void toJson(And expression, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
| generator.writeStringField(OPERATION, Expression.Operation.AND.name().toLowerCase()); | ||
| generator.writeFieldName(LEFT_OPERAND); | ||
| toJson(expression.left(), generator); | ||
| generator.writeFieldName(RIGHT_OPERAND); | ||
| toJson(expression.right(), generator); | ||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| private static void toJson(Or expression, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
| generator.writeStringField(OPERATION, Expression.Operation.OR.name().toLowerCase()); | ||
| generator.writeFieldName(LEFT_OPERAND); | ||
| toJson(expression.left(), generator); | ||
| generator.writeFieldName(RIGHT_OPERAND); | ||
| toJson(expression.right(), generator); | ||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| private static void toJson(Not expression, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
| generator.writeStringField(OPERATION, Expression.Operation.NOT.name().toLowerCase()); | ||
| generator.writeFieldName(OPERAND); | ||
| toJson(expression.child(), generator); | ||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| private static void toJson(True expression, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
| generator.writeStringField(OPERATION, Expression.Operation.TRUE.name().toLowerCase()); | ||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| private static void toJson(False expression, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
| generator.writeStringField(OPERATION, Expression.Operation.FALSE.name().toLowerCase()); | ||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| private static void toJson(Predicate<?, ?> predicate, JsonGenerator generator) throws IOException { | ||
| if (predicate instanceof UnboundPredicate) { | ||
| toJson((UnboundPredicate<?>) predicate, generator); | ||
| } else { | ||
| throw new IllegalArgumentException("Cannot convert predicate " + predicate); | ||
| } | ||
| } | ||
|
|
||
| private static void toJson(UnboundPredicate<?> predicate, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
| generator.writeStringField(OPERATION, UNBOUND_PREDICATE); | ||
| generator.writeStringField(TYPE, predicate.op().name().toLowerCase()); | ||
| generator.writeFieldName(TERM); | ||
| toJson(predicate.term(), generator); | ||
| if (!ONE_INPUTS.contains(predicate.op())) { | ||
| generator.writeFieldName(LITERALS); | ||
| generator.writeStartArray(); | ||
| for (Literal<?> literal : predicate.literals()) { | ||
| toJson(literal, generator); | ||
| } | ||
| generator.writeEndArray(); | ||
| } | ||
|
|
||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| private static void toJson(Term term, JsonGenerator generator) throws IOException { | ||
| if (term instanceof NamedReference) { | ||
| toJson((NamedReference<?>) term, generator); | ||
| } else { | ||
| throw new IllegalArgumentException("Cannot convert term " + term); | ||
| } | ||
| } | ||
|
|
||
| private static void toJson(NamedReference<?> term, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
| generator.writeStringField(TYPE, NAMED_REFERENCE); | ||
| generator.writeStringField(VALUE, term.name()); | ||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| private static void toJson(Literal<?> literal, JsonGenerator generator) throws IOException { | ||
| generator.writeStartObject(); | ||
|
|
||
| Object value = literal.value(); | ||
| Type type; | ||
| if (value instanceof Boolean) { | ||
| type = Types.BooleanType.get(); | ||
| } else if (value instanceof Integer) { | ||
| type = Types.IntegerType.get(); | ||
| } else if (value instanceof Long) { | ||
| type = Types.LongType.get(); | ||
| } else if (value instanceof Float) { | ||
| type = Types.FloatType.get(); | ||
| } else if (value instanceof Double) { | ||
| type = Types.DoubleType.get(); | ||
| } else if (value instanceof CharSequence) { | ||
| type = Types.StringType.get(); | ||
| } else if (value instanceof UUID) { | ||
| type = Types.UUIDType.get(); | ||
| } else if (value instanceof byte[]) { | ||
| type = Types.FixedType.ofLength(((byte[]) value).length); | ||
| } else if (value instanceof ByteBuffer) { | ||
| if (literal instanceof Literals.FixedLiteral) { | ||
| type = Types.FixedType.ofLength(((ByteBuffer) value).remaining()); | ||
| } else { | ||
| type = Types.BinaryType.get(); | ||
| } | ||
| } else if (value instanceof BigDecimal) { | ||
| BigDecimal decimal = (BigDecimal) value; | ||
| type = Types.DecimalType.of(decimal.precision(), decimal.scale()); | ||
| } else { | ||
| throw new IllegalArgumentException("Cannot find literal type for value class " + value.getClass().getName()); | ||
| } | ||
|
|
||
| generator.writeStringField(TYPE, type.toString()); | ||
| generator.writeStringField(VALUE, StandardCharsets.UTF_8.decode(literal.toByteBuffer()).toString()); | ||
| generator.writeEndObject(); | ||
| } | ||
|
|
||
| public static Expression fromJson(String json) { | ||
| try { | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| return fromJson(mapper.readTree(json)); | ||
| } catch (IOException e) { | ||
| throw new RuntimeIOException(e); | ||
| } | ||
| } | ||
|
|
||
| public static Expression fromJson(JsonNode json) { | ||
| String expressionType; | ||
| if (json.hasNonNull(OPERATION)) { | ||
| expressionType = JsonUtil.getString(OPERATION, json); | ||
| } else { | ||
| return null; | ||
| } | ||
|
|
||
| if (Expression.Operation.AND.name().toLowerCase().equals(expressionType)) { | ||
| return new And(fromJson(json.get(LEFT_OPERAND)), fromJson(json.get(RIGHT_OPERAND))); | ||
| } else if (Expression.Operation.OR.name().toLowerCase().equals(expressionType)) { | ||
| return new Or(fromJson(json.get(LEFT_OPERAND)), fromJson(json.get(RIGHT_OPERAND))); | ||
| } else if (Expression.Operation.NOT.name().toLowerCase().equals(expressionType)) { | ||
| return new Not(fromJson(json.get(OPERAND))); | ||
| } else if (Expression.Operation.TRUE.name().toLowerCase().equals(expressionType)) { | ||
| return True.INSTANCE; | ||
| } else if (Expression.Operation.FALSE.name().toLowerCase().equals(expressionType)) { | ||
| return False.INSTANCE; | ||
| } else { | ||
| return fromJsonToPredicate(json, expressionType); | ||
| } | ||
| } | ||
|
|
||
| private static Predicate<?, ?> fromJsonToPredicate(JsonNode json, String predicateType) { | ||
| if (UNBOUND_PREDICATE.equals(predicateType)) { | ||
| return fromJsonUnboundPredicate(json); | ||
| } else if (BOUND_LITERAL_PREDICATE.equals(predicateType) || | ||
| BOUND_SET_PREDICATE.equals(predicateType) || | ||
| BOUND_UNARY_PREDICATE.equals(predicateType)) { | ||
| throw new UnsupportedOperationException( | ||
| "Serialization of Bound Predicates is not currently supported."); | ||
| } else { | ||
| throw new IllegalArgumentException("Invalid Predicate Type"); | ||
| } | ||
| } | ||
|
|
||
| private static UnboundPredicate<?> fromJsonUnboundPredicate(JsonNode json) { | ||
| Expression.Operation operationType = Expression.Operation.valueOf( | ||
| JsonUtil.getString(TYPE, json).toUpperCase()); | ||
|
|
||
| if (ONE_INPUTS.contains(operationType)) { | ||
| return new UnboundPredicate<>(operationType, fromJsonToTerm(json.get(TERM))); | ||
| } else { | ||
| return new UnboundPredicate( | ||
| operationType, | ||
| fromJsonToTerm(json.get(TERM)), | ||
| fromJsonToLiteralValues(json.get(LITERALS))); | ||
| } | ||
| } | ||
|
|
||
| private static UnboundTerm<?> fromJsonToTerm(JsonNode json) { | ||
| String referenceType = json.get(TYPE).textValue(); | ||
|
|
||
| if (referenceType.equals(NAMED_REFERENCE)) { | ||
| return new NamedReference<>(json.get(VALUE).textValue()); | ||
| } else if (referenceType.equals(BOUND_REFERENCE)) { | ||
| throw new UnsupportedOperationException( | ||
| "Serialization of Predicate type BoundReference is not currently supported."); | ||
| } else { | ||
| throw new IllegalArgumentException("Invalid Term Reference Type"); | ||
| } | ||
| } | ||
|
|
||
| private static List<Object> fromJsonToLiteralValues(JsonNode json) { | ||
| List<Object> literals = Lists.newArrayList(); | ||
| for (int i = 0; i < json.size(); i++) { | ||
| String literalType = json.get(i).get(TYPE).textValue(); | ||
| Type primitiveType = Types.fromPrimitiveString(literalType); | ||
| Object value = Conversions.fromByteBuffer( | ||
| primitiveType, | ||
| StandardCharsets.UTF_8.encode(json.get(i).get(VALUE).textValue())); | ||
| if (primitiveType.typeId() == Type.TypeID.FIXED) { | ||
| byte[] valueByteArray = new byte[((ByteBuffer) value).remaining()]; | ||
| ((ByteBuffer) value).get(valueByteArray); | ||
| value = valueByteArray; | ||
| } | ||
|
|
||
| literals.add(value); | ||
| } | ||
|
|
||
| return literals; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Iceberg separates the recursive logic to process a tree structure from the logic to do something by using visitors. I think that this should use an in-order expression visitor to produce a JSON representation.