diff --git a/CHANGES.md b/CHANGES.md index a3adbb3d7af9..a4a37d871185 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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)). diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/parser/FieldAccessDescriptorParser.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/parser/FieldAccessDescriptorParser.java index 367042ce0480..d6bbac6d1ed2 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/parser/FieldAccessDescriptorParser.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/parser/FieldAccessDescriptorParser.java @@ -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; @@ -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 { diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/FieldAccessDescriptorParserTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/FieldAccessDescriptorParserTest.java index bcca3e88c0d6..a88d13787414 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/FieldAccessDescriptorParserTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/FieldAccessDescriptorParserTest.java @@ -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}. */ @@ -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("\"", "\""))); + } }