You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2020/01/29 21:45:40 UTC

[GitHub] [phoenix] swaroopak commented on a change in pull request #688: PHOENIX-5629 Phoenix Function to return HBase row timestamp

swaroopak commented on a change in pull request #688: PHOENIX-5629 Phoenix Function to return HBase row timestamp
URL: https://github.com/apache/phoenix/pull/688#discussion_r372647838
 
 

 ##########
 File path: phoenix-core/src/it/java/org/apache/phoenix/end2end/RowTimestampEpochFunctionIT.java
 ##########
 @@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.end2end;
+
+import com.google.common.collect.Lists;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.client.ResultScanner;
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.phoenix.query.QueryConstants;
+import org.apache.phoenix.schema.PTable.ImmutableStorageScheme;
+import org.apache.phoenix.schema.PTable.QualifierEncodingScheme;
+import org.apache.phoenix.util.EncodedColumnsUtil;
+import org.apache.phoenix.util.EnvironmentEdgeManager;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.sql.Connection;
+import java.sql.Date;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.Collection;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Parameterized.class)
+public class RowTimestampEpochFunctionIT extends ParallelStatsDisabledIT {
+
+    private final boolean encoded;
+    private final String tableDDLOptions;
+
+    public RowTimestampEpochFunctionIT(QualifierEncodingScheme encoding, ImmutableStorageScheme storage) {
+        StringBuilder optionBuilder = new StringBuilder();
+        optionBuilder.append(" COLUMN_ENCODED_BYTES = " + encoding.ordinal());
+        optionBuilder.append(",IMMUTABLE_STORAGE_SCHEME = "+ storage.toString());
+        this.tableDDLOptions = optionBuilder.toString();
+        this.encoded = (encoding != QualifierEncodingScheme.NON_ENCODED_QUALIFIERS) ? true : false;
+    }
+
+    @Parameterized.Parameters(name = "encoding={0},storage={1}")
+    public static synchronized Collection<Object[]> data() {
+        List<Object[]> list = Lists.newArrayList();
+        for (QualifierEncodingScheme encoding : QualifierEncodingScheme.values()) {
+            for (ImmutableStorageScheme storage : ImmutableStorageScheme.values()) {
+                list.add(new Object[]{encoding, storage});
+            }
+        }
+        return list;
+    }
+
+    private void verifyHbaseRowTimestamp(String tableName, ResultSet rs, int expectedRowCount) throws Exception {
+
+        Scan scan = new Scan();
+        byte[] emptyKVQualifier = EncodedColumnsUtil.getEmptyKeyValueInfo(this.encoded).getFirst();
+        try (org.apache.hadoop.hbase.client.Connection hconn = ConnectionFactory.createConnection(config)) {
+            Table table = hconn.getTable(TableName.valueOf(tableName));
+            ResultScanner resultScanner = table.getScanner(scan);
+            int rowCount = 0;
+            while (rs.next()) {
+                Result result = resultScanner.next();
+                long timeStamp = result.getColumnLatestCell(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, emptyKVQualifier).getTimestamp();
+                assertEquals(rs.getDate(1).getTime(), timeStamp);
+                ++rowCount;
+            }
+            assertEquals(expectedRowCount, rowCount);
+        }
+    }
+
+    @Test
+    public void testRowTimestampDefault() throws Exception {
+
+        String tableName =  generateUniqueName();
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            String ddl = "CREATE TABLE IF NOT EXISTS " + tableName
+                    + " (PK INTEGER NOT NULL PRIMARY KEY, KV1 VARCHAR, KV2 VARCHAR)" + this.tableDDLOptions;
+            conn.createStatement().execute(ddl);
+
+            String dml = "UPSERT INTO " + tableName + " (PK, KV1, KV2) VALUES (?, ?, ?)";
+            PreparedStatement stmt = conn.prepareStatement(dml);
+
+            int count = 5;
+            for (int id = 0; id < count; ++id) {
+                stmt.setInt(1, id);
+                stmt.setString(2, "KV1_" + id);
+                stmt.setString(3, "KV2_" + id);
+                stmt.executeUpdate();
+            }
+            conn.commit();
+
+            String dql = "SELECT ROW_TIMESTAMP_EPOCH() FROM " + tableName;
+
+            ResultSet rs = conn.createStatement().executeQuery(dql);
+            // verify row timestamp returned by the query matches the empty column cell timestamp
+            verifyHbaseRowTimestamp(tableName, rs, count);
+
+            // update one row
+            conn.createStatement().execute("UPSERT INTO " + tableName
+                    + " (PK, KV1) VALUES (2, 'KV1_foo')");
+            conn.commit();
+
+            rs = conn.createStatement().executeQuery(dql);
+            // verify again after update
+            verifyHbaseRowTimestamp(tableName, rs, count);
+        }
+    }
+
+    @Test
+    public void testRowTimestampColumn() throws Exception {
+        String tableName =  generateUniqueName();
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            String ddl = "CREATE TABLE IF NOT EXISTS " + tableName
+                    + " (PK1 INTEGER NOT NULL, PK2 DATE NOT NULL, KV1 VARCHAR, KV2 VARCHAR"
+                    + " CONSTRAINT PK PRIMARY KEY(PK1, PK2 ROW_TIMESTAMP))" + this.tableDDLOptions;
+            conn.createStatement().execute(ddl);
+
+            String dml = "UPSERT INTO " + tableName + " (PK1, PK2, KV1, KV2) VALUES (?, ?, ?, ?)";
+
+            long rowTimestamp = EnvironmentEdgeManager.currentTimeMillis();
+            Date rowTimestampDate = new Date(rowTimestamp);
+            PreparedStatement stmt = conn.prepareStatement(dml);
+
+            int count = 5;
+            for (int id = 0; id < count; ++id) {
+                stmt.setInt(1, id);
+                stmt.setDate(2, rowTimestampDate);
+                stmt.setString(3, "KV1_" + id);
+                stmt.setString(4, "KV2_" + id);
+                stmt.executeUpdate();
+            }
+            conn.commit();
+
+            String dql = "SELECT ROW_TIMESTAMP_EPOCH() FROM " + tableName;
 
 Review comment:
   could you please add one test for using this function in the WHERE clause? 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services