jpisaac commented on code in PR #1953:
URL: https://github.com/apache/phoenix/pull/1953#discussion_r1779052061


##########
phoenix-core/src/test/java/org/apache/phoenix/schema/ConditionalTTLExpressionDDLTest.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.schema;
+
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertEquals;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.query.BaseConnectionlessQueryTest;
+import org.apache.phoenix.util.PropertiesUtil;
+import org.junit.Test;
+
+public class ConditionalTTLExpressionDDLTest extends 
BaseConnectionlessQueryTest {
+
+    private static void assertConditonTTL(Connection conn, String tableName, 
String ttlExpr) throws SQLException {
+        PTable table = 
conn.unwrap(PhoenixConnection.class).getTable(tableName);
+        TTLExpression expected = new ConditionTTLExpression(ttlExpr);
+        assertEquals(expected, table.getTTL());
+    }
+
+    @Test
+    public void testBasicExpression() throws SQLException {
+        String ddlTemplate = "create table %s (k1 bigint not null, k2 bigint 
not null, col1 varchar, col2 date " +
+                "constraint pk primary key (k1,k2 desc)) TTL = '%s'";
+        String quotedValue = "k1 > 5 AND col1 < ''zzzzzz''";
+        String ttl = "k1 > 5 AND col1 < 'zzzzzz'";
+        String tableName = generateUniqueName();
+        String ddl = String.format(ddlTemplate, tableName, quotedValue);
+        try (Connection conn = DriverManager.getConnection(getUrl(), 
PropertiesUtil.deepCopy(TEST_PROPERTIES))) {
+            conn.createStatement().execute(ddl);
+            assertConditonTTL(conn, tableName, ttl);
+        }
+    }
+
+    @Test(expected = TypeMismatchException.class)
+    public void testNotBooleanExpr() throws SQLException {
+        String ddlTemplate = "create table %s (k1 bigint not null, k2 bigint 
not null, col1 varchar, col2 date " +
+                "constraint pk primary key (k1,k2 desc)) TTL = '%s'";
+        String ttl = "k1 + 100";
+        String tableName = generateUniqueName();
+        String ddl = String.format(ddlTemplate, tableName, ttl);
+        try (Connection conn = DriverManager.getConnection(getUrl(), 
PropertiesUtil.deepCopy(TEST_PROPERTIES))) {
+            conn.createStatement().execute(ddl);
+        }
+    }
+
+    @Test(expected = TypeMismatchException.class)
+    public void testWrongArgumentValue() throws SQLException {
+        String ddlTemplate = "create table %s (k1 bigint not null, k2 bigint 
not null, col1 varchar, col2 date " +
+                "constraint pk primary key (k1,k2 desc)) TTL = '%s'";
+        String ttl = "k1 = ''abc''";
+        String tableName = generateUniqueName();
+        String ddl = String.format(ddlTemplate, tableName, ttl);
+        try (Connection conn = DriverManager.getConnection(getUrl(), 
PropertiesUtil.deepCopy(TEST_PROPERTIES))) {
+            conn.createStatement().execute(ddl);
+        }
+    }
+
+    @Test
+    public void testNullExpression() throws SQLException {
+        String ddlTemplate = "create table %s (k1 bigint not null, k2 bigint 
not null, col1 varchar, col2 date " +
+                "constraint pk primary key (k1,k2 desc)) TTL = '%s'";
+        String ttl = "col1 is NULL AND col2 < CURRENT_DATE() + 30000";
+        String tableName = generateUniqueName();
+        String ddl = String.format(ddlTemplate, tableName, ttl);
+        try (Connection conn = DriverManager.getConnection(getUrl(), 
PropertiesUtil.deepCopy(TEST_PROPERTIES))) {
+            conn.createStatement().execute(ddl);
+            assertConditonTTL(conn, tableName, ttl);
+        }
+    }
+
+    @Test
+    public void testBooleanColumn() throws SQLException {
+        String ddlTemplate = "create table %s (k1 bigint not null, k2 bigint 
not null, expired BOOLEAN " +
+                "constraint pk primary key (k1,k2 desc)) TTL = '%s'";
+        String ttl = "expired";
+        String tableName = generateUniqueName();
+        String ddl = String.format(ddlTemplate, tableName, ttl);
+        try (Connection conn = DriverManager.getConnection(getUrl(), 
PropertiesUtil.deepCopy(TEST_PROPERTIES))) {
+            conn.createStatement().execute(ddl);
+            assertConditonTTL(conn, tableName, ttl);
+        }
+    }
+
+    @Test
+    public void testNot() throws SQLException {
+        String ddlTemplate = "create table %s (k1 bigint not null, k2 bigint 
not null, expired BOOLEAN " +
+                "constraint pk primary key (k1,k2 desc)) TTL = '%s'";
+        String ttl = "NOT expired";
+        String tableName = generateUniqueName();
+        String ddl = String.format(ddlTemplate, tableName, ttl);
+        try (Connection conn = DriverManager.getConnection(getUrl(), 
PropertiesUtil.deepCopy(TEST_PROPERTIES))) {
+            conn.createStatement().execute(ddl);
+            assertConditonTTL(conn, tableName, ttl);
+        }
+    }
+
+    @Test
+    public void testPhoenixRowTimestamp() throws SQLException {
+        String ddlTemplate = "create table %s (k1 bigint not null, k2 bigint 
not null, col1 varchar " +
+                "constraint pk primary key (k1,k2 desc)) TTL = '%s'";
+        String ttl = "PHOENIX_ROW_TIMESTAMP() < CURRENT_DATE() - 100";
+        String tableName = generateUniqueName();
+        String ddl = String.format(ddlTemplate, tableName, ttl);
+        try (Connection conn = DriverManager.getConnection(getUrl(), 
PropertiesUtil.deepCopy(TEST_PROPERTIES))) {
+            conn.createStatement().execute(ddl);
+            assertConditonTTL(conn, tableName, ttl);
+        }
+    }

Review Comment:
   Should add tests for views and indexes.



##########
phoenix-core-client/src/main/java/org/apache/phoenix/schema/ConditionTTLExpression.java:
##########
@@ -0,0 +1,214 @@
+/*
+ * 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.schema;
+
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.DEFAULT_TTL;
+import static 
org.apache.phoenix.schema.PTable.ImmutableStorageScheme.ONE_CELL_PER_COLUMN;
+import static 
org.apache.phoenix.schema.PTable.QualifierEncodingScheme.NON_ENCODED_QUALIFIERS;
+
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.compile.ColumnResolver;
+import org.apache.phoenix.compile.ExpressionCompiler;
+import org.apache.phoenix.compile.FromCompiler;
+import org.apache.phoenix.compile.StatementContext;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.KeyValueColumnExpression;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixStatement;
+import org.apache.phoenix.parse.ColumnDef;
+import org.apache.phoenix.parse.ColumnName;
+import org.apache.phoenix.parse.ColumnParseNode;
+import org.apache.phoenix.parse.CreateTableStatement;
+import org.apache.phoenix.parse.ParseNode;
+import org.apache.phoenix.parse.SQLParser;
+import org.apache.phoenix.parse.TableName;
+import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.apache.phoenix.util.SchemaUtil;
+
+public class ConditionTTLExpression extends TTLExpression {
+    private final String ttlExpr;
+
+    public ConditionTTLExpression(String ttlExpr) {
+        this.ttlExpr = ttlExpr;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        ConditionTTLExpression that = (ConditionTTLExpression) o;
+        return ttlExpr.equals(that.ttlExpr);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(ttlExpr);
+    }
+
+    @Override
+    public String getTTLExpression() {
+        return ttlExpr;
+    }
+
+    @Override
+    public String toString() {
+        return getTTLExpression();
+    }
+
+    @Override
+    /**
+     * @param result row to be evaluated against the conditional ttl expression
+     * @return DEFAULT_TTL (FOREVER) if the expression evaluates to False else 0
+     * if the expression evaluates to true i.e. row is expired
+     */
+    public long getTTLForRow(List<Cell> result) {
+        // TODO
+        return DEFAULT_TTL;
+    }
+
+    @Override
+    public void validateTTLOnCreation(PhoenixConnection conn, 
CreateTableStatement create) throws SQLException {
+        ParseNode ttlCondition = SQLParser.parseCondition(this.ttlExpr);
+        StatementContext ttlValidationContext = new StatementContext(new 
PhoenixStatement(conn));
+        // Construct a PTable with just enough information to be able to 
compile the TTL expression
+        PTable newTable = createTempPTable(conn, create);
+        ttlValidationContext.setCurrentTable(new TableRef(newTable));
+        VerifyCreateConditionalTTLExpression condTTLVisitor =
+                new VerifyCreateConditionalTTLExpression(conn, 
ttlValidationContext, create);
+        Expression ttlExpression = ttlCondition.accept(condTTLVisitor);
+        if (ttlExpression.getDataType() != PBoolean.INSTANCE) {
+            throw TypeMismatchException.newException(PBoolean.INSTANCE,
+                    ttlExpression.getDataType(), ttlExpression.toString());
+        }
+    }
+
+    @Override
+    public void validateTTLOnAlter(PhoenixConnection conn, PTable table) 
throws SQLException {
+        ParseNode ttlCondition = SQLParser.parseCondition(this.ttlExpr);
+        ColumnResolver resolver = FromCompiler.getResolver(new 
TableRef(table));
+        StatementContext context = new StatementContext(new 
PhoenixStatement(conn), resolver);
+        ExpressionCompiler expressionCompiler = new 
ExpressionCompiler(context);
+        Expression ttlExpression = ttlCondition.accept(expressionCompiler);
+        if (ttlExpression.getDataType() != PBoolean.INSTANCE) {
+            throw TypeMismatchException.newException(PBoolean.INSTANCE,
+                    ttlExpression.getDataType(), ttlExpression.toString());
+        }
+    }
+
+    @Override
+    public String getTTLForScanAttribute() {
+        // Conditional TTL is not sent as a scan attribute
+        // Masking is implemented using query re-write
+        return null;
+    }
+
+    /**
+     * Validates that all the columns used in the conditional TTL expression 
are present in the table
+     * or its parent table in case of view
+     */
+    private static class VerifyCreateConditionalTTLExpression extends 
ExpressionCompiler {

Review Comment:
   Can we restrict the expression to not include aggregate functions?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to