OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander Class Reference
+ Inheritance diagram for org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander:
+ Collaboration diagram for org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander:

Public Member Functions

SqlNode go ()
 
SqlNode visit (SqlLiteral literal)
 
SqlNode visit (SqlIdentifier id)
 

Protected Member Functions

SqlNode visitScoped (SqlCall call)
 

Package Functions

 OrderExpressionExpander (SqlSelect select, SqlNode root)
 

Private Member Functions

SqlNode nthSelectItem (int ordinal, final SqlParserPos pos)
 

Private Attributes

final List< String > aliasList
 
final SqlSelect select
 
final SqlNode root
 

Detailed Description

Shuttle which walks over an expression in the ORDER BY clause, replacing usages of aliases with the underlying expression.

Definition at line 5993 of file SqlValidatorImpl.java.

Constructor & Destructor Documentation

org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.OrderExpressionExpander ( SqlSelect  select,
SqlNode  root 
)
inlinepackage

Definition at line 5998 of file SqlValidatorImpl.java.

References org.apache.calcite.sql.validate.SqlValidatorImpl.getNamespace(), org.apache.calcite.sql.validate.SqlValidatorImpl.getOrderScope(), org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.root, and org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.select.

5998  {
5999  super(getOrderScope(select));
6000  this.select = select;
6001  this.root = root;
6002  this.aliasList = getNamespace(select).getRowType().getFieldNames();
6003  }
SqlValidatorNamespace getNamespace(SqlNode node, SqlValidatorScope scope)
SqlValidatorScope getOrderScope(SqlSelect select)

+ Here is the call graph for this function:

Member Function Documentation

SqlNode org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.go ( )
inline

Definition at line 6005 of file SqlValidatorImpl.java.

6005  {
6006  return root.accept(this);
6007  }
SqlNode org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.nthSelectItem ( int  ordinal,
final SqlParserPos  pos 
)
inlineprivate

Returns the ordinalth item in the select list.

Definition at line 6038 of file SqlValidatorImpl.java.

References org.apache.calcite.sql.validate.SqlValidatorImpl.expandStar(), and org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.select.

Referenced by org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.visit().

6038  {
6039  // TODO: Don't expand the list every time. Maybe keep an expanded
6040  // version of each expression -- select lists and identifiers -- in
6041  // the validator.
6042 
6043  SqlNodeList expandedSelectList =
6044  expandStar(
6045  select.getSelectList(),
6046  select,
6047  false);
6048  SqlNode expr = expandedSelectList.get(ordinal);
6049  expr = stripAs(expr);
6050  if (expr instanceof SqlIdentifier) {
6051  expr = getScope().fullyQualify((SqlIdentifier) expr).identifier;
6052  }
6053 
6054  // Create a copy of the expression with the position of the order
6055  // item.
6056  return expr.clone(pos);
6057  }
SqlNodeList expandStar(SqlNodeList selectList, SqlSelect select, boolean includeSystemVars)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

SqlNode org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.visit ( SqlLiteral  literal)
inline

Definition at line 6009 of file SqlValidatorImpl.java.

References org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.aliasList, org.apache.calcite.sql.validate.SqlValidatorImpl.config(), org.apache.calcite.sql.validate.SqlValidatorImpl.newValidationError(), org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.nthSelectItem(), and org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.root.

6009  {
6010  // Ordinal markers, e.g. 'select a, b from t order by 2'.
6011  // Only recognize them if they are the whole expression,
6012  // and if the dialect permits.
6013  if (literal == root && config.sqlConformance().isSortByOrdinal()) {
6014  switch (literal.getTypeName()) {
6015  case DECIMAL:
6016  case DOUBLE:
6017  final int intValue = literal.intValue(false);
6018  if (intValue >= 0) {
6019  if (intValue < 1 || intValue > aliasList.size()) {
6020  throw newValidationError(
6021  literal, RESOURCE.orderByOrdinalOutOfRange());
6022  }
6023 
6024  // SQL ordinals are 1-based, but Sort's are 0-based
6025  int ordinal = intValue - 1;
6026  return nthSelectItem(ordinal, literal.getParserPosition());
6027  }
6028  break;
6029  }
6030  }
6031 
6032  return super.visit(literal);
6033  }
CalciteContextException newValidationError(SqlNode node, Resources.ExInst< SqlValidatorException > e)

+ Here is the call graph for this function:

SqlNode org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.visit ( SqlIdentifier  id)
inline

Definition at line 6059 of file SqlValidatorImpl.java.

References org.apache.calcite.sql.validate.SqlValidatorImpl.alias(), org.apache.calcite.sql.validate.SqlValidatorImpl.config(), field(), org.apache.calcite.sql.validate.SqlValidatorImpl.getNamespace(), org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.nthSelectItem(), and org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.select.

6059  {
6060  // Aliases, e.g. 'select a as x, b from t order by x'.
6061  if (id.isSimple()
6062  && config.sqlConformance().isSortByAlias()) {
6063  String alias = id.getSimple();
6064  final SqlValidatorNamespace selectNs = getNamespace(select);
6065  final RelDataType rowType =
6066  selectNs.getRowTypeSansSystemColumns();
6067  final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
6068  RelDataTypeField field = nameMatcher.field(rowType, alias);
6069  if (field != null) {
6070  return nthSelectItem(
6071  field.getIndex(),
6072  id.getParserPosition());
6073  }
6074  }
6075 
6076  // No match. Return identifier unchanged.
6077  return getScope().fullyQualify(id).identifier;
6078  }
SqlValidatorNamespace getNamespace(SqlNode node, SqlValidatorScope scope)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:33

+ Here is the call graph for this function:

SqlNode org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.visitScoped ( SqlCall  call)
inlineprotected

Definition at line 6080 of file SqlValidatorImpl.java.

6080  {
6081  // Don't attempt to expand sub-queries. We haven't implemented
6082  // these yet.
6083  if (call instanceof SqlSelect) {
6084  return call;
6085  }
6086  return super.visitScoped(call);
6087  }

Member Data Documentation

final List<String> org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.aliasList
private
final SqlNode org.apache.calcite.sql.validate.SqlValidatorImpl.OrderExpressionExpander.root
private

The documentation for this class was generated from the following file: