public CastParseNode cast(ParseNode expression, String dataType, Integer maxLength, Integer scale, boolean arr) { return new CastParseNode(expression, dataType, maxLength, scale, arr); }
@Override public <T> T accept(ParseNodeVisitor<T> visitor) throws SQLException { List<T> l = Collections.emptyList(); if (visitor.visitEnter(this)) { l = acceptChildren(visitor); } return visitor.visitLeave(this, l); }
@Override public void toSQL(ColumnResolver resolver, StringBuilder buf) { List<ParseNode> children = getChildren(); buf.append(" CAST("); children.get(0).toSQL(resolver, buf); buf.append(" AS "); boolean isArray = dt.isArrayType(); PDataType type = isArray ? PDataType.arrayBaseType(dt) : dt; buf.append(type.getSqlTypeName()); if (maxLength != null) { buf.append('('); buf.append(maxLength); if (scale != null) { buf.append(','); buf.append(scale); // has both max length and scale. For ex- decimal(10,2) } buf.append(')'); } if (isArray) { buf.append(' '); buf.append(PDataType.ARRAY_TYPE_SUFFIX); } buf.append(")"); } }
@Override public ParseNode createNode(List<ParseNode> children) { return NODE_FACTORY.cast(children.get(0), node.getDataType(), node.getMaxLength(), node.getScale()); } });
@Test public void testToSQL() { ColumnParseNode columnParseNode = new ColumnParseNode(TableName.create("SCHEMA1", "TABLE1"), "V"); CastParseNode castParseNode = new CastParseNode(columnParseNode, PLong.INSTANCE, null, null, false); StringBuilder stringBuilder = new StringBuilder(); castParseNode.toSQL(null, stringBuilder); assertEquals(" CAST(TABLE1.V AS BIGINT)", stringBuilder.toString()); }
@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)); }
@Override public boolean visitEnter(CastParseNode node) throws SQLException { throw new SQLFeatureNotSupportedException(node.toString()); }
@Override public ParseNode createNode(List<ParseNode> children) { return NODE_FACTORY.cast(children.get(0), node.getDataType(), node.getMaxLength(), node.getScale()); } });
@Test public void testToSQL_ArrayType() { ColumnParseNode columnParseNode = new ColumnParseNode(TableName.create("SCHEMA1", "TABLE1"), "V"); CastParseNode castParseNode = new CastParseNode(columnParseNode, PLong.INSTANCE, null, null, true); StringBuilder stringBuilder = new StringBuilder(); castParseNode.toSQL(null, stringBuilder); assertEquals(" CAST(TABLE1.V AS BIGINT ARRAY)", stringBuilder.toString()); } }
@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)); }
@Override public E visitLeave(CastParseNode node, List<E> l) throws SQLException { throw new SQLFeatureNotSupportedException(node.toString()); }
@Override public ParseNode createNode(List<ParseNode> children) { return NODE_FACTORY.cast(children.get(0), node.getDataType(), node.getMaxLength(), node.getScale()); } });
public CastParseNode cast(ParseNode expression, PDataType dataType, Integer maxLength, Integer scale) { return new CastParseNode(expression, dataType, maxLength, scale, false); }
@Test public void testToSQL_WithLengthAndScale() { ColumnParseNode columnParseNode = new ColumnParseNode(TableName.create("SCHEMA1", "TABLE1"), "V"); CastParseNode castParseNode = new CastParseNode(columnParseNode, PDecimal.INSTANCE, 5, 3, false); StringBuilder stringBuilder = new StringBuilder(); castParseNode.toSQL(null, stringBuilder); assertEquals(" CAST(TABLE1.V AS DECIMAL(5,3))", stringBuilder.toString()); }
@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)); }
@Override public E visitLeave(CastParseNode node, List<E> l) throws SQLException { throw new SQLFeatureNotSupportedException(node.toString()); }
@Override public <T> T accept(ParseNodeVisitor<T> visitor) throws SQLException { List<T> l = Collections.emptyList(); if (visitor.visitEnter(this)) { l = acceptChildren(visitor); } return visitor.visitLeave(this, l); }
@Override public void toSQL(ColumnResolver resolver, StringBuilder buf) { List<ParseNode> children = getChildren(); buf.append(" CAST("); children.get(0).toSQL(resolver, buf); buf.append(" AS "); boolean isArray = dt.isArrayType(); PDataType type = isArray ? PDataType.arrayBaseType(dt) : dt; buf.append(type.getSqlTypeName()); if (maxLength != null) { buf.append('('); buf.append(maxLength); if (scale != null) { buf.append(','); buf.append(scale); // has both max length and scale. For ex- decimal(10,2) } buf.append(')'); } if (isArray) { buf.append(' '); buf.append(PDataType.ARRAY_TYPE_SUFFIX); } buf.append(")"); } }
public CastParseNode cast(ParseNode expression, PDataType dataType, Integer maxLength, Integer scale, boolean arr) { return new CastParseNode(expression, dataType, maxLength, scale, arr); }
@Override public boolean visitEnter(CastParseNode node) throws SQLException { throw new SQLFeatureNotSupportedException(node.toString()); }