Search in sources :

Example 6 with ParsingOptions

use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.

the class TestParameterExtractor method testParameterCount.

@Test
public void testParameterCount() {
    Statement statement = sqlParser.createStatement("SELECT c1, c2 FROM test_table WHERE c1 = ? AND c2 > ?", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 41), 0), new Parameter(new NodeLocation(1, 52), 1));
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(2);
}
Also used : NodeLocation(io.trino.sql.tree.NodeLocation) ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) Parameter(io.trino.sql.tree.Parameter) Test(org.testng.annotations.Test)

Example 7 with ParsingOptions

use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.

the class TestAnalyzer method analyze.

private Analysis analyze(Session clientSession, @Language("SQL") String query, AccessControl accessControl) {
    return transaction(transactionManager, accessControl).singleStatement().readUncommitted().execute(clientSession, session -> {
        Analyzer analyzer = createAnalyzer(session, accessControl);
        Statement statement = SQL_PARSER.createStatement(query, new ParsingOptions(new FeaturesConfig().isParseDecimalLiteralsAsDouble() ? AS_DOUBLE : AS_DECIMAL));
        return analyzer.analyze(statement);
    });
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) FeaturesConfig(io.trino.FeaturesConfig)

Example 8 with ParsingOptions

use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.

the class TestExpressionEquivalence method assertEquivalent.

private static void assertEquivalent(@Language("SQL") String left, @Language("SQL") String right) {
    ParsingOptions parsingOptions = new ParsingOptions(AS_DOUBLE);
    Expression leftExpression = planExpression(PLANNER_CONTEXT, TEST_SESSION, TYPE_PROVIDER, SQL_PARSER.createExpression(left, parsingOptions));
    Expression rightExpression = planExpression(PLANNER_CONTEXT, TEST_SESSION, TYPE_PROVIDER, SQL_PARSER.createExpression(right, parsingOptions));
    Set<Symbol> symbols = extractUnique(ImmutableList.of(leftExpression, rightExpression));
    TypeProvider types = TypeProvider.copyOf(symbols.stream().collect(toMap(identity(), TestExpressionEquivalence::generateType)));
    assertTrue(areExpressionEquivalent(leftExpression, rightExpression, types), format("Expected (%s) and (%s) to be equivalent", left, right));
    assertTrue(areExpressionEquivalent(rightExpression, leftExpression, types), format("Expected (%s) and (%s) to be equivalent", right, left));
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) ExpressionTestUtils.planExpression(io.trino.sql.ExpressionTestUtils.planExpression) Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol) TypeProvider(io.trino.sql.planner.TypeProvider)

Example 9 with ParsingOptions

use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.

the class TestParameterExtractor method testNoParameter.

@Test
public void testNoParameter() {
    Statement statement = sqlParser.createStatement("SELECT c1, c2 FROM test_table WHERE c1 = 1 AND c2 > 2", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).isEmpty();
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(0);
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) Test(org.testng.annotations.Test)

Example 10 with ParsingOptions

use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.

the class TestParameterExtractor method testLambda.

@Test
public void testLambda() {
    Statement statement = sqlParser.createStatement("SELECT * FROM test_table WHERE any_match(items, x -> x > ?)", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 58), 0));
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(1);
}
Also used : NodeLocation(io.trino.sql.tree.NodeLocation) ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) Parameter(io.trino.sql.tree.Parameter) Test(org.testng.annotations.Test)

Aggregations

ParsingOptions (io.trino.sql.parser.ParsingOptions)15 Expression (io.trino.sql.tree.Expression)6 Statement (io.trino.sql.tree.Statement)6 Test (org.testng.annotations.Test)4 Symbol (io.trino.sql.planner.Symbol)3 NodeLocation (io.trino.sql.tree.NodeLocation)3 Parameter (io.trino.sql.tree.Parameter)3 ExpressionFormatter.formatExpression (io.trino.sql.ExpressionFormatter.formatExpression)2 ExpressionTestUtils.planExpression (io.trino.sql.ExpressionTestUtils.planExpression)2 ExpressionUtils.logicalExpression (io.trino.sql.ExpressionUtils.logicalExpression)2 ParsingUtil.createParsingOptions (io.trino.sql.ParsingUtil.createParsingOptions)2 ParsingException (io.trino.sql.parser.ParsingException)2 TypeProvider (io.trino.sql.planner.TypeProvider)2 LogicalExpression (io.trino.sql.tree.LogicalExpression)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 FeaturesConfig (io.trino.FeaturesConfig)1 ResourceGroupId (io.trino.spi.resourcegroups.ResourceGroupId)1 SqlParser (io.trino.sql.parser.SqlParser)1 SymbolAllocator (io.trino.sql.planner.SymbolAllocator)1