org.eigenbase.test
Class SqlValidatorTestCase.TesterImpl

java.lang.Object
  extended by org.eigenbase.test.SqlValidatorTestCase.TesterImpl
All Implemented Interfaces:
SqlTester, SqlValidatorTestCase.Tester
Direct Known Subclasses:
SqlAdvisorTest.AdvisorTestImpl, SqlValidatorFeatureTest.FeatureTesterImpl
Enclosing class:
SqlValidatorTestCase

public static class SqlValidatorTestCase.TesterImpl
extends Object
implements SqlValidatorTestCase.Tester, SqlTester

Implementation of SqlValidatorTestCase.Tester which talks to a mock catalog.

It is also a pure-Java implementation of the SqlTester used by SqlOperatorTests. It can parse and validate queries, but it does not invoke Farrago, so it is very fast but cannot execute functions.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.eigenbase.sql.test.SqlTester
SqlTester.TypeChecker, SqlTester.VmName
 
Field Summary
protected  SqlConformance conformance
           
protected  SqlOperatorTable opTab
           
 
Constructor Summary
SqlValidatorTestCase.TesterImpl(SqlConformance conformance)
           
 
Method Summary
 void assertExceptionIsThrown(String sql, String expectedMsgPattern)
          Checks that a query is valid, or, if invalid, throws the right message at the right location.
private static String buildQuery(String expression)
           
 void check(String query, SqlTester.TypeChecker typeChecker, Object result, double delta)
          Tests that a SQL query returns a single column with the given type.
 void checkAgg(String expr, String[] inputValues, Object result, double delta)
          Checks that an aggregate expression returns the expected result.
 void checkBoolean(String expression, Boolean result)
          Tests that a scalar SQL expression returns the expected boolean result.
 void checkCharset(String sql, Charset expectedCharset)
           
 void checkCollation(String sql, String expectedCollationName, SqlCollation.Coercibility expectedCoercibility)
           
 void checkColumnType(String sql, String expected)
          Checks that a query returns one column of an expected type.
 void checkFails(String expression, String expectedError, boolean runtime)
          Tests that a scalar SQL expression fails at run time.
 void checkIntervalConv(String sql, String expected)
          Checks if the interval value conversion to milliseconds is valid.
 void checkNull(String expression)
          Tests that a SQL expression returns the SQL NULL value.
 void checkResultType(String sql, String expected)
          Checks that a query returns one column of an expected type.
 void checkRewrite(SqlValidator validator, String query, String expectedRewrite)
          Checks that a query gets rewritten to an expected form.
 void checkScalar(String expression, Object result, String resultType)
          Tests that a scalar SQL expression returns the expected result and the expected type.
 void checkScalarApprox(String expression, String expectedType, double expectedResult, double delta)
          Tests that a scalar SQL expression returns expected appoximate numeric result.
 void checkScalarExact(String expression, String result)
          Tests that a scalar SQL expression returns the expected exact numeric result as an integer.
 void checkScalarExact(String expression, String expectedType, String result)
          Tests that a scalar SQL expression returns the expected exact numeric result.
 void checkString(String expression, String result, String expectedType)
          Tests that a scalar SQL expression returns the expected string result.
 void checkType(String expression, String type)
          Tests that a SQL expression has a given type.
 void checkWinAgg(String expr, String[] inputValues, String windowSpec, String type, Object result, double delta)
          Checks that a windowed aggregate expression returns the expected result.
protected  SqlOperatorTable createOperatorTable()
           
 RelDataType getColumnType(String sql)
          Returns the data type of the sole column of a SQL query.
 SqlConformance getConformance()
           
 SqlMonotonicity getMonotonicity(String sql)
          Given a SQL query, returns the monotonicity of the first item in the SELECT clause.
 RelDataType getResultType(String sql)
          Returns the data type of the row returned by a SQL query.
 SqlValidator getValidator()
           
 SqlNode parseAndValidate(SqlValidator validator, String sql)
           
 SqlNode parseQuery(String sql)
           
 void setFor(SqlOperator operator, SqlTester.VmName... unimplementedVmNames)
          Declares that this test is for a given operator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

opTab

protected final SqlOperatorTable opTab

conformance

protected final SqlConformance conformance
Constructor Detail

SqlValidatorTestCase.TesterImpl

public SqlValidatorTestCase.TesterImpl(SqlConformance conformance)
Method Detail

getConformance

public SqlConformance getConformance()
Specified by:
getConformance in interface SqlValidatorTestCase.Tester

createOperatorTable

protected SqlOperatorTable createOperatorTable()

getValidator

public SqlValidator getValidator()
Specified by:
getValidator in interface SqlValidatorTestCase.Tester

assertExceptionIsThrown

public void assertExceptionIsThrown(String sql,
                                    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

getColumnType

public RelDataType getColumnType(String sql)
Description copied from interface: SqlValidatorTestCase.Tester
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 SqlValidatorTestCase.Tester
See Also:
SqlValidatorTestCase.Tester.getResultType(String)

getResultType

public RelDataType getResultType(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 SqlNode parseAndValidate(SqlValidator validator,
                                String sql)
Specified by:
parseAndValidate in interface SqlValidatorTestCase.Tester

parseQuery

public SqlNode parseQuery(String sql)
                   throws SqlParseException
Specified by:
parseQuery in interface SqlValidatorTestCase.Tester
Throws:
SqlParseException

checkColumnType

public void checkColumnType(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
Specified by:
checkColumnType in interface SqlValidatorTestCase.Tester
Parameters:
sql - Query expression
expected - Type string

checkResultType

public void checkResultType(String sql,
                            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(String sql,
                              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(String expression,
                      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(String sql,
                           String expectedCollationName,
                           SqlCollation.Coercibility expectedCoercibility)
Specified by:
checkCollation in interface SqlValidatorTestCase.Tester

checkCharset

public void checkCharset(String sql,
                         Charset expectedCharset)
Specified by:
checkCharset in interface SqlValidatorTestCase.Tester

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(String expr,
                     String[] inputValues,
                     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

checkWinAgg

public void checkWinAgg(String expr,
                        String[] inputValues,
                        String windowSpec,
                        String type,
                        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(String expression,
                        Object result,
                        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(String expression,
                             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(String expression,
                             String expectedType,
                             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(String expression,
                              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(String expression,
                         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(String expression,
                        String result,
                        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(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 void check(String query,
                  SqlTester.TypeChecker typeChecker,
                  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

checkRewrite

public void checkRewrite(SqlValidator validator,
                         String query,
                         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(String expression,
                       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

getMonotonicity

public SqlMonotonicity getMonotonicity(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

private static String buildQuery(String expression)