Search in sources :

Example 1 with AnchorPattern

use of io.trino.sql.tree.AnchorPattern 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")))));
}
Also used : PatternVariable(io.trino.sql.tree.PatternVariable) LongLiteral(io.trino.sql.tree.LongLiteral) PatternConcatenation(io.trino.sql.tree.PatternConcatenation) EmptyPattern(io.trino.sql.tree.EmptyPattern) ZeroOrMoreQuantifier(io.trino.sql.tree.ZeroOrMoreQuantifier) ZeroOrOneQuantifier(io.trino.sql.tree.ZeroOrOneQuantifier) RangeQuantifier(io.trino.sql.tree.RangeQuantifier) QuantifiedPattern(io.trino.sql.tree.QuantifiedPattern) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) PatternAlternation(io.trino.sql.tree.PatternAlternation) OneOrMoreQuantifier(io.trino.sql.tree.OneOrMoreQuantifier) AnchorPattern(io.trino.sql.tree.AnchorPattern) Test(org.junit.jupiter.api.Test)

Aggregations

QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)1 AnchorPattern (io.trino.sql.tree.AnchorPattern)1 EmptyPattern (io.trino.sql.tree.EmptyPattern)1 Identifier (io.trino.sql.tree.Identifier)1 LongLiteral (io.trino.sql.tree.LongLiteral)1 OneOrMoreQuantifier (io.trino.sql.tree.OneOrMoreQuantifier)1 PatternAlternation (io.trino.sql.tree.PatternAlternation)1 PatternConcatenation (io.trino.sql.tree.PatternConcatenation)1 PatternVariable (io.trino.sql.tree.PatternVariable)1 QuantifiedPattern (io.trino.sql.tree.QuantifiedPattern)1 RangeQuantifier (io.trino.sql.tree.RangeQuantifier)1 ZeroOrMoreQuantifier (io.trino.sql.tree.ZeroOrMoreQuantifier)1 ZeroOrOneQuantifier (io.trino.sql.tree.ZeroOrOneQuantifier)1 Test (org.junit.jupiter.api.Test)1