use of io.trino.sql.tree.PatternConcatenation in project trino by trinodb.
the class TestSqlParser method testRowPattern.
@Test
public void testRowPattern() {
assertThat(rowPattern("(A B)* | CC+? DD?? E | (F | G)")).isEqualTo(new PatternAlternation(location(1, 1), ImmutableList.of(new PatternAlternation(location(1, 1), ImmutableList.of(new QuantifiedPattern(location(1, 1), new PatternConcatenation(location(1, 2), ImmutableList.of(new PatternVariable(location(1, 2), new Identifier(location(1, 2), "A", false)), new PatternVariable(location(1, 4), new Identifier(location(1, 4), "B", false)))), new ZeroOrMoreQuantifier(location(1, 6), true)), new PatternConcatenation(location(1, 10), ImmutableList.of(new PatternConcatenation(location(1, 10), ImmutableList.of(new QuantifiedPattern(location(1, 10), new PatternVariable(location(1, 10), new Identifier(location(1, 10), "CC", false)), new OneOrMoreQuantifier(location(1, 12), false)), new QuantifiedPattern(location(1, 15), new PatternVariable(location(1, 15), new Identifier(location(1, 15), "DD", false)), new ZeroOrOneQuantifier(location(1, 17), false)))), new PatternVariable(location(1, 20), new Identifier(location(1, 20), "E", false)))))), new PatternAlternation(location(1, 25), ImmutableList.of(new PatternVariable(location(1, 25), new Identifier(location(1, 25), "F", false)), new PatternVariable(location(1, 29), new Identifier(location(1, 29), "G", false)))))));
assertThat(rowPattern("A | B | C D E F")).isEqualTo(new PatternAlternation(location(1, 1), ImmutableList.of(new PatternAlternation(location(1, 1), ImmutableList.of(new PatternVariable(location(1, 1), new Identifier(location(1, 1), "A", false)), new PatternVariable(location(1, 5), new Identifier(location(1, 5), "B", false)))), new PatternConcatenation(location(1, 9), ImmutableList.of(new PatternConcatenation(location(1, 9), ImmutableList.of(new PatternConcatenation(location(1, 9), ImmutableList.of(new PatternVariable(location(1, 9), new Identifier(location(1, 9), "C", false)), new PatternVariable(location(1, 11), new Identifier(location(1, 11), "D", false)))), new PatternVariable(location(1, 13), new Identifier(location(1, 13), "E", false)))), new PatternVariable(location(1, 15), new Identifier(location(1, 15), "F", false)))))));
assertThatThrownBy(() -> SQL_PARSER.createRowPattern("A!")).isInstanceOf(ParsingException.class).hasMessageMatching("line 1:2: mismatched input '!'.*");
assertThatThrownBy(() -> SQL_PARSER.createRowPattern("A**")).isInstanceOf(ParsingException.class).hasMessageMatching("line 1:3: mismatched input '*'.*");
assertThat(rowPattern("A??")).isEqualTo(new QuantifiedPattern(location(1, 1), new PatternVariable(location(1, 1), new Identifier(location(1, 1), "A", false)), new ZeroOrOneQuantifier(location(1, 2), false)));
assertThat(rowPattern("^$")).isEqualTo(new PatternConcatenation(location(1, 1), ImmutableList.of(new AnchorPattern(location(1, 1), AnchorPattern.Type.PARTITION_START), new AnchorPattern(location(1, 2), AnchorPattern.Type.PARTITION_END))));
assertThat(rowPattern("()")).isEqualTo(new EmptyPattern(location(1, 1)));
assertThat(rowPattern("A{3}")).isEqualTo(new QuantifiedPattern(location(1, 1), new PatternVariable(location(1, 1), new Identifier(location(1, 1), "A", false)), new RangeQuantifier(location(1, 2), true, Optional.of(new LongLiteral(location(1, 3), "3")), Optional.of(new LongLiteral(location(1, 3), "3")))));
assertThat(rowPattern("A{3,}")).isEqualTo(new QuantifiedPattern(location(1, 1), new PatternVariable(location(1, 1), new Identifier(location(1, 1), "A", false)), new RangeQuantifier(location(1, 2), true, Optional.of(new LongLiteral(location(1, 3), "3")), Optional.empty())));
assertThat(rowPattern("A{,3}")).isEqualTo(new QuantifiedPattern(location(1, 1), new PatternVariable(location(1, 1), new Identifier(location(1, 1), "A", false)), new RangeQuantifier(location(1, 2), true, Optional.empty(), Optional.of(new LongLiteral(location(1, 4), "3")))));
assertThat(rowPattern("A{3,4}")).isEqualTo(new QuantifiedPattern(location(1, 1), new PatternVariable(location(1, 1), new Identifier(location(1, 1), "A", false)), new RangeQuantifier(location(1, 2), true, Optional.of(new LongLiteral(location(1, 3), "3")), Optional.of(new LongLiteral(location(1, 5), "4")))));
}
Aggregations