public SqlIdentifier fullyQualify(SqlIdentifier identifier) { // If it's a simple identifier, look for an alias. if (identifier.isSimple() && validator.getConformance().isSortByAlias()) { String name = identifier.names.get(0); final SqlValidatorNamespace selectNs = validator.getNamespace(select); final RelDataType rowType = selectNs.getRowType(); if (validator.catalogReader.field(rowType, name) != null) { return identifier; } } return super.fullyQualify(identifier); }
@Test public void testOrderUnionOrdinal() { if (!tester.getConformance().isSortByOrdinal()) { return; } check( "select empno, sal from emp " + "union all " + "select deptno, deptno from dept " + "order by 2", "${plan}"); }
conformance.isSortByAliasObscures() ? "unknown column empno" conformance.isSortByAlias() ? null conformance.isSortByOrdinal() ? "Ordinal out of range" conformance.isSortByAlias() ? null conformance.isSortByOrdinal() ? "Ordinal out of range" : null);
conformance.isSortByAliasObscures() ? "unknown column empno" conformance.isSortByAlias() ? null conformance.isSortByOrdinal() ? "Ordinal out of range" conformance.isSortByAlias() ? null conformance.isSortByOrdinal() ? "Ordinal out of range" : null);
+ "group by empno, deptno " + "order by empno * sum(sal + 2)", tester.getConformance().isSortByAliasObscures() ? "xxxx" : null); "select distinct cast(empno as bigint) as empno " + "from emp order by ^empno^", tester.getConformance().isSortByAlias() ? null : "Expression 'EMPNO' is not in the select clause"); checkFails( "select distinct cast(empno as bigint) as eno " + "from emp order by ^eno^", tester.getConformance().isSortByAlias() ? null : "Column 'ENO' not found in any table"); checkFails( "select distinct cast(empno as bigint) as empno " + "from emp e order by ^empno^", tester.getConformance().isSortByAlias() ? null : "Expression 'EMPNO' is not in the select clause"); if (tester.getConformance().isSortByOrdinal()) { check( "select distinct cast(empno as bigint) from emp order by 1"); "select distinct cast(empno as varchar(10)) as eno from emp " + " order by upper(^eno^)", tester.getConformance().isSortByAlias() ? null : "Column 'ENO' not found in any table");
@Test public void testOrderByAliasOverrides() { if (!tester.getConformance().isSortByAlias()) { return; } // plan should contain '(empno + 1) + 3' check( "select empno + 1 as empno, empno - 2 as y from emp order by empno + 3", "${plan}"); }
@Test public void testOrderUnionOrdinal() { if (!tester.getConformance().isSortByOrdinal()) { return; } check( "select empno, sal from emp " + "union all " + "select deptno, deptno from dept " + "order by 2", "${plan}"); }
+ "group by empno, deptno " + "order by empno * sum(sal + 2)", tester.getConformance().isSortByAliasObscures() ? "xxxx" : null); "select distinct cast(empno as bigint) as empno " + "from emp order by ^empno^", tester.getConformance().isSortByAlias() ? null : "Expression 'EMPNO' is not in the select clause"); checkFails( "select distinct cast(empno as bigint) as eno " + "from emp order by ^eno^", tester.getConformance().isSortByAlias() ? null : "Column 'ENO' not found in any table"); checkFails( "select distinct cast(empno as bigint) as empno " + "from emp e order by ^empno^", tester.getConformance().isSortByAlias() ? null : "Expression 'EMPNO' is not in the select clause"); if (tester.getConformance().isSortByOrdinal()) { check( "select distinct cast(empno as bigint) from emp order by 1"); "select distinct cast(empno as varchar(10)) as eno from emp " + " order by upper(^eno^)", tester.getConformance().isSortByAlias() ? null : "Column 'ENO' not found in any table");
@Test public void testOrderByAliasDoesNotOverride() { if (tester.getConformance().isSortByAlias()) { return; } // plan should contain 'empno + 3', not '(empno + 1) + 3' check( "select empno + 1 as empno, empno - 2 as y from emp order by empno + 3", "${plan}"); }
public SqlNode visit(SqlLiteral literal) { // Ordinal markers, e.g. 'select a, b from t order by 2'. // Only recognize them if they are the whole expression, // and if the dialect permits. if (literal == root && getConformance().isSortByOrdinal()) { switch (literal.getTypeName()) { case DECIMAL: case DOUBLE: final int intValue = literal.intValue(false); if (intValue >= 0) { if (intValue < 1 || intValue > aliasList.size()) { throw newValidationError( literal, RESOURCE.orderByOrdinalOutOfRange()); } // SQL ordinals are 1-based, but SortRel's are 0-based int ordinal = intValue - 1; return nthSelectItem(ordinal, literal.getParserPosition()); } break; } } return super.visit(literal); }
public SqlIdentifier fullyQualify(SqlIdentifier identifier) { // If it's a simple identifier, look for an alias. if (identifier.isSimple() && validator.getConformance().isSortByAlias()) { String name = identifier.names[0]; final SqlValidatorNamespace selectNs = validator.getNamespace(select); final RelDataType rowType = selectNs.getRowType(); if (SqlValidatorUtil.lookupField(rowType, name) != null) { return identifier; } } return super.fullyQualify(identifier); }
@Test public void testOrderByOrdinalDesc() { // FRG-98 if (!tester.getConformance().isSortByOrdinal()) { return; } check( "select empno + 1, deptno, empno from emp order by 2 desc", "${plan}"); // ordinals rounded down, so 2.5 should have same effect as 2, and // generate identical plan check( "select empno + 1, deptno, empno from emp order by 2.5 desc", "${plan}"); }
@Test public void testOrderByAliasDoesNotOverride() { if (tester.getConformance().isSortByAlias()) { return; } // plan should contain 'empno + 3', not '(empno + 1) + 3' check( "select empno + 1 as empno, empno - 2 as y from emp order by empno + 3", "${plan}"); }
@Test public void testOrderByOrdinalDesc() { // FRG-98 if (!tester.getConformance().isSortByOrdinal()) { return; } check( "select empno + 1, deptno, empno from emp order by 2 desc", "${plan}"); // ordinals rounded down, so 2.5 should have same effect as 2, and // generate identical plan check( "select empno + 1, deptno, empno from emp order by 2.5 desc", "${plan}"); }
@Test public void testOrderByAliasOverrides() { if (!tester.getConformance().isSortByAlias()) { return; } // plan should contain '(empno + 1) + 3' check( "select empno + 1 as empno, empno - 2 as y from emp order by empno + 3", "${plan}"); }
public SqlNode visit(SqlLiteral literal) { // Ordinal markers, e.g. 'select a, b from t order by 2'. // Only recognize them if they are the whole expression, // and if the dialect permits. if ((literal == root) && getConformance().isSortByOrdinal()) { if ((literal.getTypeName() == SqlTypeName.DECIMAL) || (literal.getTypeName() == SqlTypeName.DOUBLE)) { final int intValue = literal.intValue(false); if (intValue >= 0) { if ((intValue < 1) || (intValue > aliasList.size())) { throw newValidationError( literal, EigenbaseResource.instance() .OrderByOrdinalOutOfRange.ex()); } // SQL ordinals are 1-based, but SortRel's are 0-based int ordinal = intValue - 1; return nthSelectItem( ordinal, literal.getParserPosition()); } } } return super.visit(literal); }
public SqlNode visit(SqlIdentifier id) { // Aliases, e.g. 'select a as x, b from t order by x'. if (id.isSimple() && getConformance().isSortByAlias()) { String alias = id.getSimple(); final SqlValidatorNamespace selectNs = getNamespace(select); final RelDataType rowType = selectNs.getRowTypeSansSystemColumns(); RelDataTypeField field = catalogReader.field(rowType, alias); if (field != null) { return nthSelectItem( field.getIndex(), id.getParserPosition()); } } // No match. Return identifier unchanged. return getScope().fullyQualify(id); }
if (tester.getConformance().isSortByOrdinal()) { checkFails( "select empno, sal from emp "
public SqlNode visit(SqlIdentifier id) { // Aliases, e.g. 'select a as x, b from t order by x'. if (id.isSimple() && getConformance().isSortByAlias()) { String alias = id.getSimple(); final SqlValidatorNamespace selectNs = getNamespace(select); final RelDataType rowType = selectNs.getRowTypeSansSystemColumns(); RelDataTypeField field = SqlValidatorUtil.lookupField(rowType, alias); if (field != null) { return nthSelectItem( field.getIndex(), id.getParserPosition()); } } // No match. Return identifier unchanged. return getScope().fullyQualify(id); }
if (tester.getConformance().isSortByOrdinal()) { checkFails( "select empno, sal from emp "