Search in sources :

Example 1 with SqlConformance

use of org.apache.calcite.sql.validate.SqlConformance in project calcite by apache.

the class CalcitePrepareImpl method createSqlValidator.

private SqlValidator createSqlValidator(Context context, CalciteCatalogReader catalogReader) {
    final SqlOperatorTable opTab0 = context.config().fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
    final SqlOperatorTable opTab = ChainedSqlOperatorTable.of(opTab0, catalogReader);
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final SqlConformance conformance = context.config().conformance();
    return new CalciteSqlValidator(opTab, catalogReader, typeFactory, conformance);
}
Also used : ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) SqlConformance(org.apache.calcite.sql.validate.SqlConformance)

Example 2 with SqlConformance

use of org.apache.calcite.sql.validate.SqlConformance in project calcite by apache.

the class SqlValidatorTest method testOrder.

@Test
public void testOrder() {
    final SqlConformance conformance = tester.getConformance();
    check("select empno as x from emp order by empno");
    // invalid use of 'asc'
    checkFails("select empno, sal from emp order by ^asc^", "Column 'ASC' not found in any table");
    // In sql92, empno is obscured by the alias.
    // Otherwise valid.
    // Checked Oracle10G -- is it valid.
    checkFails("select empno as x from emp order by empno", // in sql92, empno is obscured by the alias
    conformance.isSortByAliasObscures() ? "unknown column empno" : // otherwise valid
    null);
    checkFails("select empno as x from emp order by ^x^", // valid in oracle and pre-99 sql
    conformance.isSortByAlias() ? null : // invalid in sql:2003
    "Column 'X' not found in any table");
    checkFails("select empno as x from emp order by ^10^", // invalid in oracle and pre-99
    conformance.isSortByOrdinal() ? "Ordinal out of range" : // nothing!)
    null);
    // Has different meanings in different dialects (which makes it very
    // confusing!) but is always valid.
    check("select empno + 1 as empno from emp order by empno");
    // Always fails
    checkFails("select empno as x from emp, dept order by ^deptno^", "Column 'DEPTNO' is ambiguous");
    check("select empno + 1 from emp order by deptno asc, empno + 1 desc");
    checkFails("select empno as deptno from emp, dept order by deptno", // Checked Oracle10G -- it is valid.
    conformance.isSortByAlias() ? null : // Ambiguous in SQL:2003
    "col ambig");
    check("select deptno from dept\n" + "union\n" + "select empno from emp\n" + "order by deptno");
    checkFails("select deptno from dept\n" + "union\n" + "select empno from emp\n" + "order by ^empno^", "Column 'EMPNO' not found in any table");
    checkFails("select deptno from dept\n" + "union\n" + "select empno from emp\n" + "order by ^10^", // invalid in oracle and pre-99
    conformance.isSortByOrdinal() ? "Ordinal out of range" : null);
    // Sort by scalar sub-query
    check("select * from emp\n" + "order by (select name from dept where deptno = emp.deptno)");
    checkFails("select * from emp\n" + "order by (select name from dept where deptno = emp.^foo^)", "Column 'FOO' not found in table 'EMP'");
    // REVIEW jvs 10-Apr-2008:  I disabled this because I don't
    // understand what it means; see
    // testAggregateInOrderByFails for the discrimination I added
    // (SELECT should be aggregating for this to make sense).
    /*
        // Sort by aggregate. Oracle allows this.
        check("select 1 from emp order by sum(sal)");
*/
    // ORDER BY and SELECT *
    check("select * from emp order by empno");
    checkFails("select * from emp order by ^nonExistent^, deptno", "Column 'NONEXISTENT' not found in any table");
    // Overriding expression has different type.
    checkFails("select 'foo' as empno from emp order by ^empno + 5^", "(?s)Cannot apply '\\+' to arguments of type '<CHAR\\(3\\)> \\+ <INTEGER>'\\..*");
}
Also used : SqlConformance(org.apache.calcite.sql.validate.SqlConformance) Test(org.junit.Test)

Example 3 with SqlConformance

use of org.apache.calcite.sql.validate.SqlConformance in project calcite by apache.

the class SqlValidatorTest method testAbstractConformance.

@Test
public void testAbstractConformance() throws InvocationTargetException, IllegalAccessException {
    final SqlAbstractConformance c0 = new SqlAbstractConformance() {
    };
    final SqlConformance c1 = SqlConformanceEnum.DEFAULT;
    for (Method method : SqlConformance.class.getMethods()) {
        final Object o0 = method.invoke(c0);
        final Object o1 = method.invoke(c1);
        assertThat(method.toString(), Objects.equals(o0, o1), is(true));
    }
}
Also used : SqlAbstractConformance(org.apache.calcite.sql.validate.SqlAbstractConformance) SqlConformance(org.apache.calcite.sql.validate.SqlConformance) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 4 with SqlConformance

use of org.apache.calcite.sql.validate.SqlConformance in project calcite by apache.

the class PlannerImpl method validate.

public SqlNode validate(SqlNode sqlNode) throws ValidationException {
    ensure(State.STATE_3_PARSED);
    final SqlConformance conformance = conformance();
    final CalciteCatalogReader catalogReader = createCatalogReader();
    this.validator = new CalciteSqlValidator(operatorTable, catalogReader, typeFactory, conformance);
    this.validator.setIdentifierExpansion(true);
    try {
        validatedSqlNode = validator.validate(sqlNode);
    } catch (RuntimeException e) {
        throw new ValidationException(e);
    }
    state = State.STATE_4_VALIDATED;
    return validatedSqlNode;
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlConformance(org.apache.calcite.sql.validate.SqlConformance)

Example 5 with SqlConformance

use of org.apache.calcite.sql.validate.SqlConformance in project calcite by apache.

the class DefaultSqlTestFactory method getValidator.

public SqlValidator getValidator(SqlTestFactory factory) {
    final Xyz xyz = cache.getUnchecked(factory);
    final SqlConformance conformance = (SqlConformance) factory.get("conformance");
    return SqlValidatorUtil.newValidator(xyz.operatorTable, xyz.catalogReader, xyz.typeFactory, conformance);
}
Also used : SqlConformance(org.apache.calcite.sql.validate.SqlConformance)

Aggregations

SqlConformance (org.apache.calcite.sql.validate.SqlConformance)5 Test (org.junit.Test)2 Method (java.lang.reflect.Method)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)1 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)1 SqlAbstractConformance (org.apache.calcite.sql.validate.SqlAbstractConformance)1 ValidationException (org.apache.calcite.tools.ValidationException)1