public static Expression create(Expression expression, PDataType toType, SortOrder toSortOrder, Integer maxLength) throws SQLException { return create(expression, toType, toSortOrder, maxLength, true); }
public static Expression create(List<Expression> children) throws SQLException { Expression firstChild = children.get(0); PDataType firstChildDataType = firstChild.getDataType(); String timeUnit = (String)((LiteralExpression)children.get(1)).getValue(); if(TimeUnit.MILLISECOND.toString().equalsIgnoreCase(timeUnit)) { return new CeilTimestampExpression(children); } // Coerce TIMESTAMP to DATE, as the nanos has no affect List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size()); newChildren.add(CoerceExpression.create(firstChild, firstChildDataType == PTimestamp.INSTANCE ? PDate.INSTANCE : PUnsignedDate.INSTANCE)); newChildren.addAll(children.subList(1, children.size())); return CeilDateExpression.create(newChildren); }
public static Expression create (List<Expression> children) throws SQLException { Expression firstChild = children.get(0); PDataType firstChildDataType = firstChild.getDataType(); String timeUnit = (String)((LiteralExpression)children.get(1)).getValue(); LiteralExpression multiplierExpr = (LiteralExpression)children.get(2); /* * When rounding off timestamp to milliseconds, nanos play a part only when the multiplier value * is equal to 1. This is because for cases when multiplier value is greater than 1, number of nanos/multiplier * will always be less than half the nanos in a millisecond. */ if((timeUnit == null || TimeUnit.MILLISECOND.toString().equalsIgnoreCase(timeUnit)) && ((Number)multiplierExpr.getValue()).intValue() == 1) { return new RoundTimestampExpression(children); } // Coerce TIMESTAMP to DATE, as the nanos has no affect List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size()); newChildren.add(CoerceExpression.create(firstChild, firstChildDataType == PTimestamp.INSTANCE ? PDate.INSTANCE : PUnsignedDate.INSTANCE)); newChildren.addAll(children.subList(1, children.size())); return RoundDateExpression.create(newChildren); }
private static TupleProjector getTupleProjector(RowProjector rowProj, List<TargetDataExpression> targetTypes) throws SQLException { Expression[] exprs = new Expression[targetTypes.size()]; int i = 0; for (ColumnProjector colProj : rowProj.getColumnProjectors()) { exprs[i] = CoerceExpression.create(colProj.getExpression(), targetTypes.get(i).getType(), targetTypes.get(i).getSortOrder(), targetTypes.get(i).getMaxLength()); i++; } return new TupleProjector(exprs); }
public RowValueConstructorExpression rewriteAllChildrenAsc( RowValueConstructorExpression rvcExpression) throws SQLException { List<Expression> replacementChildren = new ArrayList<>(rvcExpression.getChildren().size()); for (int i = 0; i < rvcExpression.getChildren().size(); i++) { Expression child = rvcExpression.getChildren().get(i); if (child.getSortOrder() == SortOrder.DESC) { //As The KeySlot visitor has not been setup for InvertFunction need to Use Coerce child = CoerceExpression.create(child, child.getDataType(), SortOrder.ASC, null); } replacementChildren.add(child); } return rvcExpression.clone(replacementChildren); } }
public static Expression create(List<Expression> children) throws SQLException { Expression firstChild = children.get(0); PDataType firstChildDataType = firstChild.getDataType(); if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE){ // Coerce TIMESTAMP to DATE, as the nanos has no affect List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size()); newChildren.add(CoerceExpression.create(firstChild, firstChildDataType == PTimestamp.INSTANCE ? PDate.INSTANCE : PUnsignedDate.INSTANCE)); newChildren.addAll(children.subList(1, children.size())); children = newChildren; } Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new FloorWeekExpression(children); case MONTH: return new FloorMonthExpression(children); case YEAR: return new FloorYearExpression(children); default: return new FloorDateExpression(children); } }
@Override public Expression wrap(Expression lhs, Expression rhs, boolean rowKeyOrderOptimizable) throws SQLException { PDataType lhsType = lhs.getDataType(); Expression e = CoerceExpression.create(rhs, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }
newChildren = new ArrayList<Expression>(children); newChildren.set(i, CoerceExpression.create(child, returnType));
@Override public Expression wrap(Expression lhs, Expression rhs, boolean rowKeyOrderOptimizable) throws SQLException { Expression e = rhs; PDataType rhsType = rhs.getDataType(); PDataType lhsType = lhs.getDataType(); if (rhsType == PDecimal.INSTANCE && lhsType != PDecimal.INSTANCE) { e = CeilDecimalExpression.create(rhs); } else if ((rhsType == PTimestamp.INSTANCE || rhsType == PUnsignedTimestamp.INSTANCE) && (lhsType != PTimestamp.INSTANCE && lhsType != PUnsignedTimestamp.INSTANCE)) { e = CeilTimestampExpression.create(rhs); } e = CoerceExpression.create(e, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }
@Override public Expression wrap(Expression lhs, Expression rhs, boolean rowKeyOrderOptimizable) throws SQLException { Expression e = rhs; PDataType rhsType = rhs.getDataType(); PDataType lhsType = lhs.getDataType(); if (rhsType == PDecimal.INSTANCE && lhsType != PDecimal.INSTANCE) { e = FloorDecimalExpression.create(rhs); } else if ((rhsType == PTimestamp.INSTANCE || rhsType == PUnsignedTimestamp.INSTANCE) && (lhsType != PTimestamp.INSTANCE && lhsType != PUnsignedTimestamp.INSTANCE)) { e = FloorDateExpression.create(rhs, TimeUnit.MILLISECOND); } e = CoerceExpression.create(e, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }
private static Expression coerceIfNecessary(int index, List<? extends PDatum> targetColumns, Expression expression) throws SQLException { if (index < targetColumns.size()) { PDatum targetColumn = targetColumns.get(index); if (targetColumn.getDataType() != expression.getDataType()) { PDataType targetType = targetColumn.getDataType(); // Check if coerce allowed using more relaxed isCastableTo check, since we promote INTEGER to LONG // during expression evaluation and then convert back to INTEGER on UPSERT SELECT (and we don't have // (an actual value we can specifically check against). if (expression.getDataType() != null && !expression.getDataType().isCastableTo(targetType)) { throw new ArgumentTypeMismatchException(targetType, expression.getDataType(), "column: " + targetColumn); } expression = CoerceExpression.create(expression, targetType, targetColumn.getSortOrder(), targetColumn.getMaxLength()); } } return expression; } /**
SortOrder toSortOrder = strategy == Strategy.SORT_MERGE ? SortOrder.ASC : (strategy == Strategy.HASH_BUILD_LEFT ? right.getSortOrder() : left.getSortOrder()); if (left.getDataType() != toType || left.getSortOrder() != toSortOrder) { left = CoerceExpression.create(left, toType, toSortOrder, left.getMaxLength()); right = CoerceExpression.create(right, toType, toSortOrder, right.getMaxLength());
keyExpressions.set(i, CoerceExpression.create(expression, keyType));
public CoalesceFunction(List<Expression> children) throws SQLException { super(children); Expression firstChild = children.get(0); Expression secondChild = children.get(1); if (ExpressionUtil.isConstant(secondChild)) { // is literal ImmutableBytesWritable ptr = new ImmutableBytesPtr(); secondChild.evaluate(null, ptr); if (ptr.getLength()!=0 && !secondChild.getDataType().isCoercibleTo(firstChild.getDataType(), secondChild.getDataType().toObject(ptr))) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_MISMATCH) .setMessage(getName() + " expected " + firstChild.getDataType() + ", but got " + secondChild.getDataType()) .build().buildException(); } } else { // second parameter is expression if (!secondChild.getDataType().isCoercibleTo(getDataType())) { // cast explicitly children.add(1, CoerceExpression.create(secondChild, firstChild.getDataType())); } } }
@Override public Expression visitLeave(CastParseNode node, List<Expression> children) throws SQLException { ParseNode childNode = node.getChildren().get(0); PDataType targetDataType = node.getDataType(); Expression childExpr = children.get(0); PDataType fromDataType = childExpr.getDataType(); if (childNode instanceof BindParseNode) { context.getBindManager().addParamMetaData((BindParseNode)childNode, childExpr); } Expression expr = childExpr; if(fromDataType != null) { /* * IndexStatementRewriter creates a CAST parse node when rewriting the query to use * indexed columns. Without this check present we wrongly and unnecessarily * end up creating a RoundExpression. */ if (context.getCurrentTable().getTable().getType() != PTableType.INDEX) { expr = convertToRoundExpressionIfNeeded(fromDataType, targetDataType, children); } } boolean rowKeyOrderOptimizable = context.getCurrentTable().getTable().rowKeyOrderOptimizable(); return wrapGroupByExpression(CoerceExpression.create(expr, targetDataType, SortOrder.getDefault(), expr.getMaxLength(), rowKeyOrderOptimizable)); }
expression = CoerceExpression.create(expression, indexColumn.getDataType());
private static TupleProjector getTupleProjector(RowProjector rowProj, List<TargetDataExpression> targetTypes) throws SQLException { Expression[] exprs = new Expression[targetTypes.size()]; int i = 0; for (ColumnProjector colProj : rowProj.getColumnProjectors()) { exprs[i] = CoerceExpression.create(colProj.getExpression(), targetTypes.get(i).getType(), targetTypes.get(i).getSortOrder(), targetTypes.get(i).getMaxLength()); i++; } return new TupleProjector(exprs); }
private static TupleProjector getTupleProjector(RowProjector rowProj, List<TargetDataExpression> targetTypes) throws SQLException { Expression[] exprs = new Expression[targetTypes.size()]; int i = 0; for (ColumnProjector colProj : rowProj.getColumnProjectors()) { exprs[i] = CoerceExpression.create(colProj.getExpression(), targetTypes.get(i).getType(), targetTypes.get(i).getSortOrder(), targetTypes.get(i).getMaxLength()); i++; } return new TupleProjector(exprs); }
@Override public Expression wrap(Expression lhs, Expression rhs, boolean rowKeyOrderOptimizable) throws SQLException { PDataType lhsType = lhs.getDataType(); Expression e = CoerceExpression.create(rhs, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }
@Override public Expression wrap(Expression lhs, Expression rhs, boolean rowKeyOrderOptimizable) throws SQLException { PDataType lhsType = lhs.getDataType(); Expression e = CoerceExpression.create(rhs, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }