Class AbstractSqlTester

java.lang.Object
org.apache.calcite.sql.test.AbstractSqlTester
All Implemented Interfaces:
AutoCloseable, SqlTester
Direct Known Subclasses:
SqlOperatorTest.TesterImpl, SqlValidatorTester

public abstract class AbstractSqlTester extends Object implements SqlTester, AutoCloseable
Abstract implementation of SqlTester that talks to a mock catalog.

This is to implement the default behavior: testing is only against the SqlValidator.

  • Constructor Details

    • AbstractSqlTester

      public AbstractSqlTester()
  • Method Details

    • close

      public void close()

      This default implementation does nothing.

      Specified by:
      close in interface AutoCloseable
    • assertExceptionIsThrown

      public void assertExceptionIsThrown(SqlTestFactory factory, StringAndPos sap, @Nullable String expectedMsgPattern)
      Description copied from interface: SqlTester
      Checks that a query is valid, or, if invalid, throws the right message at the right location.

      If expectedMsgPattern is null, the query must succeed.

      If expectedMsgPattern is not null, the query must fail, and give an error location of (expectedLine, expectedColumn) through (expectedEndLine, expectedEndColumn).

      Specified by:
      assertExceptionIsThrown in interface SqlTester
      Parameters:
      factory - Factory
      sap - SQL statement
      expectedMsgPattern - If this parameter is null the query must be
    • checkParseEx

      protected void checkParseEx(Throwable e, @Nullable String expectedMsgPattern, StringAndPos sap)
    • getColumnType

      public RelDataType getColumnType(SqlTestFactory factory, String sql)
      Description copied from interface: SqlTester
      Returns the data type of the sole column of a SQL query.

      For example, getResultType("VALUES (1") returns INTEGER.

      Fails if query returns more than one column.

      Specified by:
      getColumnType in interface SqlTester
      See Also:
    • getResultType

      public RelDataType getResultType(SqlTestFactory factory, String sql)
      Description copied from interface: SqlTester
      Returns the data type of the row returned by a SQL query.

      For example, getResultType("VALUES (1, 'foo')") returns RecordType(INTEGER EXPR$0, CHAR(3) EXPR#1).

      Specified by:
      getResultType in interface SqlTester
    • parseQuery

      public SqlNode parseQuery(SqlTestFactory factory, String sql) throws SqlParseException
      Description copied from interface: SqlTester
      Parses a query.
      Specified by:
      parseQuery in interface SqlTester
      Throws:
      SqlParseException
    • parseExpression

      public SqlNode parseExpression(SqlTestFactory factory, String expr) throws SqlParseException
      Description copied from interface: SqlTester
      Parses an expression.
      Specified by:
      parseExpression in interface SqlTester
      Throws:
      SqlParseException
    • checkColumnType

      public void checkColumnType(SqlTestFactory factory, String sql, String expected)
      Description copied from interface: SqlTester
      Checks that a query returns one column of an expected type. For example, checkType("VALUES (1 + 2)", "INTEGER NOT NULL").
      Specified by:
      checkColumnType in interface SqlTester
      Parameters:
      factory - Factory
      sql - Query expression
      expected - Type string
    • setFor

      public void setFor(SqlOperator operator, SqlTester.VmName... unimplementedVmNames)
      Description copied from interface: SqlTester
      Declares that this test is for a given operator. So we can check that all operators are tested.
      Specified by:
      setFor in interface SqlTester
      Parameters:
      operator - Operator
      unimplementedVmNames - Names of virtual machines for which this
    • checkAgg

      public void checkAgg(SqlTestFactory factory, String expr, String[] inputValues, SqlTester.ResultChecker resultChecker)
      Description copied from interface: SqlTester
      Checks that an aggregate expression returns the expected result.

      For example, checkAgg("AVG(DISTINCT x)", new String[] {"2", "3", null, "3" }, new Double(2.5), 0);

      Specified by:
      checkAgg in interface SqlTester
      Parameters:
      factory - Factory
      expr - Aggregate expression, e.g. SUM(DISTINCT x)
      inputValues - Array of input values, e.g. ["1", null, "2"]
      resultChecker - Checks whether the result has the expected value
    • checkWinAgg

      public void checkWinAgg(SqlTestFactory factory, String expr, String[] inputValues, String windowSpec, String type, SqlTester.ResultChecker resultChecker)
      Description copied from interface: SqlTester
      Checks that a windowed aggregate expression returns the expected result.

      For example, checkWinAgg("FIRST_VALUE(x)", new String[] {"2", "3", null, "3" }, "INTEGER NOT NULL", 2, 0d);

      Specified by:
      checkWinAgg in interface SqlTester
      Parameters:
      factory - Factory
      expr - Aggregate expression, e.g. SUM(DISTINCT x)
      inputValues - Array of input values, e.g. ["1", null, "2"]
      type - Expected result type
      resultChecker - Checks whether the result has the expected value
    • check

      public void check(SqlTestFactory factory, String query, SqlTester.TypeChecker typeChecker, SqlTester.ParameterChecker parameterChecker, SqlTester.ResultChecker resultChecker)
      Description copied from interface: SqlTester
      Tests that a SQL query returns a result of expected type and value. Checking of type and value are abstracted using SqlTester.TypeChecker and SqlTester.ResultChecker functors.
      Specified by:
      check in interface SqlTester
      Parameters:
      factory - Factory
      query - SQL query
      typeChecker - Checks whether the result is the expected type
      parameterChecker - Checks whether the parameters are of expected types
      resultChecker - Checks whether the result has the expected value
    • validateAndThen

      public void validateAndThen(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeConsumer consumer)
      Description copied from interface: SqlTester
      Parses and validates a query, then calls an action on the result.
      Specified by:
      validateAndThen in interface SqlTester
    • validateAndApply

      public <R> R validateAndApply(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeFunction<R> function)
      Description copied from interface: SqlTester
      Parses and validates a query, then calls a function on the result.
      Specified by:
      validateAndApply in interface SqlTester
    • checkFails

      public void checkFails(SqlTestFactory factory, StringAndPos sap, String expectedError, boolean runtime)
      Description copied from interface: SqlTester
      Tests that a scalar SQL expression fails at run time.
      Specified by:
      checkFails in interface SqlTester
      Parameters:
      factory - Factory
      sap - SQL scalar expression
      expectedError - Pattern for expected error. If !runtime, must include an error location.
      runtime - If true, must fail at runtime; if false, must fail at validate time
    • checkQueryFails

      public void checkQueryFails(SqlTestFactory factory, StringAndPos sap, String expectedError)
      Description copied from interface: SqlTester
      Tests that a SQL query fails at prepare time.
      Specified by:
      checkQueryFails in interface SqlTester
      Parameters:
      factory - Factory
      sap - SQL query and error position
      expectedError - Pattern for expected error. Must include an error location.
    • checkAggFails

      public void checkAggFails(SqlTestFactory factory, String expr, String[] inputValues, String expectedError, boolean runtime)
      Description copied from interface: SqlTester
      Tests that an aggregate expression fails at run time.
      Specified by:
      checkAggFails in interface SqlTester
      Parameters:
      factory - Factory
      expr - An aggregate expression
      inputValues - Array of input values
      expectedError - Pattern for expected error
      runtime - If true, must fail at runtime; if false, must fail at validate time
    • buildQuery

      public static String buildQuery(String expression)
    • buildQueryAgg

      public static String buildQueryAgg(String expression)
    • buildQuery2

      protected String buildQuery2(SqlTestFactory factory, String expression)
      Builds a query that extracts all literals as columns in an underlying select.

      For example,

      1 < 5

      becomes

      SELECT p0 < p1 FROM (VALUES (1, 5)) AS t(p0, p1)

      Null literals don't have enough type information to be extracted. We push down CAST(NULL AS type) but raw nulls such as CASE 1 WHEN 2 THEN 'a' ELSE NULL END are left as is.

      Parameters:
      factory - Test factory
      expression - Scalar expression
      Returns:
      Query that evaluates a scalar expression
    • forEachQuery

      public void forEachQuery(SqlTestFactory factory, String expression, Consumer<String> consumer)
      Description copied from interface: SqlTester
      Given a scalar expression, generates a sequence of SQL queries that evaluate it, and calls a given action with each.
      Specified by:
      forEachQuery in interface SqlTester
      Parameters:
      factory - Factory
      expression - Scalar expression
      consumer - Action to be called for each query
    • assertConvertsTo

      public void assertConvertsTo(SqlTestFactory factory, DiffRepository diffRepos, String sql, String plan, boolean trim, boolean expression, boolean decorrelate)
      Description copied from interface: SqlTester
      Checks that a SQL statement converts to a given plan, optionally trimming columns that are not needed.
      Specified by:
      assertConvertsTo in interface SqlTester
      Parameters:
      factory - Factory
      diffRepos - Diff repository
      sql - SQL query or expression
      plan - Expected plan
      trim - Whether to trim columns that are not needed
      expression - True if sql is an expression, false if it is a query
    • convertSqlToRel2

      public Pair<SqlValidator,RelRoot> convertSqlToRel2(SqlTestFactory factory, String sql, boolean decorrelate, boolean trim)
      Description copied from interface: SqlTester
      Converts a SQL string to a (SqlValidator, RelNode) pair.
      Specified by:
      convertSqlToRel2 in interface SqlTester
    • trimRelNode

      public RelNode trimRelNode(SqlTestFactory factory, RelNode relNode)
      Description copied from interface: SqlTester
      Trims a RelNode.
      Specified by:
      trimRelNode in interface SqlTester
    • createFieldTrimmer

      public RelFieldTrimmer createFieldTrimmer(SqlValidator validator, RelBuilder relBuilder)
      Creates a RelFieldTrimmer.
      Parameters:
      validator - Validator
      relBuilder - Builder
      Returns:
      Field trimmer