phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maryann...@apache.org
Subject phoenix git commit: Add maxLength in cast(); Code refactoring for UPSERT and DELETE
Date Thu, 02 Jun 2016 18:40:09 GMT
Repository: phoenix
Updated Branches:
  refs/heads/calcite 700a94181 -> 826f0af64


Add maxLength in cast(); Code refactoring for UPSERT and DELETE


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/826f0af6
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/826f0af6
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/826f0af6

Branch: refs/heads/calcite
Commit: 826f0af64a5883bf6ada614b9f05673490bae697
Parents: 700a941
Author: maryannxue <maryann.xue@gmail.com>
Authored: Thu Jun 2 14:40:00 2016 -0400
Committer: maryannxue <maryann.xue@gmail.com>
Committed: Thu Jun 2 14:40:00 2016 -0400

----------------------------------------------------------------------
 .../apache/phoenix/calcite/CalciteDMLIT.java    |  9 +++++++++
 .../apache/phoenix/calcite/CalciteUtils.java    | 20 +++++++++++++-------
 .../phoenix/calcite/rel/PhoenixTableModify.java | 18 ++++++++++++++----
 .../calcite/rules/PhoenixConverterRules.java    |  6 ++++++
 4 files changed, 42 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/826f0af6/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteDMLIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteDMLIT.java b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteDMLIT.java
index 8b1d0fe..fc58968 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteDMLIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteDMLIT.java
@@ -100,4 +100,13 @@ public class CalciteDMLIT extends BaseCalciteIT {
                 {"x00000000000003", "y00000000000003", null}})
             .close();
     }
+    
+    @Test public void testDelete() throws Exception {
+        start(PROPS).sql("delete from atable where organization_id = '1' and entity_id =
'1'")
+            .explainIs("PhoenixToEnumerableConverter\n" +
+                       "  PhoenixTableModify(table=[[phoenix, ATABLE]], operation=[DELETE],
updateColumnList=[[]], flattened=[false])\n" +
+                       "    PhoenixTableScan(table=[[phoenix, ATABLE]], filter=[AND(=($0,
CAST('1'):CHAR(15) CHARACTER SET \"ISO-8859-1\" COLLATE \"ISO-8859-1$en_US$primary\" NOT NULL),
=($1, CAST('1'):CHAR(15) CHARACTER SET \"ISO-8859-1\" COLLATE \"ISO-8859-1$en_US$primary\"
NOT NULL))])\n")
+            //.executeUpdate()
+            .close();
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/826f0af6/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
index 252f44e..8bce4cd 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
@@ -379,7 +379,7 @@ public class CalciteUtils {
                     }
                     
                     PDataType targetType = relDataTypeToPDataType(node.getType());
-                    return cast(targetType, expr, implementor);
+                    return cast(targetType, null, expr, implementor);
                 } catch (SQLException e) {
                     throw new RuntimeException(e);
                 }
@@ -492,7 +492,7 @@ public class CalciteUtils {
                         throw TypeMismatchException.newException(theType, node.toString());
                     }
                     PDataType targetType = relDataTypeToPDataType(node.getType());
-                    return cast(targetType, expr, implementor);
+                    return cast(targetType, null, expr, implementor);
                 } catch (SQLException e) {
                     throw new RuntimeException(e);
                 }
@@ -539,7 +539,7 @@ public class CalciteUtils {
                         expr = LiteralExpression.newConstant(null, theType, determinism);
                     }
                     PDataType targetType = relDataTypeToPDataType(node.getType());
-                    return cast(targetType, expr, implementor);
+                    return cast(targetType, null, expr, implementor);
                 } catch (SQLException e) {
                     throw new RuntimeException(e);
                 }
@@ -586,7 +586,7 @@ public class CalciteUtils {
                         expr = LiteralExpression.newConstant(null, theType, determinism);
                     }
                     PDataType targetType = relDataTypeToPDataType(node.getType());
-                    return cast(targetType, expr, implementor);
+                    return cast(targetType, null, expr, implementor);
                 } catch (SQLException e) {
                     throw new RuntimeException(e);
                 }
@@ -663,8 +663,14 @@ public class CalciteUtils {
                     PhoenixRelImplementor implementor) {                
                 List<Expression> children = convertChildren((RexCall) node, implementor);
                 PDataType targetType = relDataTypeToPDataType(node.getType());
+                Integer maxLength =
+                        (targetType == PChar.INSTANCE
+                            || targetType == PCharArray.INSTANCE
+                            || targetType == PBinary.INSTANCE
+                            || targetType == PBinaryArray.INSTANCE) ?
+                        node.getType().getPrecision() : null;
                 try {
-                    return cast(targetType, children.get(0), implementor);
+                    return cast(targetType, maxLength, children.get(0), implementor);
                 } catch (SQLException e) {
                     throw new RuntimeException(e);
                 }
@@ -901,14 +907,14 @@ public class CalciteUtils {
     }
     
     @SuppressWarnings("rawtypes")
-    private static Expression cast(PDataType targetDataType, Expression childExpr, PhoenixRelImplementor
implementor) throws SQLException {
+    private static Expression cast(PDataType targetDataType, Integer maxLength, Expression
childExpr, PhoenixRelImplementor implementor) throws SQLException {
         PDataType fromDataType = childExpr.getDataType();
         
         Expression expr = childExpr;
         if(fromDataType != null && implementor.getTableMapping().getPTable().getType()
!= PTableType.INDEX) {
             expr =  convertToRoundExpressionIfNeeded(fromDataType, targetDataType, childExpr);
         }
-        return CoerceExpression.create(expr, targetDataType, SortOrder.getDefault(), expr.getMaxLength(),
implementor.getTableMapping().getPTable().rowKeyOrderOptimizable());
+        return CoerceExpression.create(expr, targetDataType, SortOrder.getDefault(), maxLength,
implementor.getTableMapping().getPTable().rowKeyOrderOptimizable());
     }
     
     @SuppressWarnings("rawtypes")

http://git-wip-us.apache.org/repos/asf/phoenix/blob/826f0af6/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableModify.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableModify.java
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableModify.java
index 6629cb4..196f839 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableModify.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableModify.java
@@ -56,6 +56,7 @@ public class PhoenixTableModify extends TableModify implements PhoenixRel
{
             RelOptTable table, CatalogReader catalogReader, RelNode child,
             Operation operation, List<String> updateColumnList, boolean flattened)
{
         super(cluster, traits, table, catalogReader, child, operation, updateColumnList,
flattened);
+        assert operation == Operation.INSERT || operation == Operation.DELETE;
     }
 
     @Override public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
@@ -72,20 +73,29 @@ public class PhoenixTableModify extends TableModify implements PhoenixRel
{
 
     @Override
     public StatementPlan implement(PhoenixRelImplementor implementor) {
-        if (getOperation() != Operation.INSERT) {
-            throw new UnsupportedOperationException();
-        }
-        
         final QueryPlan queryPlan = implementor.visitInput(0, (PhoenixQueryRel) input);
         final RowProjector projector = implementor.getTableMapping().createRowProjector();
 
         final PhoenixTable targetTable = getTable().unwrap(PhoenixTable.class);
         final PhoenixConnection connection = targetTable.pc;
         final TableRef targetTableRef = targetTable.tableMapping.getTableRef();
+        
+        if (getOperation() == Operation.INSERT) {
+            return upsert(connection, targetTable, targetTableRef, queryPlan, projector);
+        }
+        
+        // delete
+        throw new UnsupportedOperationException();
+    }
+    
+    private static MutationPlan upsert(final PhoenixConnection connection,
+            final PhoenixTable targetTable, final TableRef targetTableRef,
+            final QueryPlan queryPlan, final RowProjector projector) {
         try (PhoenixStatement stmt = new PhoenixStatement(connection)) {
             final ColumnResolver resolver = FromCompiler.getResolver(targetTableRef);
             final StatementContext context = new StatementContext(stmt, resolver, new Scan(),
new SequenceManager(stmt));
 
+            // TODO TenantId, ViewIndexId, UpdatableViewColumns
             final List<PColumn> mappedColumns = targetTable.tableMapping.getMappedColumns();
             final int[] columnIndexes = new int[mappedColumns.size()];
             final int[] pkSlotIndexes = new int[mappedColumns.size()];

http://git-wip-us.apache.org/repos/asf/phoenix/blob/826f0af6/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
index 119dad1..58ff7cc 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
@@ -24,6 +24,7 @@ import org.apache.calcite.rel.core.JoinInfo;
 import org.apache.calcite.rel.core.JoinRelType;
 import org.apache.calcite.rel.core.Project;
 import org.apache.calcite.rel.core.SemiJoin;
+import org.apache.calcite.rel.core.TableModify.Operation;
 import org.apache.calcite.rel.core.Uncollect;
 import org.apache.calcite.rel.core.Union;
 import org.apache.calcite.rel.logical.LogicalAggregate;
@@ -837,6 +838,11 @@ public class PhoenixConverterRules {
 
         public RelNode convert(RelNode rel) {
             final LogicalTableModify modify = (LogicalTableModify) rel;
+            if (modify.getOperation() != Operation.INSERT
+                    && modify.getOperation() != Operation.DELETE) {
+                return null;
+            }
+            
             final PhoenixTable phoenixTable = modify.getTable().unwrap(PhoenixTable.class);
             if (phoenixTable == null) {
                 return null;


Mime
View raw message