forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulsar SQL supports pulsar's primitive schema (apache#4728)
### Motivation Continue the PR of apache#4151
- Loading branch information
1 parent
0362944
commit 1ab35b0
Showing
10 changed files
with
442 additions
and
26 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
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
65 changes: 65 additions & 0 deletions
65
...resto-pulsar/src/main/java/org/apache/pulsar/sql/presto/PulsarPrimitiveSchemaHandler.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,65 @@ | ||
/** | ||
* 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.pulsar.sql.presto; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.ByteBufUtil; | ||
import java.sql.Time; | ||
import java.sql.Timestamp; | ||
import java.util.Date; | ||
|
||
import io.netty.util.concurrent.FastThreadLocal; | ||
import org.apache.pulsar.client.api.Schema; | ||
import org.apache.pulsar.client.impl.schema.AutoConsumeSchema; | ||
import org.apache.pulsar.common.schema.SchemaInfo; | ||
|
||
/** | ||
* A presto schema handler that interprets data using pulsar schema. | ||
*/ | ||
public class PulsarPrimitiveSchemaHandler implements SchemaHandler { | ||
|
||
private final SchemaInfo schemaInfo; | ||
private final Schema<?> schema; | ||
|
||
public PulsarPrimitiveSchemaHandler(SchemaInfo schemaInfo) { | ||
this.schemaInfo = schemaInfo; | ||
this.schema = AutoConsumeSchema.getSchema(schemaInfo); | ||
} | ||
|
||
@Override | ||
public Object deserialize(ByteBuf byteBuf) { | ||
byte[] data = ByteBufUtil.getBytes(byteBuf); | ||
Object currentRecord = schema.decode(data); | ||
switch (schemaInfo.getType()) { | ||
case DATE: | ||
return ((Date) currentRecord).getTime(); | ||
case TIME: | ||
return ((Time) currentRecord).getTime(); | ||
case TIMESTAMP: | ||
return ((Timestamp) currentRecord).getTime(); | ||
default: | ||
return currentRecord; | ||
} | ||
} | ||
|
||
@Override | ||
public Object extractField(int index, Object currentRecord) { | ||
return currentRecord; | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
...ar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/PulsarSchemaHandlers.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,53 @@ | ||
/** | ||
* 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.pulsar.sql.presto; | ||
|
||
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import com.facebook.presto.spi.PrestoException; | ||
import java.util.List; | ||
import org.apache.pulsar.common.schema.SchemaInfo; | ||
|
||
class PulsarSchemaHandlers { | ||
|
||
static SchemaHandler newPulsarSchemaHandler(SchemaInfo schemaInfo, | ||
List<PulsarColumnHandle> columnHandles) { | ||
if (schemaInfo.getType().isPrimitive()) { | ||
return new PulsarPrimitiveSchemaHandler(schemaInfo); | ||
} else if (schemaInfo.getType().isStruct()) { | ||
switch (schemaInfo.getType()) { | ||
case JSON: | ||
return new JSONSchemaHandler(columnHandles); | ||
case AVRO: | ||
return new AvroSchemaHandler(PulsarConnectorUtils | ||
.parseSchema(new String(schemaInfo.getSchema(), UTF_8) | ||
), columnHandles); | ||
default: | ||
throw new PrestoException(NOT_SUPPORTED, "Not supported schema type: " + schemaInfo.getType()); | ||
} | ||
|
||
} else { | ||
throw new PrestoException( | ||
NOT_SUPPORTED, | ||
"Schema `" + schemaInfo.getType() + "` is not supported by presto yet : " + schemaInfo); | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.