Class AbstractSqlTester
- java.lang.Object
-
- org.apache.calcite.sql.test.AbstractSqlTester
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,SqlTester
,SqlValidatorTestCase.Tester
- Direct Known Subclasses:
SqlRuntimeTester
,SqlValidatorTester
public abstract class AbstractSqlTester extends java.lang.Object implements SqlTester, java.lang.AutoCloseable
Abstract implementation ofSqlValidatorTestCase.Tester
that talks to a mock catalog.This is to implement the default behavior: testing is only against the
SqlValidator
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.calcite.sql.test.SqlTester
SqlTester.ParameterChecker, SqlTester.ResultChecker, SqlTester.TypeChecker, SqlTester.VmName
-
-
Field Summary
Fields Modifier and Type Field Description protected SqlTestFactory
factory
-
Constructor Summary
Constructors Constructor Description AbstractSqlTester(SqlTestFactory factory)
-
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()
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.
-
-
-
Field Detail
-
factory
protected final SqlTestFactory factory
-
-
Constructor Detail
-
AbstractSqlTester
public AbstractSqlTester(SqlTestFactory factory)
-
-
Method Detail
-
getFactory
public final SqlTestFactory getFactory()
- Specified by:
getFactory
in interfaceSqlTester
-
close
public void close()
This default implementation does nothing.
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
getConformance
public final org.apache.calcite.sql.validate.SqlConformance getConformance()
- Specified by:
getConformance
in interfaceSqlValidatorTestCase.Tester
-
getValidator
public final org.apache.calcite.sql.validate.SqlValidator getValidator()
- Specified by:
getValidator
in interfaceSqlValidatorTestCase.Tester
-
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 interfaceSqlValidatorTestCase.Tester
- Parameters:
sql
- SQL statementexpectedMsgPattern
- 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)
-
getColumnType
public org.apache.calcite.rel.type.RelDataType getColumnType(java.lang.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")
returnsINTEGER
.Fails if query returns more than one column.
- Specified by:
getColumnType
in interfaceSqlValidatorTestCase.Tester
- See Also:
SqlValidatorTestCase.Tester.getResultType(String)
-
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')")
returnsRecordType(INTEGER EXPR$0, CHAR(3) EXPR#1)
.- Specified by:
getResultType
in interfaceSqlValidatorTestCase.Tester
-
parseAndValidate
public org.apache.calcite.sql.SqlNode parseAndValidate(org.apache.calcite.sql.validate.SqlValidator validator, java.lang.String sql)
- Specified by:
parseAndValidate
in interfaceSqlValidatorTestCase.Tester
-
parseQuery
public org.apache.calcite.sql.SqlNode parseQuery(java.lang.String sql) throws org.apache.calcite.sql.parser.SqlParseException
- Specified by:
parseQuery
in interfaceSqlValidatorTestCase.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 interfaceSqlTester
- Specified by:
checkColumnType
in interfaceSqlValidatorTestCase.Tester
- Parameters:
sql
- Query expressionexpected
- 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 interfaceSqlValidatorTestCase.Tester
- Parameters:
sql
- SQL queryfieldOriginList
- 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 interfaceSqlValidatorTestCase.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 interfaceSqlValidatorTestCase.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)
.
-
checkCollation
public void checkCollation(java.lang.String expression, java.lang.String expectedCollationName, org.apache.calcite.sql.SqlCollation.Coercibility expectedCoercibility)
- Specified by:
checkCollation
in interfaceSqlValidatorTestCase.Tester
-
checkCharset
public void checkCharset(java.lang.String expression, java.nio.charset.Charset expectedCharset)
- Specified by:
checkCharset
in interfaceSqlValidatorTestCase.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 interfaceSqlTester
-
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 interfaceSqlTester
-
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 interfaceSqlTester
-
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 interfaceSqlTester
-
withLex
public SqlTester withLex(org.apache.calcite.config.Lex lex)
Description copied from interface:SqlTester
Returns a tester that follows a lex policy.
-
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 interfaceSqlTester
-
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 interfaceSqlTester
-
withConnectionFactory
public SqlTester withConnectionFactory(CalciteAssert.ConnectionFactory connectionFactory)
Description copied from interface:SqlTester
Returns a tester that gets connections from a given factory.- Specified by:
withConnectionFactory
in interfaceSqlTester
-
with
protected final SqlTester with(java.lang.String name, java.lang.Object value)
-
with
protected abstract SqlTester with(SqlTestFactory factory)
-
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.
-
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);
-
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 interfaceSqlTester
- 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 resultdelta
- 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 interfaceSqlTester
- Parameters:
expr
- Aggregate expression, e.g.SUM(DISTINCT x)
inputValues
- Array of input values, e.g.["1", null, "2"]
.type
- Expected result typeresult
- Expected resultdelta
- 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 interfaceSqlTester
- Parameters:
expression
- Scalar expressionresult
- Expected resultresultType
- 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 interfaceSqlTester
- Parameters:
expression
- Scalar expressionresult
- 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 interfaceSqlTester
- Parameters:
expression
- Scalar expressionexpectedType
- Type we expect the result to have, including nullability, precision and scale, for exampleDECIMAL(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 interfaceSqlTester
- Parameters:
expression
- Scalar expressionexpectedType
- Type we expect the result to have, including nullability, precision and scale, for exampleDECIMAL(2, 1) NOT NULL
.expectedResult
- Expected resultdelta
- 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 interfaceSqlTester
- Parameters:
expression
- Scalar expressionresult
- 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 interfaceSqlTester
- Parameters:
expression
- Scalar expressionresult
- Expected resultexpectedType
- 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))");
-
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. Ifresult
is aPattern
, the result must match that pattern.
-
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 usingSqlTester.TypeChecker
andSqlTester.ResultChecker
functors.- Specified by:
check
in interfaceSqlTester
- Parameters:
query
- SQL querytypeChecker
- Checks whether the result is the expected type; must not be nullparameterChecker
- Checks whether the parameters are of expected typesresultChecker
- 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 interfaceSqlTester
- Parameters:
query
- SQL queryexpectedMonotonicity
- 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 interfaceSqlValidatorTestCase.Tester
- Parameters:
validator
- validator to use; null for defaultquery
- query to testexpectedRewrite
- 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 interfaceSqlTester
- Parameters:
expression
- SQL scalar expressionexpectedError
- 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 interfaceSqlTester
- Parameters:
sql
- SQL queryexpectedError
- 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 interfaceSqlTester
- 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 interfaceSqlValidatorTestCase.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 asCASE 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
-
-