Class AbstractSqlTester

    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void assertExceptionIsThrown​(java.lang.String sql, java.lang.String expectedMsgPattern)
      Checks that a query is valid, or, if invalid, throws the right message at the right location.
      private java.lang.Iterable<java.lang.String> buildQueries​(java.lang.String expression)
      Converts a scalar expression into a list of SQL queries that evaluate it.
      static java.lang.String buildQuery​(java.lang.String expression)  
      protected java.lang.String buildQuery2​(java.lang.String expression)
      Builds a query that extracts all literals as columns in an underlying select.
      static java.lang.String buildQueryAgg​(java.lang.String expression)  
      void check​(java.lang.String query, SqlTester.TypeChecker typeChecker, java.lang.Object result, double delta)
      Tests that a SQL query returns a single column with the given type.
      void check​(java.lang.String query, SqlTester.TypeChecker typeChecker, SqlTester.ParameterChecker parameterChecker, SqlTester.ResultChecker resultChecker)
      Tests that a SQL query returns a result of expected type and value.
      void checkAgg​(java.lang.String expr, java.lang.String[] inputValues, java.lang.Object result, double delta)
      Checks that an aggregate expression returns the expected result.
      void checkAggWithMultipleArgs​(java.lang.String expr, java.lang.String[][] inputValues, java.lang.Object result, double delta)
      Checks that an aggregate expression with multiple args returns the expected result.
      void checkBoolean​(java.lang.String expression, java.lang.Boolean result)
      Tests that a scalar SQL expression returns the expected boolean result.
      void checkCharset​(java.lang.String expression, java.nio.charset.Charset expectedCharset)  
      void checkCollation​(java.lang.String expression, java.lang.String expectedCollationName, org.apache.calcite.sql.SqlCollation.Coercibility expectedCoercibility)  
      void checkColumnType​(java.lang.String sql, java.lang.String expected)
      Checks that a query returns one column of an expected type.
      void checkFails​(java.lang.String expression, java.lang.String expectedError, boolean runtime)
      Tests that a scalar SQL expression fails at run time.
      void checkFieldOrigin​(java.lang.String sql, java.lang.String fieldOriginList)
      Given a SQL query, returns a list of the origins of each result field.
      void checkIntervalConv​(java.lang.String sql, java.lang.String expected)
      Checks if the interval value conversion to milliseconds is valid.
      void checkMonotonic​(java.lang.String query, org.apache.calcite.sql.validate.SqlMonotonicity expectedMonotonicity)
      Tests that the first column of a SQL query has a given monotonicity.
      void checkNull​(java.lang.String expression)
      Tests that a SQL expression returns the SQL NULL value.
      protected void checkParseEx​(java.lang.Throwable e, java.lang.String expectedMsgPattern, java.lang.String sql)  
      void checkQuery​(java.lang.String sql)
      Tests that a SQL query succeeds at prepare time.
      void checkQueryFails​(java.lang.String sql, java.lang.String expectedError)
      Tests that a SQL query fails at prepare time.
      void checkResultType​(java.lang.String sql, java.lang.String expected)
      Checks that a query returns one column of an expected type.
      void checkRewrite​(org.apache.calcite.sql.validate.SqlValidator validator, java.lang.String query, java.lang.String expectedRewrite)
      Checks that a query gets rewritten to an expected form.
      void checkScalar​(java.lang.String expression, java.lang.Object result, java.lang.String resultType)
      Tests that a scalar SQL expression returns the expected result and the expected type.
      void checkScalarApprox​(java.lang.String expression, java.lang.String expectedType, double expectedResult, double delta)
      Tests that a scalar SQL expression returns expected appoximate numeric result.
      void checkScalarExact​(java.lang.String expression, java.lang.String result)
      Tests that a scalar SQL expression returns the expected exact numeric result as an integer.
      void checkScalarExact​(java.lang.String expression, java.lang.String expectedType, java.lang.String result)
      Tests that a scalar SQL expression returns the expected exact numeric result.
      void checkString​(java.lang.String expression, java.lang.String result, java.lang.String expectedType)
      Tests that a scalar SQL expression returns the expected string result.
      void checkType​(java.lang.String expression, java.lang.String type)
      Tests that a SQL expression has a given type.
      void checkWinAgg​(java.lang.String expr, java.lang.String[] inputValues, java.lang.String windowSpec, java.lang.String type, java.lang.Object result, double delta)
      Checks that a windowed aggregate expression returns the expected result.
      void close()
      SqlTester enableTypeCoercion​(boolean enabled)
      Returns a tester that tests with implicit type coercion on/off.
      org.apache.calcite.rel.type.RelDataType getColumnType​(java.lang.String sql)
      Returns the data type of the sole column of a SQL query.
      org.apache.calcite.sql.validate.SqlConformance getConformance()  
      SqlTestFactory getFactory()  
      org.apache.calcite.sql.validate.SqlMonotonicity getMonotonicity​(java.lang.String sql)
      Given a SQL query, returns the monotonicity of the first item in the SELECT clause.
      org.apache.calcite.rel.type.RelDataType getResultType​(java.lang.String sql)
      Returns the data type of the row returned by a SQL query.
      org.apache.calcite.sql.validate.SqlValidator getValidator()  
      org.apache.calcite.sql.SqlNode parseAndValidate​(org.apache.calcite.sql.validate.SqlValidator validator, java.lang.String sql)  
      org.apache.calcite.sql.SqlNode parseQuery​(java.lang.String sql)  
      void setFor​(org.apache.calcite.sql.SqlOperator operator, SqlTester.VmName... unimplementedVmNames)
      Declares that this test is for a given operator.
      protected SqlTester with​(java.lang.String name, java.lang.Object value)  
      protected abstract SqlTester with​(SqlTestFactory factory)  
      SqlTester withCaseSensitive​(boolean sensitive)
      Returns a tester that matches identifiers by case-sensitive or case-insensitive.
      SqlTester withConformance​(org.apache.calcite.sql.validate.SqlConformance conformance)
      Returns a tester that tests conformance to a particular SQL language version.
      SqlTester withConnectionFactory​(CalciteAssert.ConnectionFactory connectionFactory)
      Returns a tester that gets connections from a given factory.
      SqlTester withLex​(org.apache.calcite.config.Lex lex)
      Returns a tester that follows a lex policy.
      SqlTester withOperatorTable​(org.apache.calcite.sql.SqlOperatorTable operatorTable)
      Returns a tester that uses a given operator table.
      SqlTester withQuotedCasing​(org.apache.calcite.avatica.util.Casing casing)
      Returns a tester that applies a given casing policy to quoted identifiers.
      SqlTester withQuoting​(org.apache.calcite.avatica.util.Quoting quoting)
      Returns a tester that tests a given SQL quoting style.
      SqlTester withUnquotedCasing​(org.apache.calcite.avatica.util.Casing casing)
      Returns a tester that applies a given casing policy to unquoted identifiers.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AbstractSqlTester

        public AbstractSqlTester​(SqlTestFactory factory)
    • Method Detail

      • close

        public void close()

        This default implementation does nothing.

        Specified by:
        close in interface java.lang.AutoCloseable
      • assertExceptionIsThrown

        public void assertExceptionIsThrown​(java.lang.String sql,
                                            java.lang.String expectedMsgPattern)
        Description copied from interface: SqlValidatorTestCase.Tester
        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 SqlValidatorTestCase.Tester
        Parameters:
        sql - SQL statement
        expectedMsgPattern - If this parameter is null the query must be valid for the test to pass; If this parameter is not null the query must be malformed and the message given must match the pattern
      • checkParseEx

        protected void checkParseEx​(java.lang.Throwable e,
                                    java.lang.String expectedMsgPattern,
                                    java.lang.String sql)
      • getResultType

        public org.apache.calcite.rel.type.RelDataType getResultType​(java.lang.String sql)
        Description copied from interface: SqlValidatorTestCase.Tester
        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 SqlValidatorTestCase.Tester
      • parseAndValidate

        public org.apache.calcite.sql.SqlNode parseAndValidate​(org.apache.calcite.sql.validate.SqlValidator validator,
                                                               java.lang.String sql)
        Specified by:
        parseAndValidate in interface SqlValidatorTestCase.Tester
      • parseQuery

        public org.apache.calcite.sql.SqlNode parseQuery​(java.lang.String sql)
                                                  throws org.apache.calcite.sql.parser.SqlParseException
        Specified by:
        parseQuery in interface SqlValidatorTestCase.Tester
        Throws:
        org.apache.calcite.sql.parser.SqlParseException
      • checkColumnType

        public void checkColumnType​(java.lang.String sql,
                                    java.lang.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
        Specified by:
        checkColumnType in interface SqlValidatorTestCase.Tester
        Parameters:
        sql - Query expression
        expected - Type string
      • checkFieldOrigin

        public void checkFieldOrigin​(java.lang.String sql,
                                     java.lang.String fieldOriginList)
        Description copied from interface: SqlValidatorTestCase.Tester
        Given a SQL query, returns a list of the origins of each result field.
        Specified by:
        checkFieldOrigin in interface SqlValidatorTestCase.Tester
        Parameters:
        sql - SQL query
        fieldOriginList - Field origin list, e.g. "{(CATALOG.SALES.EMP.EMPNO, null)}"
      • checkResultType

        public void checkResultType​(java.lang.String sql,
                                    java.lang.String expected)
        Description copied from interface: SqlValidatorTestCase.Tester
        Checks that a query returns one column of an expected type. For example, checkType("select empno, name from emp""{EMPNO INTEGER NOT NULL, NAME VARCHAR(10) NOT NULL}").
        Specified by:
        checkResultType in interface SqlValidatorTestCase.Tester
      • checkIntervalConv

        public void checkIntervalConv​(java.lang.String sql,
                                      java.lang.String expected)
        Description copied from interface: SqlValidatorTestCase.Tester
        Checks if the interval value conversion to milliseconds is valid. For example, checkIntervalConv(VALUES (INTERVAL '1' Minute), "60000").
        Specified by:
        checkIntervalConv in interface SqlValidatorTestCase.Tester
      • checkType

        public void checkType​(java.lang.String expression,
                              java.lang.String type)
        Description copied from interface: SqlTester
        Tests that a SQL expression has a given type. For example,
        checkType("SUBSTR('hello' FROM 1 FOR 3)", "VARCHAR(3) NOT NULL");

        This method checks length/precision, scale, and whether the type allows NULL values, so is more precise than the type-checking done by methods such as SqlTester.checkScalarExact(java.lang.String, java.lang.String).

        Specified by:
        checkType in interface SqlTester
        Parameters:
        expression - Scalar expression
        type - Type string
      • checkCollation

        public void checkCollation​(java.lang.String expression,
                                   java.lang.String expectedCollationName,
                                   org.apache.calcite.sql.SqlCollation.Coercibility expectedCoercibility)
        Specified by:
        checkCollation in interface SqlValidatorTestCase.Tester
      • withQuoting

        public SqlTester withQuoting​(org.apache.calcite.avatica.util.Quoting quoting)
        Description copied from interface: SqlTester
        Returns a tester that tests a given SQL quoting style.
        Specified by:
        withQuoting in interface SqlTester
      • withQuotedCasing

        public SqlTester withQuotedCasing​(org.apache.calcite.avatica.util.Casing casing)
        Description copied from interface: SqlTester
        Returns a tester that applies a given casing policy to quoted identifiers.
        Specified by:
        withQuotedCasing in interface SqlTester
      • withUnquotedCasing

        public SqlTester withUnquotedCasing​(org.apache.calcite.avatica.util.Casing casing)
        Description copied from interface: SqlTester
        Returns a tester that applies a given casing policy to unquoted identifiers.
        Specified by:
        withUnquotedCasing in interface SqlTester
      • withCaseSensitive

        public SqlTester withCaseSensitive​(boolean sensitive)
        Description copied from interface: SqlTester
        Returns a tester that matches identifiers by case-sensitive or case-insensitive.
        Specified by:
        withCaseSensitive in interface SqlTester
      • withLex

        public SqlTester withLex​(org.apache.calcite.config.Lex lex)
        Description copied from interface: SqlTester
        Returns a tester that follows a lex policy.
        Specified by:
        withLex in interface SqlTester
      • withConformance

        public SqlTester withConformance​(org.apache.calcite.sql.validate.SqlConformance conformance)
        Description copied from interface: SqlTester
        Returns a tester that tests conformance to a particular SQL language version.
        Specified by:
        withConformance in interface SqlTester
      • enableTypeCoercion

        public SqlTester enableTypeCoercion​(boolean enabled)
        Description copied from interface: SqlTester
        Returns a tester that tests with implicit type coercion on/off.
        Specified by:
        enableTypeCoercion in interface SqlTester
      • withOperatorTable

        public SqlTester withOperatorTable​(org.apache.calcite.sql.SqlOperatorTable operatorTable)
        Description copied from interface: SqlTester
        Returns a tester that uses a given operator table.
        Specified by:
        withOperatorTable in interface SqlTester
      • with

        protected final SqlTester with​(java.lang.String name,
                                       java.lang.Object value)
      • setFor

        public void setFor​(org.apache.calcite.sql.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​(java.lang.String expr,
                             java.lang.String[] inputValues,
                             java.lang.Object result,
                             double delta)
        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:
        expr - Aggregate expression, e.g. SUM(DISTINCT x)
        inputValues - Array of input values, e.g. ["1", null, "2"].
        result - Expected result
        delta - Allowable variance from expected result
      • checkAggWithMultipleArgs

        public void checkAggWithMultipleArgs​(java.lang.String expr,
                                             java.lang.String[][] inputValues,
                                             java.lang.Object result,
                                             double delta)
        Description copied from interface: SqlTester
        Checks that an aggregate expression with multiple args returns the expected result.
        Specified by:
        checkAggWithMultipleArgs in interface SqlTester
        Parameters:
        expr - Aggregate expression, e.g. AGG_FUNC(x, x2, x3)
        inputValues - Nested array of input values, e.g. [ ["1", null, "2"] ["3", "4", null] ].
        result - Expected result
        delta - Allowable variance from expected result
      • checkWinAgg

        public void checkWinAgg​(java.lang.String expr,
                                java.lang.String[] inputValues,
                                java.lang.String windowSpec,
                                java.lang.String type,
                                java.lang.Object result,
                                double delta)
        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:
        expr - Aggregate expression, e.g. SUM(DISTINCT x)
        inputValues - Array of input values, e.g. ["1", null, "2"].
        type - Expected result type
        result - Expected result
        delta - Allowable variance from expected result
      • checkScalar

        public void checkScalar​(java.lang.String expression,
                                java.lang.Object result,
                                java.lang.String resultType)
        Description copied from interface: SqlTester
        Tests that a scalar SQL expression returns the expected result and the expected type. For example,
        checkScalar("1.1 + 2.9", "4.0", "DECIMAL(2, 1) NOT NULL");
        Specified by:
        checkScalar in interface SqlTester
        Parameters:
        expression - Scalar expression
        result - Expected result
        resultType - Expected result type
      • checkScalarExact

        public void checkScalarExact​(java.lang.String expression,
                                     java.lang.String result)
        Description copied from interface: SqlTester
        Tests that a scalar SQL expression returns the expected exact numeric result as an integer. For example,
        checkScalarExact("1 + 2", "3");
        Specified by:
        checkScalarExact in interface SqlTester
        Parameters:
        expression - Scalar expression
        result - Expected result
      • checkScalarExact

        public void checkScalarExact​(java.lang.String expression,
                                     java.lang.String expectedType,
                                     java.lang.String result)
        Description copied from interface: SqlTester
        Tests that a scalar SQL expression returns the expected exact numeric result. For example,
        checkScalarExact("1 + 2", "3");
        Specified by:
        checkScalarExact in interface SqlTester
        Parameters:
        expression - Scalar expression
        expectedType - Type we expect the result to have, including nullability, precision and scale, for example DECIMAL(2, 1) NOT NULL.
        result - Expected result
      • checkScalarApprox

        public void checkScalarApprox​(java.lang.String expression,
                                      java.lang.String expectedType,
                                      double expectedResult,
                                      double delta)
        Description copied from interface: SqlTester
        Tests that a scalar SQL expression returns expected appoximate numeric result. For example,
        checkScalarApprox("1.0 + 2.1", "3.1");
        Specified by:
        checkScalarApprox in interface SqlTester
        Parameters:
        expression - Scalar expression
        expectedType - Type we expect the result to have, including nullability, precision and scale, for example DECIMAL(2, 1) NOT NULL.
        expectedResult - Expected result
        delta - Allowed margin of error between expected and actual result
      • checkBoolean

        public void checkBoolean​(java.lang.String expression,
                                 java.lang.Boolean result)
        Description copied from interface: SqlTester
        Tests that a scalar SQL expression returns the expected boolean result. For example,
        checkScalarExact("TRUE AND FALSE", Boolean.TRUE);

        The expected result can be null:

        checkScalarExact("NOT UNKNOWN", null);
        Specified by:
        checkBoolean in interface SqlTester
        Parameters:
        expression - Scalar expression
        result - Expected result (null signifies NULL).
      • checkString

        public void checkString​(java.lang.String expression,
                                java.lang.String result,
                                java.lang.String expectedType)
        Description copied from interface: SqlTester
        Tests that a scalar SQL expression returns the expected string result. For example,
        checkScalarExact("'ab' || 'c'", "abc");
        Specified by:
        checkString in interface SqlTester
        Parameters:
        expression - Scalar expression
        result - Expected result
        expectedType - Expected result type
      • checkNull

        public void checkNull​(java.lang.String expression)
        Description copied from interface: SqlTester
        Tests that a SQL expression returns the SQL NULL value. For example,
        checkNull("CHAR_LENGTH(CAST(NULL AS VARCHAR(3))");
        Specified by:
        checkNull in interface SqlTester
        Parameters:
        expression - Scalar expression
      • check

        public final void check​(java.lang.String query,
                                SqlTester.TypeChecker typeChecker,
                                java.lang.Object result,
                                double delta)
        Description copied from interface: SqlTester
        Tests that a SQL query returns a single column with the given type. For example,
        check("VALUES (1 + 2)", "3", SqlTypeName.Integer);

        If result is null, the expression must yield the SQL NULL value. If result is a Pattern, the result must match that pattern.

        Specified by:
        check in interface SqlTester
        Parameters:
        query - SQL query
        typeChecker - Checks whether the result is the expected type; must not be null
        result - Expected result
        delta - The acceptable tolerance between the expected and actual
      • check

        public void check​(java.lang.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:
        query - SQL query
        typeChecker - Checks whether the result is the expected type; must not be null
        parameterChecker - Checks whether the parameters are of expected types
        resultChecker - Checks whether the result has the expected value; must not be null
      • checkMonotonic

        public void checkMonotonic​(java.lang.String query,
                                   org.apache.calcite.sql.validate.SqlMonotonicity expectedMonotonicity)
        Description copied from interface: SqlTester
        Tests that the first column of a SQL query has a given monotonicity.
        Specified by:
        checkMonotonic in interface SqlTester
        Parameters:
        query - SQL query
        expectedMonotonicity - Expected monotonicity
      • checkRewrite

        public void checkRewrite​(org.apache.calcite.sql.validate.SqlValidator validator,
                                 java.lang.String query,
                                 java.lang.String expectedRewrite)
        Description copied from interface: SqlValidatorTestCase.Tester
        Checks that a query gets rewritten to an expected form.
        Specified by:
        checkRewrite in interface SqlValidatorTestCase.Tester
        Parameters:
        validator - validator to use; null for default
        query - query to test
        expectedRewrite - expected SQL text after rewrite and unparse
      • checkFails

        public void checkFails​(java.lang.String expression,
                               java.lang.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:
        expression - 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​(java.lang.String sql,
                                    java.lang.String expectedError)
        Description copied from interface: SqlTester
        Tests that a SQL query fails at prepare time.
        Specified by:
        checkQueryFails in interface SqlTester
        Parameters:
        sql - SQL query
        expectedError - Pattern for expected error. Must include an error location.
      • checkQuery

        public void checkQuery​(java.lang.String sql)
        Description copied from interface: SqlTester
        Tests that a SQL query succeeds at prepare time.
        Specified by:
        checkQuery in interface SqlTester
        Parameters:
        sql - SQL query
      • getMonotonicity

        public org.apache.calcite.sql.validate.SqlMonotonicity getMonotonicity​(java.lang.String sql)
        Description copied from interface: SqlValidatorTestCase.Tester
        Given a SQL query, returns the monotonicity of the first item in the SELECT clause.
        Specified by:
        getMonotonicity in interface SqlValidatorTestCase.Tester
        Parameters:
        sql - SQL query
        Returns:
        Monotonicity
      • buildQuery

        public static java.lang.String buildQuery​(java.lang.String expression)
      • buildQueryAgg

        public static java.lang.String buildQueryAgg​(java.lang.String expression)
      • buildQuery2

        protected java.lang.String buildQuery2​(java.lang.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:
        expression - Scalar expression
        Returns:
        Query that evaluates a scalar expression
      • buildQueries

        private java.lang.Iterable<java.lang.String> buildQueries​(java.lang.String expression)
        Converts a scalar expression into a list of SQL queries that evaluate it.
        Parameters:
        expression - Scalar expression
        Returns:
        List of queries that evaluate an expression