You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2018/12/25 16:09:34 UTC

[incubator-plc4x] 03/03: [CALCITE] Some refactoring.

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch feature/calcite-adapter
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 06ea388e6f7bb09d1325008c0ae00e419f7a2c53
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Tue Dec 25 17:09:26 2018 +0100

    [CALCITE] Some refactoring.
---
 .../main/java/org/apache/plc4x/Plc4xBaseTable.java |  6 +-
 .../java/org/apache/plc4x/Plc4xSchemaFactory.java  |  4 +-
 .../java/org/apache/plc4x/Plc4xStreamTable.java    |  2 +-
 .../src/main/java/org/apache/plc4x/Plc4xTable.java |  2 +-
 .../java/org/apache/plc4x/DriverManagerTest.java   | 64 ++++++++++------------
 .../java/org/apache/plc4x/Plc4XBaseTableTest.java  |  2 +-
 .../org/apache/plc4x/Plc4xSchemaFactoryTest.java   | 19 +++++++
 7 files changed, 59 insertions(+), 40 deletions(-)

diff --git a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xBaseTable.java b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xBaseTable.java
index 52b306b..9514492 100644
--- a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xBaseTable.java
+++ b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xBaseTable.java
@@ -43,6 +43,10 @@ import java.util.function.Supplier;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+/**
+ * Base for Stream and "Table" version of the Plc4xTable.
+ * Needs to be subclassed due to usage of "instanceof" in Calcites internals.
+ */
 public abstract class Plc4xBaseTable extends AbstractTable {
 
     private static final Logger logger = LoggerFactory.getLogger(Plc4xBaseTable.class);
@@ -123,7 +127,7 @@ public abstract class Plc4xBaseTable extends AbstractTable {
     /**
      * if tableCutoff is positive, then the row gets limited to that.
      */
-    public Enumerable<Object[]> scanInternal(DataContext root) {
+    public Enumerable<Object[]> scan(DataContext root) {
         return new AbstractEnumerable<Object[]>() {
             @Override
             public Enumerator<Object[]> enumerator() {
diff --git a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xSchemaFactory.java b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xSchemaFactory.java
index 6b1d16f..28fc040 100644
--- a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xSchemaFactory.java
+++ b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xSchemaFactory.java
@@ -39,7 +39,7 @@ public class Plc4xSchemaFactory implements SchemaFactory {
         try {
             configuration = ScraperConfiguration.fromFile(config.toString());
         } catch (IOException e) {
-            throw new RuntimeException("Unable to load configuration file!", e);
+            throw new IllegalArgumentException("Unable to load configuration file!", e);
         }
 
         // Fetch limit
@@ -49,7 +49,7 @@ public class Plc4xSchemaFactory implements SchemaFactory {
         try {
             parsedLimit = Long.parseLong(limit.toString());
         } catch (NumberFormatException e) {
-            throw new RuntimeException("Given limit '" + limit + "' cannot be parsed to valid long!", e);
+            throw new IllegalArgumentException("Given limit '" + limit + "' cannot be parsed to valid long!", e);
         }
         // Pass the configuration to the Schema
         return new Plc4xSchema(configuration, parsedLimit);
diff --git a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xStreamTable.java b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xStreamTable.java
index 7725a1d..8e40944 100644
--- a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xStreamTable.java
+++ b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xStreamTable.java
@@ -35,7 +35,7 @@ public class Plc4xStreamTable extends Plc4xBaseTable implements ScannableTable,
 
     @Override
     public Enumerable<Object[]> scan(DataContext root) {
-        return super.scanInternal(root);
+        return super.scan(root);
     }
 
     @Override
diff --git a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xTable.java b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xTable.java
index cd7c142..096c661 100644
--- a/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xTable.java
+++ b/integrations/apache-calcite/src/main/java/org/apache/plc4x/Plc4xTable.java
@@ -33,7 +33,7 @@ public class Plc4xTable extends Plc4xBaseTable implements ScannableTable {
 
     @Override
     public Enumerable<Object[]> scan(DataContext root) {
-        return super.scanInternal(root);
+        return super.scan(root);
     }
 
 }
diff --git a/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java b/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java
index 5a6bc7c..d647dce 100644
--- a/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java
+++ b/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java
@@ -21,70 +21,66 @@ package org.apache.plc4x;
 import org.apache.calcite.jdbc.CalciteConnection;
 import org.apache.calcite.jdbc.Driver;
 import org.apache.plc4x.java.scraper.config.ScraperConfiguration;
+import org.assertj.core.api.WithAssertions;
 import org.junit.jupiter.api.Test;
 
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.util.Properties;
 
-public class DriverManagerTest {
+public class DriverManagerTest implements WithAssertions {
 
     @Test
-    void instanciateJdbcConnection() throws SQLException, IOException {
+    void query() throws SQLException, IOException {
         Driver driver = new Driver();
-        Connection connection = driver.connect("jdbc:calcite://asdf;config=abc;lex=MYSQL_ANSI", new Properties());
+        Connection connection = driver.connect("jdbc:calcite:asdf;lex=MYSQL_ANSI", new Properties());
 
         CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
-        calciteConnection.getRootSchema().add("plc4x", new Plc4xSchema(ScraperConfiguration.fromFile("src/test/resources/example.yml"), 10));
+        calciteConnection.getRootSchema().add("plc4x", new Plc4xSchema(ScraperConfiguration.fromFile("src/test/resources/example.yml"), 100));
 
-        // ResultSet rs = connection.prepareStatement("SELECT STREAM \"test\", \"test\" * 2, \"test2\" FROM \"plc4x\".\"job1\"").executeQuery();
-        ResultSet rs = connection.prepareStatement("SELECT STREAM * FROM \"plc4x\".\"job1\" WHERE source = 'test'").executeQuery();
-
-        // Print the header
-        int count = rs.getMetaData().getColumnCount();
-        for (int i = 1; i <= count; i++) {
-            System.out.print(rs.getMetaData().getColumnLabel(i) + "(" + rs.getMetaData().getColumnTypeName(i) + ")" + "\t");
-        }
-        System.out.println("");
-
-        while (rs.next()) {
-            for (int i = 1; i <= count; i++) {
-                System.out.print(rs.getString(i) + "\t");
-            }
-            System.out.println("");
-        }
+        ResultSet rs = connection.prepareStatement("SELECT * FROM \"plc4x\".\"job1\"").executeQuery();
+        validateResult(rs);
 
         connection.close();
     }
 
     @Test
-    void instantiateDirect() throws IOException, SQLException {
+    void query2() throws IOException, SQLException {
         Driver driver = new Driver();
         Connection connection = driver.connect("jdbc:calcite:model=src/test/resources/model.yml;lex=MYSQL_ANSI", new Properties());
 
         // ResultSet rs = connection.prepareStatement("SELECT STREAM \"test\", \"test\" * 2, \"test2\" FROM \"plc4x\".\"job1\"").executeQuery();
         ResultSet rs = connection.prepareStatement("SELECT * FROM \"plc4x-tables\".\"job1\"").executeQuery();
 
-        // Print the header
-        int count = rs.getMetaData().getColumnCount();
-        for (int i = 1; i <= count; i++) {
-            System.out.print(rs.getMetaData().getColumnLabel(i) + "(" + rs.getMetaData().getColumnTypeName(i) + ")" + "\t");
-        }
-        System.out.println("");
+        validateResult(rs);
 
-        int row = 1;
+        connection.close();
+    }
 
+    private void validateResult(ResultSet rs) throws SQLException {
+        // Assert columns
+        ResultSetMetaData metadata = rs.getMetaData();
+        assertThat(metadata.getColumnCount()).isEqualTo(4);
+        // Column names
+        assertThat(metadata.getColumnName(1)).isEqualTo("timestamp");
+        assertThat(metadata.getColumnName(2)).isEqualTo("source");
+        assertThat(metadata.getColumnName(3)).isEqualTo("test");
+        assertThat(metadata.getColumnName(4)).isEqualTo("test2");
+        // Column types
+        assertThat(metadata.getColumnTypeName(1)).isEqualTo("TIMESTAMP");
+        assertThat(metadata.getColumnTypeName(2)).isEqualTo("VARCHAR");
+        assertThat(metadata.getColumnTypeName(3)).isEqualTo("INTEGER");
+        assertThat(metadata.getColumnTypeName(4)).isEqualTo("VARCHAR");
+
+        int rowCount = 0;
         while (rs.next()) {
-            System.out.print(row++ + "\t");
-            for (int i = 1; i <= count; i++) {
-                System.out.print(rs.getString(i) + "\t");
-            }
-            System.out.println("");
+            rowCount++;
         }
 
-        connection.close();
+        assertThat(rowCount).isEqualTo(100);
     }
 
 }
diff --git a/integrations/apache-calcite/src/test/java/org/apache/plc4x/Plc4XBaseTableTest.java b/integrations/apache-calcite/src/test/java/org/apache/plc4x/Plc4XBaseTableTest.java
index c89d799..3f7d918 100644
--- a/integrations/apache-calcite/src/test/java/org/apache/plc4x/Plc4XBaseTableTest.java
+++ b/integrations/apache-calcite/src/test/java/org/apache/plc4x/Plc4XBaseTableTest.java
@@ -43,6 +43,6 @@ class Plc4XBaseTableTest implements WithAssertions {
         Enumerator<Object[]> enumerator = table.scan(null).enumerator();
 
         assertThat(enumerator.moveNext()).isTrue();
-        assertThat(enumerator.current()).containsExactly("value");
+        assertThat(enumerator.current()).contains("value");
     }
 }
\ No newline at end of file
diff --git a/integrations/apache-calcite/src/test/java/org/apache/plc4x/Plc4xSchemaFactoryTest.java b/integrations/apache-calcite/src/test/java/org/apache/plc4x/Plc4xSchemaFactoryTest.java
new file mode 100644
index 0000000..17cade5
--- /dev/null
+++ b/integrations/apache-calcite/src/test/java/org/apache/plc4x/Plc4xSchemaFactoryTest.java
@@ -0,0 +1,19 @@
+package org.apache.plc4x;
+
+import org.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class Plc4xSchemaFactoryTest implements WithAssertions {
+
+    @Test
+    void create() {
+        Plc4xSchemaFactory factory = new Plc4xSchemaFactory();
+        assertThatThrownBy(() -> factory.create(null, "", Collections.emptyMap()))
+            .isInstanceOf(NullPointerException.class)
+            .hasMessageContaining("Please specify operand 'config'...");
+    }
+}
\ No newline at end of file