Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@

## Bugfixes

* (Java) Invalid field access expressions, for example in `Select.fieldNames(...)` or `FieldAccessDescriptor.withFieldNames(...)`, now throw an `IllegalArgumentException` describing the syntax error instead of a `NullPointerException` ([#21284](https://github.com/apache/beam/issues/21284)).
* (Python) Fixed RunInference failing with `TypeError: cannot create weak reference to 'dict' object` when `ModelHandler.load_model()` returns a model that can't be weakly referenced, such as a `dict` or `list` ([#41351](https://github.com/apache/beam/issues/41351)).
* (Java) Flink `OrderedListState` now preserves multiple values with the same timestamp when reading state or clearing a different timestamp range ([#39782](https://github.com/apache/beam/issues/39782)).
* (Python) Fixed Vertex AI embedding transforms ignoring `project`, `location` and `credentials` on remote workers ([#41957](https://github.com/apache/beam/pull/41957)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

import java.util.List;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.TokenStream;
import org.apache.beam.sdk.annotations.Internal;
import org.apache.beam.sdk.schemas.FieldAccessDescriptor;
Expand Down Expand Up @@ -53,12 +56,45 @@
public class FieldAccessDescriptorParser {
public static FieldAccessDescriptor parse(String expr) {
CharStream charStream = CharStreams.fromString(expr);
ThrowingErrorListener errorListener = new ThrowingErrorListener(expr);
FieldSpecifierNotationLexer lexer = new FieldSpecifierNotationLexer(charStream);
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
TokenStream tokens = new CommonTokenStream(lexer);
FieldSpecifierNotationParser parser = new FieldSpecifierNotationParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
return new BuildFieldAccessDescriptor().visit(parser.dotExpression());
}

/**
* Fails the parse on the first syntax error. ANTLR's default listener only logs syntax errors and
* recovers, which left a partial parse tree that later failed with a {@link
* NullPointerException}.
*/
private static class ThrowingErrorListener extends BaseErrorListener {
private final String expr;

ThrowingErrorListener(String expr) {
this.expr = expr;
}

@Override
public void syntaxError(
Recognizer<?, ?> recognizer,
@Nullable Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
@Nullable RecognitionException e) {
throw new IllegalArgumentException(
String.format(
"Invalid field access expression '%s' at position %d: %s",
expr, charPositionInLine, msg),
e);
}
}

private static class BuildFieldAccessDescriptor
extends FieldSpecifierNotationBaseVisitor<FieldAccessDescriptor> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
*/
package org.apache.beam.sdk.schemas;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThrows;

import org.apache.beam.sdk.schemas.parser.FieldAccessDescriptorParser;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
import org.junit.Test;

/** Tests for {@link FieldAccessDescriptorParser}. */
Expand All @@ -26,4 +31,25 @@ public class FieldAccessDescriptorParserTest {
public void testSmoke() {
FieldAccessDescriptorParser.parse("a.b[][*].c.*");
}

@Test
public void testInvalidTokenThrowsIllegalArgumentException() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> FieldAccessDescriptorParser.parse("\""));
assertThat(e.getMessage(), containsString("Invalid field access expression '\"'"));
}

@Test
public void testIncompleteExpressionThrowsIllegalArgumentException() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> FieldAccessDescriptorParser.parse("a."));
assertThat(e.getMessage(), containsString("Invalid field access expression 'a.'"));
}

@Test
public void testWithFieldNamesAsInvalidExpressionThrowsIllegalArgumentException() {
assertThrows(
IllegalArgumentException.class,
() -> FieldAccessDescriptor.withFieldNamesAs(ImmutableMap.of("\"", "\"")));
}
}