You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2014/03/17 18:32:23 UTC
git commit: PHOENIX-853 Support CAST(x as varchar(length)) syntax
(BrunoDumon)
Repository: incubator-phoenix
Updated Branches:
refs/heads/3.0 c57746043 -> 8c2f396a5
PHOENIX-853 Support CAST(x as varchar(length)) syntax (BrunoDumon)
Project: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/commit/8c2f396a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/tree/8c2f396a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/diff/8c2f396a
Branch: refs/heads/3.0
Commit: 8c2f396a555d17084a162cf95cfb89ee491a1c1b
Parents: c577460
Author: James Taylor <ja...@apache.org>
Authored: Mon Mar 17 10:32:18 2014 -0700
Committer: James Taylor <ja...@apache.org>
Committed: Mon Mar 17 10:32:18 2014 -0700
----------------------------------------------------------------------
phoenix-core/src/main/antlr3/PhoenixSQL.g | 7 +++++-
.../phoenix/compile/IndexStatementRewriter.java | 2 +-
.../org/apache/phoenix/parse/CastParseNode.java | 26 ++++++++++++--------
.../apache/phoenix/parse/ParseNodeFactory.java | 16 ++++++------
.../apache/phoenix/parse/ParseNodeRewriter.java | 2 +-
.../phoenix/compile/QueryCompilerTest.java | 23 ++++++++++++++++-
6 files changed, 54 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/8c2f396a/phoenix-core/src/main/antlr3/PhoenixSQL.g
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g b/phoenix-core/src/main/antlr3/PhoenixSQL.g
index 7dc3c5c..6ccd03e 100644
--- a/phoenix-core/src/main/antlr3/PhoenixSQL.g
+++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g
@@ -778,7 +778,12 @@ term returns [ParseNode ret]
$ret = factory.rowValueConstructor(l);
}
}
- | CAST LPAREN e=expression AS dt=identifier ar=(ARRAY | (LSQUARE RSQUARE))? RPAREN { $ret = ar==null ? factory.cast(e, dt) : factory.cast(e, dt, true); }
+ | CAST LPAREN e=expression AS dt=identifier (LPAREN length=NUMBER (COMMA scale=NUMBER)? RPAREN)? ar=(ARRAY | (LSQUARE RSQUARE))? RPAREN
+ { $ret = factory.cast(e, dt,
+ length == null ? null : Integer.parseInt(length.getText()),
+ scale == null ? null : Integer.parseInt(scale.getText()),
+ ar!=null);
+ }
| (n=NEXT | CURRENT) VALUE FOR s=from_table_name { $ret = n==null ? factory.currentValueFor(s) : factory.nextValueFor(s);}
;
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/8c2f396a/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
index 7a5ebe1..68a1218 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
@@ -109,7 +109,7 @@ public class IndexStatementRewriter extends ParseNodeRewriter {
// This is unnecessary and problematic in the case of a null value.
// TODO: test case for this
if (!isTopLevel() && indexColType != dataColType) {
- indexColNode = FACTORY.cast(indexColNode, dataColType);
+ indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
}
return indexColNode;
}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/8c2f396a/phoenix-core/src/main/java/org/apache/phoenix/parse/CastParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/CastParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/CastParseNode.java
index e998422..e4c7424 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/CastParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/CastParseNode.java
@@ -39,24 +39,22 @@ import org.apache.phoenix.util.SchemaUtil;
public class CastParseNode extends UnaryParseNode {
private final PDataType dt;
+ private final Integer maxLength;
+ private final Integer scale;
- CastParseNode(ParseNode expr, String dataType, boolean arr) {
- super(expr);
- if(arr == true) {
- PDataType baseType = PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(dataType));
- dt = PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE);
- } else {
- dt = PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(dataType));
- }
+ CastParseNode(ParseNode expr, String dataType, Integer maxLength, Integer scale, boolean arr) {
+ this(expr, PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(dataType)), maxLength, scale, arr);
}
- CastParseNode(ParseNode expr, PDataType dataType, boolean arr) {
+ CastParseNode(ParseNode expr, PDataType dataType, Integer maxLength, Integer scale, boolean arr) {
super(expr);
if (arr == true) {
dt = PDataType.fromTypeId(dataType.getSqlType() + PDataType.ARRAY_TYPE_BASE);
} else {
dt = dataType;
}
+ this.maxLength = maxLength;
+ this.scale = scale;
}
@Override
@@ -72,7 +70,15 @@ public class CastParseNode extends UnaryParseNode {
return dt;
}
- public static Expression convertToRoundExpressionIfNeeded(PDataType fromDataType, PDataType targetDataType, List<Expression> expressions) throws SQLException {
+ public Integer getMaxLength() {
+ return maxLength;
+ }
+
+ public Integer getScale() {
+ return scale;
+ }
+
+ public static Expression convertToRoundExpressionIfNeeded(PDataType fromDataType, PDataType targetDataType, List<Expression> expressions) throws SQLException {
Expression firstChildExpr = expressions.get(0);
if(fromDataType == targetDataType) {
return firstChildExpr;
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/8c2f396a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
index ed5ac60..2523e29 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
@@ -414,20 +414,20 @@ public class ParseNodeFactory {
return new LiteralParseNode(value);
}
- public CastParseNode cast(ParseNode expression, String dataType) {
- return new CastParseNode(expression, dataType, false);
+ public CastParseNode cast(ParseNode expression, String dataType, Integer maxLength, Integer scale) {
+ return new CastParseNode(expression, dataType, maxLength, scale, false);
}
- public CastParseNode cast(ParseNode expression, PDataType dataType) {
- return new CastParseNode(expression, dataType, false);
+ public CastParseNode cast(ParseNode expression, PDataType dataType, Integer maxLength, Integer scale) {
+ return new CastParseNode(expression, dataType, maxLength, scale, false);
}
- public CastParseNode cast(ParseNode expression, PDataType dataType, boolean arr) {
- return new CastParseNode(expression, dataType, arr);
+ public CastParseNode cast(ParseNode expression, PDataType dataType, Integer maxLength, Integer scale, boolean arr) {
+ return new CastParseNode(expression, dataType, maxLength, scale, arr);
}
- public CastParseNode cast(ParseNode expression, String dataType, boolean arr) {
- return new CastParseNode(expression, dataType, arr);
+ public CastParseNode cast(ParseNode expression, String dataType, Integer maxLength, Integer scale, boolean arr) {
+ return new CastParseNode(expression, dataType, maxLength, scale, arr);
}
public ParseNode rowValueConstructor(List<ParseNode> l) {
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/8c2f396a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
index 7984a56..6e15c0f 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
@@ -329,7 +329,7 @@ public class ParseNodeRewriter extends TraverseAllParseNodeVisitor<ParseNode> {
return leaveCompoundNode(node, nodes, new CompoundNodeFactory() {
@Override
public ParseNode createNode(List<ParseNode> children) {
- return NODE_FACTORY.cast(children.get(0), node.getDataType());
+ return NODE_FACTORY.cast(children.get(0), node.getDataType(), node.getMaxLength(), node.getScale());
}
});
}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/8c2f396a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
index bf00159..7c6640e 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
@@ -1005,7 +1005,28 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest {
assertTrue(e.getErrorCode() == SQLExceptionCode.TYPE_MISMATCH.getErrorCode());
}
}
-
+
+ @Test
+ public void testCastingWithLengthInSelect() throws Exception {
+ String query = "SELECT CAST (b_string AS VARCHAR(10)) FROM aTable";
+ List<Object> binds = Collections.emptyList();
+ compileQuery(query, binds);
+ }
+
+ @Test
+ public void testCastingWithLengthInWhere() throws Exception {
+ String query = "SELECT b_string FROM aTable WHERE CAST (b_string AS VARCHAR(10)) = 'b'";
+ List<Object> binds = Collections.emptyList();
+ compileQuery(query, binds);
+ }
+
+ @Test
+ public void testCastingWithLengthAndScaleInSelect() throws Exception {
+ String query = "SELECT CAST (x_decimal AS DECIMAL(10,5)) FROM aTable";
+ List<Object> binds = Collections.emptyList();
+ compileQuery(query, binds);
+ }
+
@Test
public void testUsingNonComparableDataTypesInRowValueConstructorFails() throws Exception {
String query = "SELECT a_integer, x_integer FROM aTable WHERE (a_integer, x_integer) > (2, 'abc')";