OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
com.mapd.calcite.parser.ExtTableFunctionTypeChecker Class Reference
+ Inheritance diagram for com.mapd.calcite.parser.ExtTableFunctionTypeChecker:
+ Collaboration diagram for com.mapd.calcite.parser.ExtTableFunctionTypeChecker:

Classes

interface  ExtTableFunctionErrors
 

Public Member Functions

boolean isOptional (int argIndex)
 
boolean checkOperandTypes (SqlCallBinding callBinding, boolean throwOnFailure)
 
List< ExtTableFunctiongetOperatorOverloads (SqlOperator op)
 
SqlOperandCountRange getOperandCountRange ()
 
String getAllowedSignatures (SqlOperator op, String opName)
 
Consistency getConsistency ()
 
CalciteException newExtTableFunctionNameError (SqlCallBinding callBinding, SqlNode operand, String operandName)
 
CalciteException newExtTableFunctionSignatureError (SqlCallBinding callBinding)
 
String getCallSignature (SqlCallBinding callBinding, SqlValidator validator, SqlValidatorScope scope)
 

Static Public Attributes

static final ExtTableFunctionErrors UDTF_ERRORS
 

Package Functions

 ExtTableFunctionTypeChecker (HeavyDBSqlOperatorTable opTable)
 

Package Attributes

final HeavyDBSqlOperatorTable opTable
 

Detailed Description

Definition at line 42 of file ExtTableFunctionTypeChecker.java.

Constructor & Destructor Documentation

com.mapd.calcite.parser.ExtTableFunctionTypeChecker.ExtTableFunctionTypeChecker ( HeavyDBSqlOperatorTable  opTable)
inlinepackage

Definition at line 45 of file ExtTableFunctionTypeChecker.java.

References com.mapd.calcite.parser.ExtTableFunctionTypeChecker.opTable.

45  {
46  this.opTable = opTable;
47  }

Member Function Documentation

boolean com.mapd.calcite.parser.ExtTableFunctionTypeChecker.checkOperandTypes ( SqlCallBinding  callBinding,
boolean  throwOnFailure 
)
inline

Definition at line 58 of file ExtTableFunctionTypeChecker.java.

References com.mapd.calcite.parser.HeavyDBTypeCoercion.calculateTypeCoercionScore(), shared.contains(), com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getOperatorOverloads(), com.mapd.calcite.parser.ExtTableFunctionTypeChecker.newExtTableFunctionNameError(), and com.mapd.calcite.parser.ExtTableFunctionTypeChecker.newExtTableFunctionSignatureError().

58  {
59  Set<ExtTableFunction> candidateOverloads = new HashSet<ExtTableFunction>(
60  getOperatorOverloads(callBinding.getOperator()));
61 
62  for (SqlNode operand : callBinding.getCall().getOperandList()) {
63  if (operand != null && operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
64  final SqlCall assignmentCall = (SqlCall) operand;
65  final SqlIdentifier id = assignmentCall.operand(1);
66  final String paramName = id.getSimple();
67  if (!candidateOverloads.stream().anyMatch(
68  tf -> tf.getParamNames().contains(paramName))) {
69  throw newExtTableFunctionNameError(callBinding, operand, id.getSimple());
70  }
71  }
72  }
73 
74  // Remove all candidates whose number of formal args doesn't match the
75  // call's number of real args (accounting for possible default args).
76  candidateOverloads.removeIf(tf
77  -> (callBinding.getOperandCount()
78  < (tf.getArgTypes().size() - tf.getNumOptionalArguments()))
79  || (callBinding.getOperandCount() > tf.getArgTypes().size()));
80 
81  SqlNode[] operandArray = new SqlNode[callBinding.getCall().getOperandList().size()];
82  for (Ord<SqlNode> arg : Ord.zip(callBinding.getCall().getOperandList())) {
83  operandArray[arg.i] = arg.e;
84  }
85 
86  // Construct a candidate call binding for each overload. We need to do this because
87  // type inference of operands may differ depending on which operator is used. Thus,
88  // typechecking needs to be done on a candidate call-by-call basis.
89  HashMap<ExtTableFunction, SqlCallBinding> candidateBindings =
90  new HashMap<>(candidateOverloads.size());
91  for (ExtTableFunction tf : candidateOverloads) {
92  SqlBasicCall newCall = new SqlBasicCall(
93  tf, operandArray, callBinding.getCall().getParserPosition());
94  SqlCallBinding candidateBinding = new SqlCallBinding(
95  callBinding.getValidator(), callBinding.getScope(), newCall);
96  candidateBindings.put(tf, candidateBinding);
97  }
98 
99  // remove candidate calls that have DEFAULT values for operands which are
100  // mandatory
101  candidateOverloads.removeIf(tf
102  -> IntStream
103  .range(0,
104  candidateBindings.get(tf)
105  .permutedCall()
106  .getOperandList()
107  .size())
108  .anyMatch(idx
109  -> candidateBindings.get(tf)
110  .permutedCall()
111  .operand(idx)
112  .getKind()
113  == SqlKind.DEFAULT
114  && !tf.isArgumentOptional(idx)));
115 
116  // Compute a typechecking score for each candidate, taking into account type promotion
117  HeavyDBTypeCoercion tc =
118  (HeavyDBTypeCoercion) callBinding.getValidator().getTypeCoercion();
119  Map<ExtTableFunction, Integer> scoredCandidates =
120  new HashMap<ExtTableFunction, Integer>();
121  candidateOverloads.stream().forEach(udtf
122  -> scoredCandidates.put(udtf,
123  tc.calculateTypeCoercionScore(candidateBindings.get(udtf), udtf)));
124 
125  // Use the candidate with minimum cost
126  ExtTableFunction minCandidate;
127  try {
128  minCandidate = Collections
129  .min(scoredCandidates.entrySet()
130  .stream()
131  .filter(entry -> entry.getValue() >= 0)
132  .collect(Collectors.toSet()),
133  Map.Entry.comparingByValue())
134  .getKey();
135  } catch (NoSuchElementException e) {
136  // If there are no candidates left, the call is invalid.
137  if (throwOnFailure) {
138  throw(newExtTableFunctionSignatureError(callBinding));
139  }
140  return false;
141  }
142 
143  if (scoredCandidates.get(minCandidate) > 0) {
144  tc.extTableFunctionTypeCoercion(candidateBindings.get(minCandidate), minCandidate);
145  }
146 
147  // If there are candidates left, and the current bound operator
148  // is not one of them, rewrite the call to use a better binding.
149  ((SqlBasicCall) callBinding.getCall()).setOperator(minCandidate);
150 
151  return true;
152  }
bool contains(const T &container, const U &element)
Definition: misc.h:204
CalciteException newExtTableFunctionNameError(SqlCallBinding callBinding, SqlNode operand, String operandName)
CalciteException newExtTableFunctionSignatureError(SqlCallBinding callBinding)
List< ExtTableFunction > getOperatorOverloads(SqlOperator op)

+ Here is the call graph for this function:

String com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getAllowedSignatures ( SqlOperator  op,
String  opName 
)
inline

Definition at line 172 of file ExtTableFunctionTypeChecker.java.

References com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getOperatorOverloads().

172  {
173  List<ExtTableFunction> overloads = getOperatorOverloads(op);
174  return String.join(System.lineSeparator() + "\t",
175  overloads.stream()
176  .map(tf -> tf.getExtendedSignature())
177  .collect(Collectors.toList()));
178  }
List< ExtTableFunction > getOperatorOverloads(SqlOperator op)

+ Here is the call graph for this function:

String com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getCallSignature ( SqlCallBinding  callBinding,
SqlValidator  validator,
SqlValidatorScope  scope 
)
inline

Definition at line 203 of file ExtTableFunctionTypeChecker.java.

Referenced by com.mapd.calcite.parser.ExtTableFunctionTypeChecker.newExtTableFunctionSignatureError().

204  {
205  List<String> signatureList = new ArrayList<>();
206  for (final SqlNode operand : callBinding.permutedCall().getOperandList()) {
207  final RelDataType argType = validator.deriveType(scope, operand);
208  if (null == argType) {
209  continue;
210  } else if (argType.getSqlTypeName() == SqlTypeName.CURSOR) {
211  SqlCall cursorCall = (SqlCall) operand;
212  RelDataType cursorType = callBinding.getValidator().deriveType(
213  callBinding.getScope(), cursorCall.operand(0));
214  StringBuilder cursorTypeName = new StringBuilder();
215  cursorTypeName.append("CURSOR[");
216  for (int j = 0; j < cursorType.getFieldList().size(); j++) {
217  if (j > 0) {
218  cursorTypeName.append(",");
219  }
220  cursorTypeName.append(
221  cursorType.getFieldList().get(j).getType().getSqlTypeName());
222  }
223  cursorTypeName.append("]");
224  signatureList.add(cursorTypeName.toString());
225  } else {
226  signatureList.add(argType.toString());
227  }
228  }
229  return SqlUtil.getOperatorSignature(callBinding.getOperator(), signatureList);
230  }

+ Here is the caller graph for this function:

Consistency com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getConsistency ( )
inline

Definition at line 180 of file ExtTableFunctionTypeChecker.java.

180  {
181  return Consistency.NONE;
182  }
SqlOperandCountRange com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getOperandCountRange ( )
inline

Definition at line 168 of file ExtTableFunctionTypeChecker.java.

168  {
169  return SqlOperandCountRanges.any();
170  }
List<ExtTableFunction> com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getOperatorOverloads ( SqlOperator  op)
inline

Definition at line 154 of file ExtTableFunctionTypeChecker.java.

Referenced by com.mapd.calcite.parser.ExtTableFunctionTypeChecker.checkOperandTypes(), and com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getAllowedSignatures().

154  {
155  List<SqlOperator> overloads = new ArrayList<>();
156  opTable.lookupOperatorOverloads(op.getNameAsId(),
157  SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION,
158  SqlSyntax.FUNCTION,
159  overloads,
160  SqlNameMatchers.liberal());
161 
162  return overloads.stream()
163  .filter(p -> p instanceof ExtTableFunction)
164  .map(p -> (ExtTableFunction) p)
165  .collect(Collectors.toList());
166  }

+ Here is the caller graph for this function:

boolean com.mapd.calcite.parser.ExtTableFunctionTypeChecker.isOptional ( int  argIndex)
inline

Definition at line 54 of file ExtTableFunctionTypeChecker.java.

54  {
55  return false;
56  }
CalciteException com.mapd.calcite.parser.ExtTableFunctionTypeChecker.newExtTableFunctionNameError ( SqlCallBinding  callBinding,
SqlNode  operand,
String  operandName 
)
inline

Definition at line 184 of file ExtTableFunctionTypeChecker.java.

References com.mapd.calcite.parser.ExtTableFunctionTypeChecker.ExtTableFunctionErrors.paramNameMismatch(), and com.mapd.calcite.parser.ExtTableFunctionTypeChecker.UDTF_ERRORS.

Referenced by com.mapd.calcite.parser.ExtTableFunctionTypeChecker.checkOperandTypes().

185  {
186  return callBinding.getValidator().newValidationError(operand,
188  callBinding.getOperator().getName(), operandName));
189  }
ExInst< SqlValidatorException > paramNameMismatch(String udtf, String wrongParamName)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

CalciteException com.mapd.calcite.parser.ExtTableFunctionTypeChecker.newExtTableFunctionSignatureError ( SqlCallBinding  callBinding)
inline

Definition at line 191 of file ExtTableFunctionTypeChecker.java.

References com.mapd.calcite.parser.ExtTableFunctionTypeChecker.getCallSignature().

Referenced by com.mapd.calcite.parser.ExtTableFunctionTypeChecker.checkOperandTypes().

191  {
192  return callBinding.getValidator().newValidationError(callBinding.permutedCall(),
193  UDTF_ERRORS.typeMismatch(callBinding.getOperator().getName(),
194  getCallSignature(callBinding,
195  callBinding.getValidator(),
196  callBinding.getScope()),
197  System.getProperty("line.separator") + "\t"
198  + callBinding.getOperator().getAllowedSignatures()));
199  }
String getCallSignature(SqlCallBinding callBinding, SqlValidator validator, SqlValidatorScope scope)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

final HeavyDBSqlOperatorTable com.mapd.calcite.parser.ExtTableFunctionTypeChecker.opTable
package
final ExtTableFunctionErrors com.mapd.calcite.parser.ExtTableFunctionTypeChecker.UDTF_ERRORS
static
Initial value:
=
Resources.create(ExtTableFunctionErrors.class)

Definition at line 244 of file ExtTableFunctionTypeChecker.java.

Referenced by com.mapd.calcite.parser.ExtTableFunctionTypeChecker.newExtTableFunctionNameError().


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