All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.org.apache.calcite.sql.validate.OrderByScope Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hazelcast.org.apache.calcite.sql.validate;

import com.hazelcast.org.apache.calcite.rel.type.RelDataType;
import com.hazelcast.org.apache.calcite.rel.type.RelDataTypeField;
import com.hazelcast.org.apache.calcite.sql.SqlIdentifier;
import com.hazelcast.org.apache.calcite.sql.SqlNode;
import com.hazelcast.org.apache.calcite.sql.SqlNodeList;
import com.hazelcast.org.apache.calcite.sql.SqlSelect;

import java.util.List;

import static com.hazelcast.org.apache.calcite.util.Static.RESOURCE;

/**
 * Represents the name-resolution context for expressions in an ORDER BY clause.
 *
 * 

In some dialects of SQL, the ORDER BY clause can reference column aliases * in the SELECT clause. For example, the query

* *
SELECT empno AS x
* FROM emp
* ORDER BY x
* *

is valid.

*/ public class OrderByScope extends DelegatingScope { //~ Instance fields -------------------------------------------------------- private final SqlNodeList orderList; private final SqlSelect select; //~ Constructors ----------------------------------------------------------- OrderByScope( SqlValidatorScope parent, SqlNodeList orderList, SqlSelect select) { super(parent); this.orderList = orderList; this.select = select; } //~ Methods ---------------------------------------------------------------- public SqlNode getNode() { return orderList; } public void findAllColumnNames(List result) { final SqlValidatorNamespace ns = validator.getNamespace(select); addColumnNames(ns, result); } public SqlQualified fullyQualify(SqlIdentifier identifier) { // If it's a simple identifier, look for an alias. if (identifier.isSimple() && validator.config().sqlConformance().isSortByAlias()) { final String name = identifier.names.get(0); final SqlValidatorNamespace selectNs = validator.getNamespace(select); final RelDataType rowType = selectNs.getRowType(); final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher(); final RelDataTypeField field = nameMatcher.field(rowType, name); final int aliasCount = aliasCount(nameMatcher, name); if (aliasCount > 1) { // More than one column has this alias. throw validator.newValidationError(identifier, RESOURCE.columnAmbiguous(name)); } if (field != null && !field.isDynamicStar() && aliasCount == 1) { // if identifier is resolved to a dynamic star, use super.fullyQualify() for such case. return SqlQualified.create(this, 1, selectNs, identifier); } } return super.fullyQualify(identifier); } /** Returns the number of columns in the SELECT clause that have {@code name} * as their implicit (e.g. {@code t.name}) or explicit (e.g. * {@code t.c as name}) alias. */ private int aliasCount(SqlNameMatcher nameMatcher, String name) { int n = 0; for (SqlNode s : select.getSelectList()) { final String alias = SqlValidatorUtil.getAlias(s, -1); if (alias != null && nameMatcher.matches(alias, name)) { n++; } } return n; } public RelDataType resolveColumn(String name, SqlNode ctx) { final SqlValidatorNamespace selectNs = validator.getNamespace(select); final RelDataType rowType = selectNs.getRowType(); final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher(); final RelDataTypeField field = nameMatcher.field(rowType, name); if (field != null) { return field.getType(); } final SqlValidatorScope selectScope = validator.getSelectScope(select); return selectScope.resolveColumn(name, ctx); } public void validateExpr(SqlNode expr) { SqlNode expanded = validator.expandOrderExpr(select, expr); // expression needs to be valid in parent scope too parent.validateExpr(expanded); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy