Class AbstractSqlTester
- All Implemented Interfaces:
AutoCloseable
,SqlTester
- Direct Known Subclasses:
SqlOperatorTest.TesterImpl
,SqlValidatorTester
SqlTester
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.ValidatedNodeConsumer, SqlTester.ValidatedNodeFunction<R>, SqlTester.VmName
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
assertConvertsTo
(SqlTestFactory factory, DiffRepository diffRepos, String sql, String plan, boolean trim, boolean expression, boolean decorrelate) Checks that a SQL statement converts to a given plan, optionally trimming columns that are not needed.void
assertExceptionIsThrown
(SqlTestFactory factory, StringAndPos sap, @Nullable String expectedMsgPattern) Checks that a query is valid, or, if invalid, throws the right message at the right location.static String
buildQuery
(String expression) protected String
buildQuery2
(SqlTestFactory factory, String expression) Builds a query that extracts all literals as columns in an underlying select.static String
buildQueryAgg
(String expression) void
check
(SqlTestFactory factory, 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
(SqlTestFactory factory, String expr, String[] inputValues, SqlTester.ResultChecker resultChecker) Checks that an aggregate expression returns the expected result.void
checkAggFails
(SqlTestFactory factory, String expr, String[] inputValues, String expectedError, boolean runtime) Tests that an aggregate expression fails at run time.void
checkColumnType
(SqlTestFactory factory, String sql, String expected) Checks that a query returns one column of an expected type.void
checkFails
(SqlTestFactory factory, StringAndPos sap, String expectedError, boolean runtime) Tests that a scalar SQL expression fails at run time.protected void
checkParseEx
(Throwable e, @Nullable String expectedMsgPattern, StringAndPos sap) void
checkQueryFails
(SqlTestFactory factory, StringAndPos sap, String expectedError) Tests that a SQL query fails at prepare time.void
checkWinAgg
(SqlTestFactory factory, String expr, String[] inputValues, String windowSpec, String type, SqlTester.ResultChecker resultChecker) Checks that a windowed aggregate expression returns the expected result.void
close()
convertSqlToRel2
(SqlTestFactory factory, String sql, boolean decorrelate, boolean trim) Converts a SQL string to a (SqlValidator, RelNode) pair.createFieldTrimmer
(SqlValidator validator, RelBuilder relBuilder) Creates a RelFieldTrimmer.void
forEachQuery
(SqlTestFactory factory, String expression, Consumer<String> consumer) Given a scalar expression, generates a sequence of SQL queries that evaluate it, and calls a given action with each.getColumnType
(SqlTestFactory factory, String sql) Returns the data type of the sole column of a SQL query.getResultType
(SqlTestFactory factory, String sql) Returns the data type of the row returned by a SQL query.parseExpression
(SqlTestFactory factory, String expr) Parses an expression.parseQuery
(SqlTestFactory factory, String sql) Parses a query.void
setFor
(SqlOperator operator, SqlTester.VmName... unimplementedVmNames) Declares that this test is for a given operator.trimRelNode
(SqlTestFactory factory, RelNode relNode) Trims a RelNode.<R> R
validateAndApply
(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeFunction<R> function) Parses and validates a query, then calls a function on the result.void
validateAndThen
(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeConsumer consumer) Parses and validates a query, then calls an action on the result.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.calcite.sql.test.SqlTester
check, checkFails, convertSqlToRel
-
Constructor Details
-
AbstractSqlTester
public AbstractSqlTester()
-
-
Method Details
-
close
public void close()This default implementation does nothing.
- Specified by:
close
in interfaceAutoCloseable
-
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 interfaceSqlTester
- Parameters:
factory
- Factorysap
- SQL statementexpectedMsgPattern
- If this parameter is null the query must be
-
checkParseEx
-
getColumnType
Description copied from interface:SqlTester
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 interfaceSqlTester
- See Also:
-
getResultType
Description copied from interface:SqlTester
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 interfaceSqlTester
-
parseQuery
Description copied from interface:SqlTester
Parses a query.- Specified by:
parseQuery
in interfaceSqlTester
- Throws:
SqlParseException
-
parseExpression
Description copied from interface:SqlTester
Parses an expression.- Specified by:
parseExpression
in interfaceSqlTester
- Throws:
SqlParseException
-
checkColumnType
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
- Parameters:
factory
- Factorysql
- Query expressionexpected
- Type string
-
setFor
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(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);
-
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 interfaceSqlTester
- Parameters:
factory
- Factoryexpr
- Aggregate expression, e.g.SUM(DISTINCT x)
inputValues
- Array of input values, e.g.["1", null, "2"]
type
- Expected result typeresultChecker
- 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 usingSqlTester.TypeChecker
andSqlTester.ResultChecker
functors. -
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 interfaceSqlTester
-
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 interfaceSqlTester
-
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 interfaceSqlTester
- Parameters:
factory
- Factorysap
- 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
Description copied from interface:SqlTester
Tests that a SQL query fails at prepare time.- Specified by:
checkQueryFails
in interfaceSqlTester
- Parameters:
factory
- Factorysap
- SQL query and error positionexpectedError
- 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 interfaceSqlTester
- Parameters:
factory
- Factoryexpr
- An aggregate expressioninputValues
- Array of input valuesexpectedError
- Pattern for expected errorruntime
- If true, must fail at runtime; if false, must fail at validate time
-
buildQuery
-
buildQueryAgg
-
buildQuery2
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:
factory
- Test factoryexpression
- Scalar expression- Returns:
- Query that evaluates a scalar expression
-
forEachQuery
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 interfaceSqlTester
- Parameters:
factory
- Factoryexpression
- Scalar expressionconsumer
- 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 interfaceSqlTester
- Parameters:
factory
- FactorydiffRepos
- Diff repositorysql
- SQL query or expressionplan
- Expected plantrim
- Whether to trim columns that are not neededexpression
- True ifsql
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 interfaceSqlTester
-
trimRelNode
Description copied from interface:SqlTester
Trims a RelNode.- Specified by:
trimRelNode
in interfaceSqlTester
-
createFieldTrimmer
Creates a RelFieldTrimmer.- Parameters:
validator
- ValidatorrelBuilder
- Builder- Returns:
- Field trimmer
-