Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DRILL-2193: implement fast count / skip-all semantics for JSON reader
- Loading branch information
1 parent
f1b59ed
commit 64ad7a7
Showing
5 changed files
with
163 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/JsonProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* 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.drill.exec.store.easy.json; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.drill.exec.vector.complex.writer.BaseWriter; | ||
|
||
public interface JsonProcessor { | ||
|
||
public static enum ReadState { | ||
WRITE_FAILURE, | ||
END_OF_STREAM, | ||
WRITE_SUCCEED | ||
} | ||
|
||
ReadState write(BaseWriter.ComplexWriter writer) throws IOException; | ||
|
||
void setSource(InputStream is) throws IOException; | ||
|
||
void ensureAtLeastOneField(BaseWriter.ComplexWriter writer); | ||
} |
58 changes: 58 additions & 0 deletions
58
...va-exec/src/main/java/org/apache/drill/exec/store/easy/json/reader/BaseJsonProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* 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.drill.exec.store.easy.json.reader; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.io.IOContext; | ||
import com.fasterxml.jackson.core.sym.BytesToNameCanonicalizer; | ||
import com.fasterxml.jackson.core.util.BufferRecycler; | ||
import com.google.common.base.Preconditions; | ||
import io.netty.buffer.DrillBuf; | ||
import org.apache.drill.exec.store.easy.json.JsonProcessor; | ||
import org.apache.drill.exec.store.easy.json.RewindableUtf8Reader; | ||
|
||
public abstract class BaseJsonProcessor implements JsonProcessor { | ||
|
||
protected final RewindableUtf8Reader parser; | ||
protected DrillBuf workBuf; | ||
|
||
public BaseJsonProcessor(DrillBuf workBuf) { | ||
this.workBuf = Preconditions.checkNotNull(workBuf); | ||
this.parser = Preconditions.checkNotNull(createParser()); | ||
} | ||
|
||
protected RewindableUtf8Reader createParser() { | ||
final BufferRecycler recycler = new BufferRecycler(); | ||
final IOContext context = new IOContext(recycler, this, false); | ||
final int features = JsonParser.Feature.collectDefaults() // | ||
| JsonParser.Feature.ALLOW_COMMENTS.getMask() // | ||
| JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES.getMask(); | ||
|
||
final BytesToNameCanonicalizer can = BytesToNameCanonicalizer.createRoot(); | ||
return new RewindableUtf8Reader<>(context, features, can.makeChild(JsonFactory.Feature.collectDefaults()), context.allocReadIOBuffer()); | ||
} | ||
|
||
@Override | ||
public void setSource(InputStream is) throws IOException { | ||
parser.setInputStream(is); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...a-exec/src/main/java/org/apache/drill/exec/store/easy/json/reader/CountingJsonReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 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.drill.exec.store.easy.json.reader; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JsonToken; | ||
import io.netty.buffer.DrillBuf; | ||
import org.apache.drill.exec.store.easy.json.JsonProcessor; | ||
import org.apache.drill.exec.vector.complex.writer.BaseWriter; | ||
|
||
public class CountingJsonReader extends BaseJsonProcessor implements JsonProcessor { | ||
|
||
public CountingJsonReader(DrillBuf workBuf) { | ||
super(workBuf); | ||
} | ||
|
||
@Override | ||
public ReadState write(BaseWriter.ComplexWriter writer) throws IOException { | ||
final JsonToken token = parser.nextToken(); | ||
if (!parser.hasCurrentToken()) { | ||
return ReadState.END_OF_STREAM; | ||
} else if (token != JsonToken.START_OBJECT) { | ||
throw new IllegalStateException(String.format("Cannot read from the middle of a record. Current token was %s", token)); | ||
} | ||
writer.rootAsMap().bit("count").writeBit(1); | ||
parser.skipChildren(); | ||
return writer.ok() ? ReadState.WRITE_SUCCEED : ReadState.WRITE_FAILURE; | ||
} | ||
|
||
@Override | ||
public void ensureAtLeastOneField(BaseWriter.ComplexWriter writer) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters