Skip to content

Commit

Permalink
[CALCITE-884] Web adapter (Henry Olson)
Browse files Browse the repository at this point in the history
Some fix-ups (Julian Hyde):
* Upgrade calcite;
* Upgrade natty, jsoup;
* Rename files, and move to org.apache.calcite.adapter.file package;
* Add file adapter documentation and history (based on original README
  and HISTORY);
* Add EMPS.html and DEPTS.html examples.
  • Loading branch information
HenryOlson authored and julianhyde committed Feb 23, 2017
1 parent 1b11385 commit cf46d3b
Show file tree
Hide file tree
Showing 32 changed files with 2,490 additions and 19 deletions.
34 changes: 17 additions & 17 deletions core/src/main/java/org/apache/calcite/schema/SchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@
* <p>A schema factory allows you to include a custom schema in a model file.
* For example, here is a model that contains a custom schema whose tables
* read CSV files. (See the
* <a href="https://github.com/julianhyde/optiq-csv">optiq-csv</a> for more
* details about this particular adapter.)</p>
* <a href="http://calcite.apache.org/apidocs/org/apache/calcite/adapter/csv/package-summary.html">example CSV adapter</a>
* for more details about this particular adapter.)
*
* <pre>{@code
* {
* version: '1.0',
* defaultSchema: 'SALES',
* schemas: [
* "version": "1.0",
* "defaultSchema": "SALES",
* "schemas": [
* {
* name: 'SALES',
* type: 'custom',
* factory: 'org.apache.calcite.adapter.csv.CsvSchemaFactory',
* mutable: true,
* operand: {
* directory: 'target/test-classes/sales'
* "name": "SALES",
* "type": "custom",
* "factory": "org.apache.calcite.adapter.csv.CsvSchemaFactory",
* "mutable": true,
* "operand": {
* directory: "target/test-classes/sales"
* },
* tables: [
* "tables": [
* {
* name: 'FEMALE_EMPS',
* type: 'view',
* sql: 'SELECT * FROM emps WHERE gender = \'F\''
* "name": "FEMALE_EMPS",
* "type": "view",
* "sql": "SELECT * FROM emps WHERE gender = 'F'"
* }
* ]
* }
Expand All @@ -55,10 +55,10 @@
*
* <p>If you do not wish to allow model authors to add additional tables
* (including views) to an instance of your schema, specify
* 'mutable: false'.</p>
* 'mutable: false'.
*
* <p>A class that implements SchemaFactory specified in a schema must have a
* public default constructor.</p>
* public default constructor.
*/
public interface SchemaFactory {
/** Creates a Schema.
Expand Down
112 changes: 112 additions & 0 deletions file/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite</artifactId>
<version>1.12.0-SNAPSHOT</version>
</parent>

<!-- The basics. -->
<artifactId>calcite-file</artifactId>
<packaging>jar</packaging>
<version>1.12.0-SNAPSHOT</version>
<name>Calcite File</name>
<description>Calcite provider that reads files and URIs</description>

<properties>
<top.dir>${project.basedir}/..</top.dir>
<build.timestamp>${maven.build.timestamp}</build.timestamp>
</properties>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.joestelmach</groupId>
<artifactId>natty</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-linq4j</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jalopy-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<configuration>
<srcExcludesPattern>**/CsvTableScan.java</srcExcludesPattern>
<failOnError>false</failOnError>
<convention>${top.dir}src/main/config/jalopy.xml</convention>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>org/apache/calcite/adapter/file/FileSuite.java</include>
</includes>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependencies/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.calcite.adapter.file;

import org.apache.calcite.linq4j.Enumerator;

import org.jsoup.select.Elements;

import java.util.Iterator;

/**
* Wraps WebReader and FileRowConverter, enumerates tr Elements as
* table rows.
*/
class FileEnumerator implements Enumerator<Object> {

private final Iterator<Elements> iterator;
private final FileRowConverter converter;
private final int[] fields;
private Object current;

public FileEnumerator(Iterator<Elements> iterator,
FileRowConverter converter) {
this.iterator = iterator;
this.converter = converter;
this.fields = identityList(this.converter.width());
}

public FileEnumerator(Iterator<Elements> iterator, FileRowConverter converter,
int[] fields) {
this.iterator = iterator;
this.converter = converter;
this.fields = fields;
}

public Object current() {
if (current == null) {
this.moveNext();
}
return current;
}

public boolean moveNext() {
try {
if (this.iterator.hasNext()) {
final Elements row = this.iterator.next();
current = this.converter.toRow(row, this.fields);
return true;
} else {
current = null;
return false;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

// required by linq4j Enumerator interface
public void reset() {
throw new UnsupportedOperationException();
}

// required by linq4j Enumerator interface
public void close() {
}

/** Returns an array of integers {0, ..., n - 1}. */
private static int[] identityList(int n) {
int[] integers = new int[n];

for (int i = 0; i < n; i++) {
integers[i] = i;
}

return integers;
}

}

// End FileEnumerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.calcite.adapter.file;

import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.linq4j.tree.Primitive;
import org.apache.calcite.rel.type.RelDataType;

import com.google.common.collect.ImmutableMap;

import java.util.Map;

/**
* Type of a field in a Web (HTML) table.
*
* <p>Usually, and unless specified explicitly in the header row, a field is
* of type {@link #STRING}. But specifying the field type in the fields
* makes it easier to write SQL.
*
* <p>Trivially modified from CsvFieldType.
*/
enum FileFieldType {
STRING(null, String.class),
BOOLEAN(Primitive.BOOLEAN),
BYTE(Primitive.BYTE),
CHAR(Primitive.CHAR),
SHORT(Primitive.SHORT),
INT(Primitive.INT),
LONG(Primitive.LONG),
FLOAT(Primitive.FLOAT),
DOUBLE(Primitive.DOUBLE),
DATE(null, java.sql.Date.class),
TIME(null, java.sql.Time.class),
TIMESTAMP(null, java.sql.Timestamp.class);

private final Primitive primitive;
private final Class clazz;

private static final Map<String, FileFieldType> MAP;

static {
ImmutableMap.Builder<String, FileFieldType> builder =
ImmutableMap.builder();
for (FileFieldType value : values()) {
builder.put(value.clazz.getSimpleName(), value);

if (value.primitive != null) {
builder.put(value.primitive.primitiveClass.getSimpleName(), value);
}
}
MAP = builder.build();
}

FileFieldType(Primitive primitive) {
this(primitive, primitive.boxClass);
}

FileFieldType(Primitive primitive, Class clazz) {
this.primitive = primitive;
this.clazz = clazz;
}

public RelDataType toType(JavaTypeFactory typeFactory) {
return typeFactory.createJavaType(clazz);
}

public static FileFieldType of(String typeString) {
return MAP.get(typeString);
}
}

// End FileFieldType.java
Loading

0 comments on commit cf46d3b

Please sign in to comment.